1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include "interpre.hxx"
11 #include <rtl/strbuf.hxx>
12 #include <formula/errorcodes.hxx>
13 #include <svtools/miscopt.hxx>
15 #include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
16 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
17 #include <com/sun/star/io/XInputStream.hpp>
19 #include "libxml/xpath.h"
20 #include <dpobject.hxx>
21 #include <document.hxx>
23 #include <boost/shared_ptr.hpp>
26 using namespace com::sun::star
;
28 // TODO: Add new methods for ScInterpreter here.
30 void ScInterpreter::ScFilterXML()
32 sal_uInt8 nParamCount
= GetByte();
33 if (MustHaveParamCount( nParamCount
, 2 ) )
35 OUString aXPathExpression
= GetString().getString();
36 OUString aString
= GetString().getString();
37 if(aString
.isEmpty() || aXPathExpression
.isEmpty())
39 PushError( errNoValue
);
43 OString aOXPathExpression
= OUStringToOString( aXPathExpression
, RTL_TEXTENCODING_UTF8
);
44 const char* pXPathExpr
= aOXPathExpression
.getStr();
45 OString aOString
= OUStringToOString( aString
, RTL_TEXTENCODING_UTF8
);
46 const char* pXML
= aOString
.getStr();
48 boost::shared_ptr
<xmlParserCtxt
> pContext(
49 xmlNewParserCtxt(), xmlFreeParserCtxt
);
51 boost::shared_ptr
<xmlDoc
> pDoc( xmlParseMemory( pXML
, aOString
.getLength() ),
56 PushError( errNoValue
);
61 boost::shared_ptr
<xmlXPathContext
> pXPathCtx( xmlXPathNewContext(pDoc
.get()),
62 xmlXPathFreeContext
);
64 boost::shared_ptr
<xmlXPathObject
> pXPathObj( xmlXPathEvalExpression(BAD_CAST(pXPathExpr
), pXPathCtx
.get()),
69 PushError( errNoValue
);
73 rtl::OUString aResult
;
75 switch(pXPathObj
->type
)
81 xmlNodeSetPtr pNodeSet
= pXPathObj
->nodesetval
;
84 PushError( errNoValue
);
88 size_t nSize
= pNodeSet
->nodeNr
;
91 if(pNodeSet
->nodeTab
[0]->type
== XML_NAMESPACE_DECL
)
93 xmlNsPtr ns
= (xmlNsPtr
)pNodeSet
->nodeTab
[0];
94 xmlNodePtr cur
= (xmlNodePtr
)ns
->next
;
95 boost::shared_ptr
<xmlChar
> pChar2(xmlNodeGetContent(cur
), xmlFree
);
96 aResult
= OStringToOUString(OString((char*)pChar2
.get()), RTL_TEXTENCODING_UTF8
);
98 else if(pNodeSet
->nodeTab
[0]->type
== XML_ELEMENT_NODE
)
100 xmlNodePtr cur
= pNodeSet
->nodeTab
[0];
101 boost::shared_ptr
<xmlChar
> pChar2(xmlNodeGetContent(cur
), xmlFree
);
102 aResult
= OStringToOUString(OString((char*)pChar2
.get()), RTL_TEXTENCODING_UTF8
);
106 xmlNodePtr cur
= pNodeSet
->nodeTab
[0];
107 boost::shared_ptr
<xmlChar
> pChar2(xmlNodeGetContent(cur
), xmlFree
);
108 aResult
= OStringToOUString(OString((char*)pChar2
.get()), RTL_TEXTENCODING_UTF8
);
113 PushError( errNoValue
);
121 bool bVal
= pXPathObj
->boolval
!= 0;
127 double fVal
= pXPathObj
->floatval
;
132 PushString(OUString::createFromAscii((char*)pXPathObj
->stringval
));
140 case XPATH_LOCATIONSET
:
146 case XPATH_XSLT_TREE
:
154 void ScInterpreter::ScWebservice()
156 sal_uInt8 nParamCount
= GetByte();
157 if (MustHaveParamCount( nParamCount
, 1 ) )
159 OUString aURI
= GetString().getString();
163 PushError( errNoValue
);
167 uno::Reference
< ucb::XSimpleFileAccess3
> xFileAccess( ucb::SimpleFileAccess::create( comphelper::getProcessComponentContext() ), uno::UNO_QUERY
);
168 if(!xFileAccess
.is())
170 PushError( errNoValue
);
174 uno::Reference
< io::XInputStream
> xStream
;
176 xStream
= xFileAccess
->openFileRead( aURI
);
180 // don't let any exceptions pass
181 PushError( errNoValue
);
186 PushError( errNoValue
);
190 const sal_Int32 BUF_LEN
= 8000;
191 uno::Sequence
< sal_Int8
> buffer( BUF_LEN
);
192 OStringBuffer
aBuffer( 64000 );
195 while ( ( nRead
= xStream
->readBytes( buffer
, BUF_LEN
) ) == BUF_LEN
)
197 aBuffer
.append( reinterpret_cast< const char* >( buffer
.getConstArray() ), nRead
);
202 aBuffer
.append( reinterpret_cast< const char* >( buffer
.getConstArray() ), nRead
);
205 xStream
->closeInput();
207 OUString aContent
= OStringToOUString( aBuffer
.makeStringAndClear(), RTL_TEXTENCODING_UTF8
);
208 PushString( aContent
);
212 void ScInterpreter::ScDebugVar()
214 // This is to be used by developers only! Never document this for end
215 // users. This is a convenient way to extract arbitrary internal state to
216 // a cell for easier debugging.
218 SvtMiscOptions aMiscOptions
;
219 if (!aMiscOptions
.IsExperimentalMode())
221 PushError(ScErrorCodes::errNoName
);
225 if (!MustHaveParamCount(GetByte(), 1))
227 PushIllegalParameter();
231 rtl_uString
* p
= GetString().getDataIgnoreCase();
234 PushIllegalParameter();
238 OUString
aStrUpper(p
);
240 if (aStrUpper
== "PIVOTCOUNT")
242 // Set the number of pivot tables in the document.
245 if (pDok
->HasPivotTable())
247 const ScDPCollection
* pDPs
= pDok
->GetDPCollection();
248 fVal
= pDPs
->GetCount();
254 PushIllegalParameter();
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */