added Info.plist
[TestXSLT.git] / libsablot / src / engine / sxpath.cpp
1 /* 
2  * The contents of this file are subject to the Mozilla Public
3  * License Version 1.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.mozilla.org/MPL/
6  * 
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  * 
12  * The Original Code is the Sablotron XSLT Processor.
13  * 
14  * The Initial Developer of the Original Code is Ginger Alliance Ltd.
15  * Portions created by Ginger Alliance are Copyright (C) 2000-2002
16  * Ginger Alliance Ltd. All Rights Reserved.
17  * 
18  * Contributor(s):
19  * 
20  * Alternatively, the contents of this file may be used under the
21  * terms of the GNU General Public License Version 2 or later (the
22  * "GPL"), in which case the provisions of the GPL are applicable 
23  * instead of those above.  If you wish to allow use of your 
24  * version of this file only under the terms of the GPL and not to
25  * allow others to use your version of this file under the MPL,
26  * indicate your decision by deleting the provisions above and
27  * replace them with the notice and other provisions required by
28  * the GPL.  If you do not delete the provisions above, a recipient
29  * may use your version of this file under either the MPL or the
30  * GPL.
31  */
32
33 #define SablotAsExport
34 #include "sxpath.h"
35 #include "situa.h"
36 #include "domprovider.h"
37 #include "expr.h"
38 #include "context.h"
39 #include "guard.h"
40
41 #define SIT(S) (*(Situation*)S)
42 #define QC(Q) (*(QueryContextClass*)Q)
43
44 //
45 //    SablotRegDOMHandler
46 //
47
48 void SXP_registerDOMHandler(SablotSituation S, DOMHandler *domh, void* udata)
49 {
50     SIT(S).setDOMProvider(domh, udata);
51 }
52
53 void SXP_unregisterDOMHandler(SablotSituation S)
54 {
55     SIT(S).setDOMProvider(NULL, NULL);
56 }
57
58 /*
59  *
60  *    QueryContext functions
61  *
62  */
63
64 void SXP_setOptions(SablotSituation S, unsigned long options)
65 {
66   SIT(S).setSXPOptions(options);
67 }
68
69 unsigned long SXP_getOptions(SablotSituation S)
70 {
71   return SIT(S).getSXPOptions();
72 }
73
74 void SXP_setMaskBit(SablotSituation S, int mask)
75 {
76   SIT(S).setSXPMaskBit(mask);
77 }
78
79 int SXP_createQueryContext(SablotSituation S, QueryContext *Q)
80 {
81     *Q = new QueryContextClass(SIT(S));
82     return OK;
83 }
84
85 int SXP_addVariableNumber(QueryContext Q, 
86         const SXP_char* name, double value)
87 {
88     GP( Expression ) e = QC(Q).getNewExpr();
89     (*e).setAtom(Number(value)); 
90     if (! QC(Q).addVariableExpr(name, e))
91         e.keep();
92     return QC(Q).getError();
93 }
94
95 int SXP_addVariableString(QueryContext Q, 
96         const SXP_char* name, const SXP_char* value)
97 {
98     GP( Expression ) e = QC(Q).getNewExpr();
99     (*e).setAtom(Str((char*)value)); 
100     if (! QC(Q).addVariableExpr(name, e))
101         e.keep();
102     return QC(Q).getError();
103 }
104
105 int SXP_addVariableBoolean(QueryContext Q, 
106         const SXP_char* name, int value)
107 {
108     GP( Expression ) e = QC(Q).getNewExpr();
109     (*e).setAtom(value ? TRUE : FALSE); 
110     if (! QC(Q).addVariableExpr(name, e))
111         e.keep();
112     return QC(Q).getError();
113 }
114
115 int SXP_addVariableBinding(QueryContext Q, 
116     const SXP_char* name, QueryContext source)
117 {
118     QC(Q).addVariableBinding(name, QC(source));
119     return QC(Q).getError();
120 }
121     
122 int SXP_addNamespaceDeclaration(QueryContext Q, 
123     const SXP_char* prefix, const SXP_char* uri)
124 {
125     QC(Q).addNamespaceDeclaration(prefix, uri);
126     return QC(Q).getError();
127 }
128     
129 int SXP_query(QueryContext Q, const SXP_char* queryText,
130     SXP_Node n, int contextPosition, int contextSize)
131 {
132     QC(Q).query(queryText, n, contextPosition, contextSize);
133     return QC(Q).getError();
134 }
135
136 int SXP_destroyQueryContext(QueryContext Q)
137 {
138     delete (QueryContextClass*)Q;
139     return OK;
140 }
141
142 /*
143  *
144  *    Functions to retrieve the query result and its type
145  *
146  */
147
148 int SXP_getResultType(QueryContext Q, SXP_ExpressionType *type)
149 {
150     *type = QC(Q).getType();
151     return OK;
152 }
153
154 int SXP_getResultNumber(QueryContext Q, double *result)
155 {
156     *result = (double)(*(QC(Q).getNumber()));
157     return OK;
158 }
159
160 int SXP_getResultBool(QueryContext Q, int *result)
161 {
162     *result = QC(Q).getBoolean();
163     return OK;
164 }
165
166 int SXP_getResultString(QueryContext Q, const char **result)
167 {
168     *result = (char*)(*(QC(Q).getString()));
169     return OK;
170 }
171
172 int SXP_getResultNodeset(QueryContext Q, SXP_NodeList *result)
173 {
174     *result = (SXP_NodeList)(QC(Q).getNodeset());
175     return OK;
176 }
177
178 /*
179  *
180  *    NodeList manipulation
181  *
182  */
183
184 int SXP_getNodeListLength(SXP_NodeList l)
185 {
186     return ((Context*)l) -> getSize();
187 }
188
189 SXP_Node SXP_getNodeListItem(QueryContext Q, SXP_NodeList l, int index)
190 {
191   int count = SXP_getNodeListLength(l);
192   if (index < 0 || index >= count)
193     return NULL;
194   else
195     {
196       NodeHandle n = (*((Context*)l))[index];
197       int bit = QC(Q).getSituation().getSXPMaskBit();
198       return SXP_UNMASK_LEVEL(n, bit);
199     }
200 }
201
202