xref: /AOO41X/main/forms/source/xforms/xpathlib/xpathlib.cxx (revision 24acc54625a85f778a4f966495e8f4cd9d7b247c)
1*24acc546SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*24acc546SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*24acc546SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*24acc546SAndrew Rist  * distributed with this work for additional information
6*24acc546SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*24acc546SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*24acc546SAndrew Rist  * "License"); you may not use this file except in compliance
9*24acc546SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*24acc546SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*24acc546SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*24acc546SAndrew Rist  * software distributed under the License is distributed on an
15*24acc546SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*24acc546SAndrew Rist  * KIND, either express or implied.  See the License for the
17*24acc546SAndrew Rist  * specific language governing permissions and limitations
18*24acc546SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*24acc546SAndrew Rist  *************************************************************/
21*24acc546SAndrew Rist 
22*24acc546SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_forms.hxx"
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir #include <sal/types.h>
29cdf0e10cSrcweir #include <rtl/alloc.h>
30cdf0e10cSrcweir #include <rtl/ustring.hxx>
31cdf0e10cSrcweir #include <rtl/string.hxx>
32cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
33cdf0e10cSrcweir #include <rtl/strbuf.hxx>
34cdf0e10cSrcweir #include <tools/date.hxx>
35cdf0e10cSrcweir #include <tools/time.hxx>
36cdf0e10cSrcweir #include <tools/datetime.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
39cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
40cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
41cdf0e10cSrcweir #include <com/sun/star/xforms/XModel.hpp>
42cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNode.hpp>
43cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocument.hpp>
44cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include "xpathlib.hxx"
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include "extension.hxx"
49cdf0e10cSrcweir 
50cdf0e10cSrcweir // C interface
51cdf0e10cSrcweir 
52cdf0e10cSrcweir using namespace com::sun::star::uno;
53cdf0e10cSrcweir using namespace com::sun::star::xml::dom;
54cdf0e10cSrcweir using namespace com::sun::star::xforms;
55cdf0e10cSrcweir using namespace com::sun::star::lang;
56cdf0e10cSrcweir 
xforms_lookupFunc(void *,const xmlChar * xname,const xmlChar *)57cdf0e10cSrcweir xmlXPathFunction xforms_lookupFunc(void *, const xmlChar *xname, const xmlChar *)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     const char *name = (char *)xname;
61cdf0e10cSrcweir     if (strcmp("boolean-from-string", name)==0)
62cdf0e10cSrcweir         return xforms_booleanFromStringFunction;
63cdf0e10cSrcweir     else if ((strcmp("if", name))==0)
64cdf0e10cSrcweir         return xforms_ifFunction;
65cdf0e10cSrcweir     else if ((strcmp("avg", name))==0)
66cdf0e10cSrcweir         return xforms_avgFunction;
67cdf0e10cSrcweir     else if ((strcmp("min", name))==0)
68cdf0e10cSrcweir         return xforms_minFunction;
69cdf0e10cSrcweir     else if ((strcmp("max", name))==0)
70cdf0e10cSrcweir         return xforms_maxFunction;
71cdf0e10cSrcweir     else if ((strcmp("count-non-empty", name))==0)
72cdf0e10cSrcweir         return xforms_countNonEmptyFunction;
73cdf0e10cSrcweir     else if ((strcmp("index", name))==0)
74cdf0e10cSrcweir         return xforms_indexFunction;
75cdf0e10cSrcweir     else if ((strcmp("property", name))==0)
76cdf0e10cSrcweir         return xforms_propertyFunction;
77cdf0e10cSrcweir     else if ((strcmp("now", name))==0)
78cdf0e10cSrcweir         return xforms_nowFunction;
79cdf0e10cSrcweir     else if ((strcmp("days-from-date", name))==0)
80cdf0e10cSrcweir         return xforms_daysFromDateFunction;
81cdf0e10cSrcweir     else if ((strcmp("seconds-from-dateTime", name))==0)
82cdf0e10cSrcweir         return xforms_secondsFromDateTimeFunction;
83cdf0e10cSrcweir     else if ((strcmp("seconds", name))==0)
84cdf0e10cSrcweir         return xforms_secondsFuction;
85cdf0e10cSrcweir     else if ((strcmp("months", name))==0)
86cdf0e10cSrcweir         return xforms_monthsFuction;
87cdf0e10cSrcweir     else if ((strcmp("instance", name))==0)
88cdf0e10cSrcweir         return xforms_instanceFuction;
89cdf0e10cSrcweir     else if ((strcmp("current", name))==0)
90cdf0e10cSrcweir         return xforms_currentFunction;
91cdf0e10cSrcweir     else
92cdf0e10cSrcweir         return NULL;
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir // boolean functions
xforms_booleanFromStringFunction(xmlXPathParserContextPtr ctxt,int nargs)96cdf0e10cSrcweir void xforms_booleanFromStringFunction(xmlXPathParserContextPtr ctxt, int nargs)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
99cdf0e10cSrcweir     xmlChar *pString = xmlXPathPopString(ctxt);
100cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
101cdf0e10cSrcweir     ::rtl::OUString aString((char*)pString, strlen((char*)pString), RTL_TEXTENCODING_UTF8);
102cdf0e10cSrcweir     if (aString.equalsIgnoreAsciiCaseAscii("true") || aString.equalsIgnoreAsciiCaseAscii("1"))
103cdf0e10cSrcweir         xmlXPathReturnTrue(ctxt);
104cdf0e10cSrcweir     else if (aString.equalsIgnoreAsciiCaseAscii("false") || aString.equalsIgnoreAsciiCaseAscii("0"))
105cdf0e10cSrcweir         xmlXPathReturnFalse(ctxt);
106cdf0e10cSrcweir     else
107cdf0e10cSrcweir         XP_ERROR(XPATH_NUMBER_ERROR);
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
xforms_ifFunction(xmlXPathParserContextPtr ctxt,int nargs)110cdf0e10cSrcweir void xforms_ifFunction(xmlXPathParserContextPtr ctxt, int nargs)
111cdf0e10cSrcweir {
112cdf0e10cSrcweir     if (nargs != 3) XP_ERROR(XPATH_INVALID_ARITY);
113cdf0e10cSrcweir     xmlChar *s2 = xmlXPathPopString(ctxt);
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
116cdf0e10cSrcweir     xmlChar *s1 = xmlXPathPopString(ctxt);
117cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
118cdf0e10cSrcweir     bool aBool = xmlXPathPopBoolean(ctxt);
119cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     if (aBool)
122cdf0e10cSrcweir         xmlXPathReturnString(ctxt, s1);
123cdf0e10cSrcweir     else
124cdf0e10cSrcweir         xmlXPathReturnString(ctxt, s2);
125cdf0e10cSrcweir 
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir // Number Functions
xforms_avgFunction(xmlXPathParserContextPtr ctxt,int nargs)129cdf0e10cSrcweir void xforms_avgFunction(xmlXPathParserContextPtr ctxt, int nargs)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     // use sum(), div() and count()
132cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     // save nodeset
135cdf0e10cSrcweir     xmlXPathObjectPtr pObject = valuePop(ctxt);
136cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
137cdf0e10cSrcweir     //push back a copy
138cdf0e10cSrcweir     valuePush(ctxt, xmlXPathObjectCopy(pObject));
139cdf0e10cSrcweir     // get the Sum
140cdf0e10cSrcweir     xmlXPathSumFunction(ctxt, 1);
141cdf0e10cSrcweir     double nSum = xmlXPathPopNumber(ctxt);
142cdf0e10cSrcweir     // push a copy once more
143cdf0e10cSrcweir     valuePush(ctxt, xmlXPathObjectCopy(pObject));
144cdf0e10cSrcweir     xmlXPathCountFunction(ctxt, 1);
145cdf0e10cSrcweir     double nCount = xmlXPathPopNumber(ctxt);
146cdf0e10cSrcweir     // push args for div()
147cdf0e10cSrcweir     xmlXPathReturnNumber(ctxt, nSum);
148cdf0e10cSrcweir     xmlXPathReturnNumber(ctxt, nCount);
149cdf0e10cSrcweir     xmlXPathDivValues(ctxt);
150cdf0e10cSrcweir     // the result is now on the ctxt stack
151cdf0e10cSrcweir     xmlXPathFreeObject(pObject);
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
xforms_minFunction(xmlXPathParserContextPtr ctxt,int nargs)154cdf0e10cSrcweir void xforms_minFunction(xmlXPathParserContextPtr ctxt, int nargs)
155cdf0e10cSrcweir {
156cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
157cdf0e10cSrcweir     xmlNodeSetPtr pNodeSet = xmlXPathPopNodeSet(ctxt);
158cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
159cdf0e10cSrcweir     double nMinimum = 0;
160cdf0e10cSrcweir     double nNumber = 0;
161cdf0e10cSrcweir     for (int i = 0; i <  xmlXPathNodeSetGetLength(pNodeSet); i++)
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         nNumber = xmlXPathCastNodeToNumber(xmlXPathNodeSetItem(pNodeSet, i));
164cdf0e10cSrcweir         if (xmlXPathIsNaN(nNumber))
165cdf0e10cSrcweir         {
166cdf0e10cSrcweir             xmlXPathReturnNumber(ctxt, xmlXPathNAN);
167cdf0e10cSrcweir             return;
168cdf0e10cSrcweir         }
169cdf0e10cSrcweir         if (i == 0)
170cdf0e10cSrcweir             nMinimum = nNumber;
171cdf0e10cSrcweir         else if (nNumber < nMinimum)
172cdf0e10cSrcweir             nMinimum = nNumber;
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir     xmlXPathReturnNumber(ctxt, nMinimum);
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
xforms_maxFunction(xmlXPathParserContextPtr ctxt,int nargs)177cdf0e10cSrcweir void xforms_maxFunction(xmlXPathParserContextPtr ctxt, int nargs)
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
180cdf0e10cSrcweir     xmlNodeSetPtr pNodeSet = xmlXPathPopNodeSet(ctxt);
181cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
182cdf0e10cSrcweir     double nMaximum = 0;
183cdf0e10cSrcweir     double nNumber = 0;
184cdf0e10cSrcweir     for (int i = 0; i <  xmlXPathNodeSetGetLength(pNodeSet); i++)
185cdf0e10cSrcweir     {
186cdf0e10cSrcweir         nNumber = xmlXPathCastNodeToNumber(xmlXPathNodeSetItem(pNodeSet, i));
187cdf0e10cSrcweir         if (xmlXPathIsNaN(nNumber))
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir             xmlXPathReturnNumber(ctxt, xmlXPathNAN);
190cdf0e10cSrcweir             return;
191cdf0e10cSrcweir         }
192cdf0e10cSrcweir         if (i == 0)
193cdf0e10cSrcweir             nMaximum = nNumber;
194cdf0e10cSrcweir         else if (nNumber > nMaximum)
195cdf0e10cSrcweir             nMaximum = nNumber;
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir     xmlXPathReturnNumber(ctxt, nMaximum);
198cdf0e10cSrcweir }
xforms_countNonEmptyFunction(xmlXPathParserContextPtr ctxt,int nargs)199cdf0e10cSrcweir void xforms_countNonEmptyFunction(xmlXPathParserContextPtr ctxt, int nargs)
200cdf0e10cSrcweir {
201cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
202cdf0e10cSrcweir     xmlNodeSetPtr pNodeSet = xmlXPathPopNodeSet(ctxt);
203cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
204cdf0e10cSrcweir     xmlChar *aString;
205cdf0e10cSrcweir     sal_Int32 nNotEmpty = 0;
206cdf0e10cSrcweir     for (int i = 0; i <  xmlXPathNodeSetGetLength(pNodeSet); i++)
207cdf0e10cSrcweir     {
208cdf0e10cSrcweir         aString = xmlXPathCastNodeToString(xmlXPathNodeSetItem(pNodeSet, i));
209cdf0e10cSrcweir         if (strlen((char*)aString) > 0) nNotEmpty++;
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir     xmlXPathReturnNumber(ctxt, nNotEmpty);
212cdf0e10cSrcweir }
xforms_indexFunction(xmlXPathParserContextPtr,int)213cdf0e10cSrcweir void xforms_indexFunction(xmlXPathParserContextPtr /*ctxt*/, int /*nargs*/)
214cdf0e10cSrcweir {
215cdf0e10cSrcweir     // function index takes a string argument that is the IDREF of a
216cdf0e10cSrcweir     // 'repeat' and returns the current 1-based position of the repeat
217cdf0e10cSrcweir     // index of the identified repeat -- see xforms/9.3.1
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     // doc.getElementByID
220cdf0e10cSrcweir     // (...)
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir // String Functions
224cdf0e10cSrcweir static const char* _version = "1.0";
225cdf0e10cSrcweir static const char* _conformance = "conformance";
xforms_propertyFunction(xmlXPathParserContextPtr ctxt,int nargs)226cdf0e10cSrcweir void xforms_propertyFunction(xmlXPathParserContextPtr ctxt, int nargs)
227cdf0e10cSrcweir {
228cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
229cdf0e10cSrcweir     xmlChar* pString = xmlXPathPopString(ctxt);
230cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
231cdf0e10cSrcweir     ::rtl::OUString aString((char*)pString, strlen((char*)pString), RTL_TEXTENCODING_UTF8);
232cdf0e10cSrcweir     if (aString.equalsIgnoreAsciiCaseAscii("version"))
233cdf0e10cSrcweir         xmlXPathReturnString(ctxt, (xmlChar*)_version);
234cdf0e10cSrcweir     else if (aString.equalsIgnoreAsciiCaseAscii("conformance-level"))
235cdf0e10cSrcweir         xmlXPathReturnString(ctxt, (xmlChar*)_conformance);
236cdf0e10cSrcweir     else
237cdf0e10cSrcweir         xmlXPathReturnEmptyString(ctxt);
238cdf0e10cSrcweir }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir // Date and Time Functions
241cdf0e10cSrcweir 
makeDateTimeString(const DateTime & aDateTime,sal_Bool bUTC=sal_True)242cdf0e10cSrcweir static ::rtl::OString makeDateTimeString (const DateTime& aDateTime, sal_Bool bUTC = sal_True)
243cdf0e10cSrcweir {
244cdf0e10cSrcweir     ::rtl::OStringBuffer aDateTimeString;
245cdf0e10cSrcweir     aDateTimeString.append((sal_Int32)aDateTime.GetYear());
246cdf0e10cSrcweir     aDateTimeString.append("-");
247cdf0e10cSrcweir     if (aDateTime.GetMonth()<10) aDateTimeString.append("0");
248cdf0e10cSrcweir     aDateTimeString.append((sal_Int32)aDateTime.GetMonth());
249cdf0e10cSrcweir     aDateTimeString.append("-");
250cdf0e10cSrcweir     if (aDateTime.GetDay()<10) aDateTimeString.append("0");
251cdf0e10cSrcweir     aDateTimeString.append((sal_Int32)aDateTime.GetDay());
252cdf0e10cSrcweir     aDateTimeString.append("T");
253cdf0e10cSrcweir     if (aDateTime.GetHour()<10) aDateTimeString.append("0");
254cdf0e10cSrcweir     aDateTimeString.append((sal_Int32)aDateTime.GetHour());
255cdf0e10cSrcweir     aDateTimeString.append(":");
256cdf0e10cSrcweir     if (aDateTime.GetMin()<10) aDateTimeString.append("0");
257cdf0e10cSrcweir     aDateTimeString.append((sal_Int32)aDateTime.GetMin());
258cdf0e10cSrcweir     aDateTimeString.append(":");
259cdf0e10cSrcweir     if (aDateTime.GetSec()<10) aDateTimeString.append("0");
260cdf0e10cSrcweir     aDateTimeString.append((sal_Int32)aDateTime.GetSec());
261cdf0e10cSrcweir     if (bUTC) aDateTimeString.append("Z");
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     return aDateTimeString.makeStringAndClear();
264cdf0e10cSrcweir }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir // returns current system date and time in canonical xsd:dateTime
267cdf0e10cSrcweir // format
xforms_nowFunction(xmlXPathParserContextPtr ctxt,int)268cdf0e10cSrcweir void xforms_nowFunction(xmlXPathParserContextPtr ctxt, int /*nargs*/)
269cdf0e10cSrcweir {
270cdf0e10cSrcweir     /*
271cdf0e10cSrcweir     A single lexical representation, which is a subset of the lexical representations
272cdf0e10cSrcweir     allowed by [ISO 8601], is allowed for dateTime. This lexical representation is the
273cdf0e10cSrcweir     [ISO 8601] extended format CCYY-MM-DDThh:mm:ss where "CC" represents the century,
274cdf0e10cSrcweir     "YY" the year, "MM" the month and "DD" the day, preceded by an optional leading "-"
275cdf0e10cSrcweir     sign to indicate a negative number. If the sign is omitted, "+" is assumed. The letter
276cdf0e10cSrcweir     "T" is the date/time separator and "hh", "mm", "ss" represent hour, minute and second
277cdf0e10cSrcweir     respectively.
278cdf0e10cSrcweir     */
279cdf0e10cSrcweir 
280cdf0e10cSrcweir     /*
281cdf0e10cSrcweir     3.2.7.2 Canonical representation
282cdf0e10cSrcweir     The canonical representation for dateTime is defined by prohibiting certain options
283cdf0e10cSrcweir     from the Lexical representation (par.3.2.7.1). Specifically, either the time zone must
284cdf0e10cSrcweir     be omitted or, if present, the time zone must be Coordinated Universal Time (UTC)
285cdf0e10cSrcweir     indicated by a "Z".
286cdf0e10cSrcweir     */
287cdf0e10cSrcweir     DateTime aDateTime;
288cdf0e10cSrcweir     ::rtl::OString aDateTimeString = makeDateTimeString(aDateTime);
289cdf0e10cSrcweir     xmlChar *pString = static_cast<xmlChar*>(xmlMalloc(aDateTimeString.getLength()+1));
290cdf0e10cSrcweir     strncpy((char*)pString, (char*)aDateTimeString.getStr(), aDateTimeString.getLength());
291cdf0e10cSrcweir     pString[aDateTimeString.getLength()] = 0;
292cdf0e10cSrcweir     xmlXPathReturnString(ctxt, pString);
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
parseDateTime(const::rtl::OUString & aString,DateTime & aDateTime)295cdf0e10cSrcweir static sal_Bool parseDateTime(const ::rtl::OUString& aString, DateTime& aDateTime)
296cdf0e10cSrcweir {
297cdf0e10cSrcweir     // take apart a canonical literal xsd:dateTime string
298cdf0e10cSrcweir     //CCYY-MM-DDThh:mm:ss(Z)
299cdf0e10cSrcweir 
300cdf0e10cSrcweir     ::rtl::OUString aDateTimeString = aString.trim();
301cdf0e10cSrcweir 
302cdf0e10cSrcweir     // check length
303cdf0e10cSrcweir     if (aDateTimeString.getLength() < 19 || aDateTimeString.getLength() > 20)
304cdf0e10cSrcweir         return sal_False;
305cdf0e10cSrcweir 
306cdf0e10cSrcweir     sal_Int32 nDateLength = 10;
307cdf0e10cSrcweir     sal_Int32 nTimeLength = 8;
308cdf0e10cSrcweir 
309cdf0e10cSrcweir     ::rtl::OUString aDateTimeSep = ::rtl::OUString::createFromAscii("T");
310cdf0e10cSrcweir     ::rtl::OUString aDateSep = ::rtl::OUString::createFromAscii("-");
311cdf0e10cSrcweir     ::rtl::OUString aTimeSep = ::rtl::OUString::createFromAscii(":");
312cdf0e10cSrcweir     ::rtl::OUString aUTCString = ::rtl::OUString::createFromAscii("Z");
313cdf0e10cSrcweir 
314cdf0e10cSrcweir     ::rtl::OUString aDateString = aDateTimeString.copy(0, nDateLength);
315cdf0e10cSrcweir     ::rtl::OUString aTimeString = aDateTimeString.copy(nDateLength+1, nTimeLength);
316cdf0e10cSrcweir 
317cdf0e10cSrcweir     sal_Int32 nIndex = 0;
318cdf0e10cSrcweir     sal_Int32 nYear = aDateString.getToken(0, '-', nIndex).toInt32();
319cdf0e10cSrcweir     sal_Int32 nMonth = aDateString.getToken(0, '-', nIndex).toInt32();
320cdf0e10cSrcweir     sal_Int32 nDay = aDateString.getToken(0, '-', nIndex).toInt32();
321cdf0e10cSrcweir     nIndex = 0;
322cdf0e10cSrcweir     sal_Int32 nHour = aTimeString.getToken(0, ':', nIndex).toInt32();
323cdf0e10cSrcweir     sal_Int32 nMinute = aTimeString.getToken(0, ':', nIndex).toInt32();
324cdf0e10cSrcweir     sal_Int32 nSecond = aTimeString.getToken(0, ':', nIndex).toInt32();
325cdf0e10cSrcweir 
326cdf0e10cSrcweir     Date tmpDate((sal_uInt16)nDay, (sal_uInt16)nMonth, (sal_uInt16)nYear);
327cdf0e10cSrcweir     Time tmpTime(nHour, nMinute, nSecond);
328cdf0e10cSrcweir     DateTime tmpDateTime(tmpDate, tmpTime);
329cdf0e10cSrcweir     if (aString.indexOf(aUTCString) < 0)
330cdf0e10cSrcweir         tmpDateTime.ConvertToUTC();
331cdf0e10cSrcweir 
332cdf0e10cSrcweir     aDateTime = tmpDateTime;
333cdf0e10cSrcweir 
334cdf0e10cSrcweir     return sal_True;
335cdf0e10cSrcweir }
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 
xforms_daysFromDateFunction(xmlXPathParserContextPtr ctxt,int nargs)338cdf0e10cSrcweir void xforms_daysFromDateFunction(xmlXPathParserContextPtr ctxt, int nargs)
339cdf0e10cSrcweir {
340cdf0e10cSrcweir     // number of days from 1970-01-01 to supplied xsd:date(Time)
341cdf0e10cSrcweir 
342cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
343cdf0e10cSrcweir     xmlChar* pString = xmlXPathPopString(ctxt);
344cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
345cdf0e10cSrcweir     ::rtl::OUString aString((char*)pString, strlen((char*)pString), RTL_TEXTENCODING_UTF8);
346cdf0e10cSrcweir 
347cdf0e10cSrcweir     DateTime aDateTime;
348cdf0e10cSrcweir     if (parseDateTime(aString, aDateTime))
349cdf0e10cSrcweir     {
350cdf0e10cSrcweir         Date aReferenceDate(1, 1, 1970);
351cdf0e10cSrcweir         sal_Int32 nDays = aDateTime - aReferenceDate;
352cdf0e10cSrcweir         xmlXPathReturnNumber(ctxt, nDays);
353cdf0e10cSrcweir     }
354cdf0e10cSrcweir     else
355cdf0e10cSrcweir         xmlXPathReturnNumber(ctxt, xmlXPathNAN);
356cdf0e10cSrcweir 
357cdf0e10cSrcweir 
358cdf0e10cSrcweir }
359cdf0e10cSrcweir 
360cdf0e10cSrcweir 
xforms_secondsFromDateTimeFunction(xmlXPathParserContextPtr ctxt,int nargs)361cdf0e10cSrcweir void xforms_secondsFromDateTimeFunction(xmlXPathParserContextPtr ctxt, int nargs)
362cdf0e10cSrcweir {
363cdf0e10cSrcweir     // number of seconds from 1970-01-01T00:00:00Z to supplied xsd:date(Time)
364cdf0e10cSrcweir 
365cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
366cdf0e10cSrcweir     xmlChar* pString = xmlXPathPopString(ctxt);
367cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
368cdf0e10cSrcweir     ::rtl::OUString aString((char*)pString, strlen((char*)pString), RTL_TEXTENCODING_UTF8);
369cdf0e10cSrcweir 
370cdf0e10cSrcweir     DateTime aDateTime;
371cdf0e10cSrcweir 
372cdf0e10cSrcweir     if (parseDateTime(aString, aDateTime))
373cdf0e10cSrcweir     {
374cdf0e10cSrcweir         Date aReferenceDate(1, 1, 1970);
375cdf0e10cSrcweir         Time aReferenceTime(0, 0, 0);
376cdf0e10cSrcweir         sal_Int32 nDays = aDateTime - aReferenceDate;
377cdf0e10cSrcweir         sal_Int32 nSeconds = nDays * 24 * 60 * 60;
378cdf0e10cSrcweir         nSeconds += aDateTime.GetHour() * 60 * 60;
379cdf0e10cSrcweir         nSeconds += aDateTime.GetMin() * 60;
380cdf0e10cSrcweir         nSeconds += aDateTime.GetSec();
381cdf0e10cSrcweir         xmlXPathReturnNumber(ctxt, nSeconds);
382cdf0e10cSrcweir     }
383cdf0e10cSrcweir     else
384cdf0e10cSrcweir         xmlXPathReturnNumber(ctxt, xmlXPathNAN);
385cdf0e10cSrcweir 
386cdf0e10cSrcweir }
387cdf0e10cSrcweir 
parseDuration(const xmlChar * aString,sal_Bool & bNegative,sal_Int32 & nYears,sal_Int32 & nMonth,sal_Int32 & nDays,sal_Int32 & nHours,sal_Int32 & nMinutes,sal_Int32 & nSeconds)388cdf0e10cSrcweir static sal_Bool parseDuration(const xmlChar* aString, sal_Bool& bNegative, sal_Int32& nYears, sal_Int32& nMonth, sal_Int32& nDays,
389cdf0e10cSrcweir                               sal_Int32& nHours, sal_Int32& nMinutes, sal_Int32& nSeconds)
390cdf0e10cSrcweir {
391cdf0e10cSrcweir     sal_Bool bTime = sal_False; // in part after T
392cdf0e10cSrcweir     sal_Int32 nLength = strlen((char*)aString)+1;
393cdf0e10cSrcweir     char *pString = (char*)rtl_allocateMemory(nLength);
394cdf0e10cSrcweir     char *pString0 = pString;
395cdf0e10cSrcweir     strncpy(pString, (char*)aString, nLength);
396cdf0e10cSrcweir 
397cdf0e10cSrcweir     if (pString[0] == '-') {
398cdf0e10cSrcweir         bNegative = sal_True;
399cdf0e10cSrcweir         pString++;
400cdf0e10cSrcweir     }
401cdf0e10cSrcweir 
402cdf0e10cSrcweir     if (pString[0] != 'P')
403cdf0e10cSrcweir         return sal_False;
404cdf0e10cSrcweir     pString++;
405cdf0e10cSrcweir     char* pToken = pString;
406cdf0e10cSrcweir     while(pToken[0] != 0)
407cdf0e10cSrcweir     {
408cdf0e10cSrcweir         switch(pToken[0]) {
409cdf0e10cSrcweir         case 'Y':
410cdf0e10cSrcweir             pToken[0] = 0;
411cdf0e10cSrcweir             nYears = atoi(pString);
412cdf0e10cSrcweir             pString = ++pToken;
413cdf0e10cSrcweir             break;
414cdf0e10cSrcweir         case 'M':
415cdf0e10cSrcweir             pToken[0] = 0;
416cdf0e10cSrcweir             if (!bTime)
417cdf0e10cSrcweir                 nMonth = atoi(pString);
418cdf0e10cSrcweir             else
419cdf0e10cSrcweir                 nMinutes = atoi(pString);
420cdf0e10cSrcweir             pString = ++pToken;
421cdf0e10cSrcweir             break;
422cdf0e10cSrcweir         case 'D':
423cdf0e10cSrcweir             pToken[0] = 0;
424cdf0e10cSrcweir             nDays = atoi(pString);
425cdf0e10cSrcweir             pString = ++pToken;
426cdf0e10cSrcweir             break;
427cdf0e10cSrcweir         case 'H':
428cdf0e10cSrcweir             pToken[0] = 0;
429cdf0e10cSrcweir             nHours = atoi(pString);
430cdf0e10cSrcweir             pString = ++pToken;
431cdf0e10cSrcweir             break;
432cdf0e10cSrcweir         case 'S':
433cdf0e10cSrcweir             pToken[0] = 0;
434cdf0e10cSrcweir             nSeconds = atoi(pString);
435cdf0e10cSrcweir             pString = ++pToken;
436cdf0e10cSrcweir             break;
437cdf0e10cSrcweir         case 'T':
438cdf0e10cSrcweir             bTime = sal_True;
439cdf0e10cSrcweir             pString = ++pToken;
440cdf0e10cSrcweir             break;
441cdf0e10cSrcweir         default:
442cdf0e10cSrcweir             pToken++;
443cdf0e10cSrcweir         }
444cdf0e10cSrcweir     }
445cdf0e10cSrcweir     rtl_freeMemory(pString0);
446cdf0e10cSrcweir     return sal_True;
447cdf0e10cSrcweir }
448cdf0e10cSrcweir 
xforms_secondsFuction(xmlXPathParserContextPtr ctxt,int nargs)449cdf0e10cSrcweir void xforms_secondsFuction(xmlXPathParserContextPtr ctxt, int nargs)
450cdf0e10cSrcweir {
451cdf0e10cSrcweir     // convert a xsd:duration to seconds
452cdf0e10cSrcweir     // (-)PnYnMnDTnHnMnS
453cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
454cdf0e10cSrcweir     xmlChar* pString = xmlXPathPopString(ctxt);
455cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
456cdf0e10cSrcweir 
457cdf0e10cSrcweir     sal_Bool bNegative = sal_False;
458cdf0e10cSrcweir     sal_Int32 nYears = 0;
459cdf0e10cSrcweir     sal_Int32 nMonths = 0;
460cdf0e10cSrcweir     sal_Int32 nDays = 0;
461cdf0e10cSrcweir     sal_Int32 nHours = 0;
462cdf0e10cSrcweir     sal_Int32 nMinutes = 0;
463cdf0e10cSrcweir     sal_Int32 nSeconds = 0;
464cdf0e10cSrcweir 
465cdf0e10cSrcweir     if (parseDuration(pString, bNegative, nYears, nMonths, nDays, nHours, nMinutes, nSeconds))
466cdf0e10cSrcweir     {
467cdf0e10cSrcweir         nSeconds += nMinutes*60;
468cdf0e10cSrcweir         nSeconds += nHours*60*60;
469cdf0e10cSrcweir         nSeconds += nDays*24*60*60;
470cdf0e10cSrcweir         // year and month are ignored according to spec
471cdf0e10cSrcweir         if (bNegative)
472cdf0e10cSrcweir             nSeconds = 0 - nSeconds;
473cdf0e10cSrcweir         xmlXPathReturnNumber(ctxt, nSeconds);
474cdf0e10cSrcweir     }
475cdf0e10cSrcweir     else
476cdf0e10cSrcweir         xmlXPathReturnNumber(ctxt, xmlXPathNAN);
477cdf0e10cSrcweir }
478cdf0e10cSrcweir 
xforms_monthsFuction(xmlXPathParserContextPtr ctxt,int nargs)479cdf0e10cSrcweir void xforms_monthsFuction(xmlXPathParserContextPtr ctxt, int nargs)
480cdf0e10cSrcweir {
481cdf0e10cSrcweir     // convert a xsd:duration to seconds
482cdf0e10cSrcweir     // (-)PnYnMnDTnHnMnS
483cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
484cdf0e10cSrcweir     xmlChar* pString = xmlXPathPopString(ctxt);
485cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
486cdf0e10cSrcweir 
487cdf0e10cSrcweir     sal_Bool bNegative = sal_False;
488cdf0e10cSrcweir     sal_Int32 nYears = 0;
489cdf0e10cSrcweir     sal_Int32 nMonths = 0;
490cdf0e10cSrcweir     sal_Int32 nDays = 0;
491cdf0e10cSrcweir     sal_Int32 nHours = 0;
492cdf0e10cSrcweir     sal_Int32 nMinutes = 0;
493cdf0e10cSrcweir     sal_Int32 nSeconds = 0;
494cdf0e10cSrcweir 
495cdf0e10cSrcweir     if (parseDuration(pString, bNegative, nYears, nMonths, nDays, nHours, nMinutes, nSeconds))
496cdf0e10cSrcweir     {
497cdf0e10cSrcweir         nMonths += nYears*12;
498cdf0e10cSrcweir         // Days, Houres, Minutes and seconds are ignored, see spec
499cdf0e10cSrcweir         if (bNegative)
500cdf0e10cSrcweir             nMonths = 0 - nMonths;
501cdf0e10cSrcweir         xmlXPathReturnNumber(ctxt, nMonths);
502cdf0e10cSrcweir     }
503cdf0e10cSrcweir     else
504cdf0e10cSrcweir         xmlXPathReturnNumber(ctxt, xmlXPathNAN);
505cdf0e10cSrcweir 
506cdf0e10cSrcweir }
507cdf0e10cSrcweir 
508cdf0e10cSrcweir // Node-set Functions
xforms_instanceFuction(xmlXPathParserContextPtr ctxt,int nargs)509cdf0e10cSrcweir void xforms_instanceFuction(xmlXPathParserContextPtr ctxt, int nargs)
510cdf0e10cSrcweir {
511cdf0e10cSrcweir     if (nargs != 1) XP_ERROR(XPATH_INVALID_ARITY);
512cdf0e10cSrcweir     xmlChar *pString = xmlXPathPopString(ctxt);
513cdf0e10cSrcweir     if (xmlXPathCheckError(ctxt)) XP_ERROR(XPATH_INVALID_TYPE);
514cdf0e10cSrcweir     ::rtl::OUString aString((char*)pString, strlen((char*)pString), RTL_TEXTENCODING_UTF8);
515cdf0e10cSrcweir 
516cdf0e10cSrcweir     Reference< XModel > aModel = ((CLibxml2XFormsExtension*)ctxt->context->funcLookupData)->getModel();
517cdf0e10cSrcweir     if (aModel.is())
518cdf0e10cSrcweir     {
519cdf0e10cSrcweir         Reference< XDocument > aInstance = aModel->getInstanceDocument(aString);
520cdf0e10cSrcweir         if (aInstance.is())
521cdf0e10cSrcweir         {
522cdf0e10cSrcweir             try {
523cdf0e10cSrcweir                 // xmlXPathObjectPtr xmlXPathNewNodeSet        (xmlNodePtr val);
524cdf0e10cSrcweir                 Reference< XUnoTunnel > aTunnel(aInstance, UNO_QUERY_THROW);
525cdf0e10cSrcweir                 xmlNodePtr pNode = reinterpret_cast< xmlNodePtr >( aTunnel->getSomething(Sequence< sal_Int8 >()) );
526cdf0e10cSrcweir                 xmlXPathObjectPtr pObject = xmlXPathNewNodeSet(pNode);
527cdf0e10cSrcweir                 xmlXPathReturnNodeSet(ctxt, pObject->nodesetval);
528cdf0e10cSrcweir             } catch (RuntimeException&)
529cdf0e10cSrcweir             {
530cdf0e10cSrcweir                 xmlXPathReturnEmptyNodeSet(ctxt);
531cdf0e10cSrcweir             }
532cdf0e10cSrcweir         }
533cdf0e10cSrcweir         else
534cdf0e10cSrcweir             xmlXPathReturnEmptyNodeSet(ctxt);
535cdf0e10cSrcweir     }
536cdf0e10cSrcweir     else
537cdf0e10cSrcweir         xmlXPathReturnEmptyNodeSet(ctxt);
538cdf0e10cSrcweir 
539cdf0e10cSrcweir }
540cdf0e10cSrcweir 
541cdf0e10cSrcweir // Node-set Functions, XForms 1.1
xforms_currentFunction(xmlXPathParserContextPtr ctxt,int nargs)542cdf0e10cSrcweir void xforms_currentFunction(xmlXPathParserContextPtr ctxt, int nargs)
543cdf0e10cSrcweir {
544cdf0e10cSrcweir     if (nargs != 0) XP_ERROR(XPATH_INVALID_ARITY);
545cdf0e10cSrcweir 
546cdf0e10cSrcweir     Reference< XNode > aNode = ((CLibxml2XFormsExtension*)ctxt->context->funcLookupData)->getContextNode();
547cdf0e10cSrcweir 
548cdf0e10cSrcweir     if (aNode.is())
549cdf0e10cSrcweir     {
550cdf0e10cSrcweir         try {
551cdf0e10cSrcweir             Reference< XUnoTunnel > aTunnel(aNode, UNO_QUERY_THROW);
552cdf0e10cSrcweir             xmlNodePtr pNode = reinterpret_cast< xmlNodePtr >( aTunnel->getSomething(Sequence< sal_Int8 >()) );
553cdf0e10cSrcweir             xmlXPathObjectPtr pObject = xmlXPathNewNodeSet(pNode);
554cdf0e10cSrcweir             xmlXPathReturnNodeSet(ctxt, pObject->nodesetval);
555cdf0e10cSrcweir         }
556cdf0e10cSrcweir         catch (RuntimeException&)
557cdf0e10cSrcweir         {
558cdf0e10cSrcweir             xmlXPathReturnEmptyNodeSet(ctxt);
559cdf0e10cSrcweir         }
560cdf0e10cSrcweir     }
561cdf0e10cSrcweir     else
562cdf0e10cSrcweir         xmlXPathReturnEmptyNodeSet(ctxt);
563cdf0e10cSrcweir }
564