Search in sources :

Example 51 with Templates

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;
}
Also used : Templates(javax.xml.transform.Templates)

Example 52 with Templates

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);
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SAXSource(javax.xml.transform.sax.SAXSource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StaxSource(org.apache.cxf.staxutils.StaxSource) XMLFilter(org.xml.sax.XMLFilter) Templates(javax.xml.transform.Templates) StaxSource(org.apache.cxf.staxutils.StaxSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 53 with Templates

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;
}
Also used : ValidationExceptionImpl(ddf.catalog.validation.impl.ValidationExceptionImpl) MetacardValidationReportImpl(ddf.catalog.validation.impl.report.MetacardValidationReportImpl) ValidationViolationImpl(ddf.catalog.validation.impl.violation.ValidationViolationImpl) Templates(javax.xml.transform.Templates) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 54 with Templates

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;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) File(java.io.File) URL(java.net.URL) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ValidationException(ddf.catalog.validation.ValidationException) TransformerException(javax.xml.transform.TransformerException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 55 with Templates

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;
    }
}
Also used : Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Templates(javax.xml.transform.Templates) TransformerException(javax.xml.transform.TransformerException)

Aggregations

Templates (javax.xml.transform.Templates)61 TransformerFactory (javax.xml.transform.TransformerFactory)26 StreamSource (javax.xml.transform.stream.StreamSource)25 Transformer (javax.xml.transform.Transformer)21 Source (javax.xml.transform.Source)20 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)18 StreamResult (javax.xml.transform.stream.StreamResult)16 TransformerException (javax.xml.transform.TransformerException)10 InputStream (java.io.InputStream)8 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 DOMResult (javax.xml.transform.dom.DOMResult)7 DOMSource (javax.xml.transform.dom.DOMSource)7 File (java.io.File)5 Result (javax.xml.transform.Result)5 SAXSource (javax.xml.transform.sax.SAXSource)5 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4 BufferedOutputStream (java.io.BufferedOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3