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