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