use of javax.xml.transform.Source in project hazelcast by hazelcast.
the class ClientDiscoverySpiTest method testSchema.
@Test
public void testSchema() throws Exception {
String xmlFileName = "hazelcast-client-discovery-spi-test.xml";
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaResource = ClientDiscoverySpiTest.class.getClassLoader().getResource("hazelcast-client-config-3.9.xsd");
Schema schema = factory.newSchema(schemaResource);
InputStream xmlResource = ClientDiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
Source source = new StreamSource(xmlResource);
Validator validator = schema.newValidator();
validator.validate(source);
}
use of javax.xml.transform.Source in project hazelcast by hazelcast.
the class ConfigXmlGenerator method format.
private String format(final String input, int indent) {
if (!formatted) {
return input;
}
StreamResult xmlOutput = null;
try {
final Source xmlInput = new StreamSource(new StringReader(input));
xmlOutput = new StreamResult(new StringWriter());
TransformerFactory transformerFactory = TransformerFactory.newInstance();
/* Older versions of Xalan still use this method of setting indent values.
* Attempt to make this work but don't completely fail if it's a problem.
*/
try {
transformerFactory.setAttribute("indent-number", indent);
} catch (IllegalArgumentException e) {
if (LOGGER.isFinestEnabled()) {
LOGGER.finest("Failed to set indent-number attribute; cause: " + e.getMessage());
}
}
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
/* Newer versions of Xalan will look for a fully-qualified output property in order to specify amount of
* indentation to use. Attempt to make this work as well but again don't completely fail if it's a problem.
*/
try {
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
} catch (IllegalArgumentException e) {
if (LOGGER.isFinestEnabled()) {
LOGGER.finest("Failed to set indent-amount property; cause: " + e.getMessage());
}
}
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (Exception e) {
LOGGER.warning(e);
return input;
} finally {
if (xmlOutput != null) {
closeResource(xmlOutput.getWriter());
}
}
}
use of javax.xml.transform.Source in project gephi by gephi.
the class LayoutPresetPersistence method savePreset.
public void savePreset(String name, Layout layout) {
Preset preset = addPreset(new Preset(name, layout));
FileOutputStream fos = null;
try {
//Create file if dont exist
FileObject folder = FileUtil.getConfigFile("layoutpresets");
if (folder == null) {
folder = FileUtil.getConfigRoot().createFolder("layoutpresets");
}
File presetFile = new File(FileUtil.toFile(folder), name + ".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
preset.writeXML(document);
//Write XML file
fos = new FileOutputStream(presetFile);
Source source = new DOMSource(document);
Result result = new StreamResult(fos);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
} catch (Exception e) {
Logger.getLogger("").log(Level.SEVERE, "Error while writing preset file", e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
}
}
}
}
use of javax.xml.transform.Source in project gephi by gephi.
the class ExporterGraphML method transform.
private void transform(Document document) throws TransformerConfigurationException, TransformerException {
Source source = new DOMSource(document);
Result result = new StreamResult(writer);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
}
use of javax.xml.transform.Source in project j2objc by google.
the class ProcessorInclude method startElement.
/**
* Receive notification of the start of an xsl:include element.
*
* @param handler The calling StylesheetHandler/TemplatesBuilder.
* @param uri The Namespace URI, or the empty string if the
* element has no Namespace URI or if Namespace
* processing is not being performed.
* @param localName The local name (without prefix), or the
* empty string if Namespace processing is not being
* performed.
* @param rawName The raw XML 1.0 name (with prefix), or the
* empty string if raw names are not available.
* @param attributes The attributes attached to the element. If
* there are no attributes, it shall be an empty
* Attributes object.
*
* @throws org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
*/
public void startElement(StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) throws org.xml.sax.SAXException {
setPropertiesFromAttributes(handler, rawName, attributes, this);
try {
// Get the Source from the user's URIResolver (if any).
Source sourceFromURIResolver = getSourceFromUriResolver(handler);
// Get the system ID of the included/imported stylesheet module
String hrefUrl = getBaseURIOfIncludedStylesheet(handler, sourceFromURIResolver);
if (handler.importStackContains(hrefUrl)) {
throw new org.xml.sax.SAXException(XSLMessages.createMessage(getStylesheetInclErr(), //"(StylesheetHandler) "+hrefUrl+" is directly or indirectly importing itself!");
new Object[] { hrefUrl }));
}
// Push the system ID and corresponding Source
// on some stacks for later retrieval during parse() time.
handler.pushImportURL(hrefUrl);
handler.pushImportSource(sourceFromURIResolver);
int savedStylesheetType = handler.getStylesheetType();
handler.setStylesheetType(this.getStylesheetType());
handler.pushNewNamespaceSupport();
try {
parse(handler, uri, localName, rawName, attributes);
} finally {
handler.setStylesheetType(savedStylesheetType);
handler.popImportURL();
handler.popImportSource();
handler.popNamespaceSupport();
}
} catch (TransformerException te) {
handler.error(te.getMessage(), te);
}
}
Aggregations