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;
}
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();
}
}
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());
}
}
}
}
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;
}
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);
}
}
Aggregations