1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <processinginstruction.hxx>
29 
30 #include <string.h>
31 
32 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
33 
34 
35 namespace DOM
36 {
37     CProcessingInstruction::CProcessingInstruction(
38             CDocument const& rDocument, ::osl::Mutex const& rMutex,
39             xmlNodePtr const pNode)
40         : CProcessingInstruction_Base(rDocument, rMutex,
41             NodeType_PROCESSING_INSTRUCTION_NODE, pNode)
42     {
43     }
44 
45     void CProcessingInstruction::saxify(
46             const Reference< XDocumentHandler >& i_xHandler) {
47         if (!i_xHandler.is()) throw RuntimeException();
48         Reference< XExtendedDocumentHandler > xExtended(i_xHandler, UNO_QUERY);
49         if (xExtended.is()) {
50             xExtended->processingInstruction(getTarget(), getData());
51         }
52     }
53 
54     /**
55     The content of this processing instruction.
56     */
57     OUString SAL_CALL
58     CProcessingInstruction::getData() throw (RuntimeException)
59     {
60         ::osl::MutexGuard const g(m_rMutex);
61 
62         if (0 == m_aNodePtr) {
63             return ::rtl::OUString();
64         }
65 
66         char const*const pContent(
67                 reinterpret_cast<char const*>(m_aNodePtr->content));
68         if (0 == pContent) {
69             return ::rtl::OUString();
70         }
71         OUString const ret(pContent, strlen(pContent), RTL_TEXTENCODING_UTF8);
72         return ret;
73     }
74 
75     /**
76     The target of this processing instruction.
77     */
78     OUString SAL_CALL
79     CProcessingInstruction::getTarget() throw (RuntimeException)
80     {
81         ::osl::MutexGuard const g(m_rMutex);
82 
83         if (0 == m_aNodePtr) {
84             return ::rtl::OUString();
85         }
86 
87         char const*const pName(
88                 reinterpret_cast<char const*>(m_aNodePtr->name));
89         if (0 == pName) {
90             return ::rtl::OUString();
91         }
92         OUString const ret(pName, strlen(pName), RTL_TEXTENCODING_UTF8);
93         return ret;
94     }
95 
96     /**
97     The content of this processing instruction.
98     */
99     void SAL_CALL CProcessingInstruction::setData(OUString const& rData)
100         throw (RuntimeException, DOMException)
101     {
102         ::osl::MutexGuard const g(m_rMutex);
103 
104         if (0 == m_aNodePtr) {
105             throw RuntimeException();
106         }
107 
108         OString const data(
109                 ::rtl::OUStringToOString(rData, RTL_TEXTENCODING_UTF8));
110         xmlChar const*const pData(
111                 reinterpret_cast<xmlChar const*>(data.getStr()) );
112         xmlFree(m_aNodePtr->content);
113         m_aNodePtr->content = xmlStrdup(pData);
114     }
115 
116     OUString SAL_CALL
117     CProcessingInstruction::getNodeName() throw (RuntimeException)
118     {
119         ::osl::MutexGuard const g(m_rMutex);
120 
121         if (0 == m_aNodePtr) {
122             return ::rtl::OUString();
123         }
124 
125         sal_Char const*const pName =
126             reinterpret_cast<sal_Char const*>(m_aNodePtr->name);
127         OUString const ret(pName, strlen(pName), RTL_TEXTENCODING_UTF8);
128         return ret;
129     }
130 
131     OUString SAL_CALL CProcessingInstruction::getNodeValue()
132         throw (RuntimeException)
133     {
134         return getData();
135     }
136 
137     void SAL_CALL
138     CProcessingInstruction::setNodeValue(OUString const& rNodeValue)
139         throw (RuntimeException, DOMException)
140     {
141         return setData(rNodeValue);
142     }
143 }
144