use of javax.xml.transform.TransformerConfigurationException in project uPortal by Jasig.
the class BaseXsltDataUpgraderTest method testXsltUpgrade.
protected void testXsltUpgrade(final Resource xslResource, final PortalDataKey dataKey, final Resource inputResource, final Resource expectedResultResource, final Resource xsdResource) throws Exception {
final XmlUtilities xmlUtilities = new XmlUtilitiesImpl() {
@Override
public Templates getTemplates(Resource stylesheet) throws TransformerConfigurationException, IOException {
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
return transformerFactory.newTemplates(new StreamSource(stylesheet.getInputStream()));
}
};
final XsltDataUpgrader xsltDataUpgrader = new XsltDataUpgrader();
xsltDataUpgrader.setPortalDataKey(dataKey);
xsltDataUpgrader.setXslResource(xslResource);
xsltDataUpgrader.setXmlUtilities(xmlUtilities);
xsltDataUpgrader.afterPropertiesSet();
// Create XmlEventReader (what the JaxbPortalDataHandlerService has)
final XMLInputFactory xmlInputFactory = xmlUtilities.getXmlInputFactory();
final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(inputResource.getInputStream());
final Node sourceNode = xmlUtilities.convertToDom(xmlEventReader);
final DOMSource source = new DOMSource(sourceNode);
final DOMResult result = new DOMResult();
xsltDataUpgrader.upgradeData(source, result);
// XSD Validation
final String resultString = XmlUtilitiesImpl.toString(result.getNode());
if (xsdResource != null) {
final Schema schema = this.loadSchema(new Resource[] { xsdResource }, XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Validator validator = schema.newValidator();
try {
validator.validate(new StreamSource(new StringReader(resultString)));
} catch (Exception e) {
throw new XmlTestException("Failed to validate XSLT output against provided XSD", resultString, e);
}
}
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setNormalizeWhitespace(true);
try {
Diff d = new Diff(new InputStreamReader(expectedResultResource.getInputStream()), new StringReader(resultString));
assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
} catch (Exception e) {
throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", resultString, e);
} catch (Error e) {
throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", resultString, e);
}
}
use of javax.xml.transform.TransformerConfigurationException in project buck by facebook.
the class SchemeGenerator method serializeScheme.
private static void serializeScheme(XCScheme scheme, OutputStream stream) {
DocumentBuilder docBuilder;
Transformer transformer;
try {
docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
transformer = TransformerFactory.newInstance().newTransformer();
} catch (ParserConfigurationException | TransformerConfigurationException e) {
throw new RuntimeException(e);
}
DOMImplementation domImplementation = docBuilder.getDOMImplementation();
Document doc = domImplementation.createDocument(null, "Scheme", null);
doc.setXmlVersion("1.0");
Element rootElem = doc.getDocumentElement();
rootElem.setAttribute("LastUpgradeVersion", "9999");
rootElem.setAttribute("version", "1.7");
Optional<XCScheme.BuildAction> buildAction = scheme.getBuildAction();
if (buildAction.isPresent()) {
Element buildActionElem = serializeBuildAction(doc, buildAction.get());
rootElem.appendChild(buildActionElem);
}
Optional<XCScheme.TestAction> testAction = scheme.getTestAction();
if (testAction.isPresent()) {
Element testActionElem = serializeTestAction(doc, testAction.get());
testActionElem.setAttribute("buildConfiguration", scheme.getTestAction().get().getBuildConfiguration());
rootElem.appendChild(testActionElem);
}
Optional<XCScheme.LaunchAction> launchAction = scheme.getLaunchAction();
if (launchAction.isPresent()) {
Element launchActionElem = serializeLaunchAction(doc, launchAction.get());
launchActionElem.setAttribute("buildConfiguration", launchAction.get().getBuildConfiguration());
rootElem.appendChild(launchActionElem);
} else {
Element launchActionElem = doc.createElement("LaunchAction");
launchActionElem.setAttribute("buildConfiguration", "Debug");
rootElem.appendChild(launchActionElem);
}
Optional<XCScheme.ProfileAction> profileAction = scheme.getProfileAction();
if (profileAction.isPresent()) {
Element profileActionElem = serializeProfileAction(doc, profileAction.get());
profileActionElem.setAttribute("buildConfiguration", profileAction.get().getBuildConfiguration());
rootElem.appendChild(profileActionElem);
} else {
Element profileActionElem = doc.createElement("ProfileAction");
profileActionElem.setAttribute("buildConfiguration", "Release");
rootElem.appendChild(profileActionElem);
}
Optional<XCScheme.AnalyzeAction> analyzeAction = scheme.getAnalyzeAction();
if (analyzeAction.isPresent()) {
Element analyzeActionElem = doc.createElement("AnalyzeAction");
analyzeActionElem.setAttribute("buildConfiguration", analyzeAction.get().getBuildConfiguration());
rootElem.appendChild(analyzeActionElem);
} else {
Element analyzeActionElem = doc.createElement("AnalyzeAction");
analyzeActionElem.setAttribute("buildConfiguration", "Debug");
rootElem.appendChild(analyzeActionElem);
}
Optional<XCScheme.ArchiveAction> archiveAction = scheme.getArchiveAction();
if (archiveAction.isPresent()) {
Element archiveActionElem = doc.createElement("ArchiveAction");
archiveActionElem.setAttribute("buildConfiguration", archiveAction.get().getBuildConfiguration());
archiveActionElem.setAttribute("revealArchiveInOrganizer", "YES");
rootElem.appendChild(archiveActionElem);
} else {
Element archiveActionElem = doc.createElement("ArchiveAction");
archiveActionElem.setAttribute("buildConfiguration", "Release");
rootElem.appendChild(archiveActionElem);
}
// write out
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(stream);
try {
transformer.transform(source, result);
} catch (TransformerException e) {
throw new RuntimeException(e);
}
}
use of javax.xml.transform.TransformerConfigurationException in project libgdx by libgdx.
the class TiledMapPacker method writeUpdatedTMX.
private void writeUpdatedTMX(TiledMap tiledMap, FileHandle tmxFileHandle) throws IOException {
Document doc;
DocumentBuilder docBuilder;
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try {
docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.parse(tmxFileHandle.read());
Node map = doc.getFirstChild();
while (map.getNodeType() != Node.ELEMENT_NODE || map.getNodeName() != "map") {
if ((map = map.getNextSibling()) == null) {
throw new GdxRuntimeException("Couldn't find map node!");
}
}
setProperty(doc, map, "atlas", settings.tilesetOutputDirectory + "/" + settings.atlasOutputName + ".atlas");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
outputDir.mkdirs();
StreamResult result = new StreamResult(new File(outputDir, tmxFileHandle.name()));
transformer.transform(source, result);
} catch (ParserConfigurationException e) {
throw new RuntimeException("ParserConfigurationException: " + e.getMessage());
} catch (SAXException e) {
throw new RuntimeException("SAXException: " + e.getMessage());
} catch (TransformerConfigurationException e) {
throw new RuntimeException("TransformerConfigurationException: " + e.getMessage());
} catch (TransformerException e) {
throw new RuntimeException("TransformerException: " + e.getMessage());
}
}
use of javax.xml.transform.TransformerConfigurationException in project Smack by igniterealtime.
the class EnhancedDebugger method formatXML.
private String formatXML(String str) {
try {
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
// for Java 1.5
try {
tFactory.setAttribute("indent-number", 2);
} catch (IllegalArgumentException e) {
// Ignore
}
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// Transform the requested string into a nice formatted XML string
StreamSource source = new StreamSource(new StringReader(str));
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
transformer.transform(source, result);
return sw.toString();
} catch (TransformerConfigurationException tce) {
LOGGER.log(Level.SEVERE, "Transformer Factory error", tce);
} catch (TransformerException te) {
LOGGER.log(Level.SEVERE, "Transformation error", te);
}
return str;
}
use of javax.xml.transform.TransformerConfigurationException in project j2objc by google.
the class TransformerFactoryImpl method newTemplates.
/**
* Process the source into a Templates object, which is likely
* a compiled representation of the source. This Templates object
* may then be used concurrently across multiple threads. Creating
* a Templates object allows the TransformerFactory to do detailed
* performance optimization of transformation instructions, without
* penalizing runtime transformation.
*
* @param source An object that holds a URL, input stream, etc.
* @return A Templates object capable of being used for transformation purposes.
*
* @throws TransformerConfigurationException May throw this during the parse when it
* is constructing the Templates object and fails.
*/
public Templates newTemplates(Source source) throws TransformerConfigurationException {
String baseID = source.getSystemId();
if (null != baseID) {
baseID = SystemIDResolver.getAbsoluteURI(baseID);
}
if (source instanceof DOMSource) {
DOMSource dsource = (DOMSource) source;
Node node = dsource.getNode();
if (null != node)
return processFromNode(node, baseID);
else {
String messageStr = XSLMessages.createMessage(XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null);
throw new IllegalArgumentException(messageStr);
}
}
TemplatesHandler builder = newTemplatesHandler();
builder.setSystemId(baseID);
try {
InputSource isource = SAXSource.sourceToInputSource(source);
isource.setSystemId(baseID);
XMLReader reader = null;
if (source instanceof SAXSource)
reader = ((SAXSource) source).getXMLReader();
if (null == reader) {
// Use JAXP1.1 ( if possible )
try {
javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
if (m_isSecureProcessing) {
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (org.xml.sax.SAXException se) {
}
}
javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
reader = jaxpParser.getXMLReader();
} catch (javax.xml.parsers.ParserConfigurationException ex) {
throw new org.xml.sax.SAXException(ex);
} catch (javax.xml.parsers.FactoryConfigurationError ex1) {
throw new org.xml.sax.SAXException(ex1.toString());
} catch (NoSuchMethodError ex2) {
} catch (AbstractMethodError ame) {
}
}
if (null == reader)
reader = XMLReaderFactory.createXMLReader();
// If you set the namespaces to true, we'll end up getting double
// xmlns attributes. Needs to be fixed. -sb
// reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
reader.setContentHandler(builder);
reader.parse(isource);
} catch (org.xml.sax.SAXException se) {
if (m_errorListener != null) {
try {
m_errorListener.fatalError(new TransformerException(se));
} catch (TransformerConfigurationException ex1) {
throw ex1;
} catch (TransformerException ex1) {
throw new TransformerConfigurationException(ex1);
}
} else {
throw new TransformerConfigurationException(se.getMessage(), se);
}
} catch (Exception e) {
if (m_errorListener != null) {
try {
m_errorListener.fatalError(new TransformerException(e));
return null;
} catch (TransformerConfigurationException ex1) {
throw ex1;
} catch (TransformerException ex1) {
throw new TransformerConfigurationException(ex1);
}
} else {
throw new TransformerConfigurationException(e.getMessage(), e);
}
}
return builder.getTemplates();
}
Aggregations