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)
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
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
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 */
51 xmljCreateXPathContextPtr (xmlNodePtr node
);
54 xmljGetXPathResult (JNIEnv
*env
, xmlXPathObjectPtr obj
);
57 xmljGetXPathNodeList (JNIEnv
*env
, xmlXPathObjectPtr obj
);
60 xmljGetXPathObjectID (JNIEnv
*env
, jobject obj
);
63 * Creates an XPath context for the given node.
66 xmljCreateXPathContextPtr (xmlNodePtr node
)
68 xmlXPathContextPtr ctx
;
70 ctx
= xmlXPathNewContext (node
->doc
);
76 * Converts an xmlXPathObjectPtr to a Java XPathResult.
79 xmljGetXPathResult (JNIEnv
*env
, xmlXPathObjectPtr obj
)
90 cls
= (*env
)->FindClass (env
, "gnu/xml/libxmlj/dom/GnomeXPathResult");
95 method
= (*env
)->GetMethodID (env
, cls
, "<init>", "(Ljava/lang/Object;)V");
100 val
= xmljAsField (env
, obj
);
101 ret
= (*env
)->NewObject (env
, cls
, method
, val
);
107 * Converts an xmlXPathObjectPtr to a Java XPathNodeList.
110 xmljGetXPathNodeList (JNIEnv
*env
, xmlXPathObjectPtr obj
)
121 cls
= (*env
)->FindClass (env
, "gnu/xml/libxmlj/dom/GnomeXPathNodeList");
126 method
= (*env
)->GetMethodID (env
, cls
, "<init>", "(Ljava/lang/Object;)V");
131 val
= xmljAsField (env
, obj
);
132 ret
= (*env
)->NewObject (env
, cls
, method
, val
);
138 xmljGetXPathObjectID (JNIEnv
*env
, jobject obj
)
143 xmlXPathObjectPtr ret
;
145 cls
= (*env
)->GetObjectClass (env
, obj
);
150 field
= (*env
)->GetFieldID (env
, cls
, "obj", "Ljava/lang/Object;");
155 val
= (*env
)->GetObjectField (env
, obj
, field
);
156 ret
= (xmlXPathObjectPtr
) xmljAsPointer (env
, val
);
161 JNIEXPORT jobject JNICALL
162 Java_gnu_xml_libxmlj_dom_GnomeDocument_evaluate (JNIEnv
*env
,
164 __attribute__((__unused__
)),
173 xmlXPathContextPtr ctx
;
174 xmlXPathObjectPtr eval
= NULL
;
176 str
= xmljGetStringChars (env
, expression
);
177 node
= xmljGetNodeID (env
, contextNode
);
182 ctx
= xmljCreateXPathContextPtr (node
);
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
,
195 __attribute__((__unused__
)),
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
,
210 __attribute__((__unused__
)),
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
,
222 __attribute__((__unused__
)),
228 xmlXPathCompExprPtr expr
;
230 xmlXPathContextPtr ctx
;
231 xmlXPathObjectPtr eval
= NULL
;
233 expr
= (xmlXPathCompExprPtr
) xmljAsPointer (env
, ptr
);
234 node
= xmljGetNodeID (env
, contextNode
);
239 ctx
= xmljCreateXPathContextPtr (node
);
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
,
251 __attribute__((__unused__
)),
254 xmlXPathFreeObject ((xmlXPathObjectPtr
) xmljAsPointer (env
, obj
));
257 JNIEXPORT jshort JNICALL
258 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getResultType (JNIEnv
*env
,
261 xmlXPathObjectPtr obj
;
263 obj
= xmljGetXPathObjectID (env
, self
);
266 case XPATH_UNDEFINED
:
267 return 0; /* ANY_TYPE */
269 return 1; /* NUMBER_TYPE */
271 return 2; /* STRING_TYPE */
273 return 3; /* BOOLEAN_TYPE */
275 return 6; /* UNORDERED_NODE_SNAPSHOT_TYPE */
278 case XPATH_LOCATIONSET
:
280 case XPATH_XSLT_TREE
:
283 return -1; /* TODO */
287 JNIEXPORT jdouble JNICALL
288 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getNumberValue (JNIEnv
*env
,
291 xmlXPathObjectPtr obj
;
293 obj
= xmljGetXPathObjectID (env
, self
);
298 return obj
->floatval
;
301 JNIEXPORT jstring JNICALL
302 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getStringValue (JNIEnv
*env
,
305 xmlXPathObjectPtr obj
;
307 obj
= xmljGetXPathObjectID (env
, self
);
312 return xmljNewString (env
, obj
->stringval
);
315 JNIEXPORT jboolean JNICALL
316 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getBooleanValue (JNIEnv
*env
,
319 xmlXPathObjectPtr obj
;
321 obj
= xmljGetXPathObjectID (env
, self
);
325 JNIEXPORT jobject JNICALL
326 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getSingleNodeValue (JNIEnv
*env
,
329 xmlXPathObjectPtr obj
;
331 obj
= xmljGetXPathObjectID (env
, self
);
336 if (obj
->nodesetval
== NULL
)
340 if (obj
->nodesetval
->nodeNr
> 0)
342 return xmljGetNodeInstance (env
, obj
->nodesetval
->nodeTab
[0]);
350 JNIEXPORT jboolean JNICALL
351 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getInvalidIteratorState (JNIEnv
*env
,
354 xmlXPathObjectPtr obj
;
356 obj
= xmljGetXPathObjectID (env
, self
);
360 JNIEXPORT jint JNICALL
361 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_getSnapshotLength (JNIEnv
*env
,
364 xmlXPathObjectPtr obj
;
366 obj
= xmljGetXPathObjectID (env
, self
);
371 if (obj
->nodesetval
== NULL
)
375 return obj
->nodesetval
->nodeNr
;
378 JNIEXPORT jobject JNICALL
379 Java_gnu_xml_libxmlj_dom_GnomeXPathResult_iterateNext (JNIEnv
*env
,
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
,
393 xmlXPathObjectPtr obj
;
395 obj
= xmljGetXPathObjectID (env
, self
);
400 if (obj
->nodesetval
== NULL
)
404 if (obj
->nodesetval
->nodeNr
> 0)
406 return xmljGetNodeInstance (env
, obj
->nodesetval
->nodeTab
[index
]);
414 /* -- GnomeXPathNodeList -- */
416 JNIEXPORT jobject JNICALL
417 Java_gnu_xml_libxmlj_dom_GnomeDocument_getElementsByTagName (JNIEnv
*env
,
421 return Java_gnu_xml_libxmlj_dom_GnomeElement_getElementsByTagName (env
,
426 JNIEXPORT jobject JNICALL
427 Java_gnu_xml_libxmlj_dom_GnomeElement_getElementsByTagName (JNIEnv
*env
,
431 const xmlChar
*s_name
;
432 const xmlChar
*format
;
435 xmlXPathContextPtr ctx
;
436 xmlXPathObjectPtr eval
= NULL
;
438 node
= xmljGetNodeID (env
, self
);
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)
454 format
= xmlCharStrdup ("descendant-or-self::*[name()='%s']");
455 if (xmlStrPrintf (expr
, 256, format
, s_name
) == -1)
460 xmlFree ((xmlChar
*) s_name
);
461 ctx
= xmljCreateXPathContextPtr (node
);
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
,
476 return Java_gnu_xml_libxmlj_dom_GnomeElement_getElementsByTagNameNS (env
,
482 JNIEXPORT jobject JNICALL
483 Java_gnu_xml_libxmlj_dom_GnomeElement_getElementsByTagNameNS (JNIEnv
*env
,
488 const xmlChar
*s_uri
;
489 const xmlChar
*s_localName
;
490 const xmlChar
*format
;
493 xmlXPathContextPtr ctx
;
494 xmlXPathObjectPtr eval
= NULL
;
496 node
= xmljGetNodeID (env
, self
);
501 s_uri
= xmljGetStringChars (env
, uri
);
502 s_localName
= xmljGetStringChars (env
, localName
);
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)
516 format
= xmlCharStrdup ("descendant-or-self::*[namespace-uri()='' and local-name()='%s']");
517 if (xmlStrPrintf (expr
, 256, format
, s_localName
) == -1)
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)
536 format
= xmlCharStrdup ("descendant-or-self::*[local-name()='%s']");
537 if (xmlStrPrintf (expr
, 256, format
, s_localName
) == -1)
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)
555 format
= xmlCharStrdup ("descendant-or-self::*[namespace-uri()='%s' and local-name()='%s']");
556 if (xmlStrPrintf (expr
, 256, format
, s_uri
, s_localName
) == -1)
562 xmlFree ((xmlChar
*) s_uri
);
563 xmlFree ((xmlChar
*) s_localName
);
564 ctx
= xmljCreateXPathContextPtr (node
);
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
,
576 __attribute__((__unused__
)),
579 xmlXPathFreeObject ((xmlXPathObjectPtr
) xmljAsPointer (env
, obj
));
582 JNIEXPORT jint JNICALL
583 Java_gnu_xml_libxmlj_dom_GnomeXPathNodeList_getLength (JNIEnv
*env
,
586 xmlXPathObjectPtr obj
;
588 obj
= xmljGetXPathObjectID (env
, self
);
593 if (obj
->nodesetval
== NULL
)
597 return obj
->nodesetval
->nodeNr
;
600 JNIEXPORT jobject JNICALL
601 Java_gnu_xml_libxmlj_dom_GnomeXPathNodeList_item (JNIEnv
*env
,
605 xmlXPathObjectPtr obj
;
607 obj
= xmljGetXPathObjectID (env
, self
);
612 if (obj
->nodesetval
== NULL
)
616 if (obj
->nodesetval
->nodeNr
> 0)
618 return xmljGetNodeInstance (env
, obj
->nodesetval
->nodeTab
[index
]);