Commit 1d25353e authored by Jos van den Oever's avatar Jos van den Oever

Merge subsequent text:spans with idential styles.

parent 5f394c36
......@@ -554,8 +554,9 @@ public class OdfNormalizer {
n.normalize();
sortAutomaticStyles(doc);
fixBookmarkStart(doc, xml);
fixBookmarkStart(doc);
fixReferenceMarkOrder(doc, xml);
mergeSpans(doc);
NamespaceCleaner.clean(doc);
}
......@@ -576,7 +577,7 @@ public class OdfNormalizer {
&& marks.contains(n.getLocalName());
}
static private void fixBookmarkStart(Document document, XML xml) {
static private void fixBookmarkStart(Document document) {
// replace each text:bookmark that has a corresponding text:bookmark-end
// by text:bookmark-start
XPathResult<Element> elements = XPath.elementIterator(document,
......@@ -647,6 +648,46 @@ public class OdfNormalizer {
}
}
static private boolean compareSpans(Element span, @Nullable Node n) {
if (n == null || n.getNodeType() != Node.ELEMENT_NODE) {
return false;
}
Element e = (Element) n;
if (!span.getNamespaceURI().equals(e.getNamespaceURI())
|| !span.getLocalName().equals(e.getLocalName())) {
return false;
}
if (!span.getAttributeNodeNS(NC.text, "style-name").equals(
e.getAttributeNodeNS(NC.text, "style-name"))
|| !span.getAttributeNodeNS(NC.text, "class-names").equals(
e.getAttributeNodeNS(NC.text, "class-names"))) {
return false;
}
return true;
}
static private void mergeSpans(Document document) {
XPathResult<Element> spans = XPath.elementIterator(document,
"//text:span");
List<Element> tomerge = new LinkedList<Element>();
for (Element s : spans) {
Node n = s.getNextSibling();
if (compareSpans(s, n)) {
tomerge.add((Element) n);
}
}
for (int i = tomerge.size() - 1; i >= 0; i--) {
Element e = tomerge.get(i);
Node p = e.getPreviousSibling();
e.getParentNode().removeChild(e);
Node n = e.getFirstChild();
while (n != null) {
p.appendChild(n);
n = e.getFirstChild();
}
}
}
static private void remove(Node node, String xpath) {
for (Node n : XPath.nodeIterator(node, xpath)) {
if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
......
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