1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.nuxeo.build.ant.artifact;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26
27 import org.apache.tools.ant.Project;
28 import org.nuxeo.build.ant.profile.AntProfileManager;
29 import org.nuxeo.build.maven.AntBuildMojo;
30 import org.nuxeo.build.maven.graph.Node;
31
32
33
34
35 public class ArtifactSetParser {
36
37 protected Project project;
38
39 protected AntProfileManager profileMgr;
40
41 public ArtifactSetParser(Project project) {
42 this.project = project;
43 this.profileMgr = AntBuildMojo.getInstance().getAntProfileManager();
44 }
45
46 public void parse(File src, Collection<Node> nodes) throws IOException {
47 parse(new BufferedReader(new FileReader(src)), nodes);
48 }
49
50 public void parse(BufferedReader reader, Collection<Node> nodes)
51 throws IOException {
52 String line = reader.readLine();
53 while (line != null) {
54 line = line.trim();
55 if (line.length() == 0 || line.startsWith("#")) {
56 line = reader.readLine();
57 continue;
58 }
59 line = project.replaceProperties(line);
60 if (line.startsWith("?")) {
61 String profile = line.substring(1).trim();
62 if (profile.length() == 0) {
63 line = reader.readLine();
64 continue;
65 } else if (profileMgr.isProfileActive(profile)) {
66 line = reader.readLine();
67 continue;
68 } else {
69 readToNextActiveProfile(reader);
70 }
71 } else if (line.startsWith("@")) {
72 File file = project.resolveFile(line.substring(1).trim());
73 parse(file, nodes);
74 } else if (line.startsWith("!")) {
75
76
77 } else {
78 int p = line.lastIndexOf('?');
79 if (p > -1) {
80 List<String> profiles = split(line.substring(p + 1), ',');
81 if (!profileMgr.isAnyProfileActive(profiles)) {
82 line = reader.readLine();
83 continue;
84 }
85 line = line.substring(0, p);
86 }
87 ArtifactFile af = new ArtifactFile();
88 af.setKey(line);
89 nodes.add(af.getNode());
90 }
91 line = reader.readLine();
92 }
93 }
94
95 protected String readToNextActiveProfile(BufferedReader reader)
96 throws IOException {
97 String line = reader.readLine();
98 while (line != null) {
99 line = line.trim();
100 if (line.startsWith("?")) {
101 String profile = line.substring(1).trim();
102 if (!profileMgr.isProfileActive(profile)) {
103 return readToNextActiveProfile(reader);
104 } else {
105 return profile;
106 }
107 }
108 line = reader.readLine();
109 }
110 return null;
111 }
112
113 protected List<String> split(String text, char ch) {
114 List<String> result = new ArrayList<>();
115 int p = 0;
116 int q = text.indexOf(ch, p);
117 while (q > -1) {
118 result.add(text.substring(p, q).trim());
119 p = q + 1;
120 q = text.indexOf(ch, p);
121 }
122 if (p == 0) {
123 result.add(text.trim());
124 } else {
125 result.add(text.substring(p).trim());
126 }
127 return result;
128 }
129
130 }