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

Work around for bibliography style bug in LO.

parent b6d53141
......@@ -17,6 +17,8 @@ class OdfFixer extends XMLFilterImpl {
private int muteLevel = -1;
private boolean automaticStyles;
private int level = 0;
final private Stack<String> familyStack = new Stack<String>();
......@@ -31,6 +33,7 @@ class OdfFixer extends XMLFilterImpl {
super.startDocument();
familyStack.clear();
muteLevel = -1;
automaticStyles = false;
}
@Override
......@@ -43,11 +46,15 @@ class OdfFixer extends XMLFilterImpl {
}
level += 1;
automaticStyles = automaticStyles
|| (NC.style.equals(uri) && "automatic-styles"
.equals(localName));
// correct invalid attributes
AttributesImpl atts = new AttributesImpl(attributes);
int l = atts.getLength();
String family = null;
String styleName = null;
for (int i = 0; i < l; ++i) {
String u = atts.getURI(i);
String ln = atts.getLocalName(i);
......@@ -66,6 +73,8 @@ class OdfFixer extends XMLFilterImpl {
atts.removeAttribute(i--);
} else if ("family".equals(ln)) {
family = v;
} else if ("name".equals(ln)) {
styleName = v;
}
} else if (NC.fo.equals(u)) {
if ("hyphenation-push-char-count".equals(ln)) {
......@@ -99,6 +108,7 @@ class OdfFixer extends XMLFilterImpl {
}
if (NC.text.equals(uri)) {
if ("outline-style".equals(localName)) {
// this seems wrong
int i = atts.getIndex(NC.style, "name");
if ("1.2".equals(version)) {
if (i == -1) {
......@@ -135,6 +145,9 @@ class OdfFixer extends XMLFilterImpl {
&& "paragraph".equals(familyStack.lastElement())) {
skipElement();
}
if (family != null && styleName != null && styleNames != null) {
styleNames.addName(styleName, family, automaticStyles);
}
familyStack.push(family);
if (muteLevel == -1) {
super.startElement(uri, localName, qName, atts);
......
......@@ -15,6 +15,8 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.Nullable;
import org.w3c.dom.Attr;
......@@ -373,6 +375,7 @@ public class OdfNormalizer {
remove(doc, "//text:alphabetical-index-mark-end");
remove(doc, "//text:toc-mark-start");
remove(doc, "//text:toc-mark-end");
remove(doc, "//@text:formula[starts-with(.,'ooow:')]");
// remove continued list for which the first list item explicitly starts
// at position 1
......@@ -382,6 +385,8 @@ public class OdfNormalizer {
removeUnneededFontAttributes(doc);
fixBibliographyStyles(doc);
IdAndReferenceNormalizer n = new IdAndReferenceNormalizer(doc);
// fonts
n.addIdsAndReferences("F", "//style:font-face/@style:name",
......@@ -654,6 +659,30 @@ public class OdfNormalizer {
&& marks.contains(n.getLocalName());
}
final Matcher matcher = Pattern.compile("^_[0-9a-f][0-9a-f]__20_(.*)$")
.matcher("");
// workaround for bug in LO 5.1.2 where text:style-name="Strong_20_Emphasis"
// is saved as text:style-name="_30__20_Strong_20_Emphasis" and new
// attributes text:style-name="_30__20_" are introduces. The '_30_' can
// vary.
private void fixBibliographyStyles(Document doc) {
// TODO check with list of defined styles
XPathResult<Attr> atts = XPath.attrIterator(doc,
"//text:index-entry-bibliography/@text:style-name");
for (Attr att : atts) {
matcher.reset(att.getNodeValue());
if (matcher.find()) {
String newName = matcher.group(1);
if (newName.length() > 0) {
att.setNodeValue(newName);
} else {
att.getOwnerElement().removeAttributeNode(att);
}
}
}
}
static private void fixBookmarkStart(Document document) {
// replace each text:bookmark that has a corresponding text:bookmark-end
// by text:bookmark-start
......
package odf;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.jdt.annotation.Nullable;
public class StyleNames {
......@@ -7,6 +12,9 @@ public class StyleNames {
@Nullable
final private StyleNames styles;
final private Map<String, Set<String>> common = new HashMap<String, Set<String>>();
final private Map<String, Set<String>> automatic = new HashMap<String, Set<String>>();
StyleNames() {
this.styles = null;
}
......@@ -15,4 +23,36 @@ public class StyleNames {
this.styles = styles;
}
boolean addName(String name, String family, boolean automaticStyle) {
Map<String, Set<String>> m = automaticStyle ? automatic : common;
Set<String> s = m.get(family);
if (s == null) {
s = new HashSet<String>();
m.put(family, s);
}
boolean has = s.contains(name);
s.add(name);
return has;
}
boolean hasCommonStyle(String name, String family) {
Set<String> s = common.get(family);
return s == null ? false : s.contains(name);
}
boolean hasStyle(String name, String family) {
Set<String> s = common.get(family);
if (s != null && s.contains(name)) {
return true;
}
s = automatic.get(family);
if (s != null && s.contains(name)) {
return true;
}
if (styles != null) {
return styles.hasCommonStyle(name, family);
}
return false;
}
}
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