Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / native / jni / xmlj / xmlj_xpath.c
blob6d014cefc91982b9dabc18c0fce3724361a6a5b3
1 /* xmlj_xpath.c -
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 #include "gnu_xml_libxmlj_dom_GnomeDocument.h"
39 #include "gnu_xml_libxmlj_dom_GnomeElement.h"
40 #include "gnu_xml_libxmlj_dom_GnomeXPathExpression.h"
41 #include "gnu_xml_libxmlj_dom_GnomeXPathNodeList.h"
42 #include "gnu_xml_libxmlj_dom_GnomeXPathNSResolver.h"
43 #include "gnu_xml_libxmlj_dom_GnomeXPathResult.h"
44 #include "xmlj_node.h"
45 #include "xmlj_util.h"
46 #include <libxml/xpath.h>
48 /* Local function prototypes */
50 xmlXPathContextPtr
51 xmljCreateXPathContextPtr (xmlNodePtr node);
53 jobject
54 xmljGetXPathResult (JNIEnv *env, xmlXPathObjectPtr obj);
56 jobject
57 xmljGetXPathNodeList (JNIEnv *env, xmlXPathObjectPtr obj);
59 xmlXPathObjectPtr
60 xmljGetXPathObjectID (JNIEnv *env, jobject obj);
62 /**
63 * Creates an XPath context for the given node.
65 xmlXPathContextPtr
66 xmljCreateXPathContextPtr (xmlNodePtr node)
68 xmlXPathContextPtr ctx;
70 ctx = xmlXPathNewContext (node->doc);
71 ctx->node = node;
72 return ctx;
75 /**
76 * Converts an xmlXPathObjectPtr to a Java XPathResult.
78 jobject
79 xmljGetXPathResult (JNIEnv *env, xmlXPathObjectPtr obj)
81 jclass cls;
82 jmethodID method;
83 jobject ret;
84 jobject val;
86 if (obj == NULL)
88 return NULL;
90 cls = (*env)->FindClass (env, "gnu/xml/libxmlj/dom/GnomeXPathResult");
91 if (cls == NULL)
93 return NULL;
95 method = (*env)->GetMethodID (env, cls, "<init>", "(Ljava/lang/Object;)V");
96 if (method == NULL)
98 return NULL;
100 val = xmljAsField (env, obj);
101 ret = (*env)->NewObject (env, cls, method, val);
103 return ret;
107 * Converts an xmlXPathObjectPtr to a Java XPathNodeList.
109 jobject
110 xmljGetXPathNodeList (JNIEnv *env, xmlXPathObjectPtr obj)
112 jclass cls;
113 jmethodID method;
114 jobject ret;
115 jobject val;
117 if (obj == NULL)
119 return NULL;
121 cls = (*env)->FindClass (env, "gnu/xml/libxmlj/dom/GnomeXPathNodeList");
122 if (cls == NULL)
124 return NULL;
126 method = (*env)->GetMethodID (env, cls, "<init>", "(Ljava/lang/Object;)V");
127 if (method == NULL)
129 return NULL;
131 val = xmljAsField (env, obj);
132 ret = (*env)->NewObject (env, cls, method, val);
134 return ret;
137 xmlXPathObjectPtr
138 xmljGetXPathObjectID (JNIEnv *env, jobject obj)
140 jclass cls;
141 jfieldID field;
142 jobject val;
143 xmlXPathObjectPtr ret;
145 cls = (*env)->GetObjectClass (env, obj);
146 if (cls == NULL)
148 return NULL;
150 field = (*env)->GetFieldID (env, cls, "obj", "Ljava/lang/Object;");
151 if (field == NULL)
153 return NULL;
155 val = (*env)->GetObjectField (env, obj, field);
156 ret = (xmlXPathObjectPtr) xmljAsPointer (env, val);
158 return ret;
161 JNIEXPORT jobject JNICALL
162 Java_gnu_xml_libxmlj_dom_GnomeDocument_evaluate (JNIEnv *env,
163 jobject self
164 __attribute__((__unused__)),
165 jstring expression,
166 jobject contextNode,
167 jobject resolver,
168 jshort type,
169 jobject result)
171 const xmlChar *str;
172 xmlNodePtr node;
173 xmlXPathContextPtr ctx;
174 xmlXPathObjectPtr eval = NULL;
176 str = xmljGetStringChars (env, expression);
177 node = xmljGetNodeID (env, contextNode);
178 if (node == NULL)
180 return NULL;
182 ctx = xmljCreateXPathContextPtr (node);
183 if (ctx != NULL)
185 eval = xmlXPathEval (str, ctx);
186 xmlXPathFreeContext (ctx);
188 xmlFree ((xmlChar *) str);
189 return xmljGetXPathResult (env, eval);
192 JNIEXPORT jobject JNICALL
193 Java_gnu_xml_libxmlj_dom_GnomeXPathExpression_init (JNIEnv *env,
194 jobject self
195 __attribute__((__unused__)),
196 jstring expression)
198 const xmlChar *str;
199 xmlXPathCompExprPtr ptr;
201 str = xmljGetStringChars (env, expression);
202 ptr = xmlXPathCompile (str);
203 xmlFree ((xmlChar *) str);
204 return xmljAsField (env, ptr);
207 JNIEXPORT void JNICALL
208 Java_gnu_xml_libxmlj_dom_GnomeXPathExpression_free (JNIEnv *env,
209 jobject self
210 __attribute__((__unused__)),
211 jobject ptr)
213 xmlXPathCompExprPtr expr;
215 expr = (xmlXPathCompExprPtr) xmljAsPointer (env, ptr);
216 xmlXPathFreeCompExpr (expr);
219 JNIEXPORT jobject JNICALL
220 Java_gnu_xml_libxmlj_dom_GnomeXPathExpression_doEvaluate (JNIEnv *env,
221 jobject self
222 __attribute__((__unused__)),
223 jobject ptr,
224 jobject contextNode,
225 jshort type,
226 jobject result)
228 xmlXPathCompExprPtr expr;
229 xmlNodePtr node;
230 xmlXPathContextPtr ctx;
231 xmlXPathObjectPtr eval = NULL;
233 expr = (xmlXPathCompExprPtr) xmljAsPointer (env, ptr);
234 node = xmljGetNodeID (env, contextNode);
235 if (node == NULL)
237 return NULL;
239 ctx = xmljCreateXPathContextPtr (node);
240 if (ctx != NULL)
242 eval = xmlXPathCompiledEval (expr, ctx);
243 xmlXPathFreeContext (ctx);
245 return xmljGetXPathResult (env, eval);
248 JNIEXPORT void JNICALL
249 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_free (JNIEnv *env,
250 jobject self
251 __attribute__((__unused__)),
252 jobject obj)
254 xmlXPathFreeObject ((xmlXPathObjectPtr) xmljAsPointer (env, obj));
257 JNIEXPORT jshort JNICALL
258 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getResultType (JNIEnv *env,
259 jobject self)
261 xmlXPathObjectPtr obj;
263 obj = xmljGetXPathObjectID (env, self);
264 switch (obj->type)
266 case XPATH_UNDEFINED:
267 return 0; /* ANY_TYPE */
268 case XPATH_NUMBER:
269 return 1; /* NUMBER_TYPE */
270 case XPATH_STRING:
271 return 2; /* STRING_TYPE */
272 case XPATH_BOOLEAN:
273 return 3; /* BOOLEAN_TYPE */
274 case XPATH_NODESET:
275 return 6; /* UNORDERED_NODE_SNAPSHOT_TYPE */
276 case XPATH_POINT:
277 case XPATH_RANGE:
278 case XPATH_LOCATIONSET:
279 case XPATH_USERS:
280 case XPATH_XSLT_TREE:
281 /* TODO */
282 default:
283 return -1; /* TODO */
287 JNIEXPORT jdouble JNICALL
288 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getNumberValue (JNIEnv *env,
289 jobject self)
291 xmlXPathObjectPtr obj;
293 obj = xmljGetXPathObjectID (env, self);
294 if (obj == NULL)
296 return 0.0;
298 return obj->floatval;
301 JNIEXPORT jstring JNICALL
302 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getStringValue (JNIEnv *env,
303 jobject self)
305 xmlXPathObjectPtr obj;
307 obj = xmljGetXPathObjectID (env, self);
308 if (obj == NULL)
310 return NULL;
312 return xmljNewString (env, obj->stringval);
315 JNIEXPORT jboolean JNICALL
316 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getBooleanValue (JNIEnv *env,
317 jobject self)
319 xmlXPathObjectPtr obj;
321 obj = xmljGetXPathObjectID (env, self);
322 return obj->boolval;
325 JNIEXPORT jobject JNICALL
326 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getSingleNodeValue (JNIEnv *env,
327 jobject self)
329 xmlXPathObjectPtr obj;
331 obj = xmljGetXPathObjectID (env, self);
332 if (obj == NULL)
334 return NULL;
336 if (obj->nodesetval == NULL)
338 return NULL;
340 if (obj->nodesetval->nodeNr > 0)
342 return xmljGetNodeInstance (env, obj->nodesetval->nodeTab[0]);
344 else
346 return NULL;
350 JNIEXPORT jboolean JNICALL
351 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getInvalidIteratorState (JNIEnv *env,
352 jobject self)
354 xmlXPathObjectPtr obj;
356 obj = xmljGetXPathObjectID (env, self);
357 return 0; /* TODO */
360 JNIEXPORT jint JNICALL
361 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getSnapshotLength (JNIEnv *env,
362 jobject self)
364 xmlXPathObjectPtr obj;
366 obj = xmljGetXPathObjectID (env, self);
367 if (obj == NULL)
369 return -1;
371 if (obj->nodesetval == NULL)
373 return -1;
375 return obj->nodesetval->nodeNr;
378 JNIEXPORT jobject JNICALL
379 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_iterateNext (JNIEnv *env,
380 jobject self)
382 xmlXPathObjectPtr obj;
384 obj = xmljGetXPathObjectID (env, self);
385 return NULL; /* TODO */
388 JNIEXPORT jobject JNICALL
389 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_snapshotItem (JNIEnv *env,
390 jobject self,
391 jint index)
393 xmlXPathObjectPtr obj;
395 obj = xmljGetXPathObjectID (env, self);
396 if (obj == NULL)
398 return NULL;
400 if (obj->nodesetval == NULL)
402 return NULL;
404 if (obj->nodesetval->nodeNr > 0)
406 return xmljGetNodeInstance (env, obj->nodesetval->nodeTab[index]);
408 else
410 return NULL;
414 /* -- GnomeXPathNodeList -- */
416 JNIEXPORT jobject JNICALL
417 Java_gnu_xml_libxmlj_dom_GnomeDocument_getElementsByTagName (JNIEnv *env,
418 jobject self,
419 jstring name)
421 return Java_gnu_xml_libxmlj_dom_GnomeElement_getElementsByTagName (env,
422 self,
423 name);
426 JNIEXPORT jobject JNICALL
427 Java_gnu_xml_libxmlj_dom_GnomeElement_getElementsByTagName (JNIEnv *env,
428 jobject self,
429 jstring name)
431 const xmlChar *s_name;
432 const xmlChar *format;
433 xmlChar expr[256];
434 xmlNodePtr node;
435 xmlXPathContextPtr ctx;
436 xmlXPathObjectPtr eval = NULL;
438 node = xmljGetNodeID (env, self);
439 if (node == NULL)
441 return NULL;
443 s_name = xmljGetStringChars (env, name);
444 if (xmlStrEqual (s_name, BAD_CAST "*"))
446 format = xmlCharStrdup ("descendant-or-self::*[node-type()=1]");
447 if (xmlStrPrintf (expr, 256, format) == -1)
449 return NULL;
452 else
454 format = xmlCharStrdup ("descendant-or-self::*[name()='%s']");
455 if (xmlStrPrintf (expr, 256, format, s_name) == -1)
457 return NULL;
460 xmlFree ((xmlChar *) s_name);
461 ctx = xmljCreateXPathContextPtr (node);
462 if (ctx != NULL)
464 eval = xmlXPathEval (expr, ctx);
465 xmlXPathFreeContext (ctx);
467 return xmljGetXPathNodeList (env, eval);
470 JNIEXPORT jobject JNICALL
471 Java_gnu_xml_libxmlj_dom_GnomeDocument_getElementsByTagNameNS (JNIEnv *env,
472 jobject self,
473 jstring uri,
474 jstring localName)
476 return Java_gnu_xml_libxmlj_dom_GnomeElement_getElementsByTagNameNS (env,
477 self,
478 uri,
479 localName);
482 JNIEXPORT jobject JNICALL
483 Java_gnu_xml_libxmlj_dom_GnomeElement_getElementsByTagNameNS (JNIEnv *env,
484 jobject self,
485 jstring uri,
486 jstring localName)
488 const xmlChar *s_uri;
489 const xmlChar *s_localName;
490 const xmlChar *format;
491 xmlChar expr[256];
492 xmlNodePtr node;
493 xmlXPathContextPtr ctx;
494 xmlXPathObjectPtr eval = NULL;
496 node = xmljGetNodeID (env, self);
497 if (node == NULL)
499 return NULL;
501 s_uri = xmljGetStringChars (env, uri);
502 s_localName = xmljGetStringChars (env, localName);
503 if (uri == NULL)
505 /* namespace URI is empty */
506 if (xmlStrEqual (s_localName, BAD_CAST "*"))
508 format = xmlCharStrdup ("descendant-or-self::*[namespace-uri()='' and node-type()=1]");
509 if (xmlStrPrintf (expr, 256, format) == -1)
511 return NULL;
514 else
516 format = xmlCharStrdup ("descendant-or-self::*[namespace-uri()='' and local-name()='%s']");
517 if (xmlStrPrintf (expr, 256, format, s_localName) == -1)
519 return NULL;
523 else if (xmlStrEqual (s_uri, BAD_CAST "*"))
525 /* matches all namespaces */
526 if (xmlStrEqual (s_localName, BAD_CAST "*"))
528 format = xmlCharStrdup ("descendant-or-self::*[node-type()=1]");
529 if (xmlStrPrintf (expr, 256, format) == -1)
531 return NULL;
534 else
536 format = xmlCharStrdup ("descendant-or-self::*[local-name()='%s']");
537 if (xmlStrPrintf (expr, 256, format, s_localName) == -1)
539 return NULL;
543 else
545 if (xmlStrEqual (s_localName, BAD_CAST "*"))
547 format = xmlCharStrdup ("descendant-or-self::*[namespace-uri()='%s' and node-type()=1]");
548 if (xmlStrPrintf (expr, 256, format, s_uri) == -1)
550 return NULL;
553 else
555 format = xmlCharStrdup ("descendant-or-self::*[namespace-uri()='%s' and local-name()='%s']");
556 if (xmlStrPrintf (expr, 256, format, s_uri, s_localName) == -1)
558 return NULL;
562 xmlFree ((xmlChar *) s_uri);
563 xmlFree ((xmlChar *) s_localName);
564 ctx = xmljCreateXPathContextPtr (node);
565 if (ctx != NULL)
567 eval = xmlXPathEval (expr, ctx);
568 xmlXPathFreeContext (ctx);
570 return xmljGetXPathNodeList (env, eval);
573 JNIEXPORT void JNICALL
574 Java_gnu_xml_libxmlj_dom_GnomeXPathNodeList_free (JNIEnv *env,
575 jobject self
576 __attribute__((__unused__)),
577 jobject obj)
579 xmlXPathFreeObject ((xmlXPathObjectPtr) xmljAsPointer (env, obj));
582 JNIEXPORT jint JNICALL
583 Java_gnu_xml_libxmlj_dom_GnomeXPathNodeList_getLength (JNIEnv *env,
584 jobject self)
586 xmlXPathObjectPtr obj;
588 obj = xmljGetXPathObjectID (env, self);
589 if (obj == NULL)
591 return 0;
593 if (obj->nodesetval == NULL)
595 return 0;
597 return obj->nodesetval->nodeNr;
600 JNIEXPORT jobject JNICALL
601 Java_gnu_xml_libxmlj_dom_GnomeXPathNodeList_item (JNIEnv *env,
602 jobject self,
603 jint index)
605 xmlXPathObjectPtr obj;
607 obj = xmljGetXPathObjectID (env, self);
608 if (obj == NULL)
610 return NULL;
612 if (obj->nodesetval == NULL)
614 return NULL;
616 if (obj->nodesetval->nodeNr > 0)
618 return xmljGetNodeInstance (env, obj->nodesetval->nodeTab[index]);
620 else
622 return NULL;