Moved DOM methods up the tree, add copy c'tors, KeyInfo sample
[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 \r
43         const QName& getElementQName() const {\r
44             return m_elementQname;\r
45         }\r
46 \r
47         const std::set<Namespace>& getNamespaces() const {\r
48             return m_namespaces;\r
49         }\r
50     \r
51         void addNamespace(const Namespace& ns) const {\r
52             if (ns.alwaysDeclare() || m_namespaces.find(ns)==m_namespaces.end()) {\r
53                 m_namespaces.insert(ns);\r
54             }\r
55         }\r
56     \r
57         void removeNamespace(const Namespace& ns) {\r
58             m_namespaces.erase(ns);\r
59         }\r
60         \r
61         const QName* getSchemaType() const {\r
62             return m_typeQname;\r
63         }\r
64     \r
65         void setSchemaType(const QName* type) {\r
66             delete m_typeQname;\r
67             m_typeQname = NULL;\r
68             if (type) {\r
69                 m_typeQname = new QName(*type);\r
70                 addNamespace(Namespace(type->getNamespaceURI(), type->getPrefix()));\r
71             }\r
72         }\r
73     \r
74         bool hasParent() const {\r
75             return m_parent != NULL;\r
76         }\r
77      \r
78         XMLObject* getParent() const {\r
79             return m_parent;\r
80         }\r
81     \r
82         void setParent(XMLObject* parent) {\r
83             m_parent = parent;\r
84         }\r
85 \r
86         bool hasChildren() const {\r
87             return !m_children.empty();\r
88         }\r
89 \r
90         const std::list<XMLObject*>& getOrderedChildren() const {\r
91             return m_children;\r
92         }\r
93 \r
94      protected:\r
95         /**\r
96          * Constructor\r
97          * \r
98          * @param namespaceURI the namespace the element is in\r
99          * @param elementLocalName the local name of the XML element this Object represents\r
100          * @param namespacePrefix the namespace prefix to use\r
101          */\r
102         AbstractXMLObject(const XMLCh* namespaceURI=NULL, const XMLCh* elementLocalName=NULL, const XMLCh* namespacePrefix=NULL);\r
103 \r
104         /** Copy constructor. */\r
105         AbstractXMLObject(const AbstractXMLObject& src);\r
106         \r
107         /**\r
108          * A helper function for derived classes.\r
109          * This 'normalizes' newString, and then if it is different from oldString,\r
110          * it invalidates the DOM, frees the old string, and return the new.\r
111          * If not different, it frees the new string and just returns the old value.\r
112          * \r
113          * @param oldValue - the current value\r
114          * @param newValue - the new value\r
115          * \r
116          * @return the value that should be assigned\r
117          */\r
118         XMLCh* prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue) {\r
119             XMLCh* newString = XMLString::replicate(newValue);\r
120             XMLString::trim(newString);\r
121             if (!XMLString::equals(oldValue,newValue)) {\r
122                 releaseThisandParentDOM();\r
123                 XMLString::release(&oldValue);\r
124                 return newString;\r
125             }\r
126             XMLString::release(&newString);\r
127             return oldValue;            \r
128         }\r
129 \r
130         /**\r
131          * A helper function for derived classes, for assignment of (singleton) XML objects.\r
132          * \r
133          * It is indifferent to whether either the old or the new version of the value is null. \r
134          * This method will do a safe compare of the objects and will also invalidate the DOM if appropriate\r
135          * \r
136          * @param oldValue - current value\r
137          * @param newValue - proposed new value\r
138          * @return the value to assign \r
139          * \r
140          * @throws XMLObjectException if the new child already has a parent.\r
141          */\r
142         XMLObject* prepareForAssignment(XMLObject* oldValue, XMLObject* newValue);\r
143 \r
144         /**\r
145          * Underlying list of child objects.\r
146          * Manages the lifetime of the children.\r
147          */\r
148         std::list<XMLObject*> m_children;\r
149 \r
150         /**\r
151          * Set of namespaces associated with the object.\r
152          */\r
153         mutable std::set<Namespace> m_namespaces;\r
154 \r
155         /**\r
156          * Logging object.\r
157          */\r
158         void* m_log;\r
159 \r
160     private:\r
161         XMLObject* m_parent;\r
162         QName m_elementQname;\r
163         QName* m_typeQname;\r
164     };\r
165 \r
166 };\r
167 \r
168 #if defined (_MSC_VER)\r
169     #pragma warning( pop )\r
170 #endif\r
171 \r
172 #endif /* __xmltooling_abstractxmlobj_h__ */\r