df3ca1861f4817dfc2b01d6f0513945d802e18eb
[shibboleth/cpp-xmltooling.git] / xmltooling / XMLObjectBuilder.h
1 /*
2  *  Copyright 2001-2009 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 xmltooling/XMLObjectBuilder.h
19  * 
20  * Factory interface for XMLObjects.
21  */
22
23 #ifndef __xmltooling_xmlobjbuilder_h__
24 #define __xmltooling_xmlobjbuilder_h__
25
26 #include <xmltooling/QName.h>
27 #include <xmltooling/XMLObject.h>
28 #include <xmltooling/util/XMLHelper.h>
29
30 #include <map>
31 #include <memory>
32
33 #if defined (_MSC_VER)
34     #pragma warning( push )
35     #pragma warning( disable : 4250 4251 )
36 #endif
37
38 namespace xmltooling {
39
40     /**
41      * A factory interface for obtaining XMLObjects.
42      * Subclasses MAY supply additional factory methods.
43      */
44     class XMLTOOL_API XMLObjectBuilder
45     {
46     MAKE_NONCOPYABLE(XMLObjectBuilder);
47     public:
48         virtual ~XMLObjectBuilder();
49         
50         /**
51          * Creates an empty XMLObject with a particular element name.
52          * <p>The results are undefined if localName is NULL or empty.
53          * 
54          * @param nsURI         namespace URI for element
55          * @param localName     local name of element
56          * @param prefix        prefix of element name
57          * @param schemaType    xsi:type of the object
58          * @return the empty XMLObject
59          */
60         virtual XMLObject* buildObject(
61             const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix=NULL, const QName* schemaType=NULL
62             ) const=0;
63
64         /**
65          * Creates an empty XMLObject with a particular element name.
66          * 
67          * @param q     QName of element for object
68          * @return the empty XMLObject
69          */
70         XMLObject* buildFromQName(const QName& q) const;
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(xercesc::DOMElement* element, bool bindDocument=false) const;
80
81         /**
82          * Creates an unmarshalled XMLObject from the root of a DOM Document.
83          * 
84          * @param doc           the unmarshalling source
85          * @param bindDocument  true iff the XMLObject should take ownership of the DOM Document
86          * @return the unmarshalled XMLObject
87          */
88         XMLObject* buildFromDocument(xercesc::DOMDocument* doc, bool bindDocument=true) const;
89
90         /**
91          * Creates an unmarshalled XMLObject using the default build method, if a builder can be found.
92          * 
93          * @param element       the unmarshalling source
94          * @param bindDocument  true iff the new XMLObject should take ownership of the DOM Document
95          * @return  the unmarshalled object or NULL if no builder is available 
96          */
97         static XMLObject* buildOneFromElement(xercesc::DOMElement* element, bool bindDocument=false);
98
99         /**
100          * Retrieves an XMLObjectBuilder using the key it was registered with.
101          * 
102          * @param key the key used to register the builder
103          * @return the builder or NULL
104          */
105         static const XMLObjectBuilder* getBuilder(const QName& key);
106
107         /**
108          * Retrieves an XMLObjectBuilder for a given DOM element.
109          * If no match is found, the default builder is returned, if any.
110          * 
111          * @param element the element for which to locate a builder
112          * @return the builder or NULL
113          */
114         static const XMLObjectBuilder* getBuilder(const xercesc::DOMElement* element);
115
116         /**
117          * Retrieves the default XMLObjectBuilder for DOM elements
118          * 
119          * @return the default builder or NULL
120          */
121         static const XMLObjectBuilder* getDefaultBuilder();
122
123         /**
124          * Gets an immutable list of all the builders currently registered.
125          * 
126          * @return list of all the builders currently registered
127          */
128         static const std::map<QName,XMLObjectBuilder*>& getBuilders();
129     
130         /**
131          * Registers a new builder for the given key.
132          * 
133          * @param builderKey the key used to retrieve this builder later
134          * @param builder the builder
135          */
136         static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder);
137
138         /**
139          * Registers a default builder
140          * 
141          * @param builder the default builder
142          */
143         static void registerDefaultBuilder(XMLObjectBuilder* builder);
144
145         /**
146          * Deregisters a builder.
147          * 
148          * @param builderKey the key for the builder to be deregistered
149          */
150         static void deregisterBuilder(const QName& builderKey);
151
152         /**
153          * Deregisters default builder.
154          */
155         static void deregisterDefaultBuilder();
156
157         /**
158          * Unregisters and destroys all registered builders. 
159          */
160         static void destroyBuilders();
161
162     protected:
163         XMLObjectBuilder();
164     
165     private:
166         static std::map<QName,XMLObjectBuilder*> m_map;
167         static XMLObjectBuilder* m_default;
168     };
169
170 };
171
172 #if defined (_MSC_VER)
173     #pragma warning( pop )
174 #endif
175
176 #endif /* __xmltooling_xmlobjbuilder_h__ */