Example usage for javax.xml.transform OutputKeys METHOD

List of usage examples for javax.xml.transform OutputKeys METHOD

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys METHOD.

Prototype

String METHOD

To view the source code for javax.xml.transform OutputKeys METHOD.

Click Source Link

Document

method = "xml" | "html" | "text" | expanded name.

Usage

From source file:org.wso2.carbon.apimgt.hybrid.gateway.configurator.RegistryXmlConfigurator.java

/**
 * Configure registry.xml with provided properties
 *
 * @param configDirPath String/* w w  w  .  ja v a 2s.c  o m*/
 * @param configProperties Properties
 */
public void configure(String configDirPath, Properties configProperties) {
    String regXmlFilePath = configDirPath + File.separator + REGISTRY_XML;
    this.configProperties = configProperties;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(regXmlFilePath));
        configureTasks();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(new DOMSource(doc), new StreamResult(new File(regXmlFilePath)));
    } catch (SAXException | ParserConfigurationException | IOException | TransformerException
            | XPathExpressionException e) {
        log.error("Error occurred while replacing the configs in registry.xml", e);
        Runtime.getRuntime().exit(1);
    }
}

From source file:org.wso2.carbon.identity.user.store.configuration.UserStoreConfigAdminService.java

/**
 * Update a domain to be disabled/enabled
 *
 * @param domain:   Name of the domain to be updated
 * @param isDisable : Whether to disable/enable domain(true/false)
 *///ww w  .  ja v  a 2  s  .c  o m
public void changeUserStoreState(String domain, Boolean isDisable) throws IdentityUserStoreMgtException {

    File userStoreConfigFile = createConfigurationFile(domain);
    StreamResult result = new StreamResult(userStoreConfigFile);
    if (!userStoreConfigFile.exists()) {
        String errorMessage = "Cannot edit user store." + domain + " does not exist.";
        throw new IdentityUserStoreMgtException(errorMessage);
    }

    DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;
    try {
        documentBuilder = documentFactory.newDocumentBuilder();
        Document doc = documentBuilder.parse(userStoreConfigFile);

        NodeList elements = doc.getElementsByTagName("Property");
        for (int i = 0; i < elements.getLength(); i++) {
            //Assumes a property element only have attribute 'name'
            if ("Disabled".compareToIgnoreCase(elements.item(i).getAttributes().item(0).getNodeValue()) == 0) {
                elements.item(i).setTextContent(String.valueOf(isDisable));
                break;
            }
        }

        DOMSource source = new DOMSource(doc);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "6");
        transformer.transform(source, result);

        if (log.isDebugEnabled()) {
            log.debug("New state :" + isDisable + " of the user store \'" + domain
                    + "\' successfully written to the file system");
        }
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
        throw new IdentityUserStoreMgtException("Error while updating user store state", e);
    } catch (SAXException e) {
        log.error(e.getMessage(), e);
        throw new IdentityUserStoreMgtException("Error while updating user store state", e);
    } catch (TransformerConfigurationException e) {
        log.error(e.getMessage(), e);
        throw new IdentityUserStoreMgtException("Error while updating user store state", e);
    } catch (TransformerException e) {
        log.error(e.getMessage(), e);
        throw new IdentityUserStoreMgtException("Error while updating user store state", e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new IdentityUserStoreMgtException("Error while updating user store state", e);
    }
}

From source file:org.wso2.carbon.identity.user.store.configuration.UserStoreConfigAdminService.java

