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