use of javax.xml.transform.TransformerConfigurationException in project nokogiri by sparklemotion.
the class XsltStylesheet method parse_stylesheet_doc.
@JRubyMethod(meta = true, rest = true)
public static IRubyObject parse_stylesheet_doc(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
ensureFirstArgIsDocument(runtime, args[0]);
XmlDocument xmlDoc = (XmlDocument) args[0];
ensureDocumentHasNoError(context, xmlDoc);
Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
XsltStylesheet xslt = (XsltStylesheet) NokogiriService.XSLT_STYLESHEET_ALLOCATOR.allocate(runtime, (RubyClass) klazz);
try {
xslt.init(args[1], doc);
} catch (TransformerConfigurationException ex) {
throw runtime.newRuntimeError("could not parse xslt stylesheet");
}
return xslt;
}
use of javax.xml.transform.TransformerConfigurationException in project nokogiri by sparklemotion.
the class XsltStylesheet method transform.
@JRubyMethod(rest = true, required = 1, optional = 2)
public IRubyObject transform(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
argumentTypeCheck(runtime, args[0]);
NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
DOMSource domSource = new DOMSource(((XmlDocument) args[0]).getDocument());
final DOMResult result;
String stringResult = null;
try {
// DOMResult
result = tryXsltTransformation(context, args, domSource, elistener);
if (result.getNode().getFirstChild() == null) {
// StreamResult
stringResult = retryXsltTransformation(context, args, domSource, elistener);
}
} catch (TransformerConfigurationException ex) {
throw runtime.newRuntimeError(ex.getMessage());
} catch (TransformerException ex) {
throw runtime.newRuntimeError(ex.getMessage());
} catch (IOException ex) {
throw runtime.newRuntimeError(ex.getMessage());
}
switch(elistener.getErrorType()) {
case ERROR:
case FATAL:
throw runtime.newRuntimeError(elistener.getErrorMessage());
case WARNING:
default:
}
if (stringResult == null) {
return createDocumentFromDomResult(context, runtime, result);
} else {
return createDocumentFromString(context, runtime, stringResult);
}
}
use of javax.xml.transform.TransformerConfigurationException in project uPortal by Jasig.
the class XSLTComponentTest method getTransformer.
protected Transformer getTransformer(String file) {
final InputStream stylesheetStream = this.getClass().getResourceAsStream(file);
;
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
try {
return transformerFactory.newTransformer(new StreamSource(stylesheetStream));
} catch (TransformerConfigurationException e) {
throw new RuntimeException("Failed to create Transformer for stylesheet: " + file, e);
}
}
use of javax.xml.transform.TransformerConfigurationException in project uPortal by Jasig.
the class XmlUtilitiesImpl method toString.
public static String toString(Node node) {
final Transformer identityTransformer;
try {
identityTransformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {
throw new RuntimeException("Failed to create identity transformer to serialize Node to String", e);
}
identityTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
final StringWriter outputWriter = new StringWriter();
final StreamResult outputTarget = new StreamResult(outputWriter);
final DOMSource xmlSource = new DOMSource(node);
try {
identityTransformer.transform(xmlSource, outputTarget);
} catch (TransformerException e) {
throw new RuntimeException("Failed to convert Node to String using Transformer", e);
}
return outputWriter.toString();
}
use of javax.xml.transform.TransformerConfigurationException in project uPortal by Jasig.
the class TemplatesBuilder method loadResource.
/* (non-Javadoc)
* @see org.apereo.portal.utils.cache.resource.ResourceBuilder#buildResource(org.springframework.core.io.Resource, java.io.InputStream)
*/
@Override
public LoadedResource<Templates> loadResource(Resource resource) throws IOException {
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
if (this.transformerAttributes != null) {
for (final Map.Entry<String, Object> attributeEntry : this.transformerAttributes.entrySet()) {
transformerFactory.setAttribute(attributeEntry.getKey(), attributeEntry.getValue());
}
}
final ResourceTrackingURIResolver uriResolver = new ResourceTrackingURIResolver(this.resourceLoader);
transformerFactory.setURIResolver(uriResolver);
final URI uri = resource.getURI();
final String systemId = uri.toString();
final InputStream stream = resource.getInputStream();
final Templates templates;
try {
final StreamSource source = new StreamSource(stream, systemId);
templates = transformerFactory.newTemplates(source);
} catch (TransformerConfigurationException e) {
throw new IOException("Failed to parse stream into Templates", e);
} finally {
IOUtils.closeQuietly(stream);
}
final Map<Resource, Long> resolvedResources = uriResolver.getResolvedResources();
return new LoadedResourceImpl<Templates>(templates, resolvedResources);
}
Aggregations