Debian Codegraph @

src/main/java/capsule/PomReader.java

raw symbols SCIP
1 /*
2 * Capsule
3 * Copyright (c) 2014-2015, Parallel Universe Software Co. All rights reserved.
4 *
5 * This program and the accompanying materials are licensed under the terms
6 * of the Eclipse Public License v1.0, available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 */
9 package capsule;
10
11 import java.io.InputStream;
12 import java.util.*;
13
14 import org.apache.maven.model.Dependency;
15 import org.apache.maven.model.Exclusion;
16 import org.apache.maven.model.Model;
17 import org.apache.maven.model.Repository;
18 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
19
20 public final class PomReader {
21 private final Model pom;
22
23 public PomReader(InputStream is) {
24 try {
25 this.pom = new MavenXpp3Reader().read(is);
26 } catch (Exception e) {
27 throw new RuntimeException("Error trying to read pom.", e);
28 }
29 }
30
31 public String getArtifactId() {
32 return pom.getArtifactId();
33 }
34
35 public String getGroupId() {
36 return
37 pom.getGroupId() != null ?
38 pom.getGroupId() : (pom.getParent() != null ? pom.getParent().getGroupId() : null);
39 }
40
41 public String getVersion() {
42 return
43 pom.getVersion() != null ?
44 pom.getVersion() : (pom.getParent() != null ? pom.getParent().getVersion() : null);
45 }
46
47 public String getId() {
48 return pom.getId();
49 }
50
51 public Properties getProperties() {
52 return pom.getProperties();
53 }
54
55 public List<String> getRepositories() {
56 final List<Repository> repos = pom.getRepositories();
57 if (repos == null)
58 return null;
59 final List<String> repositories = new ArrayList<String>(repos.size());
60 for (Repository repo : repos)
61 repositories.add(convert(repo));
62 return repositories;
63 }
64
65 public List<String[]> getDependencies() {
66 final List<Dependency> deps = pom.getDependencies();
67 if (deps == null)
68 return null;
69
70 final List<String[]> dependencies = new ArrayList<>(deps.size());
71 for (Dependency dep : deps) {
72 if (includeDependency(dep))
73 dependencies.add(new String[]{convert(dep), dep.getType()});
74 }
75 return dependencies;
76 }
77
78 private static boolean includeDependency(Dependency dep) {
79 if (dep.isOptional())
80 return false;
81 if (dep.getScope() == null || dep.getScope().isEmpty())
82 return true;
83 switch (dep.getScope().toLowerCase()) {
84 case "compile":
85 case "runtime":
86 return true;
87 default:
88 return false;
89 }
90 }
91
92 private static String convert(Dependency dep) {
93 return dep2coords(dep) + exclusions2desc(dep);
94 }
95
96 private static String dep2coords(Dependency dep) {
97 return dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion()
98 + (dep.getClassifier() != null && !dep.getClassifier().isEmpty() ? ":" + dep.getClassifier() : "");
99 }
100
101 private static String exclusions2desc(Dependency dep) {
102 List<Exclusion> exclusions = dep.getExclusions();
103 if (exclusions == null || exclusions.isEmpty())
104 return "";
105
106 StringBuilder sb = new StringBuilder();
107 sb.append('(');
108 for (Exclusion ex : exclusions)
109 sb.append(exclusion2coord(ex)).append(',');
110 sb.delete(sb.length() - 1, sb.length());
111 sb.append(')');
112
113 return sb.toString();
114 }
115
116 private static String exclusion2coord(Exclusion ex) {
117 return ex.getGroupId() + ":" + ex.getArtifactId();
118 }
119
120 private static String convert(Repository repo) {
121 if (repo.getId() != null && !repo.getId().isEmpty())
122 return repo.getId() + "(" + repo.getUrl() + ")";
123 return repo.getUrl();
124 }
125
126 public String resolve(String s) {
127 if (s == null)
128 return null;
129
130 final Properties ps = getProperties();
131 String ret = s;
132 if (getGroupId() != null)
133 ret = ret.replace("${project.groupId}", getGroupId()).replace("${pom.groupId}", getGroupId());
134 if (getVersion() != null)
135 ret = ret
136 .replace("${project.version}", getVersion())
137 .replace("${pom.version}", getVersion())
138 .replace("${version}", getVersion());
139 for(final String pName : ps.stringPropertyNames())
140 ret = ret.replace("${" + pName + "}", ps.getProperty(pName));
141 return ret;
142 }
143 }