View Javadoc
1   /*
2    * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and contributors.
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the GNU Lesser General Public License
6    * (LGPL) version 2.1 which accompanies this distribution, and is available at
7    * http://www.gnu.org/licenses/lgpl-2.1.html
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * Contributors:
15   *     bstefanescu, jcarsique
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   * TODO NXBT-258
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("?")) { // a profile
61                  String profile = line.substring(1).trim();
62                  if (profile.length() == 0) { // default profile
63                      line = reader.readLine();
64                      continue;
65                  } else if (profileMgr.isProfileActive(profile)) {
66                      line = reader.readLine();
67                      continue;
68                  } else { // skip this profile content
69                      readToNextActiveProfile(reader);
70                  }
71              } else if (line.startsWith("@")) { // include another file
72                  File file = project.resolveFile(line.substring(1).trim());
73                  parse(file, nodes);
74              } else if (line.startsWith("!")) { // remove from already collected
75                                                 // nodes
76                  // TODO
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 }