Update copyright.
[shibboleth/cpp-xmltooling.git] / xmltooling / XMLObjectBuilder.h
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file XMLObjectBuilder.h
19  * 
20  * Factory interface for XMLObjects 
21  */
22
23 #if !defined(__xmltooling_xmlobjbuilder_h__)
24 #define __xmltooling_xmlobjbuilder_h__
25
26 #include <map>
27 #include <xmltooling/QName.h>
28 #include <xmltooling/XMLObject.h>
29 #include <xmltooling/util/XMLHelper.h>
30
31 #if defined (_MSC_VER)
32     #pragma warning( push )
33     #pragma warning( disable : 4250 4251 )
34 #endif
35
36 namespace xmltooling {
37
38     /**
39      * A factory interface for obtaining XMLObjects.
40      * Subclasses MAY supply additional factory methods.
41      */
42     class XMLTOOL_API XMLObjectBuilder
43     {
44     MAKE_NONCOPYABLE(XMLObjectBuilder);
45     public:
46         virtual ~XMLObjectBuilder() {}
47         
48         /**
49          * Creates an empty XMLObject with a particular element name.
50          * The results are undefined if localName is NULL or empty.
51          * 
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
57          */
58         virtual XMLObject* buildObject(
59             const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix=NULL, const QName* schemaType=NULL
60             ) const=0;
61
62         /**
63          * Creates an empty XMLObject with a particular element name.
64          * 
65          * @param q     QName of element for object
66          * @return the empty XMLObject
67          */
68         XMLObject* buildFromQName(const QName& q) const {
69             return buildObject(q.getNamespaceURI(),q.getLocalPart(),q.getPrefix());
70         }
71
72         /**
73          * Creates an unmarshalled XMLObject from a DOM Element.
74          * 
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
78          */
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))
82                 );
83             ret->unmarshall(element,bindDocument);
84             return ret.release();
85         }
86
87         /**
88          * Creates an unmarshalled XMLObject from the root of a DOM Document.
89          * 
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
93          */
94         XMLObject* buildFromDocument(DOMDocument* doc, bool bindDocument=true) const {
95             return buildFromElement(doc->getDocumentElement(),bindDocument);
96         }
97
98         /**
99          * Creates an unmarshalled XMLObject using the default build method, if a builder can be found.
100          * 
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 
104          */
105         static XMLObject* buildOneFromElement(DOMElement* element, bool bindDocument=false) {
106             const XMLObjectBuilder* b=getBuilder(element);
107             return b ? b->buildFromElement(element,bindDocument) : NULL;
108         }
109
110         /**
111          * Retrieves an XMLObjectBuilder using the key it was registered with.
112          * 
113          * @param key the key used to register the builder
114          * @return the builder or NULL
115          */
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;
119         }
120
121         /**
122          * Retrieves an XMLObjectBuilder for a given DOM element.
123          * If no match is found, the default builder is returned, if any.
124          * 
125          * @param element the element for which to locate a builder
126          * @return the builder or NULL
127          */
128         static const XMLObjectBuilder* getBuilder(const DOMElement* element);
129
130         /**
131          * Retrieves the default XMLObjectBuilder for DOM elements
132          * 
133          * @return the default builder or NULL
134          */
135         static const XMLObjectBuilder* getDefaultBuilder() {
136             return m_default;
137         }
138
139         /**
140          * Gets an immutable list of all the builders currently registered.
141          * 
142          * @return list of all the builders currently registered
143          */
144         static const std::map<QName,XMLObjectBuilder*>& getBuilders() {
145             return m_map;
146         }
147     
148         /**
149          * Registers a new builder for the given key.
150          * 
151          * @param builderKey the key used to retrieve this builder later
152          * @param builder the builder
153          */
154         static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {
155             deregisterBuilder(builderKey);
156             m_map[builderKey]=builder;
157         }
158
159         /**
160          * Registers a default builder
161          * 
162          * @param builder the default builder
163          */
164         static void registerDefaultBuilder(XMLObjectBuilder* builder) {
165             deregisterDefaultBuilder();
166             m_default=builder;
167         }
168
169         /**
170          * Deregisters a builder.
171          * 
172          * @param builderKey the key for the builder to be deregistered
173          */
174         static void deregisterBuilder(const QName& builderKey) {
175             delete getBuilder(builderKey);
176             m_map.erase(builderKey);
177         }
178
179         /**
180          * Deregisters default builder.
181          */
182         static void deregisterDefaultBuilder() {
183             delete m_default;
184             m_default=NULL;
185         }
186
187         /**
188          * Unregisters and destroys all registered builders. 
189          */
190         static void destroyBuilders();
191
192     protected:
193         XMLObjectBuilder() {}
194     
195     private:
196         static std::map<QName,XMLObjectBuilder*> m_map;
197         static XMLObjectBuilder* m_default;
198     };
199
200 };
201
202 #if defined (_MSC_VER)
203     #pragma warning( pop )
204 #endif
205
206 #endif /* __xmltooling_xmlobjbuilder_h__ */