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 <xmltooling/QName.h>
\r
28 #include <xmltooling/XMLObject.h>
\r
30 #if defined (_MSC_VER)
\r
31 #pragma warning( push )
\r
32 #pragma warning( disable : 4250 4251 )
\r
35 namespace xmltooling {
\r
38 * A factory interface for obtaining XMLObjects.
\r
39 * Subclasses MAY supply additional factory methods.
\r
41 class XMLTOOL_API XMLObjectBuilder
\r
43 MAKE_NONCOPYABLE(XMLObjectBuilder);
\r
45 virtual ~XMLObjectBuilder() {}
\r
48 * Creates an empty XMLObject.
\r
50 * @return the empty XMLObject
\r
52 virtual XMLObject* buildObject() const=0;
\r
55 * Creates an unmarshalled XMLObject from a DOM Element.
\r
57 * @param element the unmarshalling source
\r
58 * @param bindDocument true iff the XMLObject should take ownership of the DOM Document
\r
59 * @return the unmarshalled XMLObject
\r
61 virtual XMLObject* buildFromElement(DOMElement* element, bool bindDocument=false) const {
\r
62 std::auto_ptr<XMLObject> ret(buildObject());
\r
63 ret->unmarshall(element,bindDocument);
\r
64 return ret.release();
\r
68 * Creates an unmarshalled XMLObject from the root of a DOM Document.
\r
70 * @param doc the unmarshalling source
\r
71 * @param bindDocument true iff the XMLObject should take ownership of the DOM Document
\r
72 * @return the unmarshalled XMLObject
\r
74 virtual XMLObject* buildFromDocument(DOMDocument* doc, bool bindDocument=true) const {
\r
75 return buildFromElement(doc->getDocumentElement(),bindDocument);
\r
79 * Creates an empty XMLObject using the default build method, if a builder can be found.
\r
81 * @param key the key used to locate a builder
\r
82 * @return the empty object or NULL if no builder is available
\r
84 static XMLObject* buildOne(const QName& key) {
\r
85 const XMLObjectBuilder* b=getBuilder(key);
\r
87 return b->buildObject();
\r
88 b=getDefaultBuilder();
\r
89 return b ? b->buildObject() : NULL;
\r
93 * Creates an unmarshalled XMLObject using the default build method, if a builder can be found.
\r
95 * @param element the unmarshalling source
\r
96 * @param bindDocument true iff the new XMLObject should take ownership of the DOM Document
\r
97 * @return the unmarshalled object or NULL if no builder is available
\r
99 static XMLObject* buildOneFromElement(DOMElement* element, bool bindDocument=false) {
\r
100 const XMLObjectBuilder* b=getBuilder(element);
\r
101 return b ? b->buildFromElement(element,bindDocument) : NULL;
\r
105 * Retrieves an XMLObjectBuilder using the key it was registered with.
\r
107 * @param key the key used to register the builder
\r
108 * @return the builder or NULL
\r
110 static const XMLObjectBuilder* getBuilder(const QName& key) {
\r
111 std::map<QName,XMLObjectBuilder*>::const_iterator i=m_map.find(key);
\r
112 return (i==m_map.end()) ? NULL : i->second;
\r
116 * Retrieves an XMLObjectBuilder for a given DOM element.
\r
117 * If no match is found, the default builder is returned, if any.
\r
119 * @param element the element for which to locate a builder
\r
120 * @return the builder or NULL
\r
122 static const XMLObjectBuilder* getBuilder(const DOMElement* element);
\r
125 * Retrieves the default XMLObjectBuilder for DOM elements
\r
127 * @return the default builder or NULL
\r
129 static const XMLObjectBuilder* getDefaultBuilder() {
\r
134 * Gets an immutable list of all the builders currently registered.
\r
136 * @return list of all the builders currently registered
\r
138 static const std::map<QName,XMLObjectBuilder*>& getBuilders() {
\r
143 * Registers a new builder for the given key.
\r
145 * @param builderKey the key used to retrieve this builder later
\r
146 * @param builder the builder
\r
148 static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {
\r
149 deregisterBuilder(builderKey);
\r
150 m_map[builderKey]=builder;
\r
154 * Registers a default builder
\r
156 * @param builder the default builder
\r
158 static void registerDefaultBuilder(XMLObjectBuilder* builder) {
\r
159 deregisterDefaultBuilder();
\r
164 * Deregisters a builder.
\r
166 * @param builderKey the key for the builder to be deregistered
\r
168 static void deregisterBuilder(const QName& builderKey) {
\r
169 delete getBuilder(builderKey);
\r
170 m_map.erase(builderKey);
\r
174 * Deregisters default builder.
\r
176 static void deregisterDefaultBuilder() {
\r
182 * Unregisters and destroys all registered builders.
\r
184 static void destroyBuilders();
\r
187 XMLObjectBuilder() {}
\r
190 static std::map<QName,XMLObjectBuilder*> m_map;
\r
191 static XMLObjectBuilder* m_default;
\r
196 #if defined (_MSC_VER)
\r
197 #pragma warning( pop )
\r
200 #endif /* __xmltooling_xmlobjbuilder_h__ */
\r