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 unmarshalled XMLObject using the default build method, if a builder can be found.
101 * @param element the unmarshalling source
102 * @param bindDocument true iff the new XMLObject should take ownership of the DOM Document
103 * @return the unmarshalled object or NULL if no builder is available
105 static XMLObject* buildOneFromElement(DOMElement* element, bool bindDocument=false) {
106 const XMLObjectBuilder* b=getBuilder(element);
107 return b ? b->buildFromElement(element,bindDocument) : NULL;
111 * Retrieves an XMLObjectBuilder using the key it was registered with.
113 * @param key the key used to register the builder
114 * @return the builder or NULL
116 static const XMLObjectBuilder* getBuilder(const QName& key) {
117 std::map<QName,XMLObjectBuilder*>::const_iterator i=m_map.find(key);
118 return (i==m_map.end()) ? NULL : i->second;
122 * Retrieves an XMLObjectBuilder for a given DOM element.
123 * If no match is found, the default builder is returned, if any.
125 * @param element the element for which to locate a builder
126 * @return the builder or NULL
128 static const XMLObjectBuilder* getBuilder(const DOMElement* element);
131 * Retrieves the default XMLObjectBuilder for DOM elements
133 * @return the default builder or NULL
135 static const XMLObjectBuilder* getDefaultBuilder() {
140 * Gets an immutable list of all the builders currently registered.
142 * @return list of all the builders currently registered
144 static const std::map<QName,XMLObjectBuilder*>& getBuilders() {
149 * Registers a new builder for the given key.
151 * @param builderKey the key used to retrieve this builder later
152 * @param builder the builder
154 static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {
155 deregisterBuilder(builderKey);
156 m_map[builderKey]=builder;
160 * Registers a default builder
162 * @param builder the default builder
164 static void registerDefaultBuilder(XMLObjectBuilder* builder) {
165 deregisterDefaultBuilder();
170 * Deregisters a builder.
172 * @param builderKey the key for the builder to be deregistered
174 static void deregisterBuilder(const QName& builderKey) {
175 delete getBuilder(builderKey);
176 m_map.erase(builderKey);
180 * Deregisters default builder.
182 static void deregisterDefaultBuilder() {
188 * Unregisters and destroys all registered builders.
190 static void destroyBuilders();
193 XMLObjectBuilder() {}
196 static std::map<QName,XMLObjectBuilder*> m_map;
197 static XMLObjectBuilder* m_default;
202 #if defined (_MSC_VER)
203 #pragma warning( pop )
206 #endif /* __xmltooling_xmlobjbuilder_h__ */