📄 xmlj_sax.c
字号:
/* xmlj_sax.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_sax.h"#include "xmlj_io.h"#include "xmlj_util.h"#include <unistd.h>#include <string.h>xmlExternalEntityLoader defaultLoader = NULL;voidxmljDispatchError (xmlParserCtxtPtr ctx, xmlSAXLocatorPtr loc, JNIEnv *env, jobject target, jmethodID method, const char *msg, va_list args);/* -- GnomeLocator -- */JNIEXPORT jstring JNICALLJava_gnu_xml_libxmlj_sax_GnomeLocator_publicId (JNIEnv * env, jobject self __attribute__((__unused__)), jobject j_ctx, jobject j_loc){ xmlParserCtxtPtr ctx; xmlSAXLocatorPtr loc; SAXParseContext *sax; ctx = (xmlParserCtxtPtr) xmljAsPointer (env, j_ctx); loc = (xmlSAXLocatorPtr) xmljAsPointer (env, j_loc); sax = (SAXParseContext *) ctx->_private; return sax->publicId;}JNIEXPORT jstring JNICALLJava_gnu_xml_libxmlj_sax_GnomeLocator_systemId (JNIEnv * env, jobject self __attribute__((__unused__)), jobject j_ctx, jobject j_loc){ xmlParserCtxtPtr ctx; xmlSAXLocatorPtr loc; SAXParseContext *sax; ctx = (xmlParserCtxtPtr) xmljAsPointer (env, j_ctx); loc = (xmlSAXLocatorPtr) xmljAsPointer (env, j_loc); sax = (SAXParseContext *) ctx->_private; return sax->systemId;}JNIEXPORT jint JNICALLJava_gnu_xml_libxmlj_sax_GnomeLocator_lineNumber (JNIEnv * env, jobject self __attribute__((__unused__)), jobject j_ctx, jobject j_loc){ xmlParserCtxtPtr ctx; xmlSAXLocatorPtr loc; ctx = (xmlParserCtxtPtr) xmljAsPointer (env, j_ctx); loc = (xmlSAXLocatorPtr) xmljAsPointer (env, j_loc); if (ctx == NULL || ctx->input == NULL) { return -1; } return ctx->input->line;}JNIEXPORT jint JNICALLJava_gnu_xml_libxmlj_sax_GnomeLocator_columnNumber (JNIEnv * env, jobject self __attribute__((__unused__)), jobject j_ctx, jobject j_loc){ xmlParserCtxtPtr ctx; xmlSAXLocatorPtr loc; ctx = (xmlParserCtxtPtr) xmljAsPointer (env, j_ctx); loc = (xmlSAXLocatorPtr) xmljAsPointer (env, j_loc); if (ctx == NULL || ctx->input == NULL) { return -1; } return ctx->input->col;}/* -- GnomeXMLReader -- *//* * Entry point for SAX parsing. */JNIEXPORT void JNICALLJava_gnu_xml_libxmlj_sax_GnomeXMLReader_parseStream (JNIEnv * env, jobject self, jobject in, jbyteArray detectBuffer, jstring publicId, jstring systemId, jstring base, jboolean validate, jboolean contentHandler, jboolean dtdHandler, jboolean entityResolver, jboolean errorHandler, jboolean declarationHandler, jboolean lexicalHandler){ xmljParseDocument (env, self, in, detectBuffer, publicId, systemId, base, validate, 0, 0, contentHandler, dtdHandler, entityResolver, errorHandler, declarationHandler, lexicalHandler, 0);}xmlParserInputPtrxmljExternalEntityLoader (const char *url, const char *id, xmlParserCtxtPtr ctx){ const xmlChar *systemId; const xmlChar *publicId; xmlParserInputPtr ret; systemId = xmlCharStrdup (url); publicId = xmlCharStrdup (id); /* TODO convert systemId to absolute URI */ ret = xmljSAXResolveEntity (ctx, publicId, systemId); if (ret == NULL) { ret = defaultLoader (url, id, ctx); } return ret;}/* * Allocates and configures a SAX handler that can report the various * classes of callback. */xmlSAXHandlerPtrxmljNewSAXHandler (jboolean contentHandler, jboolean dtdHandler, jboolean entityResolver, jboolean errorHandler, jboolean declarationHandler, jboolean lexicalHandler){ xmlSAXHandlerPtr sax; sax = (xmlSAXHandlerPtr) malloc (sizeof (xmlSAXHandler)); if (sax == NULL) { return NULL; } memset (sax, 0, sizeof (xmlSAXHandler)); xmlSAXVersion (sax, 1); /* TODO SAX2 */ if (dtdHandler) { sax->internalSubset = &xmljSAXInternalSubset; } if (defaultLoader == NULL) { defaultLoader = xmlGetExternalEntityLoader (); xmlSetExternalEntityLoader (xmljExternalEntityLoader); } if (entityResolver) { sax->resolveEntity = &xmljSAXResolveEntity; } if (declarationHandler) { sax->entityDecl = &xmljSAXEntityDecl; sax->notationDecl = &xmljSAXNotationDecl; sax->attributeDecl = &xmljSAXAttributeDecl; sax->elementDecl = &xmljSAXElementDecl; sax->unparsedEntityDecl = &xmljSAXUnparsedEntityDecl; } /* We always listen for the locator callback */ sax->setDocumentLocator = &xmljSAXSetDocumentLocator; if (contentHandler) { sax->startDocument = &xmljSAXStartDocument; sax->endDocument = &xmljSAXEndDocument; sax->startElement = &xmljSAXStartElement; sax->endElement = &xmljSAXEndElement; sax->characters = &xmljSAXCharacters; sax->ignorableWhitespace = &xmljSAXIgnorableWhitespace; sax->processingInstruction = &xmljSAXProcessingInstruction; } /* We always intercept getEntity */ /* TODO this should only be if lexicalHandler */ sax->getEntity = &xmljSAXGetEntity; if (lexicalHandler) { sax->getEntity = &xmljSAXGetEntity; sax->reference = &xmljSAXReference; sax->comment = &xmljSAXComment; sax->cdataBlock = &xmljSAXCDataBlock; } else if (contentHandler) { sax->cdataBlock = &xmljSAXCharacters; } if (errorHandler) { sax->warning = &xmljSAXWarning; sax->error = &xmljSAXError; sax->fatalError = &xmljSAXFatalError; } return sax;}/* -- Callback functions -- */voidxmljSAXInternalSubset (void *vctx, const xmlChar * name, const xmlChar * publicId, const xmlChar * systemId){ xmlParserCtxtPtr ctx; SAXParseContext *sax; JNIEnv *env; jobject target; jstring j_name; jstring j_publicId; jstring j_systemId; xmlSAX2InternalSubset (vctx, name, publicId, systemId); ctx = (xmlParserCtxtPtr) vctx; sax = (SAXParseContext *) ctx->_private; env = sax->env; target = sax->obj; xmljCheckWellFormed (ctx); if ((*env)->ExceptionOccurred (env)) { return; } if (sax->startDTD == NULL) { sax->startDTD = xmljGetMethodID (env, target, "startDTD", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); if (sax->startDTD == NULL) { return; } } j_name = xmljNewString (env, name); j_publicId = xmljNewString (env, publicId); j_systemId = xmljNewString (env, systemId); (*env)->CallVoidMethod (env, target, sax->startDTD, j_name, j_publicId, j_systemId);}xmlParserInputPtrxmljSAXResolveEntity (void *vctx, const xmlChar * publicId, const xmlChar * systemId){ xmlParserCtxtPtr ctx; SAXParseContext *sax; JNIEnv *env; jobject target; jstring j_publicId; jstring j_systemId; jobject inputStream; /* xmlSAX2ResolveEntity (vctx, publicId, systemId); */ ctx = (xmlParserCtxtPtr) vctx; if (ctx->_private == NULL) { /* Not in Kansas */ return NULL; } sax = (SAXParseContext *) ctx->_private; env = sax->env; target = sax->obj; if ((*env)->ExceptionOccurred (env)) { return NULL; } if (sax->resolveEntity == NULL) { sax->resolveEntity = xmljGetMethodID (env, target, "resolveEntity", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/io/InputStream;"); if (sax->resolveEntity == NULL) { return NULL; } } j_publicId = xmljNewString (env, publicId); j_systemId = xmljNewString (env, systemId); inputStream = (*env)->CallObjectMethod (env, target, sax->resolveEntity, j_publicId, j_systemId, sax->systemId); /* Return an xmlParserInputPtr corresponding to the input stream */ if (inputStream != NULL) { jbyteArray detectBuffer; jmethodID getDetectBuffer; /* Get the detect buffer from the NamedInputStream */ getDetectBuffer = xmljGetMethodID (env, inputStream, "getDetectBuffer", "()[B"); if (getDetectBuffer == NULL) { return NULL; } detectBuffer = (*env)->CallObjectMethod (env, inputStream, getDetectBuffer); return xmljNewParserInput (env, inputStream, detectBuffer, ctx); } else { return NULL; }}xmlEntityPtrxmljSAXGetEntity (void *vctx __attribute__((__unused__)), const xmlChar * name){ xmlEntityPtr ret; /* TODO */ /* ret = xmlSAX2GetEntity (vctx, name); */ ret = NULL; return ret;}voidxmljSAXEntityDecl (void *vctx, const xmlChar * name, int type, const xmlChar * publicId, const xmlChar * systemId, xmlChar * content){ xmlParserCtxtPtr ctx; SAXParseContext *sax; JNIEnv *env; jobject target; jstring j_name; jstring j_publicId; jstring j_systemId; jstring j_value; xmlSAX2EntityDecl (vctx, name, type, publicId, systemId, content); ctx = (xmlParserCtxtPtr) vctx; sax = (SAXParseContext *) ctx->_private; env = sax->env; target = sax->obj; xmljCheckWellFormed (ctx); if ((*env)->ExceptionOccurred (env)) { return; } j_name = xmljNewString (env, name); switch (type) { case XML_INTERNAL_GENERAL_ENTITY: case XML_INTERNAL_PARAMETER_ENTITY: case XML_INTERNAL_PREDEFINED_ENTITY: if (sax->internalEntityDecl == NULL) { sax->internalEntityDecl = xmljGetMethodID (env, target, "internalEntityDecl", "(Ljava/lang/String;Ljava/lang/String;)V"); if (sax->internalEntityDecl == NULL) { return; } } j_value = xmljNewString (env, content); (*env)->CallVoidMethod (env, target, sax->internalEntityDecl, j_name, j_value); break; default: if (sax->externalEntityDecl == NULL) { sax->externalEntityDecl = xmljGetMethodID (env, target, "externalEntityDecl", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); if (sax->externalEntityDecl == NULL) { return; } } j_publicId = xmljNewString (env, publicId);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -