Search in sources :

Example 76 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project CloudStack-archive by CloudStack-extras.

the class VsmResponse method printResponse.

// Helper routine to check for the response received.
protected void printResponse() {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        DOMImplementationLS ls = (DOMImplementationLS) docBuilder.getDOMImplementation();
        LSSerializer lss = ls.createLSSerializer();
        System.out.println(lss.writeToString(_docResponse));
    } catch (ParserConfigurationException e) {
        s_logger.error("Error parsing the repsonse : " + e.toString());
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 77 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project CloudStack-archive by CloudStack-extras.

the class LibvirtStoragePoolXMLParser method parseStoragePoolXML.

public LibvirtStoragePoolDef parseStoragePoolXML(String poolXML) {
    DocumentBuilder builder;
    try {
        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(poolXML));
        Document doc = builder.parse(is);
        Element rootElement = doc.getDocumentElement();
        String type = rootElement.getAttribute("type");
        String uuid = getTagValue("uuid", rootElement);
        String poolName = getTagValue("name", rootElement);
        Element source = (Element) rootElement.getElementsByTagName("source").item(0);
        String host = getAttrValue("host", "name", source);
        String path = getAttrValue("dir", "path", source);
        Element target = (Element) rootElement.getElementsByTagName("target").item(0);
        String targetPath = getTagValue("path", target);
        return new LibvirtStoragePoolDef(LibvirtStoragePoolDef.poolType.valueOf(type.toUpperCase()), poolName, uuid, host, path, targetPath);
    } catch (ParserConfigurationException e) {
        s_logger.debug(e.toString());
    } catch (SAXException e) {
        s_logger.debug(e.toString());
    } catch (IOException e) {
        s_logger.debug(e.toString());
    }
    return null;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 78 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project zxing by zxing.

the class HtmlAssetTranslator method translateOneFile.

private static void translateOneFile(String language, Path targetHtmlDir, Path sourceFile, String translationTextTranslated) throws IOException {
    Path destFile = targetHtmlDir.resolve(sourceFile.getFileName());
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document document;
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(sourceFile.toFile());
    } catch (ParserConfigurationException pce) {
        throw new IllegalStateException(pce);
    } catch (SAXException sae) {
        throw new IOException(sae);
    }
    Element rootElement = document.getDocumentElement();
    rootElement.normalize();
    Queue<Node> nodes = new LinkedList<>();
    nodes.add(rootElement);
    while (!nodes.isEmpty()) {
        Node node = nodes.poll();
        if (shouldTranslate(node)) {
            NodeList children = node.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                nodes.add(children.item(i));
            }
        }
        if (node.getNodeType() == Node.TEXT_NODE) {
            String text = node.getTextContent();
            if (!text.trim().isEmpty()) {
                text = StringsResourceTranslator.translateString(text, language);
                node.setTextContent(' ' + text + ' ');
            }
        }
    }
    Node translateText = document.createTextNode(translationTextTranslated);
    Node paragraph = document.createElement("p");
    paragraph.appendChild(translateText);
    Node body = rootElement.getElementsByTagName("body").item(0);
    body.appendChild(paragraph);
    DOMImplementationRegistry registry;
    try {
        registry = DOMImplementationRegistry.newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSSerializer writer = impl.createLSSerializer();
    String fileAsString = writer.writeToString(document);
    // Replace default XML header with HTML DOCTYPE
    fileAsString = fileAsString.replaceAll("<\\?xml[^>]+>", "<!DOCTYPE HTML>");
    Files.write(destFile, Collections.singleton(fileAsString), StandardCharsets.UTF_8);
}
Also used : Path(java.nio.file.Path) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) IOException(java.io.IOException) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 79 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project ACS by ACS-Community.

the class ScriptFilter method getFileMenuSaveStatusButton.

/**
	 * This method initializes FileMenuSaveStatusButton
	 * @return javax.swing.JMenuItem
	 */
private JMenuItem getFileMenuSaveStatusButton() {
    if (FileMenuSaveStatusButton == null) {
        FileMenuSaveStatusButton = new JMenuItem();
        FileMenuSaveStatusButton.setText("Save GUI status");
        FileMenuSaveStatusButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                int returnVal = chooser.showSaveDialog(cl.utfsm.samplingSystemUI.SamplingSystemGUI.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                        String file = chooser.getSelectedFile().getAbsolutePath();
                        if (file.endsWith(".ssgst")) {
                            writeStatusFile(file);
                        } else {
                            writeStatusFile(file + ".ssgst");
                        }
                    } catch (ParserConfigurationException e1) {
                        e1.printStackTrace();
                    } catch (TransformerException e1) {
                        e1.printStackTrace();
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
    }
    return FileMenuSaveStatusButton;
}
Also used : JFileChooser(javax.swing.JFileChooser) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) JMenuItem(javax.swing.JMenuItem) ActionEvent(java.awt.event.ActionEvent) TransformerException(javax.xml.transform.TransformerException)

Example 80 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project midpoint by Evolveum.

the class XPathHolder method toElement.

public Element toElement(String elementNamespace, String localElementName) {
    // TODO: is this efficient?
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder loader = factory.newDocumentBuilder();
        return toElement(elementNamespace, localElementName, loader.newDocument());
    } catch (ParserConfigurationException ex) {
        throw new AssertionError("Error on creating XML document " + ex.getMessage());
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1353 SAXException (org.xml.sax.SAXException)975 IOException (java.io.IOException)891 Document (org.w3c.dom.Document)710 DocumentBuilder (javax.xml.parsers.DocumentBuilder)631 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)569 Element (org.w3c.dom.Element)372 InputSource (org.xml.sax.InputSource)246 NodeList (org.w3c.dom.NodeList)226 Node (org.w3c.dom.Node)210 SAXParser (javax.xml.parsers.SAXParser)175 TransformerException (javax.xml.transform.TransformerException)163 File (java.io.File)162 InputStream (java.io.InputStream)158 SAXParserFactory (javax.xml.parsers.SAXParserFactory)137 ByteArrayInputStream (java.io.ByteArrayInputStream)129 StringReader (java.io.StringReader)117 ArrayList (java.util.ArrayList)115 DOMSource (javax.xml.transform.dom.DOMSource)109 StreamResult (javax.xml.transform.stream.StreamResult)93