Search in sources :

Example 96 with TransformerException

use of javax.xml.transform.TransformerException in project felix by apache.

the class XSLTProcessor method processHttpException.

protected void processHttpException(HttpInputStream in, HttpOutputStream out, HttpException e) throws IOException {
    out.setCode(e.getCode());
    out.setHeader("Content-Type", "text/html");
    out.sendHeaders();
    // hardcoded dir :-P
    Transformer transformer = createTransformer("error.xsl");
    transformer.setURIResolver(this);
    Document doc = e.getResponseDoc();
    if (doc != null) {
        try {
            if (!in.getVariables().containsKey("locale")) {
                transformer.setParameter("request.locale", locale.toString());
            }
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            transformer.transform(new DOMSource(doc), new StreamResult(output));
            output.writeTo(out);
        } catch (TransformerException ex) {
            XSLTProcessor.log(LogService.LOG_ERROR, "Exception during error output", ex);
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException)

Example 97 with TransformerException

use of javax.xml.transform.TransformerException in project felix by apache.

the class ComponentDescriptorIO method read.

public static List<ClassDescription> read(final InputStream file, final ClassLoader classLoader, final IssueLog iLog, final String location) throws SCRDescriptorException {
    try {
        final XmlHandler xmlHandler = new XmlHandler(classLoader, iLog, location);
        IOUtils.parse(file, xmlHandler);
        return xmlHandler.components;
    } catch (final TransformerException e) {
        throw new SCRDescriptorException("Unable to read xml", location, e);
    }
}
Also used : TransformerException(javax.xml.transform.TransformerException) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException)

Example 98 with TransformerException

use of javax.xml.transform.TransformerException in project felix by apache.

the class MetaTypeIO method write.

/**
 * Generate the xml top level element and start streaming
 * the meta data.
 * @param metaData
 * @param contentHandler
 * @throws SAXException
 */
private static void write(final DescriptionContainer metaData, final List<ComponentContainer> components, final File file, final String localization) throws SCRDescriptorException {
    final String namespace = detectMetatypeVersion(metaData);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        try {
            final ContentHandler contentHandler = IOUtils.getSerializer(fos);
            contentHandler.startDocument();
            contentHandler.startPrefixMapping(PREFIX, namespace);
            final AttributesImpl ai = new AttributesImpl();
            IOUtils.addAttribute(ai, "localization", localization);
            contentHandler.startElement(namespace, METADATA_ELEMENT, METADATA_ELEMENT_QNAME, ai);
            IOUtils.newline(contentHandler);
            for (final ComponentContainer comp : components) {
                if (comp.getMetatypeContainer() != null) {
                    generateOCDXML(comp.getMetatypeContainer(), contentHandler);
                    generateDesignateXML(comp.getMetatypeContainer(), contentHandler);
                }
            }
            // end wrapper element
            contentHandler.endElement(namespace, METADATA_ELEMENT, METADATA_ELEMENT_QNAME);
            IOUtils.newline(contentHandler);
            contentHandler.endPrefixMapping(PREFIX);
            contentHandler.endDocument();
        } finally {
            fos.close();
        }
    } catch (final IOException e) {
        throw new SCRDescriptorException("Unable to generate xml", file.toString(), e);
    } catch (final TransformerException e) {
        throw new SCRDescriptorException("Unable to generate xml", file.toString(), e);
    } catch (final SAXException e) {
        throw new SCRDescriptorException("Unable to generate xml", file.toString(), e);
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) FileOutputStream(java.io.FileOutputStream) ComponentContainer(org.apache.felix.scrplugin.helper.ComponentContainer) IOException(java.io.IOException) ContentHandler(org.xml.sax.ContentHandler) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 99 with TransformerException

use of javax.xml.transform.TransformerException in project felix by apache.

the class ObrCleanRepo method writeToFile.

/**
 * write a Node in a xml file.
 *
 * @param outputFilename URI to the output file
 * @param treeToBeWrite Node root of the tree to be write in file
 * @throws MojoExecutionException if the plugin failed
 */
private void writeToFile(URI outputFilename, Node treeToBeWrite) throws MojoExecutionException {
    // init the transformer
    Transformer transformer = null;
    TransformerFactory tfabrique = TransformerFactory.newInstance();
    try {
        transformer = tfabrique.newTransformer();
    } catch (TransformerConfigurationException e) {
        getLog().error("Unable to write to file: " + outputFilename.toString());
        throw new MojoExecutionException("Unable to write to file: " + outputFilename.toString() + " : " + e.getMessage());
    }
    Properties proprietes = new Properties();
    proprietes.put("method", "xml");
    proprietes.put("version", "1.0");
    proprietes.put("encoding", "ISO-8859-1");
    proprietes.put("standalone", "yes");
    proprietes.put("indent", "yes");
    proprietes.put("omit-xml-declaration", "no");
    transformer.setOutputProperties(proprietes);
    DOMSource input = new DOMSource(treeToBeWrite);
    File fichier = null;
    FileOutputStream flux = null;
    try {
        fichier = File.createTempFile("repository", ".xml");
        flux = new FileOutputStream(fichier);
    } catch (IOException e) {
        getLog().error("Unable to write to file: " + fichier.getName());
        throw new MojoExecutionException("Unable to write to file: " + fichier.getName() + " : " + e.getMessage());
    }
    Result output = new StreamResult(flux);
    try {
        transformer.transform(input, output);
    } catch (TransformerException e) {
        throw new MojoExecutionException("Unable to write to file: " + outputFilename.toString() + " : " + e.getMessage());
    }
    try {
        flux.flush();
        flux.close();
        FileUtils.rename(fichier, new File(outputFilename));
    } catch (IOException e) {
        throw new MojoExecutionException("IOException when closing file : " + e.getMessage());
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 100 with TransformerException

use of javax.xml.transform.TransformerException in project hale by halestudio.

the class InspireInstanceWriter method writeAdditionalElements.

/**
 * @see StreamGmlWriter#writeAdditionalElements(XMLStreamWriter,
 *      TypeDefinition, IOReporter)
 */
@Override
protected void writeAdditionalElements(XMLStreamWriter writer, TypeDefinition containerDefinition, IOReporter reporter) throws XMLStreamException {
    super.writeAdditionalElements(writer, containerDefinition, reporter);
    // determine INSPIRE identifier and metadata names
    Path<Definition<?>> localIdPath = new DefinitionAccessor(containerDefinition).findChildren("identifier").findChildren("Identifier").findChildren("localId").eval(false);
    QName identifierName = localIdPath.getElements().get(1).getName();
    Definition<?> internalIdentifierDef = localIdPath.getElements().get(2);
    QName internalIdentifierName = internalIdentifierDef.getName();
    QName localIdName = localIdPath.getElements().get(3).getName();
    Path<Definition<?>> namespacePath = new DefinitionAccessor(internalIdentifierDef).findChildren("namespace").eval(false);
    QName namespaceName = namespacePath.getElements().get(1).getName();
    Path<Definition<?>> metadataPath = new DefinitionAccessor(containerDefinition).findChildren("metadata").eval(false);
    QName metadataName = metadataPath.getElements().get(1).getName();
    // write INSPIRE identifier
    writer.writeStartElement(identifierName.getNamespaceURI(), identifierName.getLocalPart());
    writer.writeStartElement(internalIdentifierName.getNamespaceURI(), internalIdentifierName.getLocalPart());
    writer.writeStartElement(localIdName.getNamespaceURI(), localIdName.getLocalPart());
    writer.writeCharacters(getParameter(PARAM_SPATIAL_DATA_SET_LOCALID).as(String.class, ""));
    writer.writeEndElement();
    writer.writeStartElement(namespaceName.getNamespaceURI(), namespaceName.getLocalPart());
    writer.writeCharacters(getParameter(PARAM_SPATIAL_DATA_SET_NAMESPACE).as(String.class, ""));
    writer.writeEndElement();
    writer.writeEndElement();
    writer.writeEndElement();
    // write metadata
    writer.writeStartElement(metadataName.getNamespaceURI(), metadataName.getLocalPart());
    // retrieve metadata element (if any)
    Element metadataElement = getParameter(PARAM_SPATIAL_DATA_SET_METADATA_DOM).as(Element.class);
    // metadata from file (if any)
    if (metadataElement == null) {
        String metadataFile = getParameter(PARAM_SPATIAL_DATA_SET_METADATA_FILE).as(String.class);
        if (metadataFile != null && !metadataFile.isEmpty()) {
            try (InputStream input = new BufferedInputStream(new FileInputStream(new File(metadataFile)))) {
                metadataElement = findMetadata(input, reporter);
            } catch (IOException e) {
                reporter.warn(new IOMessageImpl("Could not load specified metadata file.", e));
            }
        }
    }
    if (metadataElement != null) {
        try {
            writeElement(metadataElement, writer);
        } catch (TransformerException e) {
            reporter.warn(new IOMessageImpl("Couldn't include specified metadata file.", e));
        }
    } else {
        writer.writeAttribute(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil", "true");
    }
    writer.writeEndElement();
}
Also used : QName(javax.xml.namespace.QName) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) Element(org.w3c.dom.Element) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) DefinitionAccessor(eu.esdihumboldt.hale.common.schema.groovy.DefinitionAccessor) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerException (javax.xml.transform.TransformerException)786 Transformer (javax.xml.transform.Transformer)351 StreamResult (javax.xml.transform.stream.StreamResult)345 DOMSource (javax.xml.transform.dom.DOMSource)298 IOException (java.io.IOException)266 TransformerFactory (javax.xml.transform.TransformerFactory)175 Document (org.w3c.dom.Document)155 StringWriter (java.io.StringWriter)152 SAXException (org.xml.sax.SAXException)152 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)150 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)127 Source (javax.xml.transform.Source)96 StreamSource (javax.xml.transform.stream.StreamSource)90 Element (org.w3c.dom.Element)87 DocumentBuilder (javax.xml.parsers.DocumentBuilder)77 File (java.io.File)70 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)63 Node (org.w3c.dom.Node)62 ByteArrayOutputStream (java.io.ByteArrayOutputStream)58 ErrorListener (javax.xml.transform.ErrorListener)55