Commit b6d53141 authored by Jos van den Oever's avatar Jos van den Oever

Make the muting mechanism more general.

parent bca1aa32
......@@ -15,8 +15,11 @@ class OdfFixer extends XMLFilterImpl {
@Nullable
final private StyleNames styleNames;
private int muteLevel = -1;
private int level = 0;
final private Stack<String> familyStack = new Stack<String>();
private boolean mute = false; // whether or not to write output
OdfFixer(String version, @Nullable StyleNames styleNames) {
this.version = version;
......@@ -27,7 +30,7 @@ class OdfFixer extends XMLFilterImpl {
public void startDocument() throws SAXException {
super.startDocument();
familyStack.clear();
mute = false;
muteLevel = -1;
}
@Override
......@@ -39,6 +42,8 @@ class OdfFixer extends XMLFilterImpl {
throw new NullPointerException();
}
level += 1;
// correct invalid attributes
AttributesImpl atts = new AttributesImpl(attributes);
int l = atts.getLength();
......@@ -128,14 +133,20 @@ class OdfFixer extends XMLFilterImpl {
&& (NC.style.equals(uri) || "".equals(uri))
&& familyStack.size() > 0
&& "paragraph".equals(familyStack.lastElement())) {
mute = true;
skipElement();
}
familyStack.push(family);
if (!mute) {
if (muteLevel == -1) {
super.startElement(uri, localName, qName, atts);
}
}
private void skipElement() {
if (muteLevel == -1) {
muteLevel = level;
}
}
private static String fixPositiveInteger(String v) {
String nv = v;
try {
......@@ -152,19 +163,20 @@ class OdfFixer extends XMLFilterImpl {
@Override
public void endElement(@Nullable String uri, @Nullable String localName,
@Nullable String qName) throws SAXException {
if (!mute) {
if (muteLevel == -1) {
super.endElement(uri, localName, qName);
}
familyStack.pop();
final String family = (familyStack.size() == 0) ? null : familyStack
.peek();
mute = mute && family == null;
level -= 1;
if (muteLevel > level) {
muteLevel = -1;
}
}
@Override
public void characters(@Nullable char[] ch, int start, int length)
throws SAXException {
if (!mute) {
if (muteLevel == -1) {
super.characters(ch, start, length);
}
}
......
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