2 * Copyright 2001-2006 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * @file XMLObjectBuilder.h
\r
20 * Factory interface for XMLObjects
\r
23 #if !defined(__xmltooling_xmlobjbuilder_h__)
\r
24 #define __xmltooling_xmlobjbuilder_h__
\r
27 #include <xercesc/dom/DOM.hpp>
\r
28 #include <xmltooling/QName.h>
\r
29 #include <xmltooling/XMLObject.h>
\r
31 using namespace xercesc;
\r
33 #if defined (_MSC_VER)
\r
34 #pragma warning( push )
\r
35 #pragma warning( disable : 4250 4251 )
\r
38 namespace xmltooling {
\r
41 * A factory interface for obtaining XMLObjects.
\r
42 * Subclasses MAY supply additional factory methods.
\r
44 class XMLTOOL_API XMLObjectBuilder
\r
46 MAKE_NONCOPYABLE(XMLObjectBuilder);
\r
48 virtual ~XMLObjectBuilder() {}
\r
51 * Creates an empty XMLObject.
\r
53 * @param e a construction hint based on the eventual unmarshalling source
\r
54 * @return the empty XMLObject
\r
56 virtual XMLObject* buildObject(const DOMElement* e=NULL) const=0;
\r
59 * Creates an empty XMLObject using the default build method, if a builder can be found.
\r
61 * @param key the key used to locate a builder
\r
62 * @param e a construction hint based on the eventual unmarshalling source
\r
63 * @return the empty object or NULL if no builder is available
\r
65 static XMLObject* buildObject(const QName& key, const DOMElement* e=NULL) {
\r
66 const XMLObjectBuilder* b=getBuilder(key);
\r
67 return b ? b->buildObject(e) : NULL;
\r
71 * Retrieves an XMLObjectBuilder using the key it was registered with.
\r
73 * @param key the key used to register the builder
\r
74 * @return the builder or NULL
\r
76 static const XMLObjectBuilder* getBuilder(const QName& key) {
\r
77 std::map<QName,XMLObjectBuilder*>::const_iterator i=m_map.find(key);
\r
78 return (i==m_map.end()) ? NULL : i->second;
\r
82 * Retrieves an XMLObjectBuilder for a given DOM element.
\r
83 * If no match is found, the default builder is returned, if any.
\r
85 * @param element the element for which to locate a builder
\r
86 * @return the builder or NULL
\r
88 static const XMLObjectBuilder* getBuilder(const DOMElement* element);
\r
91 * Retrieves the default XMLObjectBuilder for DOM elements
\r
93 * @return the default builder or NULL
\r
95 static const XMLObjectBuilder* getDefaultBuilder(const DOMElement* element) {
\r
100 * Gets an immutable list of all the builders currently registered.
\r
102 * @return list of all the builders currently registered
\r
104 static const std::map<QName,XMLObjectBuilder*>& getBuilders() {
\r
109 * Registers a new builder for the given key.
\r
111 * @param builderKey the key used to retrieve this builder later
\r
112 * @param builder the builder
\r
114 static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {
\r
115 deregisterBuilder(builderKey);
\r
116 m_map[builderKey]=builder;
\r
120 * Registers a default builder
\r
122 * @param builder the default builder
\r
124 static void registerDefaultBuilder(XMLObjectBuilder* builder) {
\r
125 deregisterDefaultBuilder();
\r
130 * Deregisters a builder.
\r
132 * @param builderKey the key for the builder to be deregistered
\r
134 static void deregisterBuilder(const QName& builderKey) {
\r
135 delete getBuilder(builderKey);
\r
136 m_map.erase(builderKey);
\r
140 * Deregisters default builder.
\r
142 static void deregisterDefaultBuilder() {
\r
148 * Unregisters and destroys all registered builders.
\r
150 static void destroyBuilders();
\r
153 XMLObjectBuilder() {}
\r
156 static std::map<QName,XMLObjectBuilder*> m_map;
\r
157 static XMLObjectBuilder* m_default;
\r
162 #if defined (_MSC_VER)
\r
163 #pragma warning( pop )
\r
166 #endif /* __xmltooling_xmlobjbuilder_h__ */
\r