private void writeUserMgtXMLFile(File userStoreConfigFile, UserStoreDTO userStoreDTO,
        boolean editSecondaryUserStore) throws IdentityUserStoreMgtException {
    StreamResult result = new StreamResult(userStoreConfigFile);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

    try {/* w  ww . j  a v a2 s.  co  m*/
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();

        //create UserStoreManager element
        Element userStoreElement = doc
                .createElement(UserCoreConstants.RealmConfig.LOCAL_NAME_USER_STORE_MANAGER);
        doc.appendChild(userStoreElement);

        Attr attrClass = doc.createAttribute("class");
        attrClass.setValue(userStoreDTO.getClassName());
        userStoreElement.setAttributeNode(attrClass);

        addProperties(userStoreDTO.getClassName(), userStoreDTO.getProperties(), doc, userStoreElement,
                editSecondaryUserStore);
        addProperty(UserStoreConfigConstants.DOMAIN_NAME, userStoreDTO.getDomainId(), doc, userStoreElement,
                false);
        addProperty(DESCRIPTION, userStoreDTO.getDescription(), doc, userStoreElement, false);
        DOMSource source = new DOMSource(doc);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "6");
        transformer.transform(source, result);
    } catch (ParserConfigurationException e) {
        String errMsg = " Error occurred due to serious parser configuration exception of "
                + userStoreConfigFile;
        throw new IdentityUserStoreMgtException(errMsg, e);
    } catch (TransformerException e) {
        String errMsg = " Error occurred during the transformation process of " + userStoreConfigFile;
        throw new IdentityUserStoreMgtException(errMsg, e);
    }
}

From source file:org.wso2.carbon.ml.core.impl.MLModelHandler.java

/**
 * Append version attribute to pmml (temporary fix)
 *
 * @param pmmlString the pmml string to be appended
 * @return PMML with version as a String
 * @throws MLPmmlExportException//from   ww  w . j av  a2 s .com
 */
private String appendVersionToPMML(String pmmlString) throws MLPmmlExportException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    StringWriter stringWriter = null;

    try {
        //convert the string to xml to append the version attribute
        builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(pmmlString)));
        Element root = document.getDocumentElement();
        root.setAttribute("version", "4.2");

        // convert it back to string
        stringWriter = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
        return stringWriter.toString();
    } catch (Exception e) {
        String msg = "Error while appending version attribute to pmml";
        log.error(msg, e);
        throw new MLPmmlExportException(msg);
    } finally {
        try {
            if (stringWriter != null) {
                stringWriter.close();
            }
        } catch (IOException e) {
            String msg = "Error while closing stringWriter stream resource";
            log.error(msg, e);
            throw new MLPmmlExportException(msg);
        }
    }
}

From source file:ru.runa.common.web.HTMLUtils.java

public static String writeHtml(Document document) {
    try {//w  ww .j  a v a 2 s  .  c  om
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "html");
        transformer.setOutputProperty(OutputKeys.ENCODING, Charsets.UTF_8.name());
        transformer.transform(new DOMSource(document), new StreamResult(outputStream));
        String string = new String(outputStream.toByteArray(), Charsets.UTF_8);
        // crunch to fix issue #646
        // setting xalan property
        // "{http://xml.apache.org/xalan}line-separator" to "\n" did not
        // work
        string = string.replaceAll("&#13;\r\n", "\n");
        return string;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:test.unit.be.agiv.security.handler.WSSecurityHandlerTest.java

private String toString(Document document) throws TransformerException {
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    Properties properties = new Properties();
    properties.put(OutputKeys.METHOD, "html");
    properties.put(OutputKeys.INDENT, "5");
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    transformer = transformerFactory.newTransformer();
    transformer.setOutputProperties(properties);
    transformer.transform(new DOMSource(document), streamResult);
    return stringWriter.toString();
}

From source file:xmlconverter.controller.logic.GetFileCount.java

/**
 * This method filters given pathName for proper pathname for OS not to be
 * confused by characters that are not supported. Also it provides names and
 * type of detectors that are in specific location.
 *
 * @param doc//from www. ja va 2 s.  c  om
 * @param out
 * @throws java.io.IOException
 * @throws javax.xml.transform.TransformerException
 */
//P.S should work well junit test could be to compare number of items that are present
//in the file to compare with number that has been readed my this function
//Is able to print org.w3c.dom.Document out.
public void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}