Search in sources :

Example 6 with TransformerException

use of javax.xml.transform.TransformerException in project Apktool by iBotPeaches.

the class ResXmlPatcher method removeManifestVersions.

/**
     * Removes attributes like "versionCode" and "versionName" from file.
     *
     * @param file File representing AndroidManifest.xml
     * @throws AndrolibException
     */
public static void removeManifestVersions(File file) throws AndrolibException {
    if (file.exists()) {
        try {
            Document doc = loadDocument(file);
            Node manifest = doc.getFirstChild();
            NamedNodeMap attr = manifest.getAttributes();
            Node vCode = attr.getNamedItem("android:versionCode");
            Node vName = attr.getNamedItem("android:versionName");
            if (vCode != null) {
                attr.removeNamedItem("android:versionCode");
            }
            if (vName != null) {
                attr.removeNamedItem("android:versionName");
            }
            saveDocument(file, doc);
        } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) {
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 7 with TransformerException

use of javax.xml.transform.TransformerException in project jOOQ by jOOQ.

the class XMLDatabase method info.

private InformationSchema info() {
    if (info == null) {
        String xml = getProperties().getProperty(P_XML_FILE);
        String xsl = getProperties().getProperty(P_XSL_FILE);
        InputStream xmlIs = null;
        InputStream xslIs = null;
        log.info("Using XML file", xml);
        try {
            xmlIs = XMLDatabase.class.getResourceAsStream(xml);
            if (xmlIs == null)
                xmlIs = new FileInputStream(xml);
            if (StringUtils.isBlank(xsl)) {
                info = JAXB.unmarshal(new File(xml), InformationSchema.class);
            } else {
                log.info("Using XSL file", xsl);
                xslIs = XMLDatabase.class.getResourceAsStream(xsl);
                if (xslIs == null)
                    xslIs = new FileInputStream(xsl);
                try {
                    StringWriter writer = new StringWriter();
                    TransformerFactory factory = TransformerFactory.newInstance();
                    Transformer transformer = factory.newTransformer(new StreamSource(xslIs));
                    transformer.transform(new StreamSource(xmlIs), new StreamResult(writer));
                    info = JAXB.unmarshal(new StringReader(writer.getBuffer().toString()), InformationSchema.class);
                } catch (TransformerException e) {
                    throw new RuntimeException("Error while transforming XML file " + xml + " with XSL file " + xsl, e);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Error while opening files " + xml + " or " + xsl, e);
        } finally {
            if (xmlIs != null) {
                try {
                    xmlIs.close();
                } catch (Exception ignore) {
                }
            }
            if (xslIs != null) {
                try {
                    xslIs.close();
                } catch (Exception ignore) {
                }
            }
        }
    }
    return info;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TransformerException(javax.xml.transform.TransformerException) SQLException(java.sql.SQLException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 8 with TransformerException

use of javax.xml.transform.TransformerException in project sonarqube by SonarSource.

the class DebtModelXMLExporter method prettyFormatXml.

private static String prettyFormatXml(String xml) {
    try {
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT);
        Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());
        serializer.transform(xmlSource, res);
        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(), StandardCharsets.UTF_8);
    } catch (TransformerException ignored) {
    // Ignore, raw XML will be returned
    }
    return xml;
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) SAXSource(javax.xml.transform.sax.SAXSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException)

Example 9 with TransformerException

use of javax.xml.transform.TransformerException in project openhab1-addons by openhab.

the class Helper method nodeToString.

/***
     * Converts a xml Node into String
     *
     * @param node to convert
     * @return converted string
     */
public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        logger.warn("nodeToString Transformer Exception", te);
    }
    return sw.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerException(javax.xml.transform.TransformerException)

Example 10 with TransformerException

use of javax.xml.transform.TransformerException in project OpenAM by OpenRock.

the class XMLResourceExceptionHandler method write.

@Override
public void write(MessageContext context, AuthenticationException exception) {
    Reject.ifNull(exception);
    try {
        ResourceException jre;
        if (exception instanceof AuthenticationFailedException) {
            jre = new PermanentException(Status.UNAUTHORIZED.getCode(), exception.getMessage(), null);
        } else if (exception.getCause() instanceof ResourceException) {
            jre = (ResourceException) exception.getCause();
        } else {
            LOGGER.error(exception.getMessage(), exception);
            jre = new InternalServerErrorException("Authentication Failed", exception);
        }
        AuditTrail auditTrail = context.getAuditTrail();
        List<Map<String, Object>> failureReasonList = auditTrail.getFailureReasons();
        if (failureReasonList != null && !failureReasonList.isEmpty()) {
            jre.setDetail(json(object(field("failureReasons", failureReasonList))));
        }
        Response response = context.getResponse();
        response.setStatus(Status.valueOf(jre.getCode()));
        context.<Response>getResponse().getHeaders().put(ContentTypeHeader.valueOf(MediaType.XML_UTF_8.toString()));
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Transformer transformer = XMLUtils.getTransformerFactory().newTransformer();
        transformer.transform(new DOMSource(asXMLDOM(jre.includeCauseInJsonValue().toJsonValue().asMap())), new StreamResult(outputStream));
        response.getEntity().setBytes(outputStream.toByteArray());
    } catch (TransformerException e1) {
        throw new IllegalStateException("Could not write XML to response", e1);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) AuthenticationFailedException(org.forgerock.caf.authentication.framework.AuthenticationFailedException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Response(org.forgerock.http.protocol.Response) PermanentException(org.forgerock.json.resource.PermanentException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ResourceException(org.forgerock.json.resource.ResourceException) AuditTrail(org.forgerock.caf.authentication.framework.AuditTrail) Map(java.util.Map) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerException (javax.xml.transform.TransformerException)808 Transformer (javax.xml.transform.Transformer)364 StreamResult (javax.xml.transform.stream.StreamResult)362 DOMSource (javax.xml.transform.dom.DOMSource)311 IOException (java.io.IOException)277 TransformerFactory (javax.xml.transform.TransformerFactory)184 Document (org.w3c.dom.Document)161 StringWriter (java.io.StringWriter)159 SAXException (org.xml.sax.SAXException)157 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)156 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)131 Source (javax.xml.transform.Source)100 StreamSource (javax.xml.transform.stream.StreamSource)94 Element (org.w3c.dom.Element)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)83 File (java.io.File)74 Node (org.w3c.dom.Node)65 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)64 ByteArrayOutputStream (java.io.ByteArrayOutputStream)62 StringReader (java.io.StringReader)59