1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.basepom.mojo.duplicatefinder;
15
16 import java.io.File;
17 import java.util.Collection;
18 import java.util.Map;
19 import java.util.regex.Pattern;
20
21 import javax.xml.stream.XMLStreamException;
22
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
25 import org.apache.maven.project.MavenProject;
26 import org.basepom.mojo.duplicatefinder.ResultCollector.ConflictResult;
27 import org.basepom.mojo.duplicatefinder.artifact.MavenCoordinates;
28 import org.basepom.mojo.duplicatefinder.classpath.ClasspathDescriptor;
29 import org.codehaus.staxmate.out.SMOutputElement;
30
31 public final class XMLWriterUtils {
32
33 private XMLWriterUtils() {
34 throw new AssertionError("do not instantiate");
35 }
36
37 public static SMOutputElement addElement(SMOutputElement document, String name, Object value)
38 throws XMLStreamException {
39 SMOutputElement element = document.addElement(name);
40 if (value != null) {
41 element.addCharacters(value.toString());
42 }
43 return element;
44 }
45
46 public static void addAttribute(SMOutputElement document, String name, Object value)
47 throws XMLStreamException {
48 if (value != null) {
49 document.addAttribute(name, value.toString());
50 }
51 }
52
53 public static void addProjectInformation(SMOutputElement rootElement, MavenProject project)
54 throws XMLStreamException {
55 SMOutputElement projectElement = rootElement.addElement("project");
56 addAttribute(projectElement, "artifactId", project.getArtifact().getArtifactId());
57 addAttribute(projectElement, "groupId", project.getArtifact().getGroupId());
58 addAttribute(projectElement, "version", project.getArtifact().getVersion());
59 addAttribute(projectElement, "classifier", project.getArtifact().getClassifier());
60 addAttribute(projectElement, "type", project.getArtifact().getType());
61 }
62
63 public static void addConflictingDependency(SMOutputElement conflictingDependenciesElement, String name, ConflictingDependency conflictingDependency)
64 throws XMLStreamException {
65 SMOutputElement conflictingDependencyElement = conflictingDependenciesElement.addElement(name);
66
67 addAttribute(conflictingDependencyElement, "currentProject", conflictingDependency.hasCurrentProject());
68 addAttribute(conflictingDependencyElement, "currentProjectIncluded", conflictingDependency.isCurrentProjectIncluded());
69 addAttribute(conflictingDependencyElement, "wildcard", conflictingDependency.isWildcard());
70
71 SMOutputElement dependenciesElement = conflictingDependencyElement.addElement("dependencies");
72 for (MavenCoordinates dependency : conflictingDependency.getDependencies()) {
73 addMavenCoordinate(dependenciesElement, "dependency", dependency);
74 }
75
76 SMOutputElement packagesElement = conflictingDependencyElement.addElement("packages");
77 for (String packageName : conflictingDependency.getPackages()) {
78 addElement(packagesElement, "package", packageName);
79 }
80
81 SMOutputElement classesElement = conflictingDependencyElement.addElement("classes");
82 for (String className : conflictingDependency.getClasses()) {
83 addElement(classesElement, "class", className);
84 }
85
86 SMOutputElement resourcesElement = conflictingDependencyElement.addElement("resources");
87 for (String resourceName : conflictingDependency.getResources()) {
88 addElement(resourcesElement, "resource", resourceName);
89 }
90
91 SMOutputElement resourcePatternsElement = conflictingDependencyElement.addElement("resourcePatterns");
92 for (Pattern resourcePattern : conflictingDependency.getResourcePatterns()) {
93 addElement(resourcePatternsElement, "resourcePattern", resourcePattern.toString());
94 }
95 }
96
97 public static void addMavenCoordinate(SMOutputElement dependenciesElement, String name, MavenCoordinates dependency)
98 throws XMLStreamException {
99 SMOutputElement dependencyElement = dependenciesElement.addElement(name);
100 addAttribute(dependencyElement, "artifactId", dependency.getArtifactId());
101 addAttribute(dependencyElement, "groupId", dependency.getGroupId());
102
103 if (dependency.getVersion().isPresent()) {
104 addAttribute(dependencyElement, "version", dependency.getVersion().get());
105 }
106
107 if (dependency.getVersionRange().isPresent()) {
108 addAttribute(dependencyElement, "versionRange", dependency.getVersionRange().get());
109 }
110
111 if (dependency.getClassifier().isPresent()) {
112 addAttribute(dependencyElement, "classifier", dependency.getClassifier().get());
113 }
114
115 addAttribute(dependencyElement, "type", dependency.getType());
116 }
117
118 public static void addArtifact(SMOutputElement artifactElement, String name, Artifact artifact)
119 throws XMLStreamException, OverConstrainedVersionException {
120
121 MavenCoordinates coordinates = new MavenCoordinates(artifact);
122 addMavenCoordinate(artifactElement, name, coordinates);
123 }
124
125 public static void addResultCollector(SMOutputElement resultElement, ResultCollector resultCollector)
126 throws XMLStreamException, OverConstrainedVersionException {
127 addAttribute(resultElement, "conflictState", resultCollector.getConflictState());
128 addAttribute(resultElement, "failed", resultCollector.isFailed());
129 SMOutputElement conflictsElement = resultElement.addElement("conflicts");
130
131 for (Map.Entry<String, Collection<ConflictResult>> entry : resultCollector.getAllResults().entrySet()) {
132 SMOutputElement conflictElement = conflictsElement.addElement("conflict");
133 addAttribute(conflictElement, "name", entry.getKey());
134 SMOutputElement conflictResultsElement = conflictElement.addElement("conflictResults");
135 for (ConflictResult conflictResult : entry.getValue()) {
136 addConflictResult(conflictResultsElement, conflictResult);
137 }
138 }
139 }
140
141 private static void addConflictResult(SMOutputElement conflictResultsElement, ConflictResult conflictResult)
142 throws XMLStreamException, OverConstrainedVersionException {
143 SMOutputElement conflictResultElement = conflictResultsElement.addElement("conflictResult");
144 addAttribute(conflictResultElement, "name", conflictResult.getName());
145 addAttribute(conflictResultElement, "type", conflictResult.getType());
146 addAttribute(conflictResultElement, "excepted", conflictResult.isExcepted());
147 addAttribute(conflictResultElement, "failed", conflictResult.isFailed());
148 addAttribute(conflictResultElement, "printed", conflictResult.isPrinted());
149 addAttribute(conflictResultElement, "conflictState", conflictResult.getConflictState());
150 SMOutputElement conflictNames = conflictResultElement.addElement("conflictNames");
151 for (ClasspathElement entry : conflictResult.getClasspathElements()) {
152 SMOutputElement conflictName = conflictNames.addElement("conflictName");
153 addAttribute(conflictName, "name", entry.getName());
154 addAttribute(conflictName, "artifact", entry.hasArtifact());
155 addAttribute(conflictName, "localFolder", entry.isLocalFolder());
156 if (entry.hasArtifact()) {
157 addArtifact(conflictName, "artifact", entry.getArtifact());
158 } else {
159 final File file = entry.getFile();
160 if (file.isDirectory()) {
161 addElement(conflictName, "directory", file);
162 } else {
163 addElement(conflictName, "file", file);
164 }
165 }
166 }
167 }
168
169 public static void addClasspathDescriptor(SMOutputElement resultElement, int resultFileMinClasspathCount, ClasspathDescriptor classpathDescriptor)
170 throws XMLStreamException {
171 SMOutputElement resourceExclusionPatternsElement = resultElement.addElement("ignoredResourcePatterns");
172 for (Pattern resourceExclusionPattern : classpathDescriptor.getIgnoredResourcePatterns()) {
173 addElement(resourceExclusionPatternsElement, "ignoredResourcePattern", resourceExclusionPattern.toString());
174 }
175
176 SMOutputElement ignoredDirectoriesPatternsElement = resultElement.addElement("ignoredDirectoryPatterns");
177 for (Pattern ignoredDirectoriesPattern : classpathDescriptor.getIgnoredDirectoryPatterns()) {
178 addElement(ignoredDirectoriesPatternsElement, "ignoredDirectoryPattern", ignoredDirectoriesPattern.toString());
179 }
180
181 SMOutputElement classExclusionPatternsElement = resultElement.addElement("ignoredClassPatterns");
182 for (Pattern classExclusionPattern : classpathDescriptor.getIgnoredClassPatterns()) {
183 addElement(classExclusionPatternsElement, "ignoredClassPattern", classExclusionPattern.toString());
184 }
185
186 for (ConflictType type : ConflictType.values()) {
187 SMOutputElement classpathElementsElement = resultElement.addElement("classpathElements");
188 addAttribute(classpathElementsElement, "type", type);
189 for (Map.Entry<String, Collection<File>> entry : classpathDescriptor.getClasspathElementLocations(type).entrySet()) {
190 if (entry.getValue().size() >= resultFileMinClasspathCount) {
191 SMOutputElement classpathElementElement = classpathElementsElement.addElement("classpathElement");
192 addAttribute(classpathElementElement, "name", entry.getKey());
193 for (File file : entry.getValue()) {
194 addElement(classpathElementElement, "file", file.getPath());
195 }
196 }
197 }
198 }
199 }
200 }