xref: /trunk/main/unoxml/source/dom/attr.cxx (revision e9cbe144)
1*e9cbe144SAndrew Rist /**************************************************************
2*e9cbe144SAndrew Rist  *
3*e9cbe144SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e9cbe144SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e9cbe144SAndrew Rist  * distributed with this work for additional information
6*e9cbe144SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e9cbe144SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e9cbe144SAndrew Rist  * "License"); you may not use this file except in compliance
9*e9cbe144SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e9cbe144SAndrew Rist  *
11*e9cbe144SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e9cbe144SAndrew Rist  *
13*e9cbe144SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e9cbe144SAndrew Rist  * software distributed under the License is distributed on an
15*e9cbe144SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e9cbe144SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e9cbe144SAndrew Rist  * specific language governing permissions and limitations
18*e9cbe144SAndrew Rist  * under the License.
19*e9cbe144SAndrew Rist  *
20*e9cbe144SAndrew Rist  *************************************************************/
21*e9cbe144SAndrew Rist 
22*e9cbe144SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <attr.hxx>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <string.h>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <com/sun/star/xml/dom/DOMException.hdl>
31cdf0e10cSrcweir #include <com/sun/star/xml/dom/events/XMutationEvent.hpp>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <document.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace DOM
37cdf0e10cSrcweir {
CAttr(CDocument const & rDocument,::osl::Mutex const & rMutex,xmlAttrPtr const pAttr)38cdf0e10cSrcweir     CAttr::CAttr(CDocument const& rDocument, ::osl::Mutex const& rMutex,
39cdf0e10cSrcweir             xmlAttrPtr const pAttr)
40cdf0e10cSrcweir         : CAttr_Base(rDocument, rMutex,
41cdf0e10cSrcweir                 NodeType_ATTRIBUTE_NODE, reinterpret_cast<xmlNodePtr>(pAttr))
42cdf0e10cSrcweir         , m_aAttrPtr(pAttr)
43cdf0e10cSrcweir     {
44cdf0e10cSrcweir     }
45cdf0e10cSrcweir 
GetNamespace(xmlNodePtr const pNode)46cdf0e10cSrcweir     xmlNsPtr CAttr::GetNamespace(xmlNodePtr const pNode)
47cdf0e10cSrcweir     {
48cdf0e10cSrcweir         if (!m_pNamespace.get()) {
49cdf0e10cSrcweir             return 0;
50cdf0e10cSrcweir         }
51cdf0e10cSrcweir         xmlChar const*const pUri(reinterpret_cast<xmlChar const*>(
52cdf0e10cSrcweir                 m_pNamespace->first.getStr()));
53cdf0e10cSrcweir         xmlChar const*const pPrefix(reinterpret_cast<xmlChar const*>(
54cdf0e10cSrcweir                 m_pNamespace->second.getStr()));
55cdf0e10cSrcweir         xmlNsPtr pNs = xmlSearchNs(pNode->doc, pNode, pPrefix);
56cdf0e10cSrcweir         if (pNs && (0 != xmlStrcmp(pNs->href, pUri))) {
57cdf0e10cSrcweir             return pNs;
58cdf0e10cSrcweir         }
59cdf0e10cSrcweir         pNs = xmlNewNs(pNode, pUri, pPrefix);
60cdf0e10cSrcweir         if (pNs) {
61cdf0e10cSrcweir             return pNs;
62cdf0e10cSrcweir         }
63cdf0e10cSrcweir         pNs = xmlSearchNsByHref(pNode->doc, pNode, pUri);
64cdf0e10cSrcweir         // if (!pNs) hmm... now what? throw?
65cdf0e10cSrcweir         if (!pNs) { OSL_TRACE("CAtttr: cannot create namespace"); }
66cdf0e10cSrcweir         return pNs;
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir 
IsChildTypeAllowed(NodeType const nodeType)69cdf0e10cSrcweir     bool CAttr::IsChildTypeAllowed(NodeType const nodeType)
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         switch (nodeType) {
72cdf0e10cSrcweir             case NodeType_TEXT_NODE:
73cdf0e10cSrcweir             case NodeType_ENTITY_REFERENCE_NODE:
74cdf0e10cSrcweir                 return true;
75cdf0e10cSrcweir             default:
76cdf0e10cSrcweir                 return false;
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
getNodeName()80cdf0e10cSrcweir     OUString SAL_CALL CAttr::getNodeName()
81cdf0e10cSrcweir         throw (RuntimeException)
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         return getName();
84cdf0e10cSrcweir     }
getNodeValue()85cdf0e10cSrcweir     OUString SAL_CALL CAttr::getNodeValue()
86cdf0e10cSrcweir         throw (RuntimeException)
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         return getValue();
89cdf0e10cSrcweir     }
getLocalName()90cdf0e10cSrcweir     OUString SAL_CALL CAttr::getLocalName()
91cdf0e10cSrcweir         throw (RuntimeException)
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         return getName();
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     /**
98cdf0e10cSrcweir     Returns the name of this attribute.
99cdf0e10cSrcweir     */
getName()100cdf0e10cSrcweir     OUString SAL_CALL CAttr::getName() throw (RuntimeException)
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         if ((0 == m_aNodePtr) || (0 == m_aAttrPtr)) {
105cdf0e10cSrcweir             return ::rtl::OUString();
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir         OUString const aName((char*)m_aAttrPtr->name,
108cdf0e10cSrcweir                 strlen((char*)m_aAttrPtr->name), RTL_TEXTENCODING_UTF8);
109cdf0e10cSrcweir         return aName;
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     /**
113cdf0e10cSrcweir     The Element node this attribute is attached to or null if this
114cdf0e10cSrcweir     attribute is not in use.
115cdf0e10cSrcweir     */
getOwnerElement()116cdf0e10cSrcweir     Reference< XElement > SAL_CALL CAttr::getOwnerElement()
117cdf0e10cSrcweir         throw (RuntimeException)
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         if ((0 == m_aNodePtr) || (0 == m_aAttrPtr)) {
122cdf0e10cSrcweir             return 0;
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir         if (0 == m_aAttrPtr->parent) {
125cdf0e10cSrcweir             return 0;
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir         Reference< XElement > const xRet(
128cdf0e10cSrcweir             static_cast< XNode* >(GetOwnerDocument().GetCNode(
129cdf0e10cSrcweir                     m_aAttrPtr->parent).get()),
130cdf0e10cSrcweir             UNO_QUERY_THROW);
131cdf0e10cSrcweir         return xRet;
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     /**
135cdf0e10cSrcweir     If this attribute was explicitly given a value in the original
136cdf0e10cSrcweir     document, this is true; otherwise, it is false.
137cdf0e10cSrcweir     */
getSpecified()138cdf0e10cSrcweir     sal_Bool SAL_CALL CAttr::getSpecified()
139cdf0e10cSrcweir         throw (RuntimeException)
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         // FIXME if this DOM implemenatation supported DTDs it would need
142cdf0e10cSrcweir         // to check that this attribute is not default or something
143cdf0e10cSrcweir         return sal_True;
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     /**
147cdf0e10cSrcweir     On retrieval, the value of the attribute is returned as a string.
148cdf0e10cSrcweir     */
getValue()149cdf0e10cSrcweir     OUString SAL_CALL CAttr::getValue()
150cdf0e10cSrcweir         throw (RuntimeException)
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         if ((0 == m_aNodePtr) || (0 == m_aAttrPtr)) {
155cdf0e10cSrcweir             return ::rtl::OUString();
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir         if (0 == m_aAttrPtr->children) {
158cdf0e10cSrcweir             return ::rtl::OUString();
159cdf0e10cSrcweir         }
160cdf0e10cSrcweir         char const*const pContent((m_aAttrPtr->children)
161cdf0e10cSrcweir             ? reinterpret_cast<char const*>(m_aAttrPtr->children->content)
162cdf0e10cSrcweir             : "");
163cdf0e10cSrcweir         OUString const ret(pContent, strlen(pContent), RTL_TEXTENCODING_UTF8);
164cdf0e10cSrcweir         return ret;
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     /**
168cdf0e10cSrcweir     Sets the value of the attribute from a string.
169cdf0e10cSrcweir     */
setValue(const OUString & value)170cdf0e10cSrcweir     void SAL_CALL CAttr::setValue(const OUString& value)
171cdf0e10cSrcweir         throw (RuntimeException, DOMException)
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir         ::osl::ClearableMutexGuard guard(m_rMutex);
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         if ((0 == m_aNodePtr) || (0 == m_aAttrPtr)) {
176cdf0e10cSrcweir             return;
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir         // remember old value (for mutation event)
180cdf0e10cSrcweir         OUString sOldValue = getValue();
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         OString o1 = OUStringToOString(value, RTL_TEXTENCODING_UTF8);
183cdf0e10cSrcweir         xmlChar* xValue = (xmlChar*)o1.getStr();
184cdf0e10cSrcweir         // xmlChar* xName = OUStringToOString(m_aAttrPtr->name, RTL_TEXTENCODING_UTF8).getStr();
185cdf0e10cSrcweir         // this does not work if the attribute was created anew
186cdf0e10cSrcweir         // xmlNodePtr pNode = m_aAttrPtr->parent;
187cdf0e10cSrcweir         // xmlSetProp(pNode, m_aAttrPtr->name, xValue);
188cdf0e10cSrcweir         ::boost::shared_ptr<xmlChar const> const buffer(
189cdf0e10cSrcweir                 xmlEncodeEntitiesReentrant(m_aAttrPtr->doc, xValue), xmlFree);
190cdf0e10cSrcweir         xmlFreeNodeList(m_aAttrPtr->children);
191cdf0e10cSrcweir         m_aAttrPtr->children =
192cdf0e10cSrcweir             xmlStringGetNodeList(m_aAttrPtr->doc, buffer.get());
193cdf0e10cSrcweir         xmlNodePtr tmp = m_aAttrPtr->children;
194cdf0e10cSrcweir         while (tmp != NULL) {
195cdf0e10cSrcweir             tmp->parent = (xmlNodePtr) m_aNodePtr;
196cdf0e10cSrcweir             tmp->doc = m_aAttrPtr->doc;
197cdf0e10cSrcweir             if (tmp->next == NULL)
198cdf0e10cSrcweir                 m_aNodePtr->last = tmp;
199cdf0e10cSrcweir             tmp = tmp->next;
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         // dispatch DOM events to signal change in attribute value
203cdf0e10cSrcweir         // dispatch DomAttrModified + DOMSubtreeModified
204cdf0e10cSrcweir         OUString sEventName( RTL_CONSTASCII_USTRINGPARAM("DOMAttrModified") );
205cdf0e10cSrcweir         Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY);
206cdf0e10cSrcweir         Reference< XMutationEvent > event(docevent->createEvent(sEventName),UNO_QUERY);
207cdf0e10cSrcweir         event->initMutationEvent(
208cdf0e10cSrcweir                 sEventName, sal_True, sal_False,
209cdf0e10cSrcweir                 Reference<XNode>( static_cast<XAttr*>( this ) ),
210cdf0e10cSrcweir                 sOldValue, value, getName(), AttrChangeType_MODIFICATION );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir         guard.clear(); // release mutex before calling event handlers
213cdf0e10cSrcweir 
214cdf0e10cSrcweir         dispatchEvent(Reference< XEvent >(event, UNO_QUERY));
215cdf0e10cSrcweir         dispatchSubtreeModified();
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir 
setPrefix(const OUString & prefix)218cdf0e10cSrcweir     void SAL_CALL CAttr::setPrefix(const OUString& prefix)
219cdf0e10cSrcweir         throw (RuntimeException, DOMException)
220cdf0e10cSrcweir     {
221cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
222cdf0e10cSrcweir 
223cdf0e10cSrcweir         if (!m_aNodePtr) { return; }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir         if (m_pNamespace.get()) {
226cdf0e10cSrcweir             OSL_ASSERT(!m_aNodePtr->parent);
227cdf0e10cSrcweir             m_pNamespace->second =
228cdf0e10cSrcweir                 OUStringToOString(prefix, RTL_TEXTENCODING_UTF8);
229cdf0e10cSrcweir         } else {
230cdf0e10cSrcweir             CNode::setPrefix(prefix);
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir 
getPrefix()234cdf0e10cSrcweir     OUString SAL_CALL CAttr::getPrefix()
235cdf0e10cSrcweir         throw (RuntimeException)
236cdf0e10cSrcweir     {
237cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
238cdf0e10cSrcweir 
239cdf0e10cSrcweir         if (!m_aNodePtr) { return ::rtl::OUString(); }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir         if (m_pNamespace.get()) {
242cdf0e10cSrcweir             OSL_ASSERT(!m_aNodePtr->parent);
243cdf0e10cSrcweir             OUString const ret(::rtl::OStringToOUString(
244cdf0e10cSrcweir                         m_pNamespace->second, RTL_TEXTENCODING_UTF8));
245cdf0e10cSrcweir             return ret;
246cdf0e10cSrcweir         } else {
247cdf0e10cSrcweir             return CNode::getPrefix();
248cdf0e10cSrcweir         }
249cdf0e10cSrcweir     }
250cdf0e10cSrcweir 
getNamespaceURI()251cdf0e10cSrcweir     OUString SAL_CALL CAttr::getNamespaceURI()
252cdf0e10cSrcweir         throw (RuntimeException)
253cdf0e10cSrcweir     {
254cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
255cdf0e10cSrcweir 
256cdf0e10cSrcweir         if (!m_aNodePtr) { return ::rtl::OUString(); }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir         if (m_pNamespace.get()) {
259cdf0e10cSrcweir             OSL_ASSERT(!m_aNodePtr->parent);
260cdf0e10cSrcweir             OUString const ret(::rtl::OStringToOUString(
261cdf0e10cSrcweir                         m_pNamespace->first, RTL_TEXTENCODING_UTF8));
262cdf0e10cSrcweir             return ret;
263cdf0e10cSrcweir         } else {
264cdf0e10cSrcweir             return CNode::getNamespaceURI();
265cdf0e10cSrcweir         }
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir }
268