123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446 |
- /* 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 modify
- it under the terms of the GNU General Public License as published by
- the 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, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with GNU Classpath; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301 USA.
- Linking this library statically or dynamically with other modules is
- making a combined work based on this library. Thus, the terms and
- conditions of the GNU General Public License cover the whole
- combination.
- As a special exception, the copyright holders of this library give you
- permission to link this library with independent modules to produce an
- executable, regardless of the license terms of these independent
- modules, and to copy and distribute the resulting executable under
- terms of your choice, provided that you also meet, for each linked
- independent module, the terms and conditions of the license of that
- module. An independent module is a module which is not derived from
- or based on this library. If you modify this library, you may extend
- this exception to your version of the library, but you are not
- obligated to do so. If you do not wish to do so, delete this
- exception 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;
- void
- xmljDispatchError (xmlParserCtxtPtr ctx,
- xmlSAXLocatorPtr loc,
- JNIEnv *env,
- jobject target,
- jmethodID method,
- const char *msg,
- va_list args);
- /* -- GnomeLocator -- */
- JNIEXPORT jstring JNICALL
- Java_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 JNICALL
- Java_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 JNICALL
- Java_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 JNICALL
- Java_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 JNICALL
- Java_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);
- }
- xmlParserInputPtr
- xmljExternalEntityLoader (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.
- */
- xmlSAXHandlerPtr
- xmljNewSAXHandler (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 -- */
- void
- xmljSAXInternalSubset (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);
- }
- xmlParserInputPtr
- xmljSAXResolveEntity (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;
- }
- }
- xmlEntityPtr
- xmljSAXGetEntity (void *vctx __attribute__((__unused__)), const xmlChar * name)
- {
- xmlEntityPtr ret;
-
- /* TODO */
- /* ret = xmlSAX2GetEntity (vctx, name); */
- ret = NULL;
- return ret;
- }
- void
- xmljSAXEntityDecl (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);
- j_systemId = xmljNewString (env, systemId);
- (*env)->CallVoidMethod (env,
- target,
- sax->externalEntityDecl,
- j_name,
- j_publicId,
- j_systemId);
- }
- }
- void
- xmljSAXNotationDecl (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;
- xmlSAX2NotationDecl (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->notationDecl == NULL)
- {
- sax->notationDecl =
- xmljGetMethodID (env,
- target,
- "notationDecl",
- "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
- if (sax->notationDecl == NULL)
- {
- return;
- }
- }
- j_name = xmljNewString (env, name);
- j_publicId = xmljNewString (env, publicId);
- j_systemId = xmljNewString (env, systemId);
- /* Invoke the method */
- (*env)->CallVoidMethod (env,
- target,
- sax->notationDecl,
- j_name,
- j_publicId,
- j_systemId);
- }
- void
- xmljSAXAttributeDecl (void *vctx,
- const xmlChar * elem,
- const xmlChar * fullName,
- int type,
- int def,
- const xmlChar * defaultValue,
- xmlEnumerationPtr tree)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- jstring j_eName;
- jstring j_aName;
- jstring j_type;
- jstring j_mode;
- jstring j_value;
- xmlSAX2AttributeDecl (vctx, elem, fullName, type, def, defaultValue, tree);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->attributeDecl == NULL)
- {
- sax->attributeDecl =
- xmljGetMethodID (env,
- target,
- "attributeDecl",
- "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
- if (sax->attributeDecl == NULL)
- {
- return;
- }
- }
- j_eName = xmljNewString (env, elem);
- j_aName = xmljNewString (env, fullName);
- j_type = xmljAttributeTypeName (env, type);
- j_mode = xmljAttributeModeName (env, def);
- j_value = xmljNewString (env, defaultValue);
- (*env)->CallVoidMethod (env,
- target,
- sax->attributeDecl,
- j_eName,
- j_aName,
- j_type,
- j_mode,
- j_value);
- }
- void
- xmljSAXElementDecl (void *vctx,
- const xmlChar * name,
- int type,
- xmlElementContentPtr content)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- jstring j_name;
- jstring j_model;
- xmlSAX2ElementDecl (vctx, name, type, content);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->elementDecl == NULL)
- {
- sax->elementDecl =
- xmljGetMethodID (env,
- target,
- "elementDecl",
- "(Ljava/lang/String;Ljava/lang/String;)V");
- if (sax->elementDecl == NULL)
- {
- return;
- }
- }
- j_name = xmljNewString (env, name);
- j_model = NULL; /* TODO */
- (*env)->CallVoidMethod (env,
- target,
- sax->elementDecl,
- j_name,
- j_model);
- }
- void
- xmljSAXUnparsedEntityDecl (void *vctx,
- const xmlChar * name,
- const xmlChar * publicId,
- const xmlChar * systemId,
- const xmlChar * notationName)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- jstring j_name;
- jstring j_publicId;
- jstring j_systemId;
- jstring j_notationName;
- xmlSAX2UnparsedEntityDecl (vctx, name, publicId, systemId, notationName);
-
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->unparsedEntityDecl == NULL)
- {
- sax->unparsedEntityDecl =
- xmljGetMethodID (env,
- target,
- "unparsedEntityDecl",
- "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
- if (sax->unparsedEntityDecl == NULL)
- {
- return;
- }
- }
- j_name = xmljNewString (env, name);
- j_publicId = xmljNewString (env, publicId);
- j_systemId = xmljNewString (env, systemId);
- j_notationName = xmljNewString (env, notationName);
- (*env)->CallVoidMethod (env,
- target,
- sax->unparsedEntityDecl,
- j_name,
- j_publicId,
- j_systemId,
- j_notationName);
- }
- void
- xmljSAXSetDocumentLocator (void *vctx, xmlSAXLocatorPtr loc)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- xmlSAX2SetDocumentLocator (vctx, loc);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- if (target == NULL)
- {
- /* No Java parse context */
- return;
- }
- /* Update locator on sax context */
- sax->loc = loc;
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->setDocumentLocator == NULL)
- {
- sax->setDocumentLocator = xmljGetMethodID (env,
- target,
- "setDocumentLocator",
- "(Ljava/lang/Object;Ljava/lang/Object;)V");
- if (sax->setDocumentLocator == NULL)
- {
- return;
- }
- }
- (*env)->CallVoidMethod (env,
- target,
- sax->setDocumentLocator,
- xmljAsField (env, ctx),
- xmljAsField (env, loc));
- }
- void
- xmljSAXStartDocument (void *vctx)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- xmlSAX2StartDocument (vctx);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->startDocument == NULL)
- {
- sax->startDocument = xmljGetMethodID (env,
- target,
- "startDocument",
- "(Z)V");
- if (sax->startDocument == NULL)
- {
- return;
- }
- }
- (*env)->CallVoidMethod (env,
- target,
- sax->startDocument,
- ctx->standalone);
- }
- void
- xmljSAXEndDocument (void *vctx)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- xmlSAX2EndDocument (vctx);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->endDocument == NULL)
- {
- sax->endDocument = xmljGetMethodID (env,
- target,
- "endDocument",
- "()V");
- if (sax->endDocument == NULL)
- {
- return;
- }
- }
- (*env)->CallVoidMethod (env,
- target,
- sax->endDocument);
- }
- void
- xmljSAXStartElement (void *vctx,
- const xmlChar * name,
- const xmlChar ** attrs)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- jstring j_name;
- jobjectArray j_attrs;
- jstring j_attr;
- jsize len;
- xmlSAX2StartElement (vctx, name, attrs);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->startElement == NULL)
- {
- sax->startElement =
- xmljGetMethodID (env,
- target,
- "startElement",
- "(Ljava/lang/String;[Ljava/lang/String;)V");
- if (sax->startElement == NULL)
- {
- return;
- }
- }
- j_name = xmljNewString (env, name);
- /* build attributes array */
- len = 0;
- for (len = 0; attrs && attrs[len]; len++)
- {
- }
- if (len)
- {
- if (sax->stringClass == NULL)
- {
- sax->stringClass = (*env)->FindClass (env, "java/lang/String");
- if (sax->stringClass == NULL)
- {
- fprintf (stderr, "Can't find java.lang.String class!\n");
- return;
- }
- }
- j_attrs = (*env)->NewObjectArray (env, len, sax->stringClass, NULL);
- if (j_attrs == NULL)
- {
- fprintf (stderr, "Can't allocate attributes array!\n");
- return;
- }
- len = 0;
- for (len = 0; attrs && attrs[len]; len++)
- {
- j_attr = xmljNewString (env, attrs[len]);
- (*env)->SetObjectArrayElement (env, j_attrs, len, j_attr);
- }
-
- (*env)->CallVoidMethod (env,
- target,
- sax->startElement,
- j_name,
- j_attrs);
- (*env)->DeleteLocalRef (env, j_attrs);
- }
- else
- {
- (*env)->CallVoidMethod (env,
- target,
- sax->startElement,
- j_name,
- NULL);
-
- }
- }
- void
- xmljSAXEndElement (void *vctx,
- const xmlChar * name)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- jstring j_name;
- xmlSAX2EndElement (vctx, name);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->endElement == NULL)
- {
- sax->endElement = xmljGetMethodID (env,
- target,
- "endElement",
- "(Ljava/lang/String;)V");
- if (sax->endElement == NULL)
- {
- return;
- }
- }
- j_name = xmljNewString (env, name);
- (*env)->CallVoidMethod (env,
- target,
- sax->endElement,
- j_name);
- }
- void
- xmljSAXReference (void *vctx,
- const xmlChar * name)
- {
- xmlSAX2Reference (vctx, name);
- }
- void
- xmljSAXCharacters (void *vctx,
- const xmlChar * ch,
- int len)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- jstring j_ch;
- xmlChar *dup;
- xmlSAX2Characters (vctx, ch, len);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->characters == NULL)
- {
- sax->characters = xmljGetMethodID (env,
- target,
- "characters",
- "(Ljava/lang/String;)V");
- if (sax->characters == NULL)
- {
- return;
- }
- }
- dup = xmlStrndup (ch, len);
- j_ch = xmljNewString (env, dup);
- (*env)->CallVoidMethod (env,
- target,
- sax->characters,
- j_ch);
- xmlFree (dup);
- }
- void
- xmljSAXIgnorableWhitespace (void *vctx,
- const xmlChar * ch,
- int len)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- jstring j_ch;
- xmlChar *dup;
- xmlSAX2IgnorableWhitespace (vctx, ch, len);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->ignorableWhitespace == NULL)
- {
- sax->ignorableWhitespace = xmljGetMethodID (env,
- target,
- "ignorableWhitespace",
- "(Ljava/lang/String;)V");
- if (sax->ignorableWhitespace == NULL)
- {
- return;
- }
- }
- dup = xmlStrndup (ch, len);
- j_ch = xmljNewString (env, dup);
- (*env)->CallVoidMethod (env,
- target,
- sax->ignorableWhitespace,
- j_ch);
- xmlFree (dup);
- }
- void
- xmljSAXProcessingInstruction (void *vctx,
- const xmlChar * targ,
- const xmlChar * data)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- jstring j_targ;
- jstring j_data;
- xmlSAX2ProcessingInstruction (vctx, targ, data);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->processingInstruction == NULL)
- {
- sax->processingInstruction =
- xmljGetMethodID (env,
- target,
- "processingInstruction",
- "(Ljava/lang/String;Ljava/lang/String;)V");
- if (sax->processingInstruction == NULL)
- {
- return;
- }
- }
- j_targ = xmljNewString (env, targ);
- j_data = xmljNewString (env, data);
- (*env)->CallVoidMethod (env,
- target,
- sax->processingInstruction,
- j_targ,
- j_data);
- }
- void
- xmljSAXComment (void *vctx,
- const xmlChar * value)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- jstring j_text;
- xmlSAX2Comment (vctx, value);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->comment == NULL)
- {
- sax->comment =
- xmljGetMethodID (env,
- target,
- "comment",
- "(Ljava/lang/String;)V");
- if (sax->comment == NULL)
- {
- return;
- }
- }
- j_text = xmljNewString (env, value);
- (*env)->CallVoidMethod (env,
- target,
- sax->comment,
- j_text);
- }
- void
- xmljSAXCDataBlock (void *vctx,
- const xmlChar * ch,
- int len)
- {
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- JNIEnv *env;
- jobject target;
- jstring j_ch;
- xmlChar *dup;
- xmlSAX2CDataBlock (vctx, ch, len);
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- env = sax->env;
- target = sax->obj;
- xmljCheckWellFormed (ctx);
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->cdataBlock == NULL)
- {
- sax->cdataBlock =
- xmljGetMethodID (env,
- target,
- "cdataBlock",
- "(Ljava/lang/String;)V");
- if (sax->cdataBlock == NULL)
- {
- return;
- }
- }
- dup = xmlStrndup (ch, len);
- j_ch = xmljNewString (env, dup);
- (*env)->CallVoidMethod (env,
- target,
- sax->cdataBlock,
- j_ch);
- xmlFree (dup);
- }
- void
- xmljDispatchError (xmlParserCtxtPtr ctx,
- xmlSAXLocatorPtr loc,
- JNIEnv *env,
- jobject target,
- jmethodID method,
- const char *msg,
- va_list args)
- {
- jint lineNumber;
- jint columnNumber;
- jstring publicId;
- jstring systemId;
- char buffer[2048] = "";
- if (msg != NULL)
- {
- vsnprintf (buffer, sizeof buffer, msg, args);
- }
- lineNumber = loc->getLineNumber (ctx);
- columnNumber = loc->getColumnNumber (ctx);
- publicId = xmljNewString (env, loc->getPublicId (ctx));
- systemId = xmljNewString (env, loc->getSystemId (ctx));
- (*env)->CallVoidMethod (env,
- target,
- method,
- (*env)->NewStringUTF (env, buffer),
- lineNumber,
- columnNumber,
- publicId,
- systemId);
- }
- void
- xmljSAXWarning (void *vctx,
- const char *msg,
- ...)
- {
- va_list args;
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- xmlSAXLocatorPtr loc;
- JNIEnv *env;
- jobject target;
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- loc = (xmlSAXLocatorPtr) sax->loc;
- env = sax->env;
- target = sax->obj;
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->warning == NULL)
- {
- sax->warning =
- xmljGetMethodID (env,
- target,
- "warning",
- "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V");
- if (sax->warning == NULL)
- {
- return;
- }
- }
- va_start (args, msg);
- /* xmlParserWarning (vctx, msg, args); */
- xmljDispatchError (ctx, loc, env, target, sax->warning, msg, args);
- va_end (args);
- }
- void
- xmljSAXError (void *vctx,
- const char *msg,
- ...)
- {
- va_list args;
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- xmlSAXLocatorPtr loc;
- JNIEnv *env;
- jobject target;
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- loc = (xmlSAXLocatorPtr) sax->loc;
- env = sax->env;
- target = sax->obj;
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->error == NULL)
- {
- sax->error =
- xmljGetMethodID (env,
- target,
- "error",
- "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V");
- if (sax->error == NULL)
- {
- return;
- }
- }
- va_start (args, msg);
- /* xmlParserError (vctx, msg, args); */
- xmljDispatchError (ctx, loc, env, target, sax->error, msg, args);
- va_end (args);
- }
- void
- xmljSAXFatalError (void *vctx,
- const char *msg,
- ...)
- {
- va_list args;
- xmlParserCtxtPtr ctx;
- SAXParseContext *sax;
- xmlSAXLocatorPtr loc;
- JNIEnv *env;
- jobject target;
- ctx = (xmlParserCtxtPtr) vctx;
- sax = (SAXParseContext *) ctx->_private;
- loc = (xmlSAXLocatorPtr) sax->loc;
- env = sax->env;
- target = sax->obj;
- if ((*env)->ExceptionOccurred (env))
- {
- return;
- }
- if (sax->fatalError == NULL)
- {
- sax->fatalError =
- xmljGetMethodID (env,
- target,
- "fatalError",
- "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V");
- if (sax->fatalError == NULL)
- {
- return;
- }
- }
-
- va_start (args, msg);
- /* xmlParserError (vctx, msg, args); */
- xmljDispatchError (ctx, loc, env, target, sax->fatalError, msg, args);
- va_end (args);
- }
- void
- xmljCheckWellFormed (xmlParserCtxtPtr ctx)
- {
- if (!ctx->wellFormed)
- {
- xmljSAXFatalError (ctx, "document is not well-formed");
- }
- if (ctx->validate && !ctx->valid)
- {
- xmljSAXFatalError (ctx, "document is not valid");
- }
- }
- /*
- * Convert a libxml2 attribute type to a string.
- */
- jstring
- xmljAttributeTypeName (JNIEnv * env, int type)
- {
- const char *text;
- switch (type)
- {
- case XML_ATTRIBUTE_CDATA:
- text = "CDATA";
- break;
- case XML_ATTRIBUTE_ID:
- text = "ID";
- break;
- case XML_ATTRIBUTE_IDREF:
- text = "IDREF";
- break;
- case XML_ATTRIBUTE_IDREFS:
- text = "IDREFS";
- break;
- case XML_ATTRIBUTE_NMTOKEN:
- text = "NMTOKEN";
- break;
- case XML_ATTRIBUTE_NMTOKENS:
- text = "NMTOKENS";
- break;
- case XML_ATTRIBUTE_ENTITY:
- text = "ID";
- break;
- case XML_ATTRIBUTE_ENTITIES:
- text = "ID";
- break;
- default:
- return NULL;
- }
- return (*env)->NewStringUTF (env, text);
- }
- /*
- * Convert a libxml2 attribute default value type to a string.
- */
- jstring
- xmljAttributeModeName (JNIEnv * env, int type)
- {
- const char *text;
- switch (type)
- {
- case XML_ATTRIBUTE_IMPLIED:
- text = "#IMPLIED";
- break;
- case XML_ATTRIBUTE_REQUIRED:
- text = "#REQUIRED";
- break;
- case XML_ATTRIBUTE_FIXED:
- text = "#FIXED";
- break;
- default:
- return NULL;
- }
- return (*env)->NewStringUTF (env, text);
- }
|