Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / tool / interpr7.cxx
blobaa07090cdd41067a3e04f26e7a9fb0f09faa874b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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>
24 #include <cstring>
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 );
40 return;
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() ),
52 xmlFreeDoc );
54 if(!pDoc)
56 PushError( errNoValue );
57 return;
61 boost::shared_ptr<xmlXPathContext> pXPathCtx( xmlXPathNewContext(pDoc.get()),
62 xmlXPathFreeContext );
64 boost::shared_ptr<xmlXPathObject> pXPathObj( xmlXPathEvalExpression(BAD_CAST(pXPathExpr), pXPathCtx.get()),
65 xmlXPathFreeObject );
67 if(!pXPathObj)
69 PushError( errNoValue );
70 return;
73 rtl::OUString aResult;
75 switch(pXPathObj->type)
77 case XPATH_UNDEFINED:
78 break;
79 case XPATH_NODESET:
81 xmlNodeSetPtr pNodeSet = pXPathObj->nodesetval;
82 if(!pNodeSet)
84 PushError( errNoValue );
85 return;
88 size_t nSize = pNodeSet->nodeNr;
89 if( nSize >= 1 )
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);
104 else
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);
111 else
113 PushError( errNoValue );
114 return;
117 PushString(aResult);
118 break;
119 case XPATH_BOOLEAN:
121 bool bVal = pXPathObj->boolval != 0;
122 PushDouble(bVal);
124 break;
125 case XPATH_NUMBER:
127 double fVal = pXPathObj->floatval;
128 PushDouble(fVal);
130 break;
131 case XPATH_STRING:
132 PushString(OUString::createFromAscii((char*)pXPathObj->stringval));
133 break;
134 case XPATH_POINT:
135 PushNoValue();
136 break;
137 case XPATH_RANGE:
138 PushNoValue();
139 break;
140 case XPATH_LOCATIONSET:
141 PushNoValue();
142 break;
143 case XPATH_USERS:
144 PushNoValue();
145 break;
146 case XPATH_XSLT_TREE:
147 PushNoValue();
148 break;
154 void ScInterpreter::ScWebservice()
156 sal_uInt8 nParamCount = GetByte();
157 if (MustHaveParamCount( nParamCount, 1 ) )
159 OUString aURI = GetString().getString();
161 if(aURI.isEmpty())
163 PushError( errNoValue );
164 return;
167 uno::Reference< ucb::XSimpleFileAccess3 > xFileAccess( ucb::SimpleFileAccess::create( comphelper::getProcessComponentContext() ), uno::UNO_QUERY );
168 if(!xFileAccess.is())
170 PushError( errNoValue );
171 return;
174 uno::Reference< io::XInputStream > xStream;
175 try {
176 xStream = xFileAccess->openFileRead( aURI );
178 catch (...)
180 // don't let any exceptions pass
181 PushError( errNoValue );
182 return;
184 if ( !xStream.is() )
186 PushError( errNoValue );
187 return;
190 const sal_Int32 BUF_LEN = 8000;
191 uno::Sequence< sal_Int8 > buffer( BUF_LEN );
192 OStringBuffer aBuffer( 64000 );
194 sal_Int32 nRead = 0;
195 while ( ( nRead = xStream->readBytes( buffer, BUF_LEN ) ) == BUF_LEN )
197 aBuffer.append( reinterpret_cast< const char* >( buffer.getConstArray() ), nRead );
200 if ( nRead > 0 )
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);
222 return;
225 if (!MustHaveParamCount(GetByte(), 1))
227 PushIllegalParameter();
228 return;
231 rtl_uString* p = GetString().getDataIgnoreCase();
232 if (!p)
234 PushIllegalParameter();
235 return;
238 OUString aStrUpper(p);
240 if (aStrUpper == "PIVOTCOUNT")
242 // Set the number of pivot tables in the document.
244 double fVal = 0.0;
245 if (pDok->HasPivotTable())
247 const ScDPCollection* pDPs = pDok->GetDPCollection();
248 fVal = pDPs->GetCount();
250 PushDouble(fVal);
251 return;
254 PushIllegalParameter();
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */