-
Notifications
You must be signed in to change notification settings - Fork 932
TIKA-4728 - fix xhtml in widgets #2817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tballison
wants to merge
7
commits into
main
Choose a base branch
from
TIKA-4728-js-in-pdf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+939
−87
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
daef21e
TIKA-4728 - fix xhtml in widgets
tballison 571ffb5
TIKA-4728 - general xhtml fixes and tests
tballison 155d2f6
TIKA-4728 - add strict validation as an option
tballison 32d5f9e
TIKA-4728 - further tag fixes
tballison f0566ff
TIKA-4728 - respond to copilot's review
tballison 5766328
TIKA-4728 -- generalize self-healing on intermediate exceptions
tballison 2a502a7
TIKA-4728 -- fixes based on copilot review 3
tballison File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 227 additions & 0 deletions
227
tika-core/src/main/java/org/apache/tika/sax/StrictXHTMLValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.tika.sax; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import org.xml.sax.Attributes; | ||
| import org.xml.sax.ContentHandler; | ||
| import org.xml.sax.SAXException; | ||
|
|
||
| /** | ||
| * A SAX content handler decorator that enforces XHTML well-formedness on the | ||
| * incoming event stream. Any parser that emits an event sequence that would | ||
| * produce malformed XHTML triggers a {@link SAXException} synchronously — the | ||
| * stack trace points at the parser code that made the offending call, instead | ||
| * of surfacing later as a parse error on the serialized output. | ||
| * <p> | ||
| * Invariants enforced: | ||
| * <ul> | ||
| * <li>{@code startDocument} is called at most once.</li> | ||
| * <li>No SAX events arrive after {@code endDocument}.</li> | ||
| * <li>Every {@code endElement} matches the topmost open {@code startElement} | ||
| * (no cross-nesting like {@code <a><b></a></b>}).</li> | ||
| * <li>The element stack is empty when {@code endDocument} fires (no unclosed | ||
| * elements left dangling by an exception path).</li> | ||
| * <li>Within a single {@code startElement}, no two attributes share the same | ||
| * (namespaceURI, localName) pair (the bug class that produces | ||
| * {@code <div class="x" class="y">}).</li> | ||
| * </ul> | ||
| * Use as a decorator wrapping the real handler. It passes every event through | ||
| * to the downstream handler after validation, so any normal text/XHTML capture | ||
| * still works. | ||
| */ | ||
| public class StrictXHTMLValidator extends ContentHandlerDecorator { | ||
|
|
||
| private final Deque<QName> openElements = new ArrayDeque<>(); | ||
| private boolean documentStarted; | ||
| private boolean documentEnded; | ||
|
|
||
| public StrictXHTMLValidator(ContentHandler handler) { | ||
| super(handler); | ||
| } | ||
|
|
||
| @Override | ||
| public void startDocument() throws SAXException { | ||
| if (documentStarted) { | ||
| throw new SAXException("StrictXHTMLValidator: startDocument called twice"); | ||
| } | ||
| if (documentEnded) { | ||
| throw new SAXException( | ||
| "StrictXHTMLValidator: startDocument after endDocument"); | ||
| } | ||
| documentStarted = true; | ||
| super.startDocument(); | ||
| } | ||
|
|
||
| @Override | ||
| public void endDocument() throws SAXException { | ||
| if (documentEnded) { | ||
| throw new SAXException("StrictXHTMLValidator: endDocument called twice"); | ||
| } | ||
| if (!openElements.isEmpty()) { | ||
| throw new SAXException( | ||
| "StrictXHTMLValidator: endDocument with " + openElements.size() | ||
| + " unclosed element(s); topmost was <" | ||
| + openElements.peek().qOrLocal() + ">"); | ||
| } | ||
| documentEnded = true; | ||
| super.endDocument(); | ||
| } | ||
|
|
||
| @Override | ||
| public void startElement(String uri, String localName, String qName, Attributes attrs) | ||
| throws SAXException { | ||
| ensureNotEnded("startElement <" + display(qName, localName) + ">"); | ||
| checkAttributesUnique(qName, localName, attrs); | ||
| openElements.push(new QName(uri, localName, qName)); | ||
| super.startElement(uri, localName, qName, attrs); | ||
| } | ||
|
|
||
| @Override | ||
| public void endElement(String uri, String localName, String qName) throws SAXException { | ||
| ensureNotEnded("endElement </" + display(qName, localName) + ">"); | ||
| if (openElements.isEmpty()) { | ||
| throw new SAXException( | ||
| "StrictXHTMLValidator: endElement </" + display(qName, localName) | ||
| + "> with no matching startElement"); | ||
| } | ||
| QName top = openElements.pop(); | ||
| if (!top.matches(uri, localName, qName)) { | ||
| throw new SAXException( | ||
| "StrictXHTMLValidator: endElement </" + display(qName, localName) | ||
| + "> does not match topmost open element <" | ||
| + top.qOrLocal() + ">"); | ||
| } | ||
| super.endElement(uri, localName, qName); | ||
| } | ||
|
|
||
| @Override | ||
| public void characters(char[] ch, int start, int length) throws SAXException { | ||
| ensureNotEnded("characters"); | ||
| super.characters(ch, start, length); | ||
| } | ||
|
|
||
| @Override | ||
| public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { | ||
| ensureNotEnded("ignorableWhitespace"); | ||
| super.ignorableWhitespace(ch, start, length); | ||
| } | ||
|
|
||
| @Override | ||
| public void processingInstruction(String target, String data) throws SAXException { | ||
| ensureNotEnded("processingInstruction"); | ||
| super.processingInstruction(target, data); | ||
| } | ||
|
|
||
| @Override | ||
| public void startPrefixMapping(String prefix, String uri) throws SAXException { | ||
| ensureNotEnded("startPrefixMapping"); | ||
| super.startPrefixMapping(prefix, uri); | ||
| } | ||
|
|
||
| @Override | ||
| public void endPrefixMapping(String prefix) throws SAXException { | ||
| ensureNotEnded("endPrefixMapping"); | ||
| super.endPrefixMapping(prefix); | ||
| } | ||
|
|
||
| @Override | ||
| public void skippedEntity(String name) throws SAXException { | ||
| ensureNotEnded("skippedEntity"); | ||
| super.skippedEntity(name); | ||
| } | ||
|
|
||
| private void ensureNotEnded(String event) throws SAXException { | ||
| if (documentEnded) { | ||
| throw new SAXException( | ||
| "StrictXHTMLValidator: " + event + " arrived after endDocument"); | ||
| } | ||
| } | ||
|
|
||
| private void checkAttributesUnique(String elementQName, String elementLocalName, | ||
| Attributes attrs) throws SAXException { | ||
| int n = attrs.getLength(); | ||
| if (n < 2) { | ||
| return; | ||
| } | ||
| // (uri, localName) pairs must be unique per the XML namespaces spec. | ||
| // We also check raw qnames because Tika's serializers emit by qname and | ||
| // duplicate qnames produce malformed XHTML even when localnames differ. | ||
| Set<String> seenUriLocal = new HashSet<>(n); | ||
| Set<String> seenQNames = new HashSet<>(n); | ||
| for (int i = 0; i < n; i++) { | ||
| String uri = nullSafe(attrs.getURI(i)); | ||
| String local = nullSafe(attrs.getLocalName(i)); | ||
| String qn = nullSafe(attrs.getQName(i)); | ||
| String key = uri + "" + local; | ||
|
|
||
| if (!seenUriLocal.add(key)) { | ||
| throw new SAXException( | ||
| "StrictXHTMLValidator: duplicate attribute on <" | ||
| + display(elementQName, elementLocalName) + ">: " | ||
| + (uri.isEmpty() ? local : ("{" + uri + "}" + local))); | ||
| } | ||
| if (!qn.isEmpty() && !seenQNames.add(qn)) { | ||
| throw new SAXException( | ||
| "StrictXHTMLValidator: duplicate attribute qname on <" | ||
| + display(elementQName, elementLocalName) + ">: " + qn); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static String nullSafe(String s) { | ||
| return s == null ? "" : s; | ||
| } | ||
|
|
||
| private static String display(String qName, String localName) { | ||
| if (qName != null && !qName.isEmpty()) { | ||
| return qName; | ||
| } | ||
| return localName == null ? "" : localName; | ||
| } | ||
|
|
||
| private static final class QName { | ||
| final String uri; | ||
| final String localName; | ||
| final String qName; | ||
|
|
||
| QName(String uri, String localName, String qName) { | ||
| this.uri = nullSafe(uri); | ||
| this.localName = nullSafe(localName); | ||
| this.qName = nullSafe(qName); | ||
| } | ||
|
|
||
| boolean matches(String u, String l, String q) { | ||
| // SAX parsers can vary in which fields they populate. Accept a | ||
| // match on either (uri, localName) or qName, whichever is present. | ||
| String otherU = nullSafe(u); | ||
| String otherL = nullSafe(l); | ||
| String otherQ = nullSafe(q); | ||
| boolean uriLocalMatch = uri.equals(otherU) && localName.equals(otherL) | ||
| && !localName.isEmpty(); | ||
| boolean qNameMatch = !qName.isEmpty() && qName.equals(otherQ); | ||
| return uriLocalMatch || qNameMatch; | ||
| } | ||
|
|
||
| String qOrLocal() { | ||
| return qName.isEmpty() ? localName : qName; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.