1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "xpathobject.hxx"
21 
22 #include <string.h>
23 
24 #include "../dom/document.hxx"
25 #include "nodelist.hxx"
26 
27 using namespace css::uno;
28 using namespace css::xml::dom;
29 using namespace css::xml::xpath;
30 
31 namespace XPath
32 {
lcl_GetType(xmlXPathObjectPtr const pXPathObj)33     static XPathObjectType lcl_GetType(xmlXPathObjectPtr const pXPathObj)
34     {
35         switch (pXPathObj->type)
36         {
37             case XPATH_UNDEFINED:
38                 return XPathObjectType_XPATH_UNDEFINED;
39             case XPATH_NODESET:
40                 return XPathObjectType_XPATH_NODESET;
41             case XPATH_BOOLEAN:
42                 return XPathObjectType_XPATH_BOOLEAN;
43             case XPATH_NUMBER:
44                 return XPathObjectType_XPATH_NUMBER;
45             case XPATH_STRING:
46                 return XPathObjectType_XPATH_STRING;
47             case XPATH_POINT:
48                 return XPathObjectType_XPATH_POINT;
49             case XPATH_RANGE:
50                 return XPathObjectType_XPATH_RANGE;
51             case XPATH_LOCATIONSET:
52                 return XPathObjectType_XPATH_LOCATIONSET;
53             case XPATH_USERS:
54                 return XPathObjectType_XPATH_USERS;
55             case XPATH_XSLT_TREE:
56                 return XPathObjectType_XPATH_XSLT_TREE;
57             default:
58                 return XPathObjectType_XPATH_UNDEFINED;
59         }
60     }
61 
CXPathObject(::rtl::Reference<DOM::CDocument> const & pDocument,::osl::Mutex & rMutex,std::shared_ptr<xmlXPathObject> const & pXPathObj)62     CXPathObject::CXPathObject(
63             ::rtl::Reference<DOM::CDocument> const& pDocument,
64             ::osl::Mutex & rMutex,
65             std::shared_ptr<xmlXPathObject> const& pXPathObj)
66         : m_pDocument(pDocument)
67         , m_rMutex(rMutex)
68         , m_pXPathObj(pXPathObj)
69         , m_XPathObjectType(lcl_GetType(pXPathObj.get()))
70     {
71     }
72 
73     /**
74         get object type
75     */
getObjectType()76     XPathObjectType CXPathObject::getObjectType()
77     {
78         return m_XPathObjectType;
79     }
80 
81     /**
82         get the nodes from a nodelist type object
83     */
84     Reference< XNodeList > SAL_CALL
getNodeList()85     CXPathObject::getNodeList()
86     {
87         ::osl::MutexGuard const g(m_rMutex);
88 
89         Reference< XNodeList > const xRet(
90             new CNodeList(m_pDocument, m_rMutex, m_pXPathObj));
91         return xRet;
92     }
93 
94      /**
95         get value of a boolean object
96      */
getBoolean()97     sal_Bool SAL_CALL CXPathObject::getBoolean()
98     {
99         ::osl::MutexGuard const g(m_rMutex);
100 
101         return xmlXPathCastToBoolean(m_pXPathObj.get()) != 0;
102     }
103 
104     /**
105         get number as byte
106     */
getByte()107     sal_Int8 SAL_CALL CXPathObject::getByte()
108     {
109         ::osl::MutexGuard const g(m_rMutex);
110 
111         return static_cast<sal_Int8>(xmlXPathCastToNumber(m_pXPathObj.get()));
112     }
113 
114     /**
115         get number as short
116     */
getShort()117     sal_Int16 SAL_CALL CXPathObject::getShort()
118     {
119         ::osl::MutexGuard const g(m_rMutex);
120 
121         return static_cast<sal_Int16>(xmlXPathCastToNumber(m_pXPathObj.get()));
122     }
123 
124     /**
125         get number as long
126     */
getLong()127     sal_Int32 SAL_CALL CXPathObject::getLong()
128     {
129         ::osl::MutexGuard const g(m_rMutex);
130 
131         return static_cast<sal_Int32>(xmlXPathCastToNumber(m_pXPathObj.get()));
132     }
133 
134     /**
135         get number as hyper
136     */
getHyper()137     sal_Int64 SAL_CALL CXPathObject::getHyper()
138     {
139         ::osl::MutexGuard const g(m_rMutex);
140 
141         return static_cast<sal_Int64>(xmlXPathCastToNumber(m_pXPathObj.get()));
142     }
143 
144     /**
145         get number as float
146     */
getFloat()147     float SAL_CALL CXPathObject::getFloat()
148     {
149         ::osl::MutexGuard const g(m_rMutex);
150 
151         return static_cast<float>(xmlXPathCastToNumber(m_pXPathObj.get()));
152     }
153 
154     /**
155         get number as double
156     */
getDouble()157     double SAL_CALL CXPathObject::getDouble()
158     {
159         ::osl::MutexGuard const g(m_rMutex);
160 
161         return  xmlXPathCastToNumber(m_pXPathObj.get());
162     }
163 
164     /**
165         get string value
166     */
getString()167     OUString SAL_CALL CXPathObject::getString()
168     {
169         ::osl::MutexGuard const g(m_rMutex);
170 
171         std::shared_ptr<xmlChar const> str(
172             xmlXPathCastToString(m_pXPathObj.get()), xmlFree);
173         sal_Char const*const pS(reinterpret_cast<sal_Char const*>(str.get()));
174         return OUString(pS, strlen(pS), RTL_TEXTENCODING_UTF8);
175     }
176 
177 }
178 
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
180