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

Pass around an object that keeps track of all names styles.

parent 1d25353e
package odf;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.annotation.Nullable;
public class NamedStylesLists {
final private Map<String, StyleNames> lists = new HashMap<String, StyleNames>();
@Nullable
StyleNames get(String entry) {
if (entry.endsWith("content.xml") || entry.endsWith("styles.xml")) {
return null;
}
StyleNames names = lists.get(entry);
if (names == null) {
StyleNames styles = null;
if (entry.endsWith("content.xml")) {
String stylesEntry = entry.substring(0, entry.length() - 11)
+ "styles.xml";
styles = lists.get(stylesEntry);
}
names = new StyleNames(styles);
lists.put(entry, names);
}
return names;
}
}
......@@ -12,12 +12,15 @@ import org.xml.sax.helpers.XMLFilterImpl;
class OdfFixer extends XMLFilterImpl {
final String version;
@Nullable
final private StyleNames styleNames;
final private Stack<String> familyStack = new Stack<String>();
private boolean mute = false; // whether or not to write output
OdfFixer(String version) {
OdfFixer(String version, @Nullable StyleNames styleNames) {
this.version = version;
this.styleNames = styleNames;
}
@Override
......
......@@ -20,8 +20,8 @@ public class OdfHistoryParser {
TransformerException {
FileInputStream in = new FileInputStream(historyFile.toFile());
odfNormalizer.odfInfo.version = "1.2";
Document historyDoc = odfNormalizer.xml
.parse(in, odfNormalizer.odfInfo);
Document historyDoc = odfNormalizer.xml.parse(in,
odfNormalizer.odfInfo, null);
LinkedList<OdfHistoryEntry> files = parseHistory(historyDoc);
return files;
}
......
......@@ -93,7 +93,7 @@ public class OdfInfo {
throws FileNotFoundException, SAXException, IOException,
TransformerException {
Document grammar = xml.parse(new FileInputStream(new File(rngPath)),
null);
null, null);
grammar.setDocumentURI(rngPath);
return new OdfRngInfo(grammar, xml);
}
......
......@@ -448,7 +448,7 @@ class OdfRngInfoParser {
}
String href = includeElement.getAttribute("href");
File includeFile = new File(dir, href);
Document doc = xml.parse(new FileInputStream(includeFile), null);
Document doc = xml.parse(new FileInputStream(includeFile), null, null);
n = doc.getDocumentElement().getFirstChild();
while (n != null) {
Node c = n.getNextSibling();
......
......@@ -12,6 +12,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
......@@ -21,6 +22,7 @@ import java.util.zip.ZipOutputStream;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerException;
import org.eclipse.jdt.annotation.Nullable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
......@@ -42,7 +44,8 @@ public class OdfZipNormalizer {
if (input.getAbsolutePath().endsWith(".rng")) {
info = odfNormalizer.rngInfo;
}
Document doc = odfNormalizer.xml.parse(in, info);
StyleNames styleNames = new StyleNames();
Document doc = odfNormalizer.xml.parse(in, info, styleNames);
in.close();
if (NC.rng.equals(doc.getDocumentElement().getNamespaceURI())) {
RngNormalizer.normalize(doc);
......@@ -85,10 +88,27 @@ public class OdfZipNormalizer {
addUncompressedEntry(out, mimetype.getBytes(), "mimetype");
writeXML(out, manifest, "META-INF/manifest.xml", odfNormalizer.odfInfo
.getRngInfo(odfNormalizer.xml).getElementsWithNoText());
NamedStylesLists styleNames = new NamedStylesLists();
// handle all entries, make sure to handle styles.xml before the
// corresponding content.xml so that named styles are known when
// handling content.xml
Set<String> handledFiles = new HashSet<String>();
for (String entry : entries) {
System.out.println(entry);
normalizeEntry(zip, zip.getEntry(entry), out, odfNormalizer);
if (entry.equals("content.xml") || entry.endsWith("/content.xml")) {
String stylesEntry = entry.substring(0, entry.length() - 11)
+ "styles.xml";
if (entries.contains(stylesEntry)
&& !handledFiles.contains(stylesEntry)) {
normalizeEntry(zip, zip.getEntry(stylesEntry), out,
odfNormalizer, styleNames.get(entry));
handledFiles.add(stylesEntry);
}
System.out.println(entry);
normalizeEntry(zip, zip.getEntry(entry), out, odfNormalizer,
styleNames.get(entry));
handledFiles.add(entry);
}
}
out.close();
......@@ -109,9 +129,9 @@ public class OdfZipNormalizer {
}
static private void normalizeEntry(ZipFile zip, ZipEntry entry,
ZipOutputStream out, OdfNormalizer odfNormalizer)
throws IOException, SAXException, TransformerException,
NormalizeException, XMLStreamException {
ZipOutputStream out, OdfNormalizer odfNormalizer,
@Nullable StyleNames styleNames) throws IOException, SAXException,
TransformerException, NormalizeException, XMLStreamException {
if (entry.isDirectory()) {
return;
}
......@@ -120,7 +140,7 @@ public class OdfZipNormalizer {
if (entry.getSize() == 0) {
copy(zip, entry, out);
} else {
normalizeXml(zip, entry, out, odfNormalizer);
normalizeXml(zip, entry, out, odfNormalizer, styleNames);
}
} else {
copy(zip, entry, out);
......@@ -128,12 +148,12 @@ public class OdfZipNormalizer {
}
static private void normalizeXml(ZipFile zip, ZipEntry entry,
ZipOutputStream out, OdfNormalizer normalizer) throws IOException,
SAXException, TransformerException, NormalizeException,
XMLStreamException {
ZipOutputStream out, OdfNormalizer normalizer,
@Nullable StyleNames styleNames) throws IOException, SAXException,
TransformerException, NormalizeException, XMLStreamException {
Path entryPath = Paths.get(entry.getName());
InputStream in = zip.getInputStream(entry);
Document doc = normalizer.xml.parse(in, normalizer.odfInfo);
Document doc = normalizer.xml.parse(in, normalizer.odfInfo, styleNames);
in.close();
if (entryPath.endsWith("content.xml")
|| entryPath.endsWith("styles.xml")
......@@ -189,7 +209,8 @@ public class OdfZipNormalizer {
throws IOException, SAXException, TransformerException {
InputStream in = zip.getInputStream(zip
.getEntry("META-INF/manifest.xml"));
Document doc = normalizer.xml.parse(in, normalizer.odfManifestInfo);
Document doc = normalizer.xml.parse(in, normalizer.odfManifestInfo,
null);
in.close();
return doc;
}
......
package odf;
import org.eclipse.jdt.annotation.Nullable;
public class StyleNames {
@Nullable
final private StyleNames styles;
StyleNames() {
this.styles = null;
}
StyleNames(@Nullable StyleNames styles) {
this.styles = styles;
}
}
......@@ -46,9 +46,11 @@ public class Tester {
FileType type = beforePath.toString().contains("/manifest/") ? OdfInfo.FileType.ODFMANIFEST
: OdfInfo.FileType.ODF;
OdfInfo odfInfo = new OdfInfo(type);
StyleNames styleNames = new StyleNames();
try {
doc = odfNormalizer.xml.parse(
new FileInputStream(beforePath.toFile()), odfInfo);
new FileInputStream(beforePath.toFile()), odfInfo,
styleNames);
} catch (IOException e) {
throw new TestFailException("Could not read test input.", e);
}
......
......@@ -65,8 +65,8 @@ public class XML {
return false;
}
public Document parse(InputStream in, @Nullable OdfInfo info)
throws IOException {
public Document parse(InputStream in, @Nullable OdfInfo info,
@Nullable StyleNames styleNames) throws IOException {
Document doc;
try {
XMLReader reader = parser.getXMLReader();
......@@ -81,7 +81,7 @@ public class XML {
unit.setParent(whitespace);
PrefixHandler prefix = new PrefixHandler();
prefix.setParent(unit);
OdfFixer f = new OdfFixer(info.version);
OdfFixer f = new OdfFixer(info.version, styleNames);
f.setParent(prefix);
reader = f;
}
......
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