Search in sources :

Example 61 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project OpenAttestation by OpenAttestation.

the class ReadXmlTest method getTagSelectionFromXml.

public TagSelection getTagSelectionFromXml(String xml) throws ParserConfigurationException, SAXException, IOException {
    TagSelection ret = new TagSelection();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    Document doc = builder.parse(is);
    ArrayList<tag> tagList = new ArrayList<tag>();
    int cnt = 0;
    NodeList nodeList = doc.getElementsByTagName("attribute");
    for (int s = 0; s < nodeList.getLength(); s++) {
        Node fstNode = nodeList.item(s);
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
            Element fstElmnt = (Element) fstNode;
            String idValue = fstElmnt.getAttribute("oid");
            Element lstNmElmnt = (Element) nodeList.item(cnt++);
            NodeList lstNm = lstNmElmnt.getChildNodes();
            String currentAction = ((Node) lstNm.item(0)).getNodeValue();
            if (currentAction != null) {
                tagList.add(new tag("", idValue, currentAction));
            }
        }
    }
    nodeList = doc.getElementsByTagName("selection");
    Node fstNode = nodeList.item(0);
    Element e = (Element) fstNode;
    ret.id = e.getAttribute("id");
    ret.name = e.getAttribute("name");
    ret.tagList = tagList;
    return ret;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader)

Example 62 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project OpenGrok by OpenGrok.

the class SubversionRepository method setDirectoryName.

@Override
public void setDirectoryName(String directoryName) {
    super.setDirectoryName(directoryName);
    if (isWorking()) {
        // set to true if we manage to find the root directory
        Boolean rootFound = Boolean.FALSE;
        List<String> cmd = new ArrayList<>();
        cmd.add(RepoCommand);
        cmd.add("info");
        cmd.add("--xml");
        File directory = new File(getDirectoryName());
        Executor executor = new Executor(cmd, directory);
        if (executor.exec() == 0) {
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document document = builder.parse(executor.getOutputStream());
                String url = getValue(document.getElementsByTagName("url").item(0));
                if (url == null) {
                    LOGGER.log(Level.WARNING, "svn info did not contain an URL for [{0}]. Assuming remote repository.", directoryName);
                    setRemote(true);
                } else {
                    if (!url.startsWith("file")) {
                        setRemote(true);
                    }
                }
                String root = getValue(document.getElementsByTagName("root").item(0));
                if (url != null && root != null) {
                    reposPath = url.substring(root.length());
                    rootFound = Boolean.TRUE;
                }
            } catch (SAXException saxe) {
                LOGGER.log(Level.WARNING, "Parser error parsing svn output", saxe);
            } catch (ParserConfigurationException pce) {
                LOGGER.log(Level.WARNING, "Parser configuration error parsing svn output", pce);
            } catch (IOException ioe) {
                LOGGER.log(Level.WARNING, "IOException reading from svn process", ioe);
            }
        } else {
            LOGGER.log(Level.WARNING, "Failed to execute svn info for [{0}]. Repository disabled.", directoryName);
        }
        setWorking(rootFound);
    }
}
Also used : Executor(org.opensolaris.opengrok.util.Executor) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) File(java.io.File) SAXException(org.xml.sax.SAXException)

Example 63 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project OpenGrok by OpenGrok.

the class DirectoryListingTest method directoryListing.

/**
     * Test directory listing
     * @throws java.lang.Exception if an error occurs while generating the
     *         list.
     */
@Test
public void directoryListing() throws Exception {
    StringWriter out = new StringWriter();
    out.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<start>\n");
    DirectoryListing instance = new DirectoryListing();
    instance.listTo("ctx", directory, out, directory.getPath(), Arrays.asList(directory.list()));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    assertNotNull("DocumentBuilderFactory is null", factory);
    DocumentBuilder builder = factory.newDocumentBuilder();
    assertNotNull("DocumentBuilder is null", builder);
    out.append("</start>\n");
    String str = out.toString();
    System.out.println(str);
    Document document = builder.parse(new ByteArrayInputStream(str.getBytes()));
    NodeList nl = document.getElementsByTagName("tr");
    int len = nl.getLength();
    // add one extra for header and one for parent directory link
    assertEquals(entries.length + 2, len);
    // Skip the the header and parent link
    for (int i = 2; i < len; ++i) {
        validateEntry((Element) nl.item(i));
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 64 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project OpenMEAP by OpenMEAP.

the class XmlUtils method getDocument.

/**
     * Convenience method to parse an input stream into a document
     * @param inputStream
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
public static Document getDocument(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    return db.parse(inputStream);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 65 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project atlas by alibaba.

the class Configuration method readXmlConfig.

/**
     * read args from xml
     **/
void readXmlConfig(File xmlConfigFile) throws IOException, ParserConfigurationException, SAXException {
    if (!xmlConfigFile.exists()) {
        return;
    }
    System.out.printf("reading config file, %s\n", xmlConfigFile.getAbsolutePath());
    BufferedInputStream input = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        input = new BufferedInputStream(new FileInputStream(xmlConfigFile));
        InputSource source = new InputSource(input);
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(source);
        NodeList issues = document.getElementsByTagName(TAG_ISSUE);
        for (int i = 0, count = issues.getLength(); i < count; i++) {
            Node node = issues.item(i);
            Element element = (Element) node;
            String id = element.getAttribute(ATTR_ID);
            if (id.length() == 0) {
                System.err.println("Invalid config file: Missing required issue id attribute");
                continue;
            }
            if (id.equals(PROPERTY_ISSUE)) {
                readPropertyFromXml(node);
            } else if (id.equals(DEX_ISSUE)) {
                readDexPatternsFromXml(node);
            } else if (id.equals(SO_ISSUE)) {
                readLibPatternsFromXml(node);
            } else if (id.equals(RES_ISSUE)) {
                readResPatternsFromXml(node);
            } else if (id.equals(PACKAGE_CONFIG_ISSUE)) {
                readPackageConfigFromXml(node);
            } else if (id.equals(SIGN_ISSUE)) {
                if (mUseSignAPk) {
                    readSignFromXml(node);
                }
            } else {
                System.err.println("unknown issue " + id);
            }
        }
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                System.exit(-1);
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) BufferedInputStream(java.io.BufferedInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream)

Aggregations

DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)759 DocumentBuilder (javax.xml.parsers.DocumentBuilder)622 Document (org.w3c.dom.Document)526 Element (org.w3c.dom.Element)244 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)232 NodeList (org.w3c.dom.NodeList)209 IOException (java.io.IOException)197 InputSource (org.xml.sax.InputSource)193 SAXException (org.xml.sax.SAXException)173 Node (org.w3c.dom.Node)145 StringReader (java.io.StringReader)142 Test (org.junit.Test)117 File (java.io.File)86 DOMSource (javax.xml.transform.dom.DOMSource)77 ByteArrayInputStream (java.io.ByteArrayInputStream)72 InputStream (java.io.InputStream)63 StreamResult (javax.xml.transform.stream.StreamResult)50 ArrayList (java.util.ArrayList)47 SAXParseException (org.xml.sax.SAXParseException)46 Transformer (javax.xml.transform.Transformer)44