save datasources in separate zipped xml
[fedora-idea.git] / lang-impl / src / com / intellij / codeInspection / InspectionDiff.java
blob3a6412db56245c7a9103c32bb8115f6968e0cdff
1 /*
2 * Created by IntelliJ IDEA.
3 * User: max
4 * Date: Jun 21, 2002
5 * Time: 7:36:28 PM
6 * To change template for new class use
7 * Code Style | Class Templates options (Tools | IDE Options).
8 */
9 package com.intellij.codeInspection;
11 import com.intellij.openapi.util.Comparing;
12 import com.intellij.openapi.util.JDOMUtil;
13 import com.intellij.util.containers.HashMap;
14 import org.jdom.Document;
15 import org.jdom.Element;
16 import org.jetbrains.annotations.NonNls;
17 import org.jetbrains.annotations.Nullable;
19 import java.io.*;
20 import java.util.ArrayList;
21 import java.util.List;
23 public class InspectionDiff {
24 private static HashMap<String, ArrayList<Element>> ourFileToProblem;
25 @NonNls
26 private static final String FILE_ELEMENT = "file";
27 @NonNls
28 private static final String CLASS_ELEMENT = "class";
29 @NonNls
30 private static final String FIELD_ELEMENT = "field";
31 @NonNls
32 private static final String METHOD_ELEMENT = "method";
33 @NonNls
34 private static final String CONSTRUCTOR_ELEMENT = "constructor";
35 @NonNls
36 private static final String INTERFACE_ELEMENT = "interface";
37 @NonNls
38 private static final String PROBLEM_CLASS_ELEMENT = "problem_class";
39 @NonNls
40 private static final String DESCRIPTION_ELEMENT = "description";
42 public static void main(String[] args) {
43 if (args.length != 3 && args.length != 2) {
44 System.out.println(InspectionsBundle.message("inspection.diff.format.error"));
47 String oldPath = args[0];
48 String newPath = args[1];
49 String outPath = args.length == 3 ? args[2] : null;
51 final File oldResults = new File(oldPath);
52 final File newResults = new File(newPath);
53 if (oldResults.isDirectory() && newResults.isDirectory()) {
54 final File[] old = oldResults.listFiles();
55 final File[] results = newResults.listFiles();
56 for (File result : results) {
57 final String inspectionName = result.getName();
58 boolean found = false;
59 for (File oldFile : old) {
60 if (oldFile.getName().equals(inspectionName)) {
61 writeInspectionDiff(oldFile.getPath(), result.getPath(), outPath);
62 found = true;
63 break;
66 if (!found) {
67 writeInspectionDiff(null, result.getPath(), outPath);
73 private static void writeInspectionDiff(final String oldPath, final String newPath, final String outPath) {
74 try {
75 InputStream oldStream = oldPath != null ? new BufferedInputStream(new FileInputStream(oldPath)) : null;
76 InputStream newStream = new BufferedInputStream(new FileInputStream(newPath));
78 Document oldDoc = oldStream != null ? JDOMUtil.loadDocument(oldStream) : null;
79 Document newDoc = JDOMUtil.loadDocument(newStream);
81 OutputStream outStream = System.out;
82 if (outPath != null) {
83 outStream = new BufferedOutputStream(new FileOutputStream(outPath + File.separator + new File(newPath).getName()));
86 Document delta = createDelta(oldDoc, newDoc);
87 JDOMUtil.writeDocument(delta, outStream, "\n");
88 if (outStream != System.out) {
89 outStream.close();
91 } catch (Exception e) {
92 e.printStackTrace();
96 @SuppressWarnings({"HardCodedStringLiteral"})
97 private static Document createDelta(@Nullable Document oldDoc, Document newDoc) {
98 Element newRoot = newDoc.getRootElement();
100 ourFileToProblem = new HashMap<String, ArrayList<Element>>();
101 List newProblems = newRoot.getChildren("problem");
102 for (final Object o : newProblems) {
103 Element newProblem = (Element)o;
104 addProblem(newProblem);
107 if (oldDoc != null) {
108 List oldProblems = oldDoc.getRootElement().getChildren("problem");
109 for (final Object o : oldProblems) {
110 Element oldProblem = (Element)o;
111 if (!removeIfEquals(oldProblem)) {
112 addProblem(oldProblem);
117 Element root = new Element("problems");
118 Document delta = new Document(root);
120 for (ArrayList<Element> fileList : ourFileToProblem.values()) {
121 if (fileList != null) {
122 for (Element element : fileList) {
123 root.addContent((Element)element.clone());
128 return delta;
131 private static boolean removeIfEquals(Element problem) {
132 String fileName = problem.getChildText(FILE_ELEMENT);
133 ArrayList<Element> problemList = ourFileToProblem.get(fileName);
134 if (problemList != null) {
135 Element[] problems = problemList.toArray(new Element[problemList.size()]);
136 for (Element toCheck : problems) {
137 if (equals(problem, toCheck)) return problemList.remove(toCheck);
140 return false;
143 private static void addProblem(Element problem) {
144 String fileName = problem.getChildText(FILE_ELEMENT);
145 ArrayList<Element> problemList = ourFileToProblem.get(fileName);
146 if (problemList == null) {
147 problemList = new ArrayList<Element>();
148 ourFileToProblem.put(fileName, problemList);
150 problemList.add(problem);
153 private static boolean equals(Element oldProblem, Element newProblem) {
154 if (!Comparing.equal(oldProblem.getChildText(CLASS_ELEMENT), newProblem.getChildText(CLASS_ELEMENT))) return false;
155 if (!Comparing.equal(oldProblem.getChildText(FIELD_ELEMENT), newProblem.getChildText(FIELD_ELEMENT))) return false;
156 if (!Comparing.equal(oldProblem.getChildText(METHOD_ELEMENT), newProblem.getChildText(METHOD_ELEMENT))) return false;
157 if (!Comparing.equal(oldProblem.getChildText(CONSTRUCTOR_ELEMENT), newProblem.getChildText(CONSTRUCTOR_ELEMENT))) return false;
158 if (!Comparing.equal(oldProblem.getChildText(INTERFACE_ELEMENT), newProblem.getChildText(INTERFACE_ELEMENT))) return false;
159 if (!Comparing.equal(oldProblem.getChildText(PROBLEM_CLASS_ELEMENT), newProblem.getChildText(PROBLEM_CLASS_ELEMENT))) return false;
160 if (!Comparing.equal(oldProblem.getChildText(DESCRIPTION_ELEMENT), newProblem.getChildText(DESCRIPTION_ELEMENT))) return false;
162 return true;