Search in sources :

Example 26 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project openhab1-addons by openhab.

the class Helper method documentToString.

/***
     * Helper method which converts XML Document into pretty formatted string
     *
     * @param doc to convert
     * @return converted XML as String
     */
public static String documentToString(Document doc) {
    String strMsg = "";
    try {
        DOMImplementation domImpl = doc.getImplementation();
        DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
        LSSerializer lsSerializer = domImplLS.createLSSerializer();
        lsSerializer.getDomConfig().setParameter("format-pretty-print", true);
        Writer stringWriter = new StringWriter();
        LSOutput lsOutput = domImplLS.createLSOutput();
        lsOutput.setEncoding("UTF-8");
        lsOutput.setCharacterStream(stringWriter);
        lsSerializer.write(doc, lsOutput);
        strMsg = stringWriter.toString();
    } catch (Exception e) {
        logger.warn("Error occured when converting document to string", e);
    }
    return strMsg;
}
Also used : StringWriter(java.io.StringWriter) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementation(org.w3c.dom.DOMImplementation) LSSerializer(org.w3c.dom.ls.LSSerializer) LSOutput(org.w3c.dom.ls.LSOutput) StringWriter(java.io.StringWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Example 27 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project ddf by codice.

the class SAMLAuthenticationToken method getCredentialsAsXMLString.

@Override
public String getCredentialsAsXMLString() {
    String creds = "";
    Element element = getSAMLTokenAsElement();
    if (element != null) {
        DOMImplementationLS lsImpl = (DOMImplementationLS) element.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
        if (null != lsImpl) {
            LSSerializer serializer = lsImpl.createLSSerializer();
            serializer.getDomConfig().setParameter("xml-declaration", //by default its true, so set it to false to get String without xml-declaration
            false);
            creds = serializer.writeToString(element);
        }
        LOGGER.trace("XML representation of SAML token: {}", creds);
    }
    return creds;
}
Also used : Element(org.w3c.dom.Element) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer)

Example 28 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project cuba by cuba-platform.

the class XMLConverter method parseCommitRequest.

@Override
public CommitRequest parseCommitRequest(String content) {
    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS lsImpl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSParser requestConfigParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
        // Set options on the parser
        DOMConfiguration config = requestConfigParser.getDomConfig();
        config.setParameter("validate", Boolean.TRUE);
        config.setParameter("element-content-whitespace", Boolean.FALSE);
        config.setParameter("comments", Boolean.FALSE);
        requestConfigParser.setFilter(new LSParserFilter() {

            @Override
            public short startElement(Element elementArg) {
                return LSParserFilter.FILTER_ACCEPT;
            }

            @Override
            public short acceptNode(Node nodeArg) {
                return StringUtils.isBlank(nodeArg.getTextContent()) ? LSParserFilter.FILTER_REJECT : LSParserFilter.FILTER_ACCEPT;
            }

            @Override
            public int getWhatToShow() {
                return NodeFilter.SHOW_TEXT;
            }
        });
        LSInput lsInput = lsImpl.createLSInput();
        lsInput.setStringData(content);
        Document commitRequestDoc = requestConfigParser.parse(lsInput);
        Node rootNode = commitRequestDoc.getFirstChild();
        if (!"CommitRequest".equals(rootNode.getNodeName()))
            throw new IllegalArgumentException("Not a CommitRequest xml passed: " + rootNode.getNodeName());
        CommitRequest result = new CommitRequest();
        NodeList children = rootNode.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String childNodeName = child.getNodeName();
            if ("commitInstances".equals(childNodeName)) {
                NodeList entitiesNodeList = child.getChildNodes();
                Set<String> commitIds = new HashSet<>(entitiesNodeList.getLength());
                for (int j = 0; j < entitiesNodeList.getLength(); j++) {
                    Node idNode = entitiesNodeList.item(j).getAttributes().getNamedItem("id");
                    if (idNode == null)
                        continue;
                    String id = idNode.getTextContent();
                    if (id.startsWith("NEW-"))
                        id = id.substring(id.indexOf('-') + 1);
                    commitIds.add(id);
                }
                result.setCommitIds(commitIds);
                result.setCommitInstances(parseNodeList(result, entitiesNodeList));
            } else if ("removeInstances".equals(childNodeName)) {
                NodeList entitiesNodeList = child.getChildNodes();
                List removeInstances = parseNodeList(result, entitiesNodeList);
                result.setRemoveInstances(removeInstances);
            } else if ("softDeletion".equals(childNodeName)) {
                result.setSoftDeletion(Boolean.parseBoolean(child.getTextContent()));
            }
        }
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : LSParserFilter(org.w3c.dom.ls.LSParserFilter) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSParser(org.w3c.dom.ls.LSParser) ParseException(java.text.ParseException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LSInput(org.w3c.dom.ls.LSInput) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry)

Example 29 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project opennms by OpenNMS.

the class JaxbClassObjectAdapter method unmarshal.

@Override
public Object unmarshal(final Object from) throws Exception {
    LOG.trace("unmarshal: from = ({}){}", (from == null ? null : from.getClass()), from);
    if (from == null)
        return null;
    if (from instanceof Node) {
        final Node e = (Node) from;
        e.normalize();
        final String nodeName = e.getNodeName();
        final Class<?> clazz = getClassForElement(nodeName);
        LOG.trace("class type = {} (node name = {})", clazz, nodeName);
        if (clazz == null) {
            LOG.warn("Unable to determine object type for node name {}. Known elements include: {}", nodeName, m_knownElementClasses);
            return from;
        }
        final DOMImplementationLS lsImpl = (DOMImplementationLS) e.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
        LSSerializer serializer = lsImpl.createLSSerializer();
        // by default its true, so set it to false to get String without xml-declaration
        serializer.getDomConfig().setParameter("xml-declaration", false);
        final String str = serializer.writeToString(e);
        return JaxbUtils.unmarshal(clazz, str);
    } else {
        LOG.error("Unsure how to determine which class to use for unmarshaling object type {}", from.getClass());
        throw new IllegalArgumentException("Unsure how to determine which class to use for unmarshaling object type " + from.getClass());
    }
}
Also used : Node(org.w3c.dom.Node) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer)

Example 30 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project dsl-json by ngs-doo.

the class XmlConverter method serialize.

public static void serialize(final Element value, final JsonWriter sw) {
    Document document = value.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    LSOutput lsOutput = domImplLS.createLSOutput();
    lsOutput.setEncoding("UTF-8");
    StringWriter writer = new StringWriter();
    lsOutput.setCharacterStream(writer);
    serializer.write(document, lsOutput);
    StringConverter.serialize(writer.toString(), sw);
}
Also used : StringWriter(java.io.StringWriter) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) LSOutput(org.w3c.dom.ls.LSOutput)

Aggregations

DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)54 LSSerializer (org.w3c.dom.ls.LSSerializer)43 Document (org.w3c.dom.Document)22 DOMImplementationRegistry (org.w3c.dom.bootstrap.DOMImplementationRegistry)21 LSOutput (org.w3c.dom.ls.LSOutput)18 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)14 IOException (java.io.IOException)13 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Node (org.w3c.dom.Node)12 DOMImplementation (org.w3c.dom.DOMImplementation)10 Element (org.w3c.dom.Element)10 StringWriter (java.io.StringWriter)9 LSParser (org.w3c.dom.ls.LSParser)8 InputSource (org.xml.sax.InputSource)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 StringReader (java.io.StringReader)5 TransformerException (javax.xml.transform.TransformerException)5 NodeList (org.w3c.dom.NodeList)5 LSInput (org.w3c.dom.ls.LSInput)5