2 * Copyright 2001-2006 Internet2
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 * @file XMLObjectBuilder.h
\r
20 * Factory interface for XMLObjects
\r
23 #if !defined(__xmltooling_xmlobjbuilder_h__)
\r
24 #define __xmltooling_xmlobjbuilder_h__
\r
27 #include <xercesc/dom/DOM.hpp>
\r
28 #include <xmltooling/QName.h>
\r
29 #include <xmltooling/XMLObject.h>
\r
31 using namespace xercesc;
\r
33 #if defined (_MSC_VER)
\r
34 #pragma warning( push )
\r
35 #pragma warning( disable : 4250 4251 )
\r
38 namespace xmltooling {
\r
41 * A factory interface for obtaining XMLObjects.
\r
42 * Subclasses MAY supply additional factory methods.
\r
44 class XMLTOOL_API XMLObjectBuilder
\r
46 MAKE_NONCOPYABLE(XMLObjectBuilder);
\r
48 virtual ~XMLObjectBuilder() {}
\r
51 * Creates an empty XMLObject.
\r
53 * @return the empty XMLObject
\r
55 virtual XMLObject* buildObject() const=0;
\r
58 * Creates an empty XMLObject using the default build method, if a builder can be found.
\r
60 * @param key the key used to locate a builder
\r
61 * @return the empty object or NULL if no builder is available
\r
63 static XMLObject* buildObject(const QName& key) {
\r
64 const XMLObjectBuilder* b=getBuilder(key);
\r
65 return b ? b->buildObject() : NULL;
\r
69 * Retrieves an XMLObjectBuilder using the key it was registered with.
\r
71 * @param key the key used to register the builder
\r
72 * @return the builder or NULL
\r
74 static const XMLObjectBuilder* getBuilder(const QName& key) {
\r
75 std::map<QName,XMLObjectBuilder*>::const_iterator i=m_map.find(key);
\r
76 return (i==m_map.end()) ? NULL : i->second;
\r
80 * Retrieves an XMLObjectBuilder for a given DOM element.
\r
81 * If no match is found, the default builder is returned, if any.
\r
83 * @param element the element for which to locate a builder
\r
84 * @return the builder or NULL
\r
86 static const XMLObjectBuilder* getBuilder(const DOMElement* element);
\r
89 * Retrieves the default XMLObjectBuilder for DOM elements
\r
91 * @return the default builder or NULL
\r
93 static const XMLObjectBuilder* getDefaultBuilder(const DOMElement* element) {
\r
98 * Gets an immutable list of all the builders currently registered.
\r
100 * @return list of all the builders currently registered
\r
102 static const std::map<QName,XMLObjectBuilder*>& getBuilders() {
\r
107 * Registers a new builder for the given key.
\r
109 * @param builderKey the key used to retrieve this builder later
\r
110 * @param builder the builder
\r
112 static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {
\r
113 deregisterBuilder(builderKey);
\r
114 m_map[builderKey]=builder;
\r
118 * Registers a default builder
\r
120 * @param builder the default builder
\r
122 static void registerDefaultBuilder(XMLObjectBuilder* builder) {
\r
123 deregisterDefaultBuilder();
\r
128 * Deregisters a builder.
\r
130 * @param builderKey the key for the builder to be deregistered
\r
132 static void deregisterBuilder(const QName& builderKey) {
\r
133 delete getBuilder(builderKey);
\r
134 m_map.erase(builderKey);
\r
138 * Deregisters default builder.
\r
140 static void deregisterDefaultBuilder() {
\r
146 * Unregisters and destroys all registered builders.
\r
148 static void destroyBuilders();
\r
151 XMLObjectBuilder() {}
\r
154 static std::map<QName,XMLObjectBuilder*> m_map;
\r
155 static XMLObjectBuilder* m_default;
\r
160 #if defined (_MSC_VER)
\r
161 #pragma warning( pop )
\r
164 #endif /* __xmltooling_xmlobjbuilder_h__ */
\r