xmlj_io.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /* xmlj_io.c -
  2. Copyright (C) 2003, 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_io.h"
  32. #include "xmlj_error.h"
  33. #include "xmlj_node.h"
  34. #include "xmlj_sax.h"
  35. #include "xmlj_util.h"
  36. #include <math.h>
  37. #include <string.h>
  38. #include <stdio.h>
  39. #include <stdarg.h>
  40. #include <libxml/xmlIO.h>
  41. #include <libxml/parserInternals.h>
  42. #include <pthread.h>
  43. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  44. #define UNSIGN(a) (((a) < 0) ? ((a) + 0x100) : (a))
  45. #define DETECT_BUFFER_SIZE 50
  46. typedef struct _OutputStreamContext
  47. {
  48. JNIEnv *env;
  49. jobject outputStream;
  50. jmethodID outputStreamWriteFunc;
  51. jmethodID outputStreamCloseFunc;
  52. }
  53. OutputStreamContext;
  54. typedef struct _InputStreamContext
  55. {
  56. JNIEnv *env;
  57. jobject inputStream;
  58. jmethodID inputStreamReadFunc;
  59. jmethodID inputStreamCloseFunc;
  60. jobject bufferByteArray;
  61. jint bufferLength;
  62. }
  63. InputStreamContext;
  64. InputStreamContext *xmljNewInputStreamContext (JNIEnv * env,
  65. jobject inputStream);
  66. void xmljFreeInputStreamContext (InputStreamContext * inContext);
  67. int xmljInputReadCallback (void *context, char *buffer, int len);
  68. int xmljInputCloseCallback (void *context);
  69. int xmljOutputWriteCallback (void *context, const char *buffer, int len);
  70. int xmljOutputCloseCallback (void *context);
  71. OutputStreamContext *xmljNewOutputStreamContext (JNIEnv * env,
  72. jobject outputStream);
  73. void
  74. xmljFreeOutputStreamContext (OutputStreamContext * outContext);
  75. xmlCharEncoding
  76. xmljDetectCharEncoding (JNIEnv * env, jbyteArray buffer);
  77. #ifdef LIBXML2_NEW_BUFFER
  78. #define GET_XML_OUTPUT_BUFFER_CONTENT(buf) (gchar *) \
  79. (char *) xmlOutputBufferGetContent(buf)
  80. #define GET_XML_OUTPUT_BUFFER_SIZE(buf) \
  81. xmlOutputBufferGetSize(buf)
  82. #else
  83. #define GET_XML_OUTPUT_BUFFER_CONTENT(buf) \
  84. (buf)->buffer->content
  85. #define GET_XML_OUTPUT_BUFFER_SIZE(buf) \
  86. (buf)->buffer->use
  87. #endif
  88. int
  89. xmljOutputWriteCallback (void *context, const char *buffer, int len)
  90. {
  91. OutputStreamContext *outContext;
  92. JNIEnv *env;
  93. jbyteArray byteArray;
  94. outContext = (OutputStreamContext *) context;
  95. env = outContext->env;
  96. byteArray = (*env)->NewByteArray (env, len);
  97. if (0 != byteArray)
  98. {
  99. (*env)->SetByteArrayRegion (env, byteArray, 0, len, (jbyte *) buffer);
  100. (*env)->CallVoidMethod (env,
  101. outContext->outputStream,
  102. outContext->outputStreamWriteFunc, byteArray);
  103. (*env)->DeleteLocalRef (env, byteArray);
  104. return (*env)->ExceptionOccurred (env) ? -1 : len;
  105. }
  106. else
  107. {
  108. /* Out of memory, signal error */
  109. return -1;
  110. }
  111. }
  112. int
  113. xmljOutputCloseCallback (void *context)
  114. {
  115. OutputStreamContext *outContext;
  116. JNIEnv *env;
  117. outContext = (OutputStreamContext *) context;
  118. env = outContext->env;
  119. (*env)->CallVoidMethod (env,
  120. outContext->outputStream,
  121. outContext->outputStreamCloseFunc);
  122. return (*env)->ExceptionOccurred (env) ? -1 : 0;
  123. }
  124. int
  125. xmljInputReadCallback (void *context, char *buffer, int len)
  126. {
  127. InputStreamContext *inContext;
  128. JNIEnv *env;
  129. jint nread;
  130. int offset;
  131. inContext = (InputStreamContext *) context;
  132. env = inContext->env;
  133. nread = 0;
  134. for (offset = 0; offset < len && nread >= 0;)
  135. {
  136. nread = (*env)->CallIntMethod (env,
  137. inContext->inputStream,
  138. inContext->inputStreamReadFunc,
  139. inContext->bufferByteArray,
  140. 0, MIN (len - offset,
  141. inContext->bufferLength));
  142. if (nread > 0)
  143. {
  144. (*env)->GetByteArrayRegion (env,
  145. inContext->bufferByteArray,
  146. 0, nread, ((jbyte *) buffer) + offset);
  147. offset += nread;
  148. }
  149. }
  150. return (*env)->ExceptionOccurred (env) ? -1 : offset;
  151. }
  152. int
  153. xmljInputCloseCallback (void *context)
  154. {
  155. InputStreamContext *inContext;
  156. JNIEnv *env;
  157. inContext = (InputStreamContext *) context;
  158. env = inContext->env;
  159. (*env)->CallVoidMethod (env, inContext->inputStream,
  160. inContext->inputStreamCloseFunc);
  161. return (*env)->ExceptionOccurred (env) ? -1 : 0;
  162. }
  163. InputStreamContext *
  164. xmljNewInputStreamContext (JNIEnv * env, jobject inputStream)
  165. {
  166. jclass inputStreamClass;
  167. InputStreamContext *result;
  168. inputStreamClass = (*env)->FindClass (env, "java/io/InputStream");
  169. if (inputStreamClass == NULL)
  170. {
  171. return NULL;
  172. }
  173. result = (InputStreamContext *) malloc (sizeof (InputStreamContext));
  174. if (result == NULL)
  175. {
  176. return NULL;
  177. }
  178. result->env = env;
  179. result->inputStream = inputStream;
  180. result->inputStreamReadFunc =
  181. (*env)->GetMethodID (env, inputStreamClass, "read", "([BII)I");
  182. result->inputStreamCloseFunc =
  183. (*env)->GetMethodID (env, inputStreamClass, "close", "()V");
  184. result->bufferLength = 4096;
  185. result->bufferByteArray = (*env)->NewByteArray (env, result->bufferLength);
  186. return result;
  187. }
  188. void
  189. xmljFreeInputStreamContext (InputStreamContext * inContext)
  190. {
  191. JNIEnv *env;
  192. env = inContext->env;
  193. (*env)->DeleteLocalRef (env, inContext->bufferByteArray);
  194. free (inContext);
  195. }
  196. OutputStreamContext *
  197. xmljNewOutputStreamContext (JNIEnv * env, jobject outputStream)
  198. {
  199. jclass outputStreamClass;
  200. OutputStreamContext *result;
  201. outputStreamClass = (*env)->FindClass (env, "java/io/OutputStream");
  202. if (outputStreamClass == NULL)
  203. {
  204. return NULL;
  205. }
  206. result = (OutputStreamContext *) malloc (sizeof (OutputStreamContext));
  207. if (result == NULL)
  208. {
  209. return NULL;
  210. }
  211. result->env = env;
  212. result->outputStream = outputStream;
  213. result->outputStreamWriteFunc =
  214. (*env)->GetMethodID (env, outputStreamClass, "write", "([B)V");
  215. result->outputStreamCloseFunc =
  216. (*env)->GetMethodID (env, outputStreamClass, "close", "()V");
  217. return result;
  218. }
  219. void
  220. xmljFreeOutputStreamContext (OutputStreamContext * outContext)
  221. {
  222. free (outContext);
  223. }
  224. SAXParseContext *
  225. xmljNewSAXParseContext (JNIEnv * env, jobject obj, xmlParserCtxtPtr ctx,
  226. jstring publicId, jstring systemId)
  227. {
  228. SAXParseContext *ret;
  229. ret = (SAXParseContext *) malloc (sizeof (SAXParseContext));
  230. ret->env = env;
  231. ret->obj = obj;
  232. ret->ctx = ctx;
  233. ret->sax = ctx->sax;
  234. ret->loc = NULL;
  235. ret->publicId = publicId;
  236. ret->systemId = systemId;
  237. ret->startDTD = NULL;
  238. ret->externalEntityDecl = NULL;
  239. ret->internalEntityDecl = NULL;
  240. ret->resolveEntity = NULL;
  241. ret->notationDecl = NULL;
  242. ret->attributeDecl = NULL;
  243. ret->elementDecl = NULL;
  244. ret->unparsedEntityDecl = NULL;
  245. ret->setDocumentLocator = NULL;
  246. ret->startDocument = NULL;
  247. ret->endDocument = NULL;
  248. ret->startElement = NULL;
  249. ret->endElement = NULL;
  250. ret->characters = NULL;
  251. ret->ignorableWhitespace = NULL;
  252. ret->processingInstruction = NULL;
  253. ret->comment = NULL;
  254. ret->cdataBlock = NULL;
  255. ret->warning = NULL;
  256. ret->error = NULL;
  257. ret->fatalError = NULL;
  258. ret->resolveURIAndOpen = NULL;
  259. ret->stringClass = NULL;
  260. return ret;
  261. }
  262. void
  263. xmljFreeSAXParseContext (SAXParseContext * saxCtx)
  264. {
  265. free (saxCtx);
  266. }
  267. xmlCharEncoding
  268. xmljDetectCharEncoding (JNIEnv * env, jbyteArray buffer)
  269. {
  270. xmlCharEncoding ret;
  271. jint nread;
  272. if (buffer == NULL)
  273. {
  274. return XML_CHAR_ENCODING_ERROR;
  275. }
  276. nread = (*env)->GetArrayLength (env, buffer);
  277. if (nread >= 5)
  278. {
  279. jbyte nativeBuffer[DETECT_BUFFER_SIZE + 1];
  280. unsigned char converted[DETECT_BUFFER_SIZE + 1];
  281. int i;
  282. memset (nativeBuffer, 0, DETECT_BUFFER_SIZE + 1);
  283. (*env)->GetByteArrayRegion (env, buffer, 0, nread, nativeBuffer);
  284. /* Convert from signed to unsigned */
  285. for (i = 0; i < DETECT_BUFFER_SIZE + 1; i++)
  286. {
  287. converted[i] = UNSIGN (nativeBuffer[i]);
  288. }
  289. ret = xmlDetectCharEncoding (converted, nread);
  290. }
  291. else
  292. {
  293. ret = XML_CHAR_ENCODING_NONE;
  294. }
  295. return ret;
  296. }
  297. xmlParserCtxtPtr
  298. xmljNewParserContext (JNIEnv * env,
  299. jobject inputStream,
  300. jbyteArray detectBuffer,
  301. jstring publicId,
  302. jstring systemId,
  303. jstring base,
  304. jboolean validate,
  305. jboolean coalesce,
  306. jboolean expandEntities,
  307. jboolean loadEntities)
  308. {
  309. InputStreamContext *inputContext;
  310. xmlCharEncoding encoding;
  311. xmlParserCtxtPtr ctx;
  312. int options;
  313. encoding = xmljDetectCharEncoding (env, detectBuffer);
  314. if (encoding != XML_CHAR_ENCODING_ERROR)
  315. {
  316. inputContext = xmljNewInputStreamContext (env, inputStream);
  317. if (NULL != inputContext)
  318. {
  319. /* NOTE: userdata must be NULL for DOM to work */
  320. ctx = xmlCreateIOParserCtxt (NULL,
  321. NULL,
  322. xmljInputReadCallback,
  323. xmljInputCloseCallback,
  324. inputContext,
  325. encoding);
  326. if (NULL != ctx)
  327. {
  328. ctx->userData = ctx;
  329. /* Set parsing options */
  330. options = 0;
  331. if (validate)
  332. {
  333. options |= XML_PARSE_DTDVALID;
  334. }
  335. if (coalesce)
  336. {
  337. options |= XML_PARSE_NOCDATA;
  338. }
  339. if (expandEntities)
  340. {
  341. options |= XML_PARSE_NOENT;
  342. }
  343. if (loadEntities)
  344. {
  345. options |= XML_PARSE_DTDLOAD;
  346. }
  347. if (xmlCtxtUseOptions (ctx, options))
  348. {
  349. xmljThrowException (env,
  350. "java/lang/RuntimeException",
  351. "Unable to set xmlParserCtxtPtr options");
  352. }
  353. if (base != NULL)
  354. {
  355. ctx->input->directory =
  356. (*env)->GetStringUTFChars (env, base, 0);
  357. }
  358. return ctx;
  359. }
  360. xmljFreeInputStreamContext (inputContext);
  361. }
  362. }
  363. return NULL;
  364. }
  365. void
  366. xmljFreeParserContext (xmlParserCtxtPtr ctx)
  367. {
  368. InputStreamContext *inputStreamContext = NULL;
  369. if (ctx->input != NULL && ctx->input->buf != NULL)
  370. {
  371. inputStreamContext
  372. = (InputStreamContext *) ctx->input->buf->context;
  373. }
  374. xmlFreeParserCtxt (ctx);
  375. if (inputStreamContext != NULL)
  376. {
  377. xmljFreeInputStreamContext (inputStreamContext);
  378. }
  379. }
  380. xmlDocPtr
  381. xmljParseDocument (JNIEnv * env,
  382. jobject self,
  383. jobject in,
  384. jbyteArray detectBuffer,
  385. jstring publicId,
  386. jstring systemId,
  387. jstring base,
  388. jboolean validate,
  389. jboolean coalesce,
  390. jboolean expandEntities,
  391. jboolean contentHandler,
  392. jboolean dtdHandler,
  393. jboolean entityResolver,
  394. jboolean errorHandler,
  395. jboolean declarationHandler,
  396. jboolean lexicalHandler,
  397. int mode)
  398. {
  399. xmlParserCtxtPtr ctx;
  400. SAXParseContext *saxCtx;
  401. xmlSAXHandlerPtr sax;
  402. ctx = xmljNewParserContext (env, in, detectBuffer, publicId, systemId, base,
  403. validate, coalesce, expandEntities,
  404. entityResolver);
  405. if (ctx != NULL)
  406. {
  407. saxCtx = xmljNewSAXParseContext (env, self, ctx, publicId, systemId);
  408. if (saxCtx != NULL)
  409. {
  410. sax = xmljNewSAXHandler (contentHandler,
  411. dtdHandler,
  412. entityResolver,
  413. errorHandler,
  414. declarationHandler,
  415. lexicalHandler);
  416. if (sax != NULL)
  417. {
  418. return xmljParseDocument2 (env,
  419. ctx,
  420. saxCtx,
  421. sax,
  422. mode);
  423. }
  424. xmljFreeSAXParseContext (saxCtx);
  425. }
  426. xmljFreeParserContext (ctx);
  427. }
  428. if (!(*env)->ExceptionOccurred (env))
  429. {
  430. xmljThrowException (env, "java/io/IOException",
  431. "Unable to create parser context");
  432. }
  433. return NULL;
  434. }
  435. xmlDocPtr
  436. xmljParseDocument2 (JNIEnv * env,
  437. xmlParserCtxtPtr ctx,
  438. SAXParseContext *saxCtx,
  439. xmlSAXHandlerPtr sax,
  440. int mode)
  441. {
  442. xmlSAXHandlerPtr orig;
  443. xmlDocPtr doc;
  444. int ret;
  445. ctx->_private = saxCtx;
  446. ctx->userData = ctx;
  447. orig = ctx->sax;
  448. ctx->sax = sax;
  449. xmljSetThreadContext (saxCtx);
  450. ret = xmlParseDocument (ctx);
  451. doc = ctx->myDoc;
  452. if (ret || !doc)
  453. {
  454. const char *msg = ctx->lastError.message;
  455. switch (mode)
  456. {
  457. case 0:
  458. xmljSAXFatalError (ctx, msg);
  459. break;
  460. case 1:
  461. xmljThrowDOMException (env, ret, msg);
  462. break;
  463. case 2:
  464. xmljThrowException (env,
  465. "javax/xml/transform/TransformerException",
  466. msg);
  467. }
  468. }
  469. xmljClearThreadContext ();
  470. ctx->sax = orig;
  471. free(sax);
  472. xmljFreeSAXParseContext (saxCtx);
  473. xmljFreeParserContext (ctx);
  474. xmljClearStringCache ();
  475. return doc;
  476. }
  477. xmlParserInputPtr
  478. xmljNewParserInput (JNIEnv * env,
  479. jobject inputStream,
  480. jbyteArray detectBuffer,
  481. xmlParserCtxtPtr parserContext)
  482. {
  483. xmlParserInputPtr ret;
  484. xmlParserInputBufferPtr input;
  485. xmlCharEncoding encoding;
  486. encoding = xmljDetectCharEncoding (env, detectBuffer);
  487. if (encoding != XML_CHAR_ENCODING_ERROR)
  488. {
  489. input = xmljNewParserInputBuffer (env, inputStream, encoding);
  490. if (input != NULL)
  491. {
  492. ret = xmlNewIOInputStream (parserContext, input, encoding);
  493. return ret;
  494. }
  495. xmlFreeParserInputBuffer (input);
  496. }
  497. return NULL;
  498. }
  499. xmlParserInputBufferPtr
  500. xmljNewParserInputBuffer (JNIEnv * env,
  501. jobject inputStream, xmlCharEncoding encoding)
  502. {
  503. xmlParserInputBufferPtr ret;
  504. InputStreamContext *inputContext;
  505. inputContext = xmljNewInputStreamContext (env, inputStream);
  506. if (NULL != inputContext)
  507. {
  508. ret = xmlParserInputBufferCreateIO (&xmljInputReadCallback,
  509. &xmljInputCloseCallback,
  510. inputContext, encoding);
  511. if (ret != NULL)
  512. return ret;
  513. xmljFreeInputStreamContext (inputContext);
  514. }
  515. return NULL;
  516. }
  517. void
  518. xmljSaveFileToJavaOutputStream (JNIEnv * env, jobject outputStream,
  519. xmlDocPtr tree,
  520. const char *outputEncodingName)
  521. {
  522. OutputStreamContext *outputContext =
  523. xmljNewOutputStreamContext (env, outputStream);
  524. xmlCharEncoding outputEncoding = xmlParseCharEncoding (outputEncodingName);
  525. xmlOutputBufferPtr outputBuffer =
  526. xmlOutputBufferCreateIO (xmljOutputWriteCallback,
  527. xmljOutputCloseCallback,
  528. outputContext,
  529. xmlGetCharEncodingHandler (outputEncoding));
  530. /* Write result to output stream */
  531. xmlSaveFileTo (outputBuffer, tree, outputEncodingName);
  532. xmljFreeOutputStreamContext (outputContext);
  533. }
  534. /*
  535. jobject
  536. xmljResolveURI (SaxErrorContext * saxErrorContext,
  537. const char *URL, const char *ID)
  538. {
  539. JNIEnv *env = saxErrorContext->env;
  540. jstring hrefString = (*env)->NewStringUTF (env, URL);
  541. jstring baseString = saxErrorContext->systemId;
  542. jobject sourceWrapper = (*env)->CallObjectMethod (env,
  543. saxErrorContext->
  544. saxErrorAdapter,
  545. saxErrorContext->
  546. resolveURIMethodID,
  547. hrefString,
  548. baseString);
  549. (*env)->DeleteLocalRef (env, hrefString);
  550. if (NULL == sourceWrapper)
  551. {
  552. return NULL;
  553. }
  554. else
  555. {
  556. jobject sourceInputStream = (*env)->CallObjectMethod (env,
  557. sourceWrapper,
  558. saxErrorContext->
  559. getInputStreamMethodID);
  560. (*env)->DeleteLocalRef (env, sourceWrapper);
  561. if ((*env)->ExceptionOccurred (env))
  562. {
  563. -* Report to ErrorAdapter here? *-
  564. return NULL;
  565. }
  566. return sourceInputStream;
  567. }
  568. }*/
  569. xmlDocPtr
  570. xmljResolveURIAndOpen (SAXParseContext *saxContext,
  571. const char *URL,
  572. const char *ID)
  573. {
  574. jobject libxmlDocument;
  575. xmlDocPtr doc;
  576. JNIEnv *env = saxContext->env;
  577. jstring hrefString = (*env)->NewStringUTF (env, URL);
  578. jstring baseString = saxContext->systemId;
  579. if (saxContext->resolveURIAndOpen == NULL)
  580. {
  581. jclass cls = (*env)->GetObjectClass (env, saxContext->obj);
  582. saxContext->resolveURIAndOpen =
  583. (*env)->GetMethodID (env, cls, "resolveURIAndOpen",
  584. "Ljava/lang/String;Ljava/lang/String)Lgnu/xml/libxmlj/transform/LibxmlDocument;");
  585. }
  586. libxmlDocument =
  587. (*env)->CallObjectMethod (env,
  588. saxContext->obj,
  589. saxContext->resolveURIAndOpen,
  590. hrefString,
  591. baseString);
  592. doc = (xmlDocPtr) xmljGetNodeID (env, libxmlDocument);
  593. (*env)->DeleteLocalRef (env, libxmlDocument);
  594. if ((*env)->ExceptionOccurred (env))
  595. {
  596. /* Report to ErrorAdapter here? */
  597. return NULL;
  598. }
  599. else
  600. {
  601. return doc;
  602. }
  603. }
  604. /*xmlParserInputPtr
  605. xmljLoadExternalEntity (const char *URL, const char *ID,
  606. xmlParserCtxtPtr ctxt)
  607. {
  608. SaxErrorContext *saxErrorContext = xmljGetThreadContext ();
  609. JNIEnv *env = saxErrorContext->env;
  610. jstring hrefString = (*env)->NewStringUTF (env, URL);
  611. jstring baseString = saxErrorContext->systemId;
  612. jobject sourceWrapper = (*env)->CallObjectMethod (env,
  613. saxErrorContext->
  614. saxErrorAdapter,
  615. saxErrorContext->
  616. resolveURIMethodID,
  617. hrefString,
  618. baseString);
  619. (*env)->DeleteLocalRef (env, hrefString);
  620. if (NULL == sourceWrapper)
  621. {
  622. return NULL;
  623. }
  624. else
  625. {
  626. InputStreamContext *inputContext;
  627. xmlParserInputBufferPtr inputBuffer;
  628. xmlParserInputPtr inputStream;
  629. jobject sourceInputStream = (*env)->CallObjectMethod (env,
  630. sourceWrapper,
  631. saxErrorContext->
  632. getInputStreamMethodID);
  633. (*env)->DeleteLocalRef (env, sourceWrapper);
  634. if ((*env)->ExceptionOccurred (env))
  635. {
  636. -* Report to ErrorAdapter *-
  637. return NULL;
  638. }
  639. inputContext = xmljNewInputStreamContext (env, sourceInputStream);
  640. inputBuffer
  641. = xmlParserInputBufferCreateIO (xmljInputReadCallback,
  642. xmljInputCloseCallback,
  643. inputContext, XML_CHAR_ENCODING_NONE);
  644. inputStream = xmlNewInputStream (ctxt);
  645. if (inputStream == NULL)
  646. {
  647. return (NULL);
  648. }
  649. inputStream->filename = NULL;
  650. inputStream->directory = NULL;
  651. inputStream->buf = inputBuffer;
  652. inputStream->base = GET_XML_OUTPUT_BUFFER_CONTENT (inputStream->buf);
  653. inputStream->cur = GET_XML_OUTPUT_BUFFER_CONTENT (inputStream->buf);
  654. inputStream->end =
  655. &inputStream->base[GET_XML_OUTPUT_BUFFER_SIZE (inputStream->buf)];
  656. if ((ctxt->directory == NULL) && (inputStream->directory != NULL))
  657. ctxt->directory =
  658. (char *) xmlStrdup ((const xmlChar *) inputStream->directory);
  659. return (inputStream);
  660. }
  661. }*/
  662. /* Key for the thread-specific buffer */
  663. static pthread_key_t thread_context_key;
  664. /* Once-only initialisation of the key */
  665. static pthread_once_t thread_context_once = PTHREAD_ONCE_INIT;
  666. static void
  667. thread_context_key_alloc (void);
  668. /* Allocate the key */
  669. static void
  670. thread_context_key_alloc ()
  671. {
  672. pthread_key_create (&thread_context_key, NULL);
  673. }
  674. void
  675. xmljSetThreadContext (SAXParseContext * context)
  676. {
  677. pthread_once (&thread_context_once, thread_context_key_alloc);
  678. pthread_setspecific (thread_context_key, context);
  679. }
  680. void
  681. xmljClearThreadContext (void)
  682. {
  683. pthread_setspecific (thread_context_key, NULL);
  684. }
  685. /* Return the thread-specific buffer */
  686. SAXParseContext *
  687. xmljGetThreadContext (void)
  688. {
  689. return (SAXParseContext *) pthread_getspecific (thread_context_key);
  690. }