5d6a82e071c35f1386dad9df6f28d93030dd7e1c
[shibboleth/cpp-xmltooling.git] / xmltooling / io / Marshaller.cpp
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  * Marshaller.cpp\r
19  * \r
20  * Transforms XMLObjects into DOM trees \r
21  */\r
22 \r
23 #include "internal.h"\r
24 #include "Marshaller.h"\r
25 #include "util/NDC.h"\r
26 #include "util/XMLHelper.h"\r
27 \r
28 #include <log4cpp/Category.hh>\r
29 \r
30 using namespace xmltooling;\r
31 using namespace log4cpp;\r
32 using namespace std;\r
33 \r
34 map<QName,Marshaller*> Marshaller::m_map;\r
35 \r
36 Marshaller* Marshaller::m_default=NULL;\r
37 \r
38 const Marshaller* Marshaller::getMarshaller(const XMLObject* xmlObject)\r
39 {\r
40 #ifdef _DEBUG\r
41     xmltooling::NDC ndc("getMarshaller");\r
42 #endif\r
43     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Marshaller");\r
44  \r
45     const QName* type=xmlObject->getSchemaType();\r
46     const Marshaller* m = type ? getMarshaller(*type) : NULL;\r
47     if (m) {\r
48         if (log.isDebugEnabled()) {\r
49             log.debug("located Marshaller for schema type: %s", type->toString().c_str());\r
50         }\r
51         return m;\r
52     }\r
53     \r
54     m = getMarshaller(xmlObject->getElementQName());\r
55     if (m) {\r
56         if (log.isDebugEnabled()) {\r
57             log.debug("located Marshaller for element name: %s", xmlObject->getElementQName().toString().c_str());\r
58         }\r
59         return m;\r
60     }\r
61 \r
62     log.error("no Marshaller registered for element (%s), returning default", xmlObject->getElementQName().toString().c_str());\r
63     return m_default;\r
64 }\r
65 \r
66 void Marshaller::destroyMarshallers()\r
67 {\r
68     for_each(m_map.begin(),m_map.end(),cleanup_pair<QName,Marshaller>());\r
69     m_map.clear();\r
70     deregisterDefaultMarshaller();\r
71 }\r