Claus Ibsen created CAMEL-27201:
-----------------------------------

             Summary: camel-tika - html and xml output fail on a NUL character 
in document metadata
                 Key: CAMEL-27201
                 URL: https://issues.apache.org/jira/browse/CAMEL-27201
             Project: Camel
          Issue Type: Bug
          Components: camel-tika
            Reporter: Claus Ibsen


With {{tikaParseOutputFormat=html}} or {{xml}}, parsing a document whose 
metadata (here the PDF title) contains a character that XML does not allow, 
such as NUL (U+0000), fails. With {{text}} the same document is read fine. This 
happened with 3 of 4 ordinary PDFs tested (two made by macOS {{cupsfilter}}, 
and {{multi_page.pdf}} from the docling test data).

h3. How to reproduce
{code}
from("file:in")
  .to("tika:parse?tikaParseOutputFormat=html")
  .to("log:out");
{code}
{noformat}
org.apache.tika.exception.TikaException: Unable to extract PDF content
...
Caused by: org.xml.sax.SAXException: An invalid XML character (Unicode: 0x0) 
was found in the node's character data content.
        at 
java.xml/com.sun.org.apache.xml.internal.serializer.ToStream.accumDefaultEscape(ToStream.java:1749)
        ...
        at 
java.xml/com.sun.org.apache.xml.internal.serializer.ToHTMLStream.endElement(ToHTMLStream.java:913)
        at 
java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerHandlerImpl.endElement(TransformerHandlerImpl.java:282)
        at 
org.apache.tika.sax.ContentHandlerDecorator.endElement(ContentHandlerDecorator.java:134)
        at 
org.apache.tika.sax.ExpandedTitleContentHandler.endElement(ExpandedTitleContentHandler.java:69)
        ...
        at 
org.apache.tika.sax.XHTMLContentHandler.lazyEndHead(XHTMLContentHandler.java:226)
{noformat}
(Camel 4.24.0-SNAPSHOT, Tika 4.1.0.)

h3. Where
{{components/camel-tika/src/main/java/org/apache/camel/component/tika/TikaProducer.java}}:
 {{getContentHandler}} / {{getTransformerHandler}}. The JDK 
{{TransformerHandler}} gets the SAX events as they are, and its serializer 
rejects characters that are not allowed in XML.

h3. Suggested fix
Filter the characters before they reach the {{TransformerHandler}}, for example 
by wrapping it in Tika's {{org.apache.tika.sax.SafeContentHandler}} (which 
replaces invalid XML characters with U+FFFD), or a small 
{{ContentHandlerDecorator}} that does the same for {{characters}} and attribute 
values. Add a test with a document whose title holds a NUL.

Found while adding an {{outputFormat}} option to the 
{{tika-extract-text-action}} Kamelet; its description now warns that html/xml 
fail on such documents.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to