Search in sources :

Example 36 with Document

use of org.jdom.Document in project intellij-community by JetBrains.

the class InspectionDiff method createDelta.

@SuppressWarnings({ "HardCodedStringLiteral" })
private static Document createDelta(@Nullable Document oldDoc, Document newDoc) {
    Element newRoot = newDoc.getRootElement();
    ourFileToProblem = new HashMap<>();
    List newProblems = newRoot.getChildren("problem");
    for (final Object o : newProblems) {
        Element newProblem = (Element) o;
        addProblem(newProblem);
    }
    if (oldDoc != null) {
        List oldProblems = oldDoc.getRootElement().getChildren("problem");
        for (final Object o : oldProblems) {
            Element oldProblem = (Element) o;
            if (!removeIfEquals(oldProblem)) {
                addProblem(oldProblem);
            }
        }
    }
    Element root = new Element("problems");
    Document delta = new Document(root);
    for (ArrayList<Element> fileList : ourFileToProblem.values()) {
        if (fileList != null) {
            for (Element element : fileList) {
                root.addContent((Element) element.clone());
            }
        }
    }
    return delta;
}
Also used : Element(org.jdom.Element) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.jdom.Document)

Example 37 with Document

use of org.jdom.Document in project intellij-community by JetBrains.

the class InspectionDiff method writeInspectionDiff.

private static void writeInspectionDiff(final String oldPath, final String newPath, final String outPath) {
    try {
        InputStream oldStream = oldPath != null ? new BufferedInputStream(new FileInputStream(oldPath)) : null;
        InputStream newStream = new BufferedInputStream(new FileInputStream(newPath));
        Document oldDoc = oldStream != null ? JDOMUtil.loadDocument(oldStream) : null;
        Document newDoc = JDOMUtil.loadDocument(newStream);
        OutputStream outStream = System.out;
        if (outPath != null) {
            outStream = new BufferedOutputStream(new FileOutputStream(outPath + File.separator + new File(newPath).getName()));
        }
        Document delta = createDelta(oldDoc, newDoc);
        JDOMUtil.writeDocument(delta, outStream, "\n");
        if (outStream != System.out) {
            outStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Document(org.jdom.Document)

Example 38 with Document

use of org.jdom.Document in project intellij-community by JetBrains.

the class PlainTextFormatter method convert.

@Override
public void convert(@NotNull final String rawDataDirectoryPath, @Nullable final String outputPath, @NotNull final Map<String, Tools> tools, @NotNull final List<File> inspectionsResults) throws ConversionException {
    final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
    final URL descrExtractorXsltUrl = getClass().getResource("description-text.xsl");
    final Source xslSource;
    final Transformer transformer;
    try {
        xslSource = new StreamSource(URLUtil.openStream(descrExtractorXsltUrl));
        transformer = transformerFactory.newTransformer(xslSource);
    } catch (IOException e) {
        throw new ConversionException("Cannot find inspection descriptions converter.");
    } catch (TransformerConfigurationException e) {
        throw new ConversionException("Fail to load inspection descriptions converter.");
    }
    // write to file/stdout:
    final Writer w;
    if (outputPath != null) {
        final File outputFile = new File(outputPath);
        try {
            w = new FileWriter(outputFile);
        } catch (IOException e) {
            throw new ConversionException("Cannot edit file: " + outputFile.getPath());
        }
    } else {
        w = new PrintWriter(System.out);
    }
    try {
        for (File inspectionData : inspectionsResults) {
            if (inspectionData.isDirectory()) {
                warn("Folder isn't expected here: " + inspectionData.getName());
                continue;
            }
            final String fileNameWithoutExt = FileUtil.getNameWithoutExtension(inspectionData);
            if (InspectionApplication.DESCRIPTIONS.equals(fileNameWithoutExt)) {
                continue;
            }
            InspectionToolWrapper toolWrapper = tools.get(fileNameWithoutExt).getTool();
            // Tool name and group
            w.append(getToolPresentableName(toolWrapper)).append("\n");
            // Description is HTML based, need to be converted in plain text
            writeInspectionDescription(w, toolWrapper, transformer);
            // separator before file list
            w.append("\n");
            // parse xml and output results
            final SAXBuilder builder = new SAXBuilder();
            try {
                final Document doc = builder.build(inspectionData);
                final Element root = doc.getRootElement();
                final List problems = root.getChildren(PROBLEM_ELEMENT);
                // let's count max file path & line_number length to align problem descriptions
                final int maxFileColonLineLength = getMaxFileColonLineNumLength(inspectionData, toolWrapper, problems);
                for (Object problem : problems) {
                    // Format:
                    //   file_path:line_num   [severity] problem description
                    final Element fileElement = ((Element) problem).getChild(FILE_ELEMENT);
                    final String filePath = getPath(fileElement);
                    // skip suppressed results
                    if (resultsIgnored(inspectionData, toolWrapper)) {
                        continue;
                    }
                    final Element lineElement = ((Element) problem).getChild(LINE_ELEMENT);
                    final Element problemDescrElement = ((Element) problem).getChild(DESCRIPTION_ELEMENT);
                    final String severity = ((Element) problem).getChild(PROBLEM_CLASS_ELEMENT).getAttributeValue(SEVERITY_ATTRIBUTE);
                    final String fileLineNum = lineElement.getText();
                    w.append("  ").append(filePath).append(':').append(fileLineNum);
                    // align descriptions
                    for (int i = maxFileColonLineLength - 1 - filePath.length() - fileLineNum.length() + 4; i >= 0; i--) {
                        w.append(' ');
                    }
                    w.append("[").append(severity).append("] ");
                    w.append(problemDescrElement.getText()).append('\n');
                }
            } catch (JDOMException e) {
                throw new ConversionException("Unknown results format, file = " + inspectionData.getPath() + ". Error: " + e.getMessage());
            }
            // separator between neighbour inspections
            w.append("\n");
        }
    } catch (IOException e) {
        throw new ConversionException("Cannot write inspection results: " + e.getMessage());
    } finally {
        if (w != null) {
            try {
                w.close();
            } catch (IOException e) {
                warn("Cannot save inspection results: " + e.getMessage());
            }
        }
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) StreamSource(javax.xml.transform.stream.StreamSource) Element(org.jdom.Element) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) List(java.util.List)

Example 39 with Document

use of org.jdom.Document in project intellij-plugins by JetBrains.

the class RuntimeModulesGenerateConfigTask method getClonedRootElementOfMainConfigFile.

@Nullable
private static Element getClonedRootElementOfMainConfigFile(final String filePath) {
    final VirtualFile configFile = LocalFileSystem.getInstance().findFileByPath(filePath);
    if (configFile != null) {
        try {
            final Document document = JDOMUtil.loadDocument(configFile.getInputStream());
            final Element clonedRootElement = document.clone().getRootElement();
            if (clonedRootElement.getName().equals(FLEX_CONFIG)) {
                return clonedRootElement;
            }
        } catch (JDOMException ignored) {
        /*ignore*/
        } catch (IOException ignored) {
        /*ignore*/
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Element(org.jdom.Element) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with Document

use of org.jdom.Document in project libresonic by Libresonic.

the class JAXBWriter method getRESTProtocolVersion.

private String getRESTProtocolVersion() throws Exception {
    InputStream in = null;
    try {
        in = StringUtil.class.getResourceAsStream("/libresonic-rest-api.xsd");
        Document document = new SAXBuilder().build(in);
        Attribute version = document.getRootElement().getAttribute("version");
        return version.getValue();
    } finally {
        IOUtils.closeQuietly(in);
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Attribute(org.jdom.Attribute) InputStream(java.io.InputStream) StringUtil(org.libresonic.player.util.StringUtil) Document(org.jdom.Document)

Aggregations

Document (org.jdom.Document)144 Element (org.jdom.Element)102 SAXBuilder (org.jdom.input.SAXBuilder)51 IOException (java.io.IOException)49 JDOMException (org.jdom.JDOMException)29 File (java.io.File)27 ArrayList (java.util.ArrayList)22 XMLOutputter (org.jdom.output.XMLOutputter)22 List (java.util.List)16 StringReader (java.io.StringReader)15 Format (org.jdom.output.Format)12 VCDocument (org.vcell.util.document.VCDocument)11 XPath (org.jdom.xpath.XPath)10 StringWriter (java.io.StringWriter)9 InputStream (java.io.InputStream)8 URL (java.net.URL)8 NotNull (org.jetbrains.annotations.NotNull)7 Nullable (org.jetbrains.annotations.Nullable)7 FileNotFoundException (java.io.FileNotFoundException)5 Writer (java.io.Writer)5