1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <processinginstruction.hxx>
25 
26 #include <string.h>
27 
28 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
29 
30 
31 namespace DOM
32 {
CProcessingInstruction(CDocument const & rDocument,::osl::Mutex const & rMutex,xmlNodePtr const pNode)33     CProcessingInstruction::CProcessingInstruction(
34             CDocument const& rDocument, ::osl::Mutex const& rMutex,
35             xmlNodePtr const pNode)
36         : CProcessingInstruction_Base(rDocument, rMutex,
37             NodeType_PROCESSING_INSTRUCTION_NODE, pNode)
38     {
39     }
40 
saxify(const Reference<XDocumentHandler> & i_xHandler)41     void CProcessingInstruction::saxify(
42             const Reference< XDocumentHandler >& i_xHandler) {
43         if (!i_xHandler.is()) throw RuntimeException();
44         Reference< XExtendedDocumentHandler > xExtended(i_xHandler, UNO_QUERY);
45         if (xExtended.is()) {
46             xExtended->processingInstruction(getTarget(), getData());
47         }
48     }
49 
50     /**
51     The content of this processing instruction.
52     */
53     OUString SAL_CALL
getData()54     CProcessingInstruction::getData() throw (RuntimeException)
55     {
56         ::osl::MutexGuard const g(m_rMutex);
57 
58         if (0 == m_aNodePtr) {
59             return ::rtl::OUString();
60         }
61 
62         char const*const pContent(
63                 reinterpret_cast<char const*>(m_aNodePtr->content));
64         if (0 == pContent) {
65             return ::rtl::OUString();
66         }
67         OUString const ret(pContent, strlen(pContent), RTL_TEXTENCODING_UTF8);
68         return ret;
69     }
70 
71     /**
72     The target of this processing instruction.
73     */
74     OUString SAL_CALL
getTarget()75     CProcessingInstruction::getTarget() throw (RuntimeException)
76     {
77         ::osl::MutexGuard const g(m_rMutex);
78 
79         if (0 == m_aNodePtr) {
80             return ::rtl::OUString();
81         }
82 
83         char const*const pName(
84                 reinterpret_cast<char const*>(m_aNodePtr->name));
85         if (0 == pName) {
86             return ::rtl::OUString();
87         }
88         OUString const ret(pName, strlen(pName), RTL_TEXTENCODING_UTF8);
89         return ret;
90     }
91 
92     /**
93     The content of this processing instruction.
94     */
setData(OUString const & rData)95     void SAL_CALL CProcessingInstruction::setData(OUString const& rData)
96         throw (RuntimeException, DOMException)
97     {
98         ::osl::MutexGuard const g(m_rMutex);
99 
100         if (0 == m_aNodePtr) {
101             throw RuntimeException();
102         }
103 
104         OString const data(
105                 ::rtl::OUStringToOString(rData, RTL_TEXTENCODING_UTF8));
106         xmlChar const*const pData(
107                 reinterpret_cast<xmlChar const*>(data.getStr()) );
108         xmlFree(m_aNodePtr->content);
109         m_aNodePtr->content = xmlStrdup(pData);
110     }
111 
112     OUString SAL_CALL
getNodeName()113     CProcessingInstruction::getNodeName() throw (RuntimeException)
114     {
115         ::osl::MutexGuard const g(m_rMutex);
116 
117         if (0 == m_aNodePtr) {
118             return ::rtl::OUString();
119         }
120 
121         sal_Char const*const pName =
122             reinterpret_cast<sal_Char const*>(m_aNodePtr->name);
123         OUString const ret(pName, strlen(pName), RTL_TEXTENCODING_UTF8);
124         return ret;
125     }
126 
getNodeValue()127     OUString SAL_CALL CProcessingInstruction::getNodeValue()
128         throw (RuntimeException)
129     {
130         return getData();
131     }
132 
133     void SAL_CALL
setNodeValue(OUString const & rNodeValue)134     CProcessingInstruction::setNodeValue(OUString const& rNodeValue)
135         throw (RuntimeException, DOMException)
136     {
137         return setData(rNodeValue);
138     }
139 }
140