2 * Created by IntelliJ IDEA.
6 * To change template for new class use
7 * Code Style | Class Templates options (Tools | IDE Options).
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
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 public class InspectionDiff
{
24 private static HashMap
<String
, ArrayList
<Element
>> ourFileToProblem
;
26 private static final String FILE_ELEMENT
= "file";
28 private static final String CLASS_ELEMENT
= "class";
30 private static final String FIELD_ELEMENT
= "field";
32 private static final String METHOD_ELEMENT
= "method";
34 private static final String CONSTRUCTOR_ELEMENT
= "constructor";
36 private static final String INTERFACE_ELEMENT
= "interface";
38 private static final String PROBLEM_CLASS_ELEMENT
= "problem_class";
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
);
67 writeInspectionDiff(null, result
.getPath(), outPath
);
73 private static void writeInspectionDiff(final String oldPath
, final String newPath
, final String outPath
) {
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
) {
91 } catch (Exception e
) {
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());
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
);
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;