Initial unit test plus fixes
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractXMLObject.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 AbstractXMLObject.h\r
19  * \r
20  * An abstract implementation of XMLObject.\r
21  */\r
22 \r
23 #if !defined(__xmltooling_abstractxmlobj_h__)\r
24 #define __xmltooling_abstractxmlobj_h__\r
25 \r
26 #include <xmltooling/XMLObject.h>\r
27 \r
28 #if defined (_MSC_VER)\r
29     #pragma warning( push )\r
30     #pragma warning( disable : 4250 4251 )\r
31 #endif\r
32 \r
33 namespace xmltooling {\r
34 \r
35     /**\r
36      * An abstract implementation of XMLObject.\r
37      */\r
38     class XMLTOOL_API AbstractXMLObject : public virtual XMLObject\r
39     {\r
40     public:\r
41         virtual ~AbstractXMLObject() {\r
42             delete m_typeQname;\r
43         }\r
44 \r
45         /**\r
46          * @see XMLObject::getElementQName()\r
47          */\r
48         const QName& getElementQName() const {\r
49             return m_elementQname;\r
50         }\r
51 \r
52         /**\r
53          * @see XMLObject::setElementNamespacePrefix()\r
54          */\r
55         void setElementNamespacePrefix(const XMLCh* prefix) {\r
56             m_elementQname.setPrefix(prefix);\r
57         }\r
58 \r
59         /**\r
60          * @see XMLObject::getNamespaces()\r
61          */\r
62         const std::set<Namespace>& getNamespaces() const {\r
63             return m_namespaces;\r
64         }\r
65     \r
66         /**\r
67          * @see XMLObject::addNamespace()\r
68          */\r
69         void addNamespace(const Namespace& ns) {\r
70             if (ns.alwaysDeclare() || m_namespaces.find(ns)==m_namespaces.end()) {\r
71                 m_namespaces.insert(ns);\r
72             }\r
73         }\r
74     \r
75         /**\r
76          * @see XMLObject::removeNamespace()\r
77          */\r
78         void removeNamespace(const Namespace& ns) {\r
79             m_namespaces.erase(ns);\r
80         }\r
81         \r
82         /**\r
83          * @see XMLObject::getSchemaType()\r
84          */\r
85         const QName* getSchemaType() const {\r
86             return m_typeQname;\r
87         }\r
88     \r
89         /**\r
90          * @see XMLObject::setSchemaType()\r
91          */\r
92         void setSchemaType(const QName* type) {\r
93             delete m_typeQname;\r
94             m_typeQname = NULL;\r
95             if (type) {\r
96                 m_typeQname = new QName(*type);\r
97                 addNamespace(Namespace(type->getNamespaceURI(), type->getPrefix()));\r
98             }\r
99         }\r
100     \r
101         /**\r
102          * @see XMLObject::hasParent()\r
103          */\r
104         bool hasParent() const {\r
105             return m_parent != NULL;\r
106         }\r
107      \r
108         /**\r
109          * @see XMLObject::getParent()\r
110          */\r
111         XMLObject* getParent() const {\r
112             return m_parent;\r
113         }\r
114     \r
115         /**\r
116          * @see XMLObject::setParent()\r
117          */\r
118         void setParent(XMLObject* parent) {\r
119             m_parent = parent;\r
120         }\r
121     \r
122      protected:\r
123          AbstractXMLObject() : m_typeQname(NULL), m_parent(NULL) {}\r
124 \r
125         /**\r
126          * Constructor\r
127          * \r
128          * @param namespaceURI the namespace the element is in\r
129          * @param elementLocalName the local name of the XML element this Object represents\r
130          */\r
131         AbstractXMLObject(const XMLCh* namespaceURI, const XMLCh* elementLocalName, const XMLCh* namespacePrefix)\r
132             : m_elementQname(namespaceURI,elementLocalName, namespacePrefix), m_typeQname(NULL), m_parent(NULL) {\r
133             addNamespace(Namespace(namespaceURI, namespacePrefix));\r
134         }\r
135         \r
136     private:\r
137         XMLObject* m_parent;\r
138         QName m_elementQname;\r
139         QName* m_typeQname;\r
140         std::set<Namespace> m_namespaces;\r
141     };\r
142 \r
143 };\r
144 \r
145 #if defined (_MSC_VER)\r
146     #pragma warning( pop )\r
147 #endif\r
148 \r
149 #endif /* __xmltooling_abstractxmlobj_h__ */\r