use of javax.xml.transform.Templates in project tomee by apache.
the class XSLTJaxbProvider method getInTemplates.
protected Templates getInTemplates(Annotation[] anns, MediaType mt) {
Templates t = createTemplatesFromContext();
if (t != null) {
return t;
}
t = inTemplates != null ? inTemplates : inMediaTemplates != null ? inMediaTemplates.get(mt.getType() + "/" + mt.getSubtype()) : null;
if (t == null) {
t = getAnnotationTemplates(anns);
}
return t;
}
use of javax.xml.transform.Templates in project tomee by apache.
the class XSLTJaxbProvider method unmarshalFromInputStream.
@Override
protected Object unmarshalFromInputStream(Unmarshaller unmarshaller, InputStream is, Annotation[] anns, MediaType mt) throws JAXBException {
try {
Templates t = createTemplates(getInTemplates(anns, mt), inParamsMap, inProperties);
if (t == null && supportJaxbOnly) {
return super.unmarshalFromInputStream(unmarshaller, is, anns, mt);
}
if (unmarshaller.getClass().getName().contains("eclipse")) {
// eclipse MOXy doesn't work properly with the XMLFilter/Reader thing
// so we need to bounce through a DOM
Source reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
DOMResult dom = new DOMResult();
t.newTransformer().transform(reader, dom);
return unmarshaller.unmarshal(dom.getNode());
}
XMLFilter filter;
try {
filter = factory.newXMLFilter(t);
} catch (TransformerConfigurationException ex) {
TemplatesImpl ti = (TemplatesImpl) t;
filter = factory.newXMLFilter(ti.getTemplates());
trySettingProperties(filter, ti);
}
XMLReader reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
filter.setParent(reader);
SAXSource source = new SAXSource();
source.setXMLReader(filter);
if (systemId != null) {
source.setSystemId(systemId);
}
return unmarshaller.unmarshal(source);
} catch (TransformerException ex) {
LOG.warning("Transformation exception : " + ex.getMessage());
throw ExceptionUtils.toInternalServerErrorException(ex, null);
}
}
use of javax.xml.transform.Templates in project ddf by codice.
the class SchematronValidationService method generateReport.
private MetacardValidationReport generateReport(Metacard metacard) throws ValidationExceptionImpl {
MetacardValidationReportImpl report = new MetacardValidationReportImpl();
Set<String> attributes = ImmutableSet.of("metadata");
String metadata = metacard.getMetadata();
boolean canBeValidated = !(StringUtils.isEmpty(metadata) || (namespace != null && !namespace.equals(XML_UTILS.getRootNamespace(metadata))));
if (canBeValidated) {
try {
for (Future<Templates> validator : validators) {
schematronReport = generateReport(metadata, validator.get(10, TimeUnit.MINUTES));
schematronReport.getErrors().forEach(errorMsg -> report.addMetacardViolation(new ValidationViolationImpl(attributes, sanitize(errorMsg), ValidationViolation.Severity.ERROR)));
schematronReport.getWarnings().forEach(warningMsg -> report.addMetacardViolation(new ValidationViolationImpl(attributes, sanitize(warningMsg), ValidationViolation.Severity.WARNING)));
}
} catch (TimeoutException | ExecutionException e) {
throw new ValidationExceptionImpl(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ValidationExceptionImpl(e);
}
}
return report;
}
use of javax.xml.transform.Templates in project ddf by codice.
the class SchematronValidationService method compileSchematronRules.
private Templates compileSchematronRules(String schematronFileName) throws SchematronInitializationException {
Templates template;
File schematronFile = new File(schematronFileName);
if (!schematronFile.exists()) {
throw new SchematronInitializationException("Could not locate schematron file " + schematronFileName);
}
try {
URL schUrl = schematronFile.toURI().toURL();
Source schSource = new StreamSource(schUrl.toString());
// Stage 1: Perform inclusion expansion on Schematron schema file
DOMResult stage1Result = performStage(schSource, getClass().getClassLoader().getResource("iso-schematron/iso_dsdl_include.xsl"));
DOMSource stage1Output = new DOMSource(stage1Result.getNode());
// Stage 2: Perform abstract expansion on output file from Stage 1
DOMResult stage2Result = performStage(stage1Output, getClass().getClassLoader().getResource("iso-schematron/iso_abstract_expand.xsl"));
DOMSource stage2Output = new DOMSource(stage2Result.getNode());
// Stage 3: Compile the .sch rules that have been prepocessed by Stages 1 and 2 (i.e.,
// the output of Stage 2)
DOMResult stage3Result = performStage(stage2Output, getClass().getClassLoader().getResource("iso-schematron/iso_svrl_for_xslt2.xsl"));
DOMSource stage3Output = new DOMSource(stage3Result.getNode());
// Setting the system ID let's us resolve relative paths in the schematron files.
// We need the URL string so that the string is properly formatted (e.g. space = %20).
stage3Output.setSystemId(schUrl.toString());
template = transformerFactory.newTemplates(stage3Output);
} catch (Exception e) {
throw new SchematronInitializationException("Error trying to create SchematronValidationService using sch file " + schematronFileName, e);
}
return template;
}
use of javax.xml.transform.Templates in project j2objc by google.
the class TransformerFactoryImpl method newTransformer.
/**
* Process the source into a Transformer object. Care must
* be given to know that this object can not be used concurrently
* in multiple threads.
*
* @param source An object that holds a URL, input stream, etc.
*
* @return A Transformer object capable of
* being used for transformation purposes in a single thread.
*
* @throws TransformerConfigurationException May throw this during the parse when it
* is constructing the Templates object and fails.
*/
public Transformer newTransformer(Source source) throws TransformerConfigurationException {
try {
Templates tmpl = newTemplates(source);
/* this can happen if an ErrorListener is present and it doesn't
throw any exception in fatalError.
The spec says: "a Transformer must use this interface
instead of throwing an exception" - the newTemplates() does
that, and returns null.
*/
if (tmpl == null)
return null;
Transformer transformer = tmpl.newTransformer();
transformer.setURIResolver(m_uriResolver);
return transformer;
} catch (TransformerConfigurationException ex) {
if (m_errorListener != null) {
try {
m_errorListener.fatalError(ex);
// TODO: but the API promises to never return null...
return null;
} catch (TransformerConfigurationException ex1) {
throw ex1;
} catch (TransformerException ex1) {
throw new TransformerConfigurationException(ex1);
}
}
throw ex;
}
}
Aggregations