Initial DOM handling interfaces.
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractDOMCachingXMLObject.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 AbstractDOMCachingXMLObject.h\r
19  * \r
20  * Extension of AbstractXMLObject that implements a DOMCachingXMLObject. \r
21  */\r
22 \r
23 #if !defined(__xmltooling_abstractdomxmlobj_h__)\r
24 #define __xmltooling_abstractdomxmlobj_h__\r
25 \r
26 #include <xmltooling/AbstractXMLObject.h>\r
27 #include <xmltooling/DOMCachingXMLObject.h>\r
28 \r
29 #pragma warning( push )\r
30 #pragma warning( disable : 4250 4251 )\r
31 \r
32 namespace xmltooling {\r
33 \r
34     /**\r
35      * Extension of AbstractXMLObject that implements a DOMCachingXMLObject.\r
36      */\r
37     class XMLTOOL_API AbstractDOMCachingXMLObject : public virtual AbstractXMLObject, public virtual DOMCachingXMLObject\r
38     {\r
39     public:\r
40         virtual ~AbstractDOMCachingXMLObject();\r
41         \r
42         /**\r
43          * @see DOMCachingXMLObject::getDOM()\r
44          */\r
45         const DOMElement* getDOM() const {\r
46             return m_dom;\r
47         }\r
48         \r
49         /**\r
50          * @see DOMCachingXMLObject::setDOM()\r
51          */\r
52         void setDOM(DOMElement* dom) {\r
53             m_dom=dom;\r
54         }\r
55         \r
56         /**\r
57          * @see DOMCachingXMLObject::setDocument()\r
58          */\r
59         DOMDocument* setDocument(DOMDocument* doc) {\r
60             DOMDocument* ret=m_document;\r
61             m_document=doc;\r
62             return ret;\r
63         }\r
64     \r
65         /**\r
66          * @see DOMCachingXMLObject::releaseDOM()\r
67          */\r
68         void releaseDOM();\r
69         \r
70         /**\r
71          * @see DOMCachingXMLObject::releaseParentDOM()\r
72          */\r
73         void releaseParentDOM(bool propagateRelease=true);\r
74         \r
75         /**\r
76          * @see DOMCachingXMLObject::releaseChildrenDOM()\r
77          */\r
78         void releaseChildrenDOM(bool propagateRelease=true);\r
79     \r
80         /**\r
81          * A convenience method that is equal to calling releaseDOM() then releaseParentDOM(true).\r
82          */\r
83         void releaseThisandParentDOM() {\r
84             if (m_dom) {\r
85                 releaseDOM();\r
86                 releaseParentDOM(true);\r
87             }\r
88         }\r
89     \r
90         /**\r
91          * A convenience method that is equal to calling releaseDOM() then releaseChildrenDOM(true).\r
92          */\r
93         void releaseThisAndChildrenDOM() {\r
94             if (m_dom) {\r
95                 releaseDOM();\r
96                 releaseChildrenDOM(true);\r
97             }\r
98         }\r
99     \r
100      protected:\r
101         /**\r
102          * A helper function for derived classes.\r
103          * This 'normalizes' newString and then if it is different from oldString\r
104          * invalidates the DOM. It returns the normalized value.\r
105          * \r
106          * @param oldValue - the current value\r
107          * @param newValue - the new value\r
108          * \r
109          * @return the value that should be assigned\r
110          */\r
111         XMLCh* prepareForAssignment(const XMLCh* oldValue, const XMLCh* newValue) {\r
112             XMLCh* newString = XMLString::replicate(newValue);\r
113             XMLString::trim(newString);\r
114 \r
115             if (oldValue && !newValue || !oldValue && newValue || XMLString::compareString(oldValue,newValue))\r
116                 releaseThisandParentDOM();\r
117     \r
118             return newString;\r
119         }\r
120     \r
121         /**\r
122          * A helper function for derived classes, for assignment of (singleton) XML objects.\r
123          * \r
124          * It is indifferent to whether either the old or the new version of the value is null. \r
125          * This method will do a safe compare of the objects and will also invalidate the DOM if appropriate\r
126          * \r
127          * @param oldValue - current value\r
128          * @param newValue - proposed new value\r
129          * @return The value to assign to the saved Object.\r
130          * \r
131          * @throws IllegalArgumentException if the child already has a parent.\r
132          */\r
133         XMLObject* prepareForAssignment(const XMLObject* oldValue, XMLObject* newValue);\r
134 \r
135         /**\r
136          * Constructor\r
137          * \r
138          * @param namespaceURI the namespace the element is in\r
139          * @param elementLocalName the local name of the XML element this Object represents\r
140          */\r
141         explicit AbstractDOMCachingXMLObject(const XMLCh* namespaceURI, const XMLCh* elementLocalName)\r
142             : AbstractXMLObject(namespaceURI,elementLocalName), m_dom(NULL), m_document(NULL) {}\r
143 \r
144     private:\r
145         DOMElement* m_dom;\r
146         DOMDocument* m_document;\r
147     };\r
148     \r
149 };\r
150 \r
151 #pragma warning( pop )\r
152 \r
153 #endif /* __xmltooling_abstractdomxmlobj_h__ */\r