Commit 3501cf6a authored by Jos van den Oever's avatar Jos van den Oever

Filter out misplaced <style:graphic-properties/> elements.

parent b608a6b2
package odf;
import java.util.Stack;
import org.eclipse.jdt.annotation.Nullable;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
......@@ -11,10 +13,20 @@ class OdfFixer extends XMLFilterImpl {
final String version;
final private Stack<String> familyStack = new Stack<String>();
private boolean mute = false; // whether or not to write output
OdfFixer(String version) {
this.version = version;
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
familyStack.clear();
mute = false;
}
@Override
public void startElement(@Nullable String uri, @Nullable String localName,
@Nullable String qName, @Nullable Attributes attributes)
......@@ -24,8 +36,10 @@ class OdfFixer extends XMLFilterImpl {
throw new NullPointerException();
}
// correct invalid attributes
AttributesImpl atts = new AttributesImpl(attributes);
int l = atts.getLength();
String family = null;
for (int i = 0; i < l; ++i) {
String u = atts.getURI(i);
String ln = atts.getLocalName(i);
......@@ -42,6 +56,8 @@ class OdfFixer extends XMLFilterImpl {
atts.removeAttribute(i--);
} else if ("text-position".equals(ln) && "".equals(v)) {
atts.removeAttribute(i--);
} else if ("family".equals(ln)) {
family = v;
}
} else if (NC.fo.equals(u)) {
if ("hyphenation-push-char-count".equals(ln)) {
......@@ -101,7 +117,19 @@ class OdfFixer extends XMLFilterImpl {
}
}
}
super.startElement(uri, localName, qName, atts);
// correct invalid nesting
// element <style:style style:family="paragraph"> may not have child
// element <style:graphic-properties>
// (workaround for invalid ODF from LibreOffice 5
if (NC.style.equals(uri) && "graphic-properties".equals(localName)
&& familyStack.size() > 0
&& "paragraph".equals(familyStack.lastElement())) {
mute = true;
}
familyStack.push(family);
if (!mute) {
super.startElement(uri, localName, qName, atts);
}
}
private static String fixPositiveInteger(String v) {
......@@ -120,13 +148,19 @@ class OdfFixer extends XMLFilterImpl {
@Override
public void endElement(@Nullable String uri, @Nullable String localName,
@Nullable String qName) throws SAXException {
super.endElement(uri, localName, qName);
final String family = familyStack.pop();
mute = mute && family == null;
if (!mute) {
super.endElement(uri, localName, qName);
}
}
@Override
public void characters(@Nullable char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
if (!mute) {
super.characters(ch, start, length);
}
}
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment