xref: /trunk/main/unoxml/source/xpath/xpathobject.cxx (revision e9cbe144)
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 <xpathobject.hxx>
25 
26 #include <string.h>
27 
28 #include "../dom/document.hxx"
29 #include <nodelist.hxx>
30 
31 
32 namespace XPath
33 {
lcl_GetType(xmlXPathObjectPtr const pXPathObj)34     static XPathObjectType lcl_GetType(xmlXPathObjectPtr const pXPathObj)
35     {
36         switch (pXPathObj->type)
37         {
38             case XPATH_UNDEFINED:
39                 return XPathObjectType_XPATH_UNDEFINED;
40             case XPATH_NODESET:
41                 return XPathObjectType_XPATH_NODESET;
42             case XPATH_BOOLEAN:
43                 return XPathObjectType_XPATH_BOOLEAN;
44             case XPATH_NUMBER:
45                 return XPathObjectType_XPATH_NUMBER;
46             case XPATH_STRING:
47                 return XPathObjectType_XPATH_STRING;
48             case XPATH_POINT:
49                 return XPathObjectType_XPATH_POINT;
50             case XPATH_RANGE:
51                 return XPathObjectType_XPATH_RANGE;
52             case XPATH_LOCATIONSET:
53                 return XPathObjectType_XPATH_LOCATIONSET;
54             case XPATH_USERS:
55                 return XPathObjectType_XPATH_USERS;
56             case XPATH_XSLT_TREE:
57                 return XPathObjectType_XPATH_XSLT_TREE;
58             default:
59                 return XPathObjectType_XPATH_UNDEFINED;
60         }
61     }
62 
CXPathObject(::rtl::Reference<DOM::CDocument> const & pDocument,::osl::Mutex & rMutex,::boost::shared_ptr<xmlXPathObject> const & pXPathObj)63     CXPathObject::CXPathObject(
64             ::rtl::Reference<DOM::CDocument> const& pDocument,
65             ::osl::Mutex & rMutex,
66             ::boost::shared_ptr<xmlXPathObject> const& pXPathObj)
67         : m_pDocument(pDocument)
68         , m_rMutex(rMutex)
69         , m_pXPathObj(pXPathObj)
70         , m_XPathObjectType(lcl_GetType(pXPathObj.get()))
71     {
72     }
73 
74     /**
75         get object type
76     */
getObjectType()77     XPathObjectType CXPathObject::getObjectType() throw (RuntimeException)
78     {
79         return m_XPathObjectType;
80     }
81 
82     /**
83         get the nodes from a nodelist type object
84     */
85     Reference< XNodeList > SAL_CALL
getNodeList()86     CXPathObject::getNodeList() throw (RuntimeException)
87     {
88         ::osl::MutexGuard const g(m_rMutex);
89 
90         Reference< XNodeList > const xRet(
91             new CNodeList(m_pDocument, m_rMutex, m_pXPathObj));
92         return xRet;
93     }
94 
95      /**
96         get value of a boolean object
97      */
getBoolean()98     sal_Bool SAL_CALL CXPathObject::getBoolean() throw (RuntimeException)
99     {
100         ::osl::MutexGuard const g(m_rMutex);
101 
102         return (sal_Bool) xmlXPathCastToBoolean(m_pXPathObj.get());
103     }
104 
105     /**
106         get number as byte
107     */
getByte()108     sal_Int8 SAL_CALL CXPathObject::getByte() throw (RuntimeException)
109     {
110         ::osl::MutexGuard const g(m_rMutex);
111 
112         return (sal_Int8) xmlXPathCastToNumber(m_pXPathObj.get());
113     }
114 
115     /**
116         get number as short
117     */
getShort()118     sal_Int16 SAL_CALL CXPathObject::getShort() throw (RuntimeException)
119     {
120         ::osl::MutexGuard const g(m_rMutex);
121 
122         return (sal_Int16) xmlXPathCastToNumber(m_pXPathObj.get());
123     }
124 
125     /**
126         get number as long
127     */
getLong()128     sal_Int32 SAL_CALL CXPathObject::getLong() throw (RuntimeException)
129     {
130         ::osl::MutexGuard const g(m_rMutex);
131 
132         return (sal_Int32) xmlXPathCastToNumber(m_pXPathObj.get());
133     }
134 
135     /**
136         get number as hyper
137     */
getHyper()138     sal_Int64 SAL_CALL CXPathObject::getHyper() throw (RuntimeException)
139     {
140         ::osl::MutexGuard const g(m_rMutex);
141 
142         return (sal_Int64) xmlXPathCastToNumber(m_pXPathObj.get());
143     }
144 
145     /**
146         get number as float
147     */
getFloat()148     float SAL_CALL CXPathObject::getFloat() throw (RuntimeException)
149     {
150         ::osl::MutexGuard const g(m_rMutex);
151 
152         return (float) xmlXPathCastToNumber(m_pXPathObj.get());
153     }
154 
155     /**
156         get number as double
157     */
getDouble()158     double SAL_CALL CXPathObject::getDouble() throw (RuntimeException)
159     {
160         ::osl::MutexGuard const g(m_rMutex);
161 
162         return  xmlXPathCastToNumber(m_pXPathObj.get());
163     }
164 
165     /**
166         get string value
167     */
getString()168     OUString SAL_CALL CXPathObject::getString() throw (RuntimeException)
169     {
170         ::osl::MutexGuard const g(m_rMutex);
171 
172         ::boost::shared_ptr<xmlChar const> str(
173             xmlXPathCastToString(m_pXPathObj.get()), xmlFree);
174         sal_Char const*const pS(reinterpret_cast<sal_Char const*>(str.get()));
175         return OUString(pS, strlen(pS), RTL_TEXTENCODING_UTF8);
176     }
177 
178 }
179 
180