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>
29 #include <xmltooling/util/XMLHelper.h>
31 #if defined (_MSC_VER)
32 #pragma warning( push )
33 #pragma warning( disable : 4250 4251 )
36 namespace xmltooling {
39 * A factory interface for obtaining XMLObjects.
40 * Subclasses MAY supply additional factory methods.
42 class XMLTOOL_API XMLObjectBuilder
44 MAKE_NONCOPYABLE(XMLObjectBuilder);
46 virtual ~XMLObjectBuilder() {}
49 * Creates an empty XMLObject with a particular element name.
50 * The results are undefined if localName is NULL or empty.
52 * @param nsURI namespace URI for element
53 * @param localName local name of element
54 * @param prefix prefix of element name
55 * @param schemaType xsi:type of the object
56 * @return the empty XMLObject
58 virtual XMLObject* buildObject(
59 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix=NULL, const QName* schemaType=NULL
63 * Creates an empty XMLObject with a particular element name.
65 * @param q QName of element for object
66 * @return the empty XMLObject
68 XMLObject* buildFromQName(const QName& q) const {
69 return buildObject(q.getNamespaceURI(),q.getLocalPart(),q.getPrefix());
73 * Creates an unmarshalled XMLObject from a DOM Element.
75 * @param element the unmarshalling source
76 * @param bindDocument true iff the XMLObject should take ownership of the DOM Document
77 * @return the unmarshalled XMLObject
79 XMLObject* buildFromElement(DOMElement* element, bool bindDocument=false) const {
80 std::auto_ptr<XMLObject> ret(
81 buildObject(element->getNamespaceURI(),element->getLocalName(),element->getPrefix(),XMLHelper::getXSIType(element))
83 ret->unmarshall(element,bindDocument);
88 * Creates an unmarshalled XMLObject from the root of a DOM Document.
90 * @param doc the unmarshalling source
91 * @param bindDocument true iff the XMLObject should take ownership of the DOM Document
92 * @return the unmarshalled XMLObject
94 XMLObject* buildFromDocument(DOMDocument* doc, bool bindDocument=true) const {
95 return buildFromElement(doc->getDocumentElement(),bindDocument);
99 * Creates an empty XMLObject using the default build method, if a builder can be found.
101 * @param key the element key used to locate a builder
102 * @return the empty object or NULL if no builder is available
104 static XMLObject* buildOne(const QName& key) {
105 const XMLObjectBuilder* b=getBuilder(key);
107 return b->buildFromQName(key);
108 b=getDefaultBuilder();
109 return b ? b->buildFromQName(key) : NULL;
113 * Creates an unmarshalled XMLObject using the default build method, if a builder can be found.
115 * @param element the unmarshalling source
116 * @param bindDocument true iff the new XMLObject should take ownership of the DOM Document
117 * @return the unmarshalled object or NULL if no builder is available
119 static XMLObject* buildOneFromElement(DOMElement* element, bool bindDocument=false) {
120 const XMLObjectBuilder* b=getBuilder(element);
121 return b ? b->buildFromElement(element,bindDocument) : NULL;
125 * Retrieves an XMLObjectBuilder using the key it was registered with.
127 * @param key the key used to register the builder
128 * @return the builder or NULL
130 static const XMLObjectBuilder* getBuilder(const QName& key) {
131 std::map<QName,XMLObjectBuilder*>::const_iterator i=m_map.find(key);
132 return (i==m_map.end()) ? NULL : i->second;
136 * Retrieves an XMLObjectBuilder for a given DOM element.
137 * If no match is found, the default builder is returned, if any.
139 * @param element the element for which to locate a builder
140 * @return the builder or NULL
142 static const XMLObjectBuilder* getBuilder(const DOMElement* element);
145 * Retrieves the default XMLObjectBuilder for DOM elements
147 * @return the default builder or NULL
149 static const XMLObjectBuilder* getDefaultBuilder() {
154 * Gets an immutable list of all the builders currently registered.
156 * @return list of all the builders currently registered
158 static const std::map<QName,XMLObjectBuilder*>& getBuilders() {
163 * Registers a new builder for the given key.
165 * @param builderKey the key used to retrieve this builder later
166 * @param builder the builder
168 static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {
169 deregisterBuilder(builderKey);
170 m_map[builderKey]=builder;
174 * Registers a default builder
176 * @param builder the default builder
178 static void registerDefaultBuilder(XMLObjectBuilder* builder) {
179 deregisterDefaultBuilder();
184 * Deregisters a builder.
186 * @param builderKey the key for the builder to be deregistered
188 static void deregisterBuilder(const QName& builderKey) {
189 delete getBuilder(builderKey);
190 m_map.erase(builderKey);
194 * Deregisters default builder.
196 static void deregisterDefaultBuilder() {
202 * Unregisters and destroys all registered builders.
204 static void destroyBuilders();
207 XMLObjectBuilder() {}
210 static std::map<QName,XMLObjectBuilder*> m_map;
211 static XMLObjectBuilder* m_default;
216 #if defined (_MSC_VER)
217 #pragma warning( pop )
220 #endif /* __xmltooling_xmlobjbuilder_h__ */