Permalink
Browse files

Add cx:ditaa and cx:plantuml steps

  • Loading branch information...
ndw committed Sep 22, 2014
1 parent 318f7cb commit 6740ba29a03e33d9eea32865d6a800f553af21fb
View
@@ -232,6 +232,8 @@
<src path="src"/>
<!-- Optional features -->
<exclude name="com/xmlcalabash/extensions/MetadataExtractor.java"/>
<exclude name="com/xmlcalabash/extensions/DiTAA.java"/>
<exclude name="com/xmlcalabash/extensions/PlantUML.java"/>
<exclude name="com/xmlcalabash/extensions/DeltaXML.java"/>
<exclude name="com/xmlcalabash/extensions/SendMail.java"/>
<exclude name="com/xmlcalabash/extensions/xmlunit/Compare.java"/>
@@ -286,7 +288,7 @@
</target>
<target name="optional-features"
depends="compile-MetadataException,
depends="compile-MetadataException,compile-DiTAA,compile-PlantUML,
compile-XEP,compile-FOP,compile-AH,compile-SendMail,
compile-DeltaXML,compile-XMLUnit,compile-XCC,
compile-RDF"/>
@@ -342,6 +344,40 @@
</javac>
</target>
<available classname="org.stathissideris.ascii2image.graphics.Diagram"
property="DiTAA.present">
<classpath refid="build.classpath"/>
</available>
<target name="compile-DiTAA" depends="init" if="DiTAA.present">
<javac destdir="${build.dir}"
classpathref="build.classpath"
source="${j.ver}" target="${j.ver}" includeantruntime="false">
<!--
<compilerarg value="-Xlint"/>
-->
<src path="src"/>
<include name="com/xmlcalabash/extensions/DiTAA.java"/>
</javac>
</target>
<available classname="net.sourceforge.plantuml.SourceStringReader"
property="PlantUML.present">
<classpath refid="build.classpath"/>
</available>
<target name="compile-PlantUML" depends="init" if="PlantUML.present">
<javac destdir="${build.dir}"
classpathref="build.classpath"
source="${j.ver}" target="${j.ver}" includeantruntime="false">
<!--
<compilerarg value="-Xlint"/>
-->
<src path="src"/>
<include name="com/xmlcalabash/extensions/PlantUML.java"/>
</javac>
</target>
<available classname="com.deltaxml.core.DXPConfiguration" property="DeltaXML.present">
<classpath refid="build.classpath"/>
</available>
@@ -154,6 +154,9 @@
<implementation type="cx:delta-xml"
class-name="com.xmlcalabash.extensions.DeltaXML"/>
<implementation type="cx:ditaa"
class-name="com.xmlcalabash.extensions.DiTAA"/>
<implementation type="cx:eval"
class-name="com.xmlcalabash.extensions.Eval"/>
@@ -181,6 +184,9 @@
<implementation type="cx:pretty-print"
class-name="com.xmlcalabash.extensions.PrettyPrint"/>
<implementation type="cx:plantuml"
class-name="com.xmlcalabash.extensions.PlantUML"/>
<implementation type="cx:metadata-extractor"
class-name="com.xmlcalabash.extensions.MetadataExtractor"/>
@@ -32,6 +32,17 @@
<p:output port="result"/>
</p:declare-step>
<p:declare-step type="cx:ditaa">
<p:input port="source"/>
<p:output port="result"/>
<p:option name="shadows" select="true()" cx:type="xsd:boolean"/>
<p:option name="antialias" select="true()" cx:type="xsd:boolean"/>
<p:option name="corners" select="'square'" cx:type="rounded|square"/>
<p:option name="separation" select="true()" cx:type="xsd:boolean"/>
<p:option name="scale" cx:type="xsd:float"/>
<p:option name="html" select="false()" cx:type="xsd:boolean"/>
</p:declare-step>
<p:declare-step type="cx:eval">
<p:input port="pipeline"/>
<p:input port="source" sequence="true"/>
@@ -113,6 +124,13 @@
<p:option name="assert-valid" select="'true'" cx:type="xsd:boolean"/>
</p:declare-step>
<p:declare-step type="cx:plantuml">
<p:input port="source"/>
<p:output port="result"/>
<p:option name="format" select="'png'" cx:type="png|svg"/>
<p:option name="html" select="false()" cx:type="xsd:boolean"/>
</p:declare-step>
<p:declare-step type="cx:pretty-print">
<p:input port="source"/>
<p:output port="result"/>
@@ -0,0 +1,138 @@
package com.xmlcalabash.extensions;
import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.io.ReadablePipe;
import com.xmlcalabash.io.WritablePipe;
import com.xmlcalabash.library.DefaultStep;
import com.xmlcalabash.runtime.XAtomicStep;
import com.xmlcalabash.util.Base64;
import com.xmlcalabash.util.TreeWriter;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
import org.stathissideris.ascii2image.core.ConversionOptions;
import org.stathissideris.ascii2image.graphics.BitmapRenderer;
import org.stathissideris.ascii2image.graphics.Diagram;
import org.stathissideris.ascii2image.text.TextGrid;
import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
/**
* Created by IntelliJ IDEA.
* User: ndw
* Date: Oct 8, 2008
* Time: 7:44:07 AM
* To change this template use File | Settings | File Templates.
*/
public class DiTAA extends DefaultStep {
private static final QName _shadows = new QName("", "shadows");
private static final QName _antialias = new QName("", "antialias");
private static final QName _corners = new QName("", "corners");
private static final QName _separation = new QName("", "separation");
private static final QName _scale = new QName("", "scale");
private static final QName _html = new QName("", "html");
private static final QName h_img = new QName("", "http://www.w3.org/1999/xhtml", "img");
private static final QName _src = new QName("", "src");
private ReadablePipe source = null;
private WritablePipe result = null;
private ConversionOptions options = null;
/**
* Creates a new instance of Identity
*/
public DiTAA(XProcRuntime runtime, XAtomicStep step) {
super(runtime, step);
}
public void setInput(String port, ReadablePipe pipe) {
source = pipe;
}
public void setOutput(String port, WritablePipe pipe) {
result = pipe;
}
public void reset() {
source.resetReader();
result.resetWriter();
}
public void run() throws SaxonApiException {
super.run();
String base64 = null;
boolean html = false;
try {
options = new ConversionOptions();
boolean shadows = getOption(_shadows, true);
boolean antialias = getOption(_antialias, true);
boolean roundedCorners = "rounded".equals(getOption(_corners, "rounded"));
boolean separation = getOption(_separation, true);
html = getOption(_html, false);
String scaleStr = getOption(_scale, (String) null);
if (scaleStr != null) {
float scale = Float.parseFloat(scaleStr);
options.renderingOptions.setScale(scale);
}
options.processingOptions.setCharacterEncoding("utf-8");
options.renderingOptions.setDropShadows(shadows);
options.processingOptions.setAllCornersAreRound(roundedCorners);
options.processingOptions.setPerformSeparationOfCommonEdges(separation);
options.renderingOptions.setAntialias(antialias);
TextGrid grid = new TextGrid();
if(options.processingOptions.getCustomShapes() != null){
grid.addToMarkupTags(options.processingOptions.getCustomShapes().keySet());
}
XdmNode doc = source.read();
String text = doc.getStringValue();
ArrayList<StringBuffer> lines = new ArrayList<StringBuffer>();
String[] linesArray = text.split("\n");
for (int i = 0; i < linesArray.length; i++) {
lines.add(new StringBuffer(linesArray[i]));
}
grid.initialiseWithLines(lines, options.processingOptions);
Diagram diagram = new Diagram(grid, options);
RenderedImage image = new BitmapRenderer().renderToImage(diagram, options.renderingOptions);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
base64 = Base64.encodeBytes(baos.toByteArray(), 0, baos.size());
} catch (Exception e) {
throw new XProcException(e);
}
TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(step.getNode().getBaseURI());
if (html) {
tree.addStartElement(h_img);
tree.addAttribute(_src, "data:image/png;base64," + base64);
tree.startContent();
tree.addEndElement();
} else {
tree.addStartElement(XProcConstants.c_data);
tree.startContent();
tree.addText("data:image/png;base64," + base64);
tree.addEndElement();
}
tree.endDocument();
result.write(tree.getResult());
}
}
@@ -0,0 +1,117 @@
package com.xmlcalabash.extensions;
import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.io.ReadablePipe;
import com.xmlcalabash.io.WritablePipe;
import com.xmlcalabash.library.DefaultStep;
import com.xmlcalabash.runtime.XAtomicStep;
import com.xmlcalabash.util.Base64;
import com.xmlcalabash.util.TreeWriter;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
import org.xml.sax.InputSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* Created by IntelliJ IDEA.
* User: ndw
* Date: Oct 8, 2008
* Time: 7:44:07 AM
* To change this template use File | Settings | File Templates.
*/
public class PlantUML extends DefaultStep {
private static final QName _format = new QName("", "format");
private static final QName _html = new QName("", "html");
private static final QName h_img = new QName("", "http://www.w3.org/1999/xhtml", "img");
private static final QName _src = new QName("", "src");
private ReadablePipe source = null;
private WritablePipe result = null;
/**
* Creates a new instance of Identity
*/
public PlantUML(XProcRuntime runtime, XAtomicStep step) {
super(runtime, step);
}
public void setInput(String port, ReadablePipe pipe) {
source = pipe;
}
public void setOutput(String port, WritablePipe pipe) {
result = pipe;
}
public void reset() {
source.resetReader();
result.resetWriter();
}
public void run() throws SaxonApiException {
super.run();
boolean html = getOption(_html, false);
String format = getOption(_format, "png");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
XdmNode doc = source.read();
String text = doc.getStringValue();
SourceStringReader reader = new SourceStringReader(text);
FileFormat fmt = "svg".equals(format) ? FileFormat.SVG : FileFormat.PNG;
String desc = reader.generateImage(baos, new FileFormatOption(fmt));
if (desc == null) {
throw new XProcException("PlantUML diagram returned null");
}
} catch (XProcException e) {
throw e;
} catch (Exception e) {
throw new XProcException(e);
}
TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(step.getNode().getBaseURI());
if ("svg".equals(format)) {
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray(), 0, baos.size());
XdmNode svg = runtime.parse(new InputSource(bais));
if (html) {
// Perhaps I should remove the SVG namespace and do other stuff here?
tree.addSubtree(svg);
} else {
tree.addSubtree(svg);
}
} else {
String base64 = Base64.encodeBytes(baos.toByteArray(), 0, baos.size());
if (html) {
tree.addStartElement(h_img);
tree.addAttribute(_src, "data:image/png;base64," + base64);
tree.startContent();
tree.addEndElement();
} else {
tree.addStartElement(XProcConstants.c_data);
tree.startContent();
tree.addText("data:image/png;base64," + base64);
tree.addEndElement();
}
}
tree.endDocument();
result.write(tree.getResult());
}
}

0 comments on commit 6740ba2

Please sign in to comment.