Merged marshalling/unmarshalling methods into core interface.
[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 <xercesc/dom/DOM.hpp>\r
28 #include <xmltooling/QName.h>\r
29 #include <xmltooling/XMLObject.h>\r
30 \r
31 using namespace xercesc;\r
32 \r
33 #if defined (_MSC_VER)\r
34     #pragma warning( push )\r
35     #pragma warning( disable : 4250 4251 )\r
36 #endif\r
37 \r
38 namespace xmltooling {\r
39 \r
40     /**\r
41      * A factory interface for obtaining XMLObjects.\r
42      * Subclasses MAY supply additional factory methods.\r
43      */\r
44     class XMLTOOL_API XMLObjectBuilder\r
45     {\r
46     MAKE_NONCOPYABLE(XMLObjectBuilder);\r
47     public:\r
48         virtual ~XMLObjectBuilder() {}\r
49         \r
50         /**\r
51          * Creates an empty XMLObject.\r
52          * \r
53          * @param e     a construction hint based on the eventual unmarshalling source\r
54          * @return the empty XMLObject\r
55          */\r
56         virtual XMLObject* buildObject(const DOMElement* e=NULL) const=0;\r
57 \r
58         /**\r
59          * Creates an empty XMLObject using the default build method, if a builder can be found.\r
60          * \r
61          * @param key   the key used to locate a builder\r
62          * @param e     a construction hint based on the eventual unmarshalling source\r
63          * @return  the empty object or NULL if no builder is available \r
64          */\r
65         static XMLObject* buildObject(const QName& key, const DOMElement* e=NULL) {\r
66             const XMLObjectBuilder* b=getBuilder(key);\r
67             return b ? b->buildObject(e) : NULL;\r
68         }\r
69 \r
70         /**\r
71          * Retrieves an XMLObjectBuilder using the key it was registered with.\r
72          * \r
73          * @param key the key used to register the builder\r
74          * @return the builder or NULL\r
75          */\r
76         static const XMLObjectBuilder* getBuilder(const QName& key) {\r
77             std::map<QName,XMLObjectBuilder*>::const_iterator i=m_map.find(key);\r
78             return (i==m_map.end()) ? NULL : i->second;\r
79         }\r
80 \r
81         /**\r
82          * Retrieves an XMLObjectBuilder for a given DOM element.\r
83          * If no match is found, the default builder is returned, if any.\r
84          * \r
85          * @param element the element for which to locate a builder\r
86          * @return the builder or NULL\r
87          */\r
88         static const XMLObjectBuilder* getBuilder(const DOMElement* element);\r
89 \r
90         /**\r
91          * Retrieves the default XMLObjectBuilder for DOM elements\r
92          * \r
93          * @return the default builder or NULL\r
94          */\r
95         static const XMLObjectBuilder* getDefaultBuilder(const DOMElement* element) {\r
96             return m_default;\r
97         }\r
98 \r
99         /**\r
100          * Gets an immutable list of all the builders currently registered.\r
101          * \r
102          * @return list of all the builders currently registered\r
103          */\r
104         static const std::map<QName,XMLObjectBuilder*>& getBuilders() {\r
105             return m_map;\r
106         }\r
107     \r
108         /**\r
109          * Registers a new builder for the given key.\r
110          * \r
111          * @param builderKey the key used to retrieve this builder later\r
112          * @param builder the builder\r
113          */\r
114         static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {\r
115             deregisterBuilder(builderKey);\r
116             m_map[builderKey]=builder;\r
117         }\r
118 \r
119         /**\r
120          * Registers a default builder\r
121          * \r
122          * @param builder the default builder\r
123          */\r
124         static void registerDefaultBuilder(XMLObjectBuilder* builder) {\r
125             deregisterDefaultBuilder();\r
126             m_default=builder;\r
127         }\r
128 \r
129         /**\r
130          * Deregisters a builder.\r
131          * \r
132          * @param builderKey the key for the builder to be deregistered\r
133          */\r
134         static void deregisterBuilder(const QName& builderKey) {\r
135             delete getBuilder(builderKey);\r
136             m_map.erase(builderKey);\r
137         }\r
138 \r
139         /**\r
140          * Deregisters default builder.\r
141          */\r
142         static void deregisterDefaultBuilder() {\r
143             delete m_default;\r
144             m_default=NULL;\r
145         }\r
146 \r
147         /**\r
148          * Unregisters and destroys all registered builders. \r
149          */\r
150         static void destroyBuilders();\r
151 \r
152     protected:\r
153         XMLObjectBuilder() {}\r
154     \r
155     private:\r
156         static std::map<QName,XMLObjectBuilder*> m_map;\r
157         static XMLObjectBuilder* m_default;\r
158     };\r
159 \r
160 };\r
161 \r
162 #if defined (_MSC_VER)\r
163     #pragma warning( pop )\r
164 #endif\r
165 \r
166 #endif /* __xmltooling_xmlobjbuilder_h__ */\r