xmlj_node.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* xmlj_node.c -
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include "xmlj_error.h"
  32. #include "xmlj_node.h"
  33. #include "xmlj_util.h"
  34. #include <libxml/xmlstring.h>
  35. /*
  36. * Returns the node ID for the given GnomeNode object.
  37. */
  38. xmlNodePtr
  39. xmljGetNodeID (JNIEnv * env, jobject self)
  40. {
  41. jclass cls;
  42. jfieldID field;
  43. jobject id;
  44. xmlNodePtr node;
  45. if (self == NULL)
  46. {
  47. xmljThrowDOMException (env, 8, NULL); /* NOT_FOUND_ERR */
  48. return NULL;
  49. }
  50. cls = (*env)->GetObjectClass (env, self);
  51. if (cls == NULL)
  52. {
  53. return NULL;
  54. }
  55. field = (*env)->GetFieldID (env, cls, "id", "Ljava/lang/Object;");
  56. if (field == NULL)
  57. {
  58. return NULL;
  59. }
  60. id = (*env)->GetObjectField (env, self, field);
  61. node = (xmlNodePtr) xmljAsPointer (env, id);
  62. if (node == NULL)
  63. {
  64. xmljThrowDOMException (env, 8, NULL); /* NOT_FOUND_ERR */
  65. }
  66. return node;
  67. }
  68. /*
  69. * Returns the Java node instanced corresponding to the specified node ID.
  70. */
  71. jobject
  72. xmljGetNodeInstance (JNIEnv * env, xmlNodePtr node)
  73. {
  74. jclass cls;
  75. jmethodID method;
  76. xmlElementType type;
  77. if (node == NULL)
  78. return NULL;
  79. /* Invoke the GnomeNode.newInstance class method */
  80. cls = (*env)->FindClass (env, "gnu/xml/libxmlj/dom/GnomeNode");
  81. if (cls == NULL)
  82. {
  83. return NULL;
  84. }
  85. method = (*env)->GetStaticMethodID (env, cls, "newInstance",
  86. "(Ljava/lang/Object;Ljava/lang/Object;I)Lgnu/xml/libxmlj/dom/GnomeNode;");
  87. if (method == NULL)
  88. {
  89. return NULL;
  90. }
  91. type = node->type;
  92. switch (type)
  93. {
  94. case XML_DTD_NODE:
  95. type = XML_DOCUMENT_TYPE_NODE;
  96. break;
  97. case XML_ATTRIBUTE_DECL:
  98. type = XML_ATTRIBUTE_NODE;
  99. break;
  100. case XML_ENTITY_DECL:
  101. type = XML_ENTITY_NODE;
  102. break;
  103. default:
  104. break;
  105. }
  106. return (*env)->CallStaticObjectMethod (env, cls, method,
  107. xmljAsField (env, node->doc),
  108. xmljAsField (env, node),
  109. type);
  110. }
  111. void
  112. xmljFreeDoc (JNIEnv * env, xmlDocPtr doc)
  113. {
  114. jclass cls;
  115. jmethodID method;
  116. /* Invoke the GnomeNode.freeDocument class method */
  117. cls = (*env)->FindClass (env, "gnu/xml/libxmlj/dom/GnomeNode");
  118. if (cls == NULL)
  119. {
  120. return;
  121. }
  122. method = (*env)->GetStaticMethodID (env, cls, "freeDocument",
  123. "(Ljava/lang/Object;)V");
  124. if (method == NULL)
  125. {
  126. return;
  127. }
  128. (*env)->CallStaticVoidMethod (env, cls, method, xmljAsField (env, doc));
  129. }
  130. int
  131. xmljMatch (const xmlChar * name, xmlNodePtr node)
  132. {
  133. switch (node->type)
  134. {
  135. case XML_ELEMENT_NODE:
  136. case XML_ATTRIBUTE_NODE:
  137. return xmlStrcmp (node->name, name);
  138. default:
  139. return 1;
  140. }
  141. }
  142. int
  143. xmljMatchNS (const xmlChar * uri, const xmlChar * localName, xmlNodePtr node)
  144. {
  145. xmlNsPtr ns;
  146. const xmlChar *nodeLocalName;
  147. int *len;
  148. int ret;
  149. switch (node->type)
  150. {
  151. case XML_ELEMENT_NODE:
  152. case XML_ATTRIBUTE_NODE:
  153. len = (int *) malloc (sizeof (int));
  154. if (xmlSplitQName3 (node->name, len) != NULL)
  155. {
  156. nodeLocalName = node->name + (*len);
  157. }
  158. else
  159. {
  160. nodeLocalName = node->name;
  161. }
  162. free (len);
  163. ns = node->ns;
  164. if (ns == NULL || ns->href == NULL)
  165. {
  166. if (uri != NULL)
  167. {
  168. return 0;
  169. }
  170. ret = xmlStrcmp (localName, nodeLocalName);
  171. }
  172. else
  173. {
  174. if (uri == NULL)
  175. {
  176. return 0;
  177. }
  178. ret = (xmlStrcmp (localName, nodeLocalName) &&
  179. xmlStrcmp (uri, ns->href));
  180. }
  181. return ret;
  182. default:
  183. return 1;
  184. }
  185. }