Permalink
Browse files

Support using the Validator.nu serializer for HTML; fixes issue #150

  • Loading branch information...
ndw committed May 7, 2014
1 parent e6e1c50 commit 5849540f6cce088e87302251b3e6a234c04fe085
@@ -105,6 +105,7 @@
public boolean transparentJSON = false;
public String jsonFlavor = JSONtoXML.MARKLOGIC;
public boolean useXslt10 = false;
+ public boolean htmlSerializer = false;
public int piperackPort = 8088;
public int piperackDefaultExpires = 300;
@@ -317,6 +318,7 @@ private void loadConfiguration() {
safeMode = "true".equals(System.getProperty("com.xmlcalabash.safe-mode", ""+safeMode));
jsonFlavor = System.getProperty("com.xmlcalabash.json-flavor", jsonFlavor);
useXslt10 = "true".equals(System.getProperty("com.xmlcalabash.use-xslt-10", ""+useXslt10));
+ htmlSerializer = "true".equals(System.getProperty("com.xmlcalabash.html-serializer", ""+htmlSerializer));
entityResolver = System.getProperty("com.xmlcalabash.entity-resolver", entityResolver);
uriResolver = System.getProperty("com.xmlcalabash.uri-resolver", uriResolver);
errorListener = System.getProperty("com.xmlcalabash.error-listener", errorListener);
@@ -649,6 +651,8 @@ private void parseExtension(XdmNode node) {
}
} else if ("use-xslt-1.0".equals(name) || "use-xslt-10".equals(name)) {
useXslt10 = "true".equals(value);
+ } else if ("html-serializer".equals(name)) {
+ htmlSerializer = "true".equals(value);
} else {
throw new XProcException("Unrecognized extension in configuration: " + name);
}
@@ -133,6 +133,7 @@
private boolean transparentJSON = false;
private String jsonFlavor = JSONtoXML.MARKLOGIC;
private boolean useXslt10 = false;
+ private boolean htmlSerializer = false;
private XProcData xprocData = null;
private Logger log = null;
private XProcMessageListener msgListener = null;
@@ -220,6 +221,7 @@ public XProcRuntime(XProcConfiguration config) {
transparentJSON = config.transparentJSON;
jsonFlavor = config.jsonFlavor;
useXslt10 = config.useXslt10;
+ htmlSerializer = config.htmlSerializer;
if (config.profile != null) {
profile = config.profile;
@@ -260,6 +262,7 @@ public XProcRuntime(XProcRuntime runtime) {
config = runtime.config;
staticBaseURI = runtime.staticBaseURI;
useXslt10 = runtime.useXslt10;
+ htmlSerializer = runtime.htmlSerializer;
log = runtime.log;
msgListener = runtime.msgListener;
standardLibrary = runtime.standardLibrary;
@@ -437,6 +440,10 @@ public boolean getUseXslt10Processor() {
return useXslt10;
}
+ public boolean getHtmlSerializer() {
+ return htmlSerializer;
+ }
+
public void cache(XdmNode doc, URI baseURI) {
uriResolver.cache(doc, baseURI);
}
@@ -131,6 +131,8 @@ public Sequence call(XPathContext xPathContext, Sequence[] sequences) throws XPa
value = runtime.getAllowXPointerOnText() ? "true" : "false";
} else if ("use-xslt-1.0".equals(local) || "use-xslt-10".equals(local)) {
value = runtime.getUseXslt10Processor() ? "true" : "false";
+ } else if ("html-serializer".equals(local)) {
+ value = runtime.getHtmlSerializer() ? "true" : "false";
} else if ("saxon-version".equals(local)) {
value = runtime.getConfiguration().getProcessor().getSaxonProductVersion();
} else if ("saxon-edition".equals(local)) {
@@ -151,6 +151,8 @@ public UserArgs parse(String[] args) {
userArgs.setAllowXPointerOnText(true);
} else if ("use-xslt-1.0".equals(ext) || "use-xslt-10".equals(ext)) {
userArgs.setUseXslt10(true);
+ } else if ("html-serializer".equals(ext)) {
+ userArgs.setHtmlSerializer(true);
} else if ("transparent-json".equals(ext)) {
userArgs.setTransparentJSON(true);
} else if (ext.startsWith("json-flavor=")) {
@@ -27,6 +27,7 @@
import net.sf.saxon.s9api.Destination;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
+import net.sf.saxon.s9api.SAXDestination;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.XPathCompiler;
@@ -51,12 +52,19 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.io.Writer;
import java.util.Iterator;
import java.util.Vector;
import java.util.HashSet;
import java.net.URI;
import net.sf.saxon.tree.util.NamespaceIterator;
+import nu.validator.htmlparser.sax.HtmlSerializer;
+import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
/**
@@ -163,7 +171,35 @@ public static void serialize(XProcRuntime xproc, Vector<XdmNode> nodes, Serializ
XQueryExecutable xqexec = xqcomp.compile(".");
XQueryEvaluator xqeval = xqexec.load();
- xqeval.setDestination(serializer);
+ if (xproc.getHtmlSerializer() && "html".equals(serializer.getOutputProperty(Serializer.Property.METHOD))) {
+ ContentHandler ch = null;
+ Object outputDest = serializer.getOutputDestination();
+
+ if (outputDest == null) {
+ //???
+ xqeval.setDestination(serializer);
+ } else if (outputDest instanceof OutputStream) {
+ ch = new HtmlSerializer((OutputStream) outputDest);
+ xqeval.setDestination(new SAXDestination(ch));
+ } else if (outputDest instanceof Writer) {
+ ch = new HtmlSerializer((Writer) outputDest);
+ xqeval.setDestination(new SAXDestination(ch));
+ } else if (outputDest instanceof File) {
+ try {
+ FileOutputStream fos = new FileOutputStream((File) outputDest);
+ ch = new HtmlSerializer(fos);
+ xqeval.setDestination(new SAXDestination(ch));
+ } catch (FileNotFoundException fnfe) {
+ xqeval.setDestination(serializer);
+ }
+ } else {
+ //???
+ xqeval.setDestination(serializer);
+ }
+ } else {
+ xqeval.setDestination(serializer);
+ }
+
for (XdmNode node : nodes) {
xqeval.setContextItem(node);
xqeval.run();
@@ -86,6 +86,7 @@
protected boolean extensionValues = false;
protected boolean allowXPointerOnText = false;
protected boolean useXslt10 = false;
+ protected boolean htmlSerializer = false;
protected boolean transparentJSON = false;
protected String jsonFlavor = null;
protected Integer piperackPort = null;
@@ -407,6 +408,10 @@ public void setUseXslt10(boolean useXslt10) {
this.useXslt10 = useXslt10;
}
+ public void setHtmlSerializer(boolean htmlSerializer) {
+ this.htmlSerializer = htmlSerializer;
+ }
+
public void setTransparentJSON(boolean transparentJSON) {
this.transparentJSON = transparentJSON;
}
@@ -552,6 +557,7 @@ public XProcConfiguration createConfiguration() throws SaxonApiException {
config.jsonFlavor = jsonFlavor;
}
config.useXslt10 |= useXslt10;
+ config.htmlSerializer |= htmlSerializer;
if (piperackPort != null) {
config.piperackPort = piperackPort;

0 comments on commit 5849540

Please sign in to comment.