1 /*----------------------------------------------------------------------------
2 |   Copyright (c) 1999 Jochen Loewer (loewerj@hotmail.com)
3 |-----------------------------------------------------------------------------
4 |
5 |   $Id$
6 |
7 |
8 |   A (partial) XPath implementation (lexer/parser/evaluator) for tDOM,
9 |   the DOM implementation for Tcl.
10 |   Based on the August 13 working draft of the W3C
11 |   (http://www.w3.org/1999/08/WD-xpath-19990813.html)
12 |
13 |
14 |   The contents of this file are subject to the Mozilla Public License
15 |   Version 1.1 (the "License"); you may not use this file except in
16 |   compliance with the License. You may obtain a copy of the License at
17 |   http://www.mozilla.org/MPL/
18 |
19 |   Software distributed under the License is distributed on an "AS IS"
20 |   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
21 |   License for the specific language governing rights and limitations
22 |   under the License.
23 |
24 |   The Original Code is tDOM.
25 |
26 |   The Initial Developer of the Original Code is Jochen Loewer
27 |   Portions created by Jochen Loewer are Copyright (C) 1998, 1999
28 |   Jochen Loewer. All Rights Reserved.
29 |
30 |   Contributor(s):
31 |
32 |
33 |
34 |   written by Jochen Loewer
35 |   July, 1999
36 |
37 \---------------------------------------------------------------------------*/
38 
39 
40 #ifndef __DOMXPATH_H__
41 #define __DOMXPATH_H__
42 
43 #include <float.h>
44 #include <dom.h>
45 
46 /*----------------------------------------------------------------------------
47 |   Macros
48 |
49 \---------------------------------------------------------------------------*/
50 #define XPATH_OK             0
51 #define XPATH_LEX_ERR       -1
52 #define XPATH_SYNTAX_ERR    -2
53 #define XPATH_EVAL_ERR      -3
54 #define XPATH_VAR_NOT_FOUND -4
55 #define XPATH_I18N_ERR      -5
56 
57 /*
58  * Macros for testing floating-point values for certain special cases. Test
59  * for not-a-number by comparing a value against itself; test for infinity
60  * by comparing against the largest floating-point value.
61  */
62 
63 #define IS_NAN(v) ((v) != (v))
64 #ifdef DBL_MAX
65 #   define IS_INF(v) ((v) > DBL_MAX ? 1 : ((v) < -DBL_MAX ? -1 : 0))
66 #else
67 #   define IS_INF(v) 0
68 #endif
69 
70 /*----------------------------------------------------------------------------
71 |   Types for abstract syntax trees
72 |
73 \---------------------------------------------------------------------------*/
74 typedef enum {
75     Int, Real, Mult, Div, Mod, UnaryMinus, IsNSElement,
76     IsNode, IsComment, IsText, IsPI, IsSpecificPI, IsElement,
77     IsFQElement, GetVar, GetFQVar, Literal, ExecFunction, Pred,
78     EvalSteps, SelectRoot, CombineSets, Add, Subtract, Less,
79     LessOrEq, Greater, GreaterOrEq, Equal,  NotEqual, And, Or,
80     IsNSAttr, IsAttr, AxisAncestor, AxisAncestorOrSelf,
81     AxisAttribute, AxisChild,
82     AxisDescendant, AxisDescendantOrSelf, AxisFollowing,
83     AxisFollowingSibling, AxisNamespace, AxisParent,
84     AxisPreceding, AxisPrecedingSibling, AxisSelf,
85     GetContextNode, GetParentNode, AxisDescendantOrSelfLit,
86     AxisDescendantLit, SlashSlash,
87 
88     CombinePath, IsRoot, ToParent, ToAncestors, FillNodeList,
89     FillWithCurrentNode,
90     ExecIdKey
91 
92 } astType;
93 
94 
95 typedef struct astElem {
96     astType         type;
97     struct astElem *child;
98     struct astElem *next;
99     char           *strvalue;
100     long            intvalue;
101     double          realvalue;
102 } astElem;
103 
104 typedef astElem *ast;
105 
106 
107 /*----------------------------------------------------------------------------
108 |   Types for XPath result set
109 |
110 \---------------------------------------------------------------------------*/
111 typedef enum {
112     EmptyResult, BoolResult, IntResult, RealResult, StringResult,
113     xNodeSetResult, NaNResult, InfResult, NInfResult
114 } xpathResultType;
115 
116 
117 typedef struct xpathResultSet {
118 
119     xpathResultType type;
120     char           *string;
121     int             string_len;
122     long            intvalue;
123     double          realvalue;
124     domNode       **nodes;
125     int             nr_nodes;
126     int             allocated;
127 
128 } xpathResultSet;
129 
130 typedef xpathResultSet *xpathResultSets;
131 
132 typedef int (*xpathFuncCallback)
133                 (void *clientData, char *functionName,
134                  domNode *ctxNode, int position, xpathResultSet *nodeList,
135                  domNode *exprContext, int argc, xpathResultSets *args,
136                  xpathResultSet *result, char  **errMsg);
137 
138 typedef int (*xpathVarCallback)
139                 (void *clientData, char *variableName, char *varURI,
140                  xpathResultSet *result, char  **errMsg);
141 
142 typedef struct xpathCBs {               /* all xpath callbacks + clientData */
143 
144     xpathVarCallback    varCB;
145     void              * varClientData;
146     xpathFuncCallback   funcCB;
147     void              * funcClientData;
148 
149 } xpathCBs;
150 
151 typedef char * (*xpathParseVarCallback)
152     (void *clientData, char *strToParse, int *offset, char **errMsg);
153 
154 typedef struct xpathParseVarCB {
155     xpathParseVarCallback parseVarCB;
156     void                * parseVarClientData;
157 } xpathParseVarCB;
158 
159 /* XPath expr/pattern types */
160 typedef enum {
161     XPATH_EXPR, XPATH_FORMAT_PATTERN, XPATH_TEMPMATCH_PATTERN,
162     XPATH_KEY_USE_EXPR, XPATH_KEY_MATCH_PATTERN
163 } xpathExprType;
164 
165 /*----------------------------------------------------------------------------
166 |   Prototypes
167 |
168 \---------------------------------------------------------------------------*/
169 int    xpathParse   (char *xpath, domNode *exprContext, xpathExprType type,
170                      char **prefixMappings, xpathParseVarCB *varParseCB,
171                      ast *t, char **errMsg);
172 void   xpathFreeAst (ast t);
173 double xpathGetPrio (ast t);
174 int    xpathEval    (domNode *node, domNode *exprContext, char *xpath,
175                      char **prefixMappings, xpathCBs *cbs,
176                      xpathParseVarCB *parseVarCB, Tcl_HashTable *catch,
177                      char **errMsg, xpathResultSet *rs);
178 int    xpathMatches (ast steps, domNode * exprContext, domNode *nodeToMatch,
179                      xpathCBs *cbs, char **errMsg
180                     );
181 
182 int xpathEvalSteps (ast steps, xpathResultSet *nodeList,
183                     domNode *currentNode, domNode *exprContext, int currentPos,
184                     int *docOrder,
185                     xpathCBs *cbs,
186                     xpathResultSet *result, char **errMsg);
187 
188 #define xpathRSInit(x) (x)->type = EmptyResult; \
189                        (x)->intvalue = 0; \
190                        (x)->nr_nodes = 0;
191 void   xpathRSFree (xpathResultSet *rs );
192 
193 int    xpathFuncBoolean  (xpathResultSet *rs);
194 double xpathFuncNumber   (xpathResultSet *rs, int *NaN);
195 char * xpathFuncString   (xpathResultSet *rs);
196 char * xpathFuncStringForNode (domNode *node);
197 int    xpathRound        (double r);
198 
199 char * xpathGetStringValue (domNode *node, int *strLen);
200 
201 char * xpathNodeToXPath  (domNode *node, int legacy);
202 
203 void rsSetBool      ( xpathResultSet *rs, long         i    );
204 void rsSetInt       ( xpathResultSet *rs, long         i    );
205 void rsSetReal      ( xpathResultSet *rs, double       d    );
206 void rsSetString    ( xpathResultSet *rs, const char  *s    );
207 void rsAddNode      ( xpathResultSet *rs, domNode     *node );
208 void rsAddNodeFast  ( xpathResultSet *rs, domNode     *node );
209 void rsCopy         ( xpathResultSet *to, xpathResultSet *from );
210 
211 /* This function is only used for debugging code */
212 void rsPrint        ( xpathResultSet *rs );
213 
214 #endif
215 
216