1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: attr.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include "element.hxx"
33 #include <com/sun/star/xml/dom/DOMException.hdl>
38 CAttr::CAttr(const xmlAttrPtr pAttr
)
41 m_aNodeType
= NodeType_ATTRIBUTE_NODE
;
42 init_node((xmlNodePtr
)pAttr
);
45 OUString SAL_CALL
CAttr::getNodeName()
46 throw (RuntimeException
)
50 OUString SAL_CALL
CAttr::getNodeValue()
51 throw (RuntimeException
)
55 OUString SAL_CALL
CAttr::getLocalName()
56 throw (RuntimeException
)
63 Returns the name of this attribute.
65 OUString SAL_CALL
CAttr::getName() throw (RuntimeException
)
68 if (m_aAttrPtr
!= NULL
)
70 aName
= OUString((char*)m_aAttrPtr
->name
, strlen((char*)m_aAttrPtr
->name
), RTL_TEXTENCODING_UTF8
);
76 The Element node this attribute is attached to or null if this
77 attribute is not in use.
79 Reference
< XElement
> SAL_CALL
CAttr::getOwnerElement()
80 throw (RuntimeException
)
82 Reference
< XElement
> aElement
;
83 if (m_aAttrPtr
!= NULL
&& m_aAttrPtr
->parent
!= NULL
)
85 aElement
= Reference
< XElement
>(static_cast< CElement
* >(CNode::get(m_aAttrPtr
->parent
)));
91 If this attribute was explicitly given a value in the original
92 document, this is true; otherwise, it is false.
94 sal_Bool SAL_CALL
CAttr::getSpecified()
95 throw (RuntimeException
)
97 // XXX what is this supposed do exactly?
102 On retrieval, the value of the attribute is returned as a string.
104 OUString SAL_CALL
CAttr::getValue()
105 throw (RuntimeException
)
108 if (m_aAttrPtr
!= NULL
&& m_aAttrPtr
->children
!= NULL
)
110 aName
= OUString((char*)m_aAttrPtr
->children
->content
, strlen((char*)m_aAttrPtr
->children
->content
),
111 RTL_TEXTENCODING_UTF8
);
117 Sets the value of the attribute from a string.
119 void SAL_CALL
CAttr::setValue(const OUString
& value
)
120 throw (RuntimeException
, DOMException
)
122 // remember old value (for mutation event)
123 OUString sOldValue
= getValue();
125 OString o1
= OUStringToOString(value
, RTL_TEXTENCODING_UTF8
);
126 xmlChar
* xValue
= (xmlChar
*)o1
.getStr();
127 // xmlChar* xName = OUStringToOString(m_aAttrPtr->name, RTL_TEXTENCODING_UTF8).getStr();
128 // this does not work if the attribute was created anew
129 // xmlNodePtr pNode = m_aAttrPtr->parent;
130 // xmlSetProp(pNode, m_aAttrPtr->name, xValue);
131 xmlChar
*buffer
= xmlEncodeEntitiesReentrant(m_aAttrPtr
->doc
, xValue
);
132 m_aAttrPtr
->children
= xmlStringGetNodeList(m_aAttrPtr
->doc
, buffer
);
133 xmlNodePtr tmp
= m_aAttrPtr
->children
;
134 while (tmp
!= NULL
) {
135 tmp
->parent
= (xmlNodePtr
) m_aNodePtr
;
136 tmp
->doc
= m_aAttrPtr
->doc
;
137 if (tmp
->next
== NULL
)
138 m_aNodePtr
->last
= tmp
;
142 // dispatch DOM events to signal change in attribute value
143 // dispatch DomAttrModified + DOMSubtreeModified
144 OUString
sEventName( RTL_CONSTASCII_USTRINGPARAM("DOMAttrModified") );
145 Reference
< XDocumentEvent
> docevent(getOwnerDocument(), UNO_QUERY
);
146 Reference
< XMutationEvent
> event(docevent
->createEvent(sEventName
),UNO_QUERY
);
147 event
->initMutationEvent(
148 sEventName
, sal_True
, sal_False
,
149 Reference
<XNode
>( static_cast<XAttr
*>( this ) ),
150 sOldValue
, value
, getName(), AttrChangeType_MODIFICATION
);
151 dispatchEvent(Reference
< XEvent
>(event
, UNO_QUERY
));
152 dispatchSubtreeModified();