From 56b054e33a9f6839d345f7c2c4b052eb701f136d Mon Sep 17 00:00:00 2001 From: cantor Date: Tue, 21 Feb 2006 19:47:02 +0000 Subject: [PATCH] Add factory methods and default builder git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/trunk@23 de75baf8-a10c-0410-a50a-987c0e22f00f --- xmltooling/XMLObjectBuilder.cpp | 71 +++++++++++++++++++++++++++++++++++++++++ xmltooling/XMLObjectBuilder.h | 58 +++++++++++++++++++++++++-------- 2 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 xmltooling/XMLObjectBuilder.cpp diff --git a/xmltooling/XMLObjectBuilder.cpp b/xmltooling/XMLObjectBuilder.cpp new file mode 100644 index 0000000..c3aa881 --- /dev/null +++ b/xmltooling/XMLObjectBuilder.cpp @@ -0,0 +1,71 @@ +/* + * Copyright 2001-2006 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * XMLObjectBuilder.cpp + * + * Factory interface for XMLObjects + */ + +#include "internal.h" +#include "XMLObjectBuilder.h" +#include "util/NDC.h" +#include "util/XMLHelper.h" + +#include + +using namespace xmltooling; +using namespace log4cpp; +using namespace std; + +map XMLObjectBuilder::m_map; +XMLObjectBuilder* XMLObjectBuilder::m_default=NULL; + +const XMLObjectBuilder* XMLObjectBuilder::getBuilder(const DOMElement* domElement) +{ +#ifdef _DEBUG + xmltooling::NDC ndc("getBuilder"); +#endif + Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObjectBuilder"); + + auto_ptr schemaType(XMLHelper::getXSIType(domElement)); + const XMLObjectBuilder* xmlObjectBuilder = getBuilder(*(schemaType.get())); + if (xmlObjectBuilder) { + if (log.isDebugEnabled()) { + log.debug("Located XMLObjectBuilder for schema type: %s", schemaType->toString().c_str()); + } + return xmlObjectBuilder; + } + + auto_ptr elementName(XMLHelper::getNodeQName(domElement)); + xmlObjectBuilder = getBuilder(*(elementName.get())); + if (xmlObjectBuilder) { + if (log.isDebugEnabled()) { + log.debug("Located XMLObjectBuilder for element name: %s", elementName->toString().c_str()); + } + return xmlObjectBuilder; + } + + log.error("No XMLObjectBuilder was registered for element: %s", elementName->toString().c_str()); + return NULL; +} + +void XMLObjectBuilder::destroyBuilders() +{ + for_each(m_map.begin(),m_map.end(),cleanup_pair()); + m_map.clear(); + deregisterDefaultBuilder(); +} diff --git a/xmltooling/XMLObjectBuilder.h b/xmltooling/XMLObjectBuilder.h index cd746f2..43e9daa 100644 --- a/xmltooling/XMLObjectBuilder.h +++ b/xmltooling/XMLObjectBuilder.h @@ -24,9 +24,12 @@ #define __xmltooling_xmlobjbuilder_h__ #include +#include #include #include +using namespace xercesc; + #if defined (_MSC_VER) #pragma warning( push ) #pragma warning( disable : 4250 4251 ) @@ -49,27 +52,36 @@ namespace xmltooling { * * @return the empty XMLObject */ - virtual XMLObject* buildObject()=0; + virtual XMLObject* buildObject() const=0; /** - * Resets the state of the builder. - * - * This normally means null'ing out any properties that were - * needed to build an object. - */ - virtual void resetState()=0; - - /** * Retrieves an XMLObjectBuilder using the key it was registered with. * * @param key the key used to register the builder * @return the builder */ - static XMLObjectBuilder* getBuilder(const QName& key) { + static const XMLObjectBuilder* getBuilder(const QName& key) { std::map::const_iterator i=m_map.find(key); return (i==m_map.end()) ? NULL : i->second; } - + + /** + * Retrieves an XMLObjectBuilder for a given DOM element + * + * @param element the element for which to locate a builder + * @return the builder or NULL + */ + static const XMLObjectBuilder* getBuilder(const DOMElement* element); + + /** + * Retrieves the default XMLObjectBuilder for DOM elements + * + * @return the default builder or NULL + */ + static const XMLObjectBuilder* getDefaultBuilder(const DOMElement* element) { + return m_default; + } + /** * Gets an immutable list of all the builders currently registered. * @@ -86,9 +98,20 @@ namespace xmltooling { * @param builder the builder */ static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) { + deregisterBuilder(builderKey); m_map[builderKey]=builder; } - + + /** + * Registers a default builder + * + * @param builder the default builder + */ + static void registerDefaultBuilder(XMLObjectBuilder* builder) { + deregisterDefaultBuilder(); + m_default=builder; + } + /** * Deregisters a builder. * @@ -98,7 +121,15 @@ namespace xmltooling { delete getBuilder(builderKey); m_map.erase(builderKey); } - + + /** + * Deregisters default builder. + */ + static void deregisterDefaultBuilder() { + delete m_default; + m_default=NULL; + } + /** * Unregisters and destroys all registered builders. */ @@ -106,6 +137,7 @@ namespace xmltooling { private: static std::map m_map; + static XMLObjectBuilder* m_default; }; }; -- 2.1.4