use of javax.xml.transform.stream.StreamResult in project midpoint by Evolveum.
the class DomToSchemaProcessor method parseSchema.
private XSSchemaSet parseSchema(Element schema) throws SchemaException {
// Make sure that the schema parser sees all the namespace declarations
DOMUtil.fixNamespaceDeclarations(schema);
XSSchemaSet xss = null;
try {
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(schema);
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult result = new StreamResult(out);
trans.transform(source, result);
XSOMParser parser = createSchemaParser();
InputSource inSource = new InputSource(new ByteArrayInputStream(out.toByteArray()));
// XXX: hack: it's here to make entity resolver work...
inSource.setSystemId("SystemId");
// XXX: end hack
inSource.setEncoding("utf-8");
parser.parse(inSource);
xss = parser.getResult();
} catch (SAXException e) {
throw new SchemaException("XML error during XSD schema parsing: " + e.getMessage() + "(embedded exception " + e.getException() + ") in " + shortDescription, e);
} catch (TransformerException e) {
throw new SchemaException("XML transformer error during XSD schema parsing: " + e.getMessage() + "(locator: " + e.getLocator() + ", embedded exception:" + e.getException() + ") in " + shortDescription, e);
} catch (RuntimeException e) {
// This sometimes happens, e.g. NPEs in Saxon
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Unexpected error {} during parsing of schema:\n{}", e.getMessage(), DOMUtil.serializeDOMToString(schema));
}
throw new SchemaException("XML error during XSD schema parsing: " + e.getMessage() + " in " + shortDescription, e);
}
return xss;
}
use of javax.xml.transform.stream.StreamResult in project midpoint by Evolveum.
the class DOMUtil method printDom.
public static StringBuffer printDom(Node node, boolean indent, boolean omitXmlDeclaration) {
StringWriter writer = new StringWriter();
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans;
try {
trans = transfac.newTransformer();
} catch (TransformerConfigurationException e) {
throw new SystemException("Error in XML configuration: " + e.getMessage(), e);
}
trans.setOutputProperty(OutputKeys.INDENT, (indent ? "yes" : "no"));
// XALAN-specific
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
trans.setParameter(OutputKeys.ENCODING, "utf-8");
// Note: serialized XML does not contain xml declaration
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, (omitXmlDeclaration ? "yes" : "no"));
DOMSource source = new DOMSource(node);
try {
trans.transform(source, new StreamResult(writer));
} catch (TransformerException e) {
throw new SystemException("Error in XML transformation: " + e.getMessage(), e);
}
return writer.getBuffer();
}
use of javax.xml.transform.stream.StreamResult in project goci by EBISPOT.
the class ChromosomeRenderlet method render.
public void render(RenderletNexus nexus, C context, E entity) {
int position = getPosition();
int height = SVGCanvas.canvasHeight;
int width = SVGCanvas.canvasWidth;
double chromWidth = (double) width / 12;
double chromHeight = (double) height / 2;
double xCoordinate;
double yCoordinate = 0;
if (position < 12) {
xCoordinate = position * chromWidth;
} else {
xCoordinate = (position - 12) * chromWidth;
yCoordinate = (double) height / 2;
}
InputStream in = null;
try {
in = getSVGFile().openStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document chromSVG = db.parse(in);
Element root = chromSVG.getDocumentElement();
Element g = (Element) root.getElementsByTagName("g").item(0).cloneNode(true);
setChromBands(g, nexus);
StringBuilder builder = new StringBuilder();
builder.append("translate(");
builder.append(Double.toString(xCoordinate));
builder.append(",");
builder.append(Double.toString(yCoordinate));
builder.append(")");
g.setAttribute("transform", builder.toString());
g.setAttribute("gwasname", getName());
g.removeAttribute("title");
SVGArea currentArea = new SVGArea(xCoordinate, yCoordinate, chromWidth, chromHeight, 0);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(g), new StreamResult(buffer));
String str = buffer.toString();
RenderingEvent<E> event = new RenderingEvent<E>(entity, str, currentArea, this);
nexus.renderingEventOccurred(event);
} catch (ParserConfigurationException e) {
throw new RuntimeException("Failed to read in template chromosome SVG from original resource", e);
} catch (SAXException e) {
throw new RuntimeException("Failed to read in template chromosome SVG from original resource", e);
} catch (IOException e) {
throw new RuntimeException("Failed to render chromosome SVG", e);
} catch (TransformerConfigurationException e) {
throw new RuntimeException("Failed to render chromosome SVG", e);
} catch (TransformerException e) {
throw new RuntimeException("Failed to render chromosome SVG", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// tried our best!
}
}
}
}
use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.
the class Test method toString.
private static String toString(Source response) throws TransformerException, IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(response, new StreamResult(bos));
bos.close();
return new String(bos.toByteArray());
}
use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.
the class XSLTExFuncTest method testTemplatesEnableExtFunc.
/**
* use Templates template = factory.newTemplates(new StreamSource( new
* FileInputStream(xslFilename))); // Use the template to create a
* transformer Transformer xformer = template.newTransformer();
*
* @param factory
* @return
*/
/**
* Security is enabled, use new feature: enableExtensionFunctions Use the
* template to create a transformer
*/
public void testTemplatesEnableExtFunc() {
Policy p = new SimplePolicy(new AllPermission());
Policy.setPolicy(p);
System.setSecurityManager(new SecurityManager());
TransformerFactory factory = TransformerFactory.newInstance();
/**
* Use of the extension function 'http://exslt.org/strings:tokenize' is
* not allowed when the secure processing feature is set to true.
* Attempt to use the new property to enable extension function
*/
boolean isExtensionSupported = enableExtensionFunction(factory);
try {
SAXSource xslSource = new SAXSource(new InputSource(xslFile));
xslSource.setSystemId(xslFileId);
Templates template = factory.newTemplates(xslSource);
Transformer transformer = template.newTransformer();
StringWriter stringResult = new StringWriter();
Result result = new StreamResult(stringResult);
transformer.transform(new SAXSource(new InputSource(xmlFile)), result);
System.out.println("testTemplatesEnableExtFunc: OK");
} catch (TransformerConfigurationException e) {
fail(e.getMessage());
} catch (TransformerException e) {
fail(e.getMessage());
} finally {
System.setSecurityManager(null);
}
}
Aggregations