Commit 27cd29aa authored by Jos van den Oever's avatar Jos van den Oever

Check that a style exists before applying it.

parent c5bbcc51
......@@ -10,7 +10,7 @@ public class NamedStylesLists {
@Nullable
StyleNames get(String entry) {
if (entry.endsWith("content.xml") || entry.endsWith("styles.xml")) {
if (!entry.endsWith("content.xml") && !entry.endsWith("styles.xml")) {
return null;
}
StyleNames names = lists.get(entry);
......
......@@ -47,7 +47,7 @@ class OdfFixer extends XMLFilterImpl {
level += 1;
automaticStyles = automaticStyles
|| (NC.style.equals(uri) && "automatic-styles"
|| (NC.office.equals(uri) && "automatic-styles"
.equals(localName));
// correct invalid attributes
......
......@@ -343,7 +343,8 @@ public class OdfNormalizer {
}
}
public void normalize(Document doc) throws NormalizeException {
public void normalize(Document doc, StyleNames styleNames)
throws NormalizeException {
final Element documentElement = doc.getDocumentElement();
if (documentElement.getNamespaceURI().equals(NC.manifest)) {
cleanManifest(doc);
......@@ -385,8 +386,8 @@ public class OdfNormalizer {
removeUnneededFontAttributes(doc);
fixBibliographyStyles(doc);
setLinkStyle(doc);
fixBibliographyStyles(doc, styleNames);
addLinkStyle(doc, styleNames);
IdAndReferenceNormalizer n = new IdAndReferenceNormalizer(doc);
// fonts
......@@ -667,15 +668,15 @@ public class OdfNormalizer {
// 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
private void fixBibliographyStyles(Document doc, StyleNames styleNames) {
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) {
if (newName.length() > 0
&& styleNames.hasStyle(newName, "text")) {
att.setNodeValue(newName);
} else {
att.getOwnerElement().removeAttributeNode(att);
......@@ -686,13 +687,16 @@ public class OdfNormalizer {
// workaround for LO behaviour where text:a without a style gets a default
// style
private void setLinkStyle(Document doc) {
private void addLinkStyle(Document doc, StyleNames styleNames) {
XPathResult<Element> es = XPath.elementIterator(doc, "//text:a");
for (Element e : es) {
if (!e.hasAttributeNS(NC.text, "style-name")) {
if (!e.hasAttributeNS(NC.text, "style-name")
&& styleNames.hasStyle("Visited_20_Internet_20_Link",
"text")) {
e.setAttributeNS(NC.text, "text:style-name", "Internet_20_link");
}
if (!e.hasAttributeNS(NC.text, "visited-style-name")) {
if (!e.hasAttributeNS(NC.text, "visited-style-name")
&& styleNames.hasStyle("Internet_20_link", "text")) {
e.setAttributeNS(NC.text, "text:visited-style-name",
"Visited_20_Internet_20_Link");
}
......
......@@ -50,7 +50,7 @@ public class OdfZipNormalizer {
if (NC.rng.equals(doc.getDocumentElement().getNamespaceURI())) {
RngNormalizer.normalize(doc);
} else {
normalizeOdf(doc, odfNormalizer);
normalizeOdf(doc, odfNormalizer, styleNames);
}
OdfHistory.pretty(doc, output, info.getRngInfo(odfNormalizer.xml)
.getElementsWithNoText());
......@@ -161,19 +161,22 @@ public class OdfZipNormalizer {
if (entryPath.endsWith("content.xml")
|| entryPath.endsWith("styles.xml")
|| entryPath.endsWith("meta.xml")) {
normalizeOdf(doc, normalizer);
normalizeOdf(doc, normalizer, styleNames);
}
writeXML(out, doc, entry.getName(),
normalizer.odfInfo.getRngInfo(normalizer.xml)
.getElementsWithNoText());
}
static private void normalizeOdf(Document doc, OdfNormalizer normalizer)
throws TransformerException, IOException, SAXException,
NormalizeException, XMLStreamException {
static private void normalizeOdf(Document doc, OdfNormalizer normalizer,
@Nullable StyleNames styleNames) throws TransformerException,
IOException, SAXException, NormalizeException, XMLStreamException {
if (styleNames == null) {
styleNames = new StyleNames();
}
if (NC.office.equals(doc.getDocumentElement().getNamespaceURI())) {
validate(doc, normalizer.odfInfo);
normalizer.normalize(doc);
normalizer.normalize(doc, styleNames);
validate(doc, normalizer.odfInfo);
}
}
......
......@@ -57,7 +57,7 @@ public class Tester {
byte result[];
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
odfNormalizer.normalize(doc);
odfNormalizer.normalize(doc, styleNames);
OdfHistory.pretty(doc, out, odfInfo.getRngInfo(odfNormalizer.xml)
.getElementsWithNoText());
out.close();
......
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