use of javax.xml.transform.Result in project asciidoctor-fopub by asciidoctor.
the class InputHandler method renderTo.
/**
* Generate a document, given an initialized Fop object
* @param userAgent the user agent
* @param outputFormat the output format to generate (MIME type, see MimeConstants)
* @param out the output stream to write the generated output to (may be null if not applicable)
* @throws FOPException in case of an error during processing
*/
public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out) throws FOPException {
String baseURL = null;
try {
baseURL = new File(sourcefile.getAbsolutePath()).getParentFile().toURI().toURL().toExternalForm();
} catch (Exception e) {
baseURL = "";
}
FopFactory factory = new FopFactoryBuilder(URI.create(baseURL)).build();
Fop fop;
if (out != null) {
fop = factory.newFop(outputFormat, userAgent, out);
} else {
fop = factory.newFop(outputFormat, userAgent);
}
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
transformTo(res);
}
use of javax.xml.transform.Result in project gephi by gephi.
the class PresetUtils method savePreset.
public void savePreset(PreviewPreset preset) {
int exist = -1;
for (int i = 0; i < presets.size(); i++) {
PreviewPreset p = presets.get(i);
if (p.getName().equals(preset.getName())) {
exist = i;
break;
}
}
if (exist == -1) {
addPreset(preset);
} else {
presets.set(exist, preset);
}
try {
//Create file if dont exist
FileObject folder = FileUtil.getConfigFile("previewpresets");
if (folder == null) {
folder = FileUtil.getConfigRoot().createFolder("previewpresets");
}
FileObject presetFile = folder.getFileObject(preset.getName(), "xml");
if (presetFile == null) {
presetFile = folder.createData(preset.getName(), "xml");
}
//Create doc
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
final Document document = documentBuilder.newDocument();
document.setXmlVersion("1.0");
document.setXmlStandalone(true);
//Write doc
writeXML(document, preset);
//Write XML file
Source source = new DOMSource(document);
Result result = new StreamResult(FileUtil.toFile(presetFile));
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
use of javax.xml.transform.Result in project hazelcast by hazelcast.
the class AbstractXmlConfigHelper method schemaValidation.
protected void schemaValidation(Document doc) throws Exception {
ArrayList<StreamSource> schemas = new ArrayList<StreamSource>();
InputStream inputStream = null;
String schemaLocation = doc.getDocumentElement().getAttribute("xsi:schemaLocation");
schemaLocation = schemaLocation.replaceAll("^ +| +$| (?= )", "");
// get every two pair. every pair includes namespace and uri
String[] xsdLocations = schemaLocation.split("(?<!\\G\\S+)\\s");
for (String xsdLocation : xsdLocations) {
if (xsdLocation.isEmpty()) {
continue;
}
String namespace = xsdLocation.split('[' + LINE_SEPARATOR + " ]+")[0];
String uri = xsdLocation.split('[' + LINE_SEPARATOR + " ]+")[1];
// if this is hazelcast namespace but location is different log only warning
if (namespace.equals(xmlns) && !uri.endsWith(hazelcastSchemaLocation)) {
LOGGER.warning("Name of the hazelcast schema location incorrect using default");
}
// if this is not hazelcast namespace then try to load from uri
if (!namespace.equals(xmlns)) {
inputStream = loadSchemaFile(uri);
schemas.add(new StreamSource(inputStream));
}
}
// include hazelcast schema
schemas.add(new StreamSource(getClass().getClassLoader().getResourceAsStream(hazelcastSchemaLocation)));
// document to InputStream conversion
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(doc);
Result outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
// schema validation
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemas.toArray(new Source[schemas.size()]));
Validator validator = schema.newValidator();
try {
SAXSource source = new SAXSource(new InputSource(is));
validator.validate(source);
} catch (Exception e) {
throw new InvalidConfigurationException(e.getMessage());
} finally {
for (StreamSource source : schemas) {
closeResource(source.getInputStream());
}
closeResource(inputStream);
}
}
use of javax.xml.transform.Result in project jOOQ by jOOQ.
the class XMLasDOMBinding method toString.
// ------------------------------------------------------------------------
// The following logic originates from jOOX
// ------------------------------------------------------------------------
/**
* Transform an {@link Node} into a <code>String</code>.
*/
static final String toString(Node node) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Source source = new DOMSource(node);
Result target = new StreamResult(out);
transformer.transform(source, target);
return out.toString("UTF-8");
} catch (Exception e) {
return "[ ERROR IN toString() : " + e.getMessage() + " ]";
}
}
use of javax.xml.transform.Result in project spring-framework by spring-projects.
the class SourceHttpMessageConverter method writeInternal.
@Override
protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
try {
Result result = new StreamResult(outputMessage.getBody());
transform(t, result);
} catch (TransformerException ex) {
throw new HttpMessageNotWritableException("Could not transform [" + t + "] to output message", ex);
}
}
Aggregations