Shortcuts for unmarshalling in builder interface, adjusted tests
[shibboleth/cpp-xmltooling.git] / xmltooling / XMLObjectBuilder.h
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * @file XMLObjectBuilder.h\r
19  * \r
20  * Factory interface for XMLObjects \r
21  */\r
22 \r
23 #if !defined(__xmltooling_xmlobjbuilder_h__)\r
24 #define __xmltooling_xmlobjbuilder_h__\r
25 \r
26 #include <map>\r
27 #include <xmltooling/QName.h>\r
28 #include <xmltooling/XMLObject.h>\r
29 \r
30 #if defined (_MSC_VER)\r
31     #pragma warning( push )\r
32     #pragma warning( disable : 4250 4251 )\r
33 #endif\r
34 \r
35 namespace xmltooling {\r
36 \r
37     /**\r
38      * A factory interface for obtaining XMLObjects.\r
39      * Subclasses MAY supply additional factory methods.\r
40      */\r
41     class XMLTOOL_API XMLObjectBuilder\r
42     {\r
43     MAKE_NONCOPYABLE(XMLObjectBuilder);\r
44     public:\r
45         virtual ~XMLObjectBuilder() {}\r
46         \r
47         /**\r
48          * Creates an empty XMLObject.\r
49          * \r
50          * @return the empty XMLObject\r
51          */\r
52         virtual XMLObject* buildObject() const=0;\r
53 \r
54         /**\r
55          * Creates an unmarshalled XMLObject from a DOM Element.\r
56          * \r
57          * @param element       the unmarshalling source\r
58          * @param bindDocument  true iff the XMLObject should take ownership of the DOM Document\r
59          * @return the unmarshalled XMLObject\r
60          */\r
61         virtual XMLObject* buildFromElement(DOMElement* element, bool bindDocument=false) const {\r
62             std::auto_ptr<XMLObject> ret(buildObject());\r
63             ret->unmarshall(element,bindDocument);\r
64             return ret.release();\r
65         }\r
66 \r
67         /**\r
68          * Creates an unmarshalled XMLObject from the root of a DOM Document.\r
69          * \r
70          * @param doc           the unmarshalling source\r
71          * @param bindDocument  true iff the XMLObject should take ownership of the DOM Document\r
72          * @return the unmarshalled XMLObject\r
73          */\r
74         virtual XMLObject* buildFromDocument(DOMDocument* doc, bool bindDocument=true) const {\r
75             return buildFromElement(doc->getDocumentElement(),bindDocument);\r
76         }\r
77 \r
78         /**\r
79          * Creates an empty XMLObject using the default build method, if a builder can be found.\r
80          * \r
81          * @param key   the key used to locate a builder\r
82          * @return  the empty object or NULL if no builder is available \r
83          */\r
84         static XMLObject* buildOne(const QName& key) {\r
85             const XMLObjectBuilder* b=getBuilder(key);\r
86             if (b)\r
87                 return b->buildObject();\r
88             b=getDefaultBuilder();\r
89             return b ? b->buildObject() : NULL;\r
90         }\r
91 \r
92         /**\r
93          * Creates an unmarshalled XMLObject using the default build method, if a builder can be found.\r
94          * \r
95          * @param element       the unmarshalling source\r
96          * @param bindDocument  true iff the new XMLObject should take ownership of the DOM Document\r
97          * @return  the unmarshalled object or NULL if no builder is available \r
98          */\r
99         static XMLObject* buildOneFromElement(DOMElement* element, bool bindDocument=false) {\r
100             const XMLObjectBuilder* b=getBuilder(element);\r
101             return b ? b->buildFromElement(element,bindDocument) : NULL;\r
102         }\r
103 \r
104         /**\r
105          * Retrieves an XMLObjectBuilder using the key it was registered with.\r
106          * \r
107          * @param key the key used to register the builder\r
108          * @return the builder or NULL\r
109          */\r
110         static const XMLObjectBuilder* getBuilder(const QName& key) {\r
111             std::map<QName,XMLObjectBuilder*>::const_iterator i=m_map.find(key);\r
112             return (i==m_map.end()) ? NULL : i->second;\r
113         }\r
114 \r
115         /**\r
116          * Retrieves an XMLObjectBuilder for a given DOM element.\r
117          * If no match is found, the default builder is returned, if any.\r
118          * \r
119          * @param element the element for which to locate a builder\r
120          * @return the builder or NULL\r
121          */\r
122         static const XMLObjectBuilder* getBuilder(const DOMElement* element);\r
123 \r
124         /**\r
125          * Retrieves the default XMLObjectBuilder for DOM elements\r
126          * \r
127          * @return the default builder or NULL\r
128          */\r
129         static const XMLObjectBuilder* getDefaultBuilder() {\r
130             return m_default;\r
131         }\r
132 \r
133         /**\r
134          * Gets an immutable list of all the builders currently registered.\r
135          * \r
136          * @return list of all the builders currently registered\r
137          */\r
138         static const std::map<QName,XMLObjectBuilder*>& getBuilders() {\r
139             return m_map;\r
140         }\r
141     \r
142         /**\r
143          * Registers a new builder for the given key.\r
144          * \r
145          * @param builderKey the key used to retrieve this builder later\r
146          * @param builder the builder\r
147          */\r
148         static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {\r
149             deregisterBuilder(builderKey);\r
150             m_map[builderKey]=builder;\r
151         }\r
152 \r
153         /**\r
154          * Registers a default builder\r
155          * \r
156          * @param builder the default builder\r
157          */\r
158         static void registerDefaultBuilder(XMLObjectBuilder* builder) {\r
159             deregisterDefaultBuilder();\r
160             m_default=builder;\r
161         }\r
162 \r
163         /**\r
164          * Deregisters a builder.\r
165          * \r
166          * @param builderKey the key for the builder to be deregistered\r
167          */\r
168         static void deregisterBuilder(const QName& builderKey) {\r
169             delete getBuilder(builderKey);\r
170             m_map.erase(builderKey);\r
171         }\r
172 \r
173         /**\r
174          * Deregisters default builder.\r
175          */\r
176         static void deregisterDefaultBuilder() {\r
177             delete m_default;\r
178             m_default=NULL;\r
179         }\r
180 \r
181         /**\r
182          * Unregisters and destroys all registered builders. \r
183          */\r
184         static void destroyBuilders();\r
185 \r
186     protected:\r
187         XMLObjectBuilder() {}\r
188     \r
189     private:\r
190         static std::map<QName,XMLObjectBuilder*> m_map;\r
191         static XMLObjectBuilder* m_default;\r
192     };\r
193 \r
194 };\r
195 \r
196 #if defined (_MSC_VER)\r
197     #pragma warning( pop )\r
198 #endif\r
199 \r
200 #endif /* __xmltooling_xmlobjbuilder_h__ */\r