Merged marshalling/unmarshalling methods into core interface.
[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 #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      * Extension of AbstractXMLObject that implements a DOMCachingXMLObject.\r
38      * This is the primary base class for XMLObject implementation classes to use.\r
39      */\r
40     class XMLTOOL_API AbstractDOMCachingXMLObject : public AbstractXMLObject, public virtual DOMCachingXMLObject\r
41     {\r
42     public:\r
43         virtual ~AbstractDOMCachingXMLObject();\r
44         \r
45         /**\r
46          * @see DOMCachingXMLObject::getDOM()\r
47          */\r
48         DOMElement* getDOM() const {\r
49             return m_dom;\r
50         }\r
51         \r
52         /**\r
53          * @see DOMCachingXMLObject::setDOM()\r
54          */\r
55         void setDOM(DOMElement* dom, bool bindDocument=false) const;\r
56         \r
57         /**\r
58          * @see DOMCachingXMLObject::setDocument()\r
59          */\r
60         void setDocument(DOMDocument* doc) const {\r
61             if (m_document)\r
62                 m_document->release();\r
63             m_document=doc;\r
64         }\r
65     \r
66         /**\r
67          * @see DOMCachingXMLObject::releaseDOM()\r
68          */\r
69         virtual void releaseDOM() const;\r
70         \r
71         /**\r
72          * @see DOMCachingXMLObject::releaseParentDOM()\r
73          */\r
74         virtual void releaseParentDOM(bool propagateRelease=true) const;\r
75         \r
76         /**\r
77          * @see DOMCachingXMLObject::releaseChildrenDOM()\r
78          */\r
79         virtual void releaseChildrenDOM(bool propagateRelease=true) const;\r
80     \r
81         /**\r
82          * A convenience method that is equal to calling releaseDOM() then releaseParentDOM(true).\r
83          */\r
84         void releaseThisandParentDOM() const {\r
85             if (m_dom) {\r
86                 releaseDOM();\r
87                 releaseParentDOM(true);\r
88             }\r
89         }\r
90     \r
91         /**\r
92          * A convenience method that is equal to calling releaseChildrenDOM(true) then releaseDOM().\r
93          */\r
94         void releaseThisAndChildrenDOM() const {\r
95             if (m_dom) {\r
96                 releaseChildrenDOM(true);\r
97                 releaseDOM();\r
98             }\r
99         }\r
100     \r
101         /**\r
102          * @see XMLObject::clone()\r
103          */\r
104         XMLObject* clone() const;\r
105 \r
106      protected:\r
107         /**\r
108          * Constructor\r
109          * \r
110          * @param namespaceURI the namespace the element is in\r
111          * @param elementLocalName the local name of the XML element this Object represents\r
112          * @param namespacePrefix the namespace prefix to use\r
113          */\r
114         AbstractDOMCachingXMLObject(\r
115             const XMLCh* namespaceURI=NULL, const XMLCh* elementLocalName=NULL, const XMLCh* namespacePrefix=NULL\r
116             ) : AbstractXMLObject(namespaceURI,elementLocalName, namespacePrefix), m_dom(NULL), m_document(NULL) {}\r
117 \r
118         /**\r
119          * If a DOM representation exists, this clones it into a new document.\r
120          * \r
121          * @param doc   the document to clone into, or NULL, in which case a new document is created\r
122          * @return  the cloned DOM\r
123          */\r
124         DOMElement* cloneDOM(DOMDocument* doc=NULL) const;\r
125 \r
126         /**\r
127          * A helper function for derived classes.\r
128          * This 'normalizes' newString, and then if it is different from oldString,\r
129          * it invalidates the DOM, frees the old string, and return the new.\r
130          * If not different, it frees the new string and just returns the old value.\r
131          * \r
132          * @param oldValue - the current value\r
133          * @param newValue - the new value\r
134          * \r
135          * @return the value that should be assigned\r
136          */\r
137         XMLCh* prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue) {\r
138             XMLCh* newString = XMLString::replicate(newValue);\r
139             XMLString::trim(newString);\r
140             if (!XMLString::equals(oldValue,newValue)) {\r
141                 releaseThisandParentDOM();\r
142                 XMLString::release(&oldValue);\r
143                 return newString;\r
144             }\r
145             XMLString::release(&newString);\r
146             return oldValue;            \r
147         }\r
148 \r
149         /**\r
150          * A helper function for derived classes, for assignment of (singleton) XML objects.\r
151          * \r
152          * It is indifferent to whether either the old or the new version of the value is null. \r
153          * This method will do a safe compare of the objects and will also invalidate the DOM if appropriate\r
154          * \r
155          * @param oldValue - current value\r
156          * @param newValue - proposed new value\r
157          * @return the value to assign \r
158          * \r
159          * @throws XMLObjectException if the new child already has a parent.\r
160          */\r
161         XMLObject* prepareForAssignment(XMLObject* oldValue, XMLObject* newValue);\r
162 \r
163     private:\r
164         mutable DOMElement* m_dom;\r
165         mutable DOMDocument* m_document;\r
166     };\r
167     \r
168 };\r
169 \r
170 #if defined (_MSC_VER)\r
171     #pragma warning( pop )\r
172 #endif\r
173 \r
174 #endif /* __xmltooling_abstractdomxmlobj_h__ */\r