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