Search in sources :

Example 76 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project violations-plugin by jenkinsci.

the class ViolationsDOMParser method parse.

/*
     * Parse a violations file.
     * @param model the model to store the violations in.
     * @param projectPath the project path used for resolving paths.
     * @param fileName the name of the violations file to parse
     *                       (relative to the projectPath).
     * @param sourcePaths a list of source paths to resolve classes against
     * @throws IOException if there is an error.
     */
public void parse(FullBuildModel model, File projectPath, String fileName, String[] sourcePaths) throws IOException {
    boolean success = false;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        dom = db.parse(new File(projectPath, fileName));
        setProjectPath(projectPath);
        setModel(model);
        setSourcePaths(sourcePaths);
        execute();
        success = true;
    } catch (IOException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new IOException2("Cannot parse " + fileName, ex);
    } finally {
    // ? terminate the parser
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) IOException2(hudson.util.IOException2)

Example 77 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project violations-plugin by jenkinsci.

the class ReSharperParser method parse.

public void parse(final FullBuildModel model, final File projectPath, final String fileName, final String[] sourcePaths) throws IOException {
    absoluteFileFinder.addSourcePath(projectPath.getAbsolutePath());
    absoluteFileFinder.addSourcePaths(sourcePaths);
    final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder;
    try {
        docBuilder = docBuilderFactory.newDocumentBuilder();
        final Document docElement = docBuilder.parse(new FileInputStream(new File(projectPath, fileName)));
        NodeList nl = docElement.getElementsByTagName("IssueType");
        if (nl == null)
            return;
        for (int i = 0; i < nl.getLength(); i++) {
            final Element issueTypeElement = (Element) nl.item(i);
            final IssueType issueType = parseIssueType(issueTypeElement);
            issueTypes.put(issueType.getId(), issueType);
        }
        nl = docElement.getElementsByTagName("Issue");
        if (nl == null)
            return;
        for (int i = 0; i < nl.getLength(); i++) {
            final Element issueElement = (Element) nl.item(i);
            final Issue issue = parseIssue(issueElement);
            final IssueType issueType = issueTypes.get(issue.getTypeId());
            if (// couldn't find the issue type, skip it
            issueType == null)
                continue;
            final Violation violation = new Violation();
            violation.setType("resharper");
            violation.setMessage(issue.getMessage());
            violation.setPopupMessage(issueType.getDescription() + " - " + issue.getMessage());
            violation.setSource(issueType.getCategory());
            violation.setLine(issue.getLine());
            violation.setSeverity(SEVERITIES.get(issueType.getSeverity()));
            violation.setSeverityLevel(Severity.getSeverityLevel(violation.getSeverity()));
            final File file = absoluteFileFinder.getFileForName(issue.getFile());
            final FullFileModel fullFileModel = getFileModel(model, issue.getFile().replace('\\', '/'), file);
            fullFileModel.addViolation(violation);
        }
    } catch (final ParserConfigurationException pce) {
        throw new IOException2(pce);
    } catch (final SAXException se) {
        throw new IOException2(se);
    }
}
Also used : Violation(hudson.plugins.violations.model.Violation) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) FullFileModel(hudson.plugins.violations.model.FullFileModel) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) IOException2(hudson.util.IOException2)

Example 78 with DocumentBuilderFactory

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

the class WadlResourceTest method extractWadlAsDocument.

/**
     * Extracts WADL as {@link Document} from given {@link Response}.
     *
     * @param response The response to extract {@code Document} from.
     * @return The extracted {@code Document}.
     * @throws ParserConfigurationException In case of parser configuration issues.
     * @throws SAXException                 In case of parsing issues.
     * @throws IOException                  In case of IO error.
     */
static Document extractWadlAsDocument(final Response response) throws ParserConfigurationException, SAXException, IOException {
    assertEquals(200, response.getStatus());
    final File tmpFile = response.readEntity(File.class);
    final DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
    }
    final DocumentBuilder b = bf.newDocumentBuilder();
    final Document d = b.parse(tmpFile);
    Wadl5Test.printSource(new DOMSource(d));
    return d;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) File(java.io.File)

Example 79 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project android-selector-intellij-plugin by importre.

the class AndroidSelectorDialog method createDrawableV21.

private void createDrawableV21(String filename, String color, String pressed) throws Exception {
    VirtualFile child = dir.findChild(drawableV21Dir);
    if (child == null) {
        child = dir.createChildDirectory(null, drawableV21Dir);
    }
    VirtualFile newXmlFile = child.findChild(filename);
    if (newXmlFile != null && newXmlFile.exists()) {
        newXmlFile.delete(null);
    }
    newXmlFile = child.createChildData(null, filename);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element root = doc.createElement("ripple");
    root.setAttributeNS(nsUri, "xmlns:android", androidUri);
    root.setAttribute("android:color", pressed);
    doc.appendChild(root);
    Element item = doc.createElement("item");
    item.setAttribute("android:drawable", color);
    root.appendChild(item);
    OutputStream os = newXmlFile.getOutputStream(null);
    PrintWriter out = new PrintWriter(os);
    StringWriter writer = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(INDENT_SPACE, "4");
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    out.println(writer.getBuffer().toString());
    out.close();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 80 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project languagetool by languagetool-org.

the class XMLValidator method mergeIntoSource.

private static Source mergeIntoSource(InputStream baseXmlStream, InputStream xmlStream) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setIgnoringComments(true);
    domFactory.setValidating(false);
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document baseDoc = builder.parse(baseXmlStream);
    Document ruleDoc = builder.parse(xmlStream);
    // Shall this be more generic, i.e. reuse not just unification ???
    NodeList unificationNodes = baseDoc.getElementsByTagName("unification");
    Node ruleNode = ruleDoc.getElementsByTagName("rules").item(0);
    Node firstChildRuleNode = ruleNode.getChildNodes().item(1);
    for (int i = 0; i < unificationNodes.getLength(); i++) {
        Node unificationNode = ruleDoc.importNode(unificationNodes.item(i), true);
        ruleNode.insertBefore(unificationNode, firstChildRuleNode);
    }
    return new DOMSource(ruleDoc);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

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