2 * Copyright 2001-2006 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file XMLObjectBuilder.h
20 * Factory interface for XMLObjects
23 #if !defined(__xmltooling_xmlobjbuilder_h__)
24 #define __xmltooling_xmlobjbuilder_h__
27 #include <xmltooling/QName.h>
28 #include <xmltooling/XMLObject.h>
30 #if defined (_MSC_VER)
31 #pragma warning( push )
32 #pragma warning( disable : 4250 4251 )
35 namespace xmltooling {
38 * A factory interface for obtaining XMLObjects.
39 * Subclasses MAY supply additional factory methods.
41 class XMLTOOL_API XMLObjectBuilder
43 MAKE_NONCOPYABLE(XMLObjectBuilder);
45 virtual ~XMLObjectBuilder() {}
48 * Creates an empty XMLObject with a particular element name.
50 * @param namespaceURI namespace URI for element
51 * @param elementLocalName local name of element
52 * @param namespacePrefix prefix of element name
53 * @return the empty XMLObject
55 virtual XMLObject* buildObject(
56 const XMLCh* namespaceURI, const XMLCh* elementLocalName, const XMLCh* namespacePrefix=NULL
60 * Creates an empty XMLObject with a defaulted element name.
62 * @return the empty XMLObject
64 virtual XMLObject* buildObject() const {
65 return buildObject(NULL,NULL,NULL);
69 * Creates an empty XMLObject with a particular element name.
71 * @param q QName of element for object
72 * @return the empty XMLObject
74 virtual XMLObject* buildObject(const QName& q) const {
75 return buildObject(q.getNamespaceURI(),q.getLocalPart(),q.getPrefix());
79 * Creates an unmarshalled XMLObject from a DOM Element.
81 * @param element the unmarshalling source
82 * @param bindDocument true iff the XMLObject should take ownership of the DOM Document
83 * @return the unmarshalled XMLObject
85 virtual XMLObject* buildFromElement(DOMElement* element, bool bindDocument=false) const {
86 std::auto_ptr<XMLObject> ret(buildObject(element->getNamespaceURI(),element->getLocalName(),element->getPrefix()));
87 ret->unmarshall(element,bindDocument);
92 * Creates an unmarshalled XMLObject from the root of a DOM Document.
94 * @param doc the unmarshalling source
95 * @param bindDocument true iff the XMLObject should take ownership of the DOM Document
96 * @return the unmarshalled XMLObject
98 virtual XMLObject* buildFromDocument(DOMDocument* doc, bool bindDocument=true) const {
99 return buildFromElement(doc->getDocumentElement(),bindDocument);
103 * Creates an empty XMLObject using the default build method, if a builder can be found.
105 * @param key the key used to locate a builder
106 * @return the empty object or NULL if no builder is available
108 static XMLObject* buildOne(const QName& key) {
109 const XMLObjectBuilder* b=getBuilder(key);
111 return b->buildObject();
112 b=getDefaultBuilder();
113 return b ? b->buildObject() : NULL;
117 * Creates an unmarshalled XMLObject using the default build method, if a builder can be found.
119 * @param element the unmarshalling source
120 * @param bindDocument true iff the new XMLObject should take ownership of the DOM Document
121 * @return the unmarshalled object or NULL if no builder is available
123 static XMLObject* buildOneFromElement(DOMElement* element, bool bindDocument=false) {
124 const XMLObjectBuilder* b=getBuilder(element);
125 return b ? b->buildFromElement(element,bindDocument) : NULL;
129 * Retrieves an XMLObjectBuilder using the key it was registered with.
131 * @param key the key used to register the builder
132 * @return the builder or NULL
134 static const XMLObjectBuilder* getBuilder(const QName& key) {
135 std::map<QName,XMLObjectBuilder*>::const_iterator i=m_map.find(key);
136 return (i==m_map.end()) ? NULL : i->second;
140 * Retrieves an XMLObjectBuilder for a given DOM element.
141 * If no match is found, the default builder is returned, if any.
143 * @param element the element for which to locate a builder
144 * @return the builder or NULL
146 static const XMLObjectBuilder* getBuilder(const DOMElement* element);
149 * Retrieves the default XMLObjectBuilder for DOM elements
151 * @return the default builder or NULL
153 static const XMLObjectBuilder* getDefaultBuilder() {
158 * Gets an immutable list of all the builders currently registered.
160 * @return list of all the builders currently registered
162 static const std::map<QName,XMLObjectBuilder*>& getBuilders() {
167 * Registers a new builder for the given key.
169 * @param builderKey the key used to retrieve this builder later
170 * @param builder the builder
172 static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {
173 deregisterBuilder(builderKey);
174 m_map[builderKey]=builder;
178 * Registers a default builder
180 * @param builder the default builder
182 static void registerDefaultBuilder(XMLObjectBuilder* builder) {
183 deregisterDefaultBuilder();
188 * Deregisters a builder.
190 * @param builderKey the key for the builder to be deregistered
192 static void deregisterBuilder(const QName& builderKey) {
193 delete getBuilder(builderKey);
194 m_map.erase(builderKey);
198 * Deregisters default builder.
200 static void deregisterDefaultBuilder() {
206 * Unregisters and destroys all registered builders.
208 static void destroyBuilders();
211 XMLObjectBuilder() {}
214 static std::map<QName,XMLObjectBuilder*> m_map;
215 static XMLObjectBuilder* m_default;
220 #if defined (_MSC_VER)
221 #pragma warning( pop )
224 #endif /* __xmltooling_xmlobjbuilder_h__ */