davsclaus opened a new pull request, #30506:
URL: https://github.com/apache/camel/pull/30506

   ## What
   
   Fixes [CAMEL-27304](https://issues.apache.org/jira/browse/CAMEL-27304): 
`tika:parse?tikaParseOutputFormat=html` failed on JDK 21+ for any document 
without a title (plain text, HTML without `<title>` or with an empty one, and 
also the `.doc`, `.odt` and `.gif` files in the module's tests):
   
   ```
   TIKA-237: Illegal SAXException ... An invalid XML character (Unicode: 0x0) 
was found in the node's character data content
   ```
   
   Found through the `tika-extract-text-action` Kamelet 
(apache/camel-kamelets#3310).
   
   ## Why
   
   `TikaProducer` wrapped the JDK `TransformerHandler` in Tika's 
`ExpandedTitleContentHandler` (since the component was added in CAMEL-10740, 
the same as `TikaCLI`). Tika writes the XHTML elements in the XHTML namespace, 
and the JDK html output method writes namespaced elements XML style, so an 
empty title becomes `<title/>`, which browsers do not handle (TIKA-725). The 
workaround in `ExpandedTitleContentHandler` calls `characters(new char[0], 0, 
1)` and catches the `ArrayIndexOutOfBoundsException` the old Xalan serializer 
threw after closing the start tag. The JDK serializer copies the char into its 
buffer instead, so it holds a NUL: JDK 17 writes `<title>&#0;</title>`, JDK 21+ 
rejects it.
   
   Tika has not changed this: `TikaCLI` in Tika main still uses 
`ExpandedTitleContentHandler`, and the `OutputKeys.VERSION=1.1` workaround from 
TIKA-4137 in tika-server does not help here (checked on JDK 25).
   
   ## The fix
   
   A small `ContentHandlerDecorator` in `TikaProducer` replaces 
`ExpandedTitleContentHandler`. It holds back the start of the XHTML `title` 
element until it sees text:
   - a title with text is passed on unchanged;
   - an empty title is written **without the XHTML namespace**, which the html 
output method treats as an HTML element, so it writes `<title></title>` (not 
`<title/>`, and no NUL).
   
   Alternatives considered:
   - dropping the NUL (skipping the zero-length `characters`) gives `<title/>`, 
the TIKA-725 problem again;
   - Tika's `ToHTMLContentHandler` writes `<title></title>`, but its output is 
very different (no indentation, `<meta>` without `/>`), so it would change the 
output of every document that works today;
   - removing the namespace from every title changes the indentation of the 
rest of the document, so only the empty title is changed.
   
   ## Tests
   
   - 5 new tests in `TikaParseTest` with `html` output: plain text as String 
and as bytes, HTML without a title, HTML with an empty title (all four assert 
`<title></title>`, no `<title/>`, no NUL or `&#0;`, and the text), and HTML 
with a title.
   - Before the fix the 4 title-less tests fail with the reported exception 
(JDK 25); the titled one passes.
   - With a one-off dump test (not committed), the html output of two titled 
HTML documents (one with links, tables, lists, an image and escaped title text) 
is byte-for-byte the same before and after the fix. The `.doc`, `.odt` and 
`.gif` test files failed before and now give `<title></title>`.
   - `camel-tika`: 19 tests, 0 failures, 0 errors (JDK 25).
   
   _Claude Code on behalf of Claus Ibsen_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to