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.
49 * The results are undefined if elementLocalName is NULL or empty.
51 * @param namespaceURI namespace URI for element
52 * @param elementLocalName local name of element
53 * @param namespacePrefix prefix of element name
54 * @return the empty XMLObject
56 virtual XMLObject* buildObject(
57 const XMLCh* namespaceURI, const XMLCh* elementLocalName, const XMLCh* namespacePrefix=NULL
61 * Creates an empty XMLObject with a particular element name.
63 * @param q QName of element for object
64 * @return the empty XMLObject
66 XMLObject* buildFromQName(const QName& q) const {
67 return buildObject(q.getNamespaceURI(),q.getLocalPart(),q.getPrefix());
71 * Creates an unmarshalled XMLObject from a DOM Element.
73 * @param element the unmarshalling source
74 * @param bindDocument true iff the XMLObject should take ownership of the DOM Document
75 * @return the unmarshalled XMLObject
77 XMLObject* buildFromElement(DOMElement* element, bool bindDocument=false) const {
78 std::auto_ptr<XMLObject> ret(buildObject(element->getNamespaceURI(),element->getLocalName(),element->getPrefix()));
79 ret->unmarshall(element,bindDocument);
84 * Creates an unmarshalled XMLObject from the root of a DOM Document.
86 * @param doc the unmarshalling source
87 * @param bindDocument true iff the XMLObject should take ownership of the DOM Document
88 * @return the unmarshalled XMLObject
90 XMLObject* buildFromDocument(DOMDocument* doc, bool bindDocument=true) const {
91 return buildFromElement(doc->getDocumentElement(),bindDocument);
95 * Creates an empty XMLObject using the default build method, if a builder can be found.
97 * @param key the element key used to locate a builder
98 * @return the empty object or NULL if no builder is available
100 static XMLObject* buildOne(const QName& key) {
101 const XMLObjectBuilder* b=getBuilder(key);
103 return b->buildFromQName(key);
104 b=getDefaultBuilder();
105 return b ? b->buildFromQName(key) : NULL;
109 * Creates an unmarshalled XMLObject using the default build method, if a builder can be found.
111 * @param element the unmarshalling source
112 * @param bindDocument true iff the new XMLObject should take ownership of the DOM Document
113 * @return the unmarshalled object or NULL if no builder is available
115 static XMLObject* buildOneFromElement(DOMElement* element, bool bindDocument=false) {
116 const XMLObjectBuilder* b=getBuilder(element);
117 return b ? b->buildFromElement(element,bindDocument) : NULL;
121 * Retrieves an XMLObjectBuilder using the key it was registered with.
123 * @param key the key used to register the builder
124 * @return the builder or NULL
126 static const XMLObjectBuilder* getBuilder(const QName& key) {
127 std::map<QName,XMLObjectBuilder*>::const_iterator i=m_map.find(key);
128 return (i==m_map.end()) ? NULL : i->second;
132 * Retrieves an XMLObjectBuilder for a given DOM element.
133 * If no match is found, the default builder is returned, if any.
135 * @param element the element for which to locate a builder
136 * @return the builder or NULL
138 static const XMLObjectBuilder* getBuilder(const DOMElement* element);
141 * Retrieves the default XMLObjectBuilder for DOM elements
143 * @return the default builder or NULL
145 static const XMLObjectBuilder* getDefaultBuilder() {
150 * Gets an immutable list of all the builders currently registered.
152 * @return list of all the builders currently registered
154 static const std::map<QName,XMLObjectBuilder*>& getBuilders() {
159 * Registers a new builder for the given key.
161 * @param builderKey the key used to retrieve this builder later
162 * @param builder the builder
164 static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {
165 deregisterBuilder(builderKey);
166 m_map[builderKey]=builder;
170 * Registers a default builder
172 * @param builder the default builder
174 static void registerDefaultBuilder(XMLObjectBuilder* builder) {
175 deregisterDefaultBuilder();
180 * Deregisters a builder.
182 * @param builderKey the key for the builder to be deregistered
184 static void deregisterBuilder(const QName& builderKey) {
185 delete getBuilder(builderKey);
186 m_map.erase(builderKey);
190 * Deregisters default builder.
192 static void deregisterDefaultBuilder() {
198 * Unregisters and destroys all registered builders.
200 static void destroyBuilders();
203 XMLObjectBuilder() {}
206 static std::map<QName,XMLObjectBuilder*> m_map;
207 static XMLObjectBuilder* m_default;
212 #if defined (_MSC_VER)
213 #pragma warning( pop )
216 #endif /* __xmltooling_xmlobjbuilder_h__ */