📄 xmlj_dom.c
字号:
/* xmlj_dom.c - Copyright (C) 2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */#include "xmlj_dom.h"#include "xmlj_error.h"#include "xmlj_io.h"#include "xmlj_node.h"#include "xmlj_sax.h"#include "xmlj_util.h"#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>JNIEnv *dom_cb_env;jobject dom_cb_obj;typedef struct{ int index; int count; xmlNodePtr node;}xmljHashScanData;/* Prototypes for local functions */voidxmljAddAttribute (xmlNodePtr node, xmlAttrPtr attr);voidxmljHashScanner (void *payload, void *vdata, xmlChar *name);xmlChar *xmljGetNodeValue (xmlNodePtr node);/* * Determines whether a child node is suitable for insertion in the list of * children for a given parent node. * Returns 0 on success, a DOMException code otherwise. */voidxmljValidateChildNode (JNIEnv *env, xmlNodePtr parent, xmlNodePtr child){ xmlNodePtr cur; if (child == NULL || parent == NULL) { xmljThrowDOMException (env, 8, NULL); /* NOT_FOUND_ERR */ return; } if (child->doc != parent->doc) { xmljThrowDOMException (env, 4, NULL); /* WRONG_DOCUMENT_ERR */ return; } /* Check that new parent is of an allowed type */ switch (parent->type) { case XML_CDATA_SECTION_NODE: case XML_COMMENT_NODE: case XML_TEXT_NODE: case XML_ENTITY_NODE: case XML_ENTITY_REF_NODE: case XML_NOTATION_NODE: case XML_PI_NODE: /* these can't have any children */ /* HIERARCHY_REQUEST_ERR */ xmljThrowDOMException (env, 3, "parent type does not allow children"); return; case XML_ATTRIBUTE_NODE: if (child->type != XML_TEXT_NODE && child->type != XML_ENTITY_REF_NODE) { /* HIERARCHY_REQUEST_ERR */ xmljThrowDOMException (env, 3, "attributes may only contain text or entity reference nodes"); return; } break; case XML_DOCUMENT_FRAG_NODE: case XML_ELEMENT_NODE: if (child->type == XML_DTD_NODE || child->type == XML_DOCUMENT_TYPE_NODE || child->type == XML_ENTITY_NODE || child->type == XML_NOTATION_NODE || child->type == XML_PI_NODE) { /* HIERARCHY_REQUEST_ERR */ xmljThrowDOMException (env, 3, "parent type does not allow child of this type"); return; } /* fall through */ default: if (child->type == XML_ATTRIBUTE_NODE || child->type == XML_DOCUMENT_NODE || child->type == XML_DOCUMENT_FRAG_NODE) { /* HIERARCHY_REQUEST_ERR */ xmljThrowDOMException (env, 3, "node type may not be a child"); return; } /* TODO others? */ } /* Check that new parent is not self or an ancestor */ for (cur = parent; cur != NULL; cur = cur->parent) { if (cur == child) { /* HIERARCHY_REQUEST_ERR */ xmljThrowDOMException (env, 3, "child cannot be an ancestor of itself"); return; } } /* Check that new parent does not add a second doctype or root element * to a document parent */ if (parent->type == XML_DOCUMENT_NODE) { cur = parent->children; while (cur != NULL) { if (cur->type == XML_DTD_NODE || cur->type == XML_DOCUMENT_TYPE_NODE || (cur->type == XML_ELEMENT_NODE && parent->type == XML_DOCUMENT_NODE)) { if (child->type == cur->type && child != cur) { /* HIERARCHY_REQUEST_ERR */ xmljThrowDOMException (env, 3, "cannot add a second doctype or root element"); return; } } cur = cur->next; } }}/* * Adds the specified attribute node to the list of attributes for the given * element. */voidxmljAddAttribute (xmlNodePtr node, xmlAttrPtr attr){ xmlAttrPtr cur = node->properties; if (cur == NULL) { node->properties = attr; attr->prev = NULL; attr->next = NULL; attr->parent = node; attr->doc = node->doc; } else { while (cur->next != NULL) { cur = cur->next; } cur->next = attr; attr->prev = cur; attr->next = NULL; attr->parent = node; attr->doc = node->doc; }}/* -- GnomeAttr -- */JNIEXPORT jboolean JNICALLJava_gnu_xml_libxmlj_dom_GnomeAttr_getSpecified (JNIEnv * env, jobject self){ xmlAttrPtr attr; attr = (xmlAttrPtr) xmljGetNodeID (env, self); return (attr->atype != 0);}JNIEXPORT jstring JNICALLJava_gnu_xml_libxmlj_dom_GnomeAttr_getValue (JNIEnv * env, jobject self){ xmlNodePtr node; xmlChar *text; jstring ret; node = xmljGetNodeID (env, self); text = xmlNodeGetContent (node); ret = xmljNewString (env, (const xmlChar *) text); if (text != NULL) { xmlFree (text); } return ret;}JNIEXPORT void JNICALLJava_gnu_xml_libxmlj_dom_GnomeAttr_setValue (JNIEnv * env, jobject self, jstring value){ xmlNodePtr node; const xmlChar *s_value; node = xmljGetNodeID (env, self); s_value = xmljGetStringChars (env, value); xmlNodeSetContent (node, s_value);}JNIEXPORT jboolean JNICALLJava_gnu_xml_libxmlj_dom_GnomeAttr_xmljIsId (JNIEnv * env, jobject self){ xmlAttrPtr attr; attr = (xmlAttrPtr) xmljGetNodeID (env, self); return (attr->atype == XML_ATTRIBUTE_ID);}/* -- GnomeDocument -- */JNIEXPORT void JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_free (JNIEnv * env, jobject self __attribute__ ((__unused__)), jobject id){ xmlDocPtr doc; doc = (xmlDocPtr) xmljAsPointer (env, id); xmljFreeDoc (env, doc); xmlFree (doc);}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_getDoctype (JNIEnv * env, jobject self){ xmlDocPtr doc; xmlDtdPtr dtd; doc = (xmlDocPtr) xmljGetNodeID (env, self); dtd = doc->extSubset; if (dtd == NULL) { dtd = doc->intSubset; } return xmljGetNodeInstance (env, (xmlNodePtr) dtd);}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_getDocumentElement (JNIEnv * env, jobject self){ xmlDocPtr doc; doc = (xmlDocPtr) xmljGetNodeID (env, self); return xmljGetNodeInstance (env, xmlDocGetRootElement (doc));}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_createDocumentType (JNIEnv * env, jobject self, jstring name, jstring publicId, jstring systemId){ xmlDocPtr doc; xmlDtdPtr dtd; doc = (xmlDocPtr) xmljGetNodeID (env, self); dtd = xmlNewDtd (doc, xmljGetStringChars (env, name), xmljGetStringChars (env, publicId), xmljGetStringChars (env, systemId)); return xmljGetNodeInstance (env, (xmlNodePtr) dtd);}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_createDocumentFragment (JNIEnv * env, jobject self){ xmlDocPtr doc; doc = (xmlDocPtr) xmljGetNodeID (env, self); return xmljGetNodeInstance (env, xmlNewDocFragment (doc));}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_createTextNode (JNIEnv * env, jobject self, jstring data){ xmlDocPtr doc; xmlNodePtr text; const xmlChar *s_data; doc = (xmlDocPtr) xmljGetNodeID (env, self); s_data = xmljGetStringChars (env, data); text = xmlNewDocText (doc, s_data); return xmljGetNodeInstance (env, text);}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_createComment (JNIEnv * env, jobject self, jstring data){ xmlDocPtr doc; xmlNodePtr comment; const xmlChar *s_data; doc = (xmlDocPtr) xmljGetNodeID (env, self); s_data = xmljGetStringChars (env, data); comment = xmlNewDocComment (doc, s_data); return xmljGetNodeInstance (env, comment);}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_createCDATASection (JNIEnv * env, jobject self, jstring data){ xmlDocPtr doc; xmlNodePtr cdata; const xmlChar *s_data; int len; doc = (xmlDocPtr) xmljGetNodeID (env, self); s_data = xmljGetStringChars (env, data); len = xmlStrlen (s_data); cdata = xmlNewCDataBlock (doc, s_data, len); return xmljGetNodeInstance (env, cdata);}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_createProcessingInstruction (JNIEnv * env, jobject self, jstring target, jstring data){ xmlDocPtr doc; xmlNodePtr pi; const xmlChar *s_target; const xmlChar *s_data; doc = (xmlDocPtr) xmljGetNodeID (env, self); s_target = xmljGetStringChars (env, target); s_data = xmljGetStringChars (env, data); pi = xmlNewPI (s_target, s_data); pi->doc = doc; return xmljGetNodeInstance (env, pi);}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_createEntityReference (JNIEnv * env, jobject self, jstring name){ xmlDocPtr doc; xmlNodePtr ref; const xmlChar *s_name; doc = (xmlDocPtr) xmljGetNodeID (env, self); s_name = xmljGetStringChars (env, name); ref = xmlNewReference (doc, s_name); return xmljGetNodeInstance (env, ref);}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_xmljImportNode (JNIEnv * env, jobject self, jobject importedNode, jboolean deep){ xmlDocPtr doc; xmlNodePtr node; doc = (xmlDocPtr) xmljGetNodeID (env, self); node = xmljGetNodeID (env, importedNode); if (node == NULL) { xmljThrowDOMException (env, 8, NULL); /* NOT_FOUND_ERR */ return NULL; } if (node->type == XML_DOCUMENT_NODE || node->type == XML_DOCUMENT_TYPE_NODE) { xmljThrowDOMException (env, 9, NULL); /* NOT_SUPPORTED_ERR */ return NULL; } node = xmlDocCopyNode (node, doc, deep); return xmljGetNodeInstance (env, node);}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_createElementNS (JNIEnv * env, jobject self, jstring uri, jstring qName){ xmlDocPtr doc; xmlNodePtr element; xmlNsPtr ns = NULL; const xmlChar *s_uri; const xmlChar *s_qName; const xmlChar *s_prefix; const xmlChar *s_localName; doc = (xmlDocPtr) xmljGetNodeID (env, self); s_qName = xmljGetStringChars (env, qName); if (xmlValidateQName (s_qName, 0)) { xmljThrowDOMException (env, 5, NULL); /* INVALID_CHARACTER_ERR */ return NULL; } if (uri != NULL) { s_uri = xmljGetStringChars (env, uri); s_prefix = xmljGetPrefix (s_qName); s_localName = xmljGetLocalName (s_qName); ns = xmlNewNs ((xmlNodePtr) doc, s_uri, s_prefix); } element = xmlNewDocNode (doc, ns, s_qName, NULL); return xmljGetNodeInstance (env, element);}JNIEXPORT jobject JNICALLJava_gnu_xml_libxmlj_dom_GnomeDocument_createAttributeNS (JNIEnv * env, jobject self, jstring uri, jstring qName){ xmlDocPtr doc; xmlNodePtr attr; xmlNsPtr ns = NULL; const xmlChar *s_uri; const xmlChar *s_qName; const xmlChar *s_prefix; const xmlChar *s_localName; doc = (xmlDocPtr) xmljGetNodeID (env, self); s_qName = xmljGetStringChars (env, qName); if (xmlValidateQName (s_qName, 0)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -