View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  
20  package org.eclipse.jetty.maven.plugin;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.codehaus.plexus.util.xml.Xpp3Dom;
28  
29  /**
30   * OverlayConfig
31   *
32   *
33   */
34  public class OverlayConfig
35  {
36      private String targetPath;
37      private String groupId;
38      private String artifactId;
39      private String classifier;
40      private List<String> includes;
41      private List<String> excludes;
42      private boolean skip;
43      private boolean filtered;
44      
45      public OverlayConfig() {}
46      
47      public OverlayConfig(String fmt, List<String> defaultIncludes, List<String> defaultExcludes)
48      {
49          if (fmt == null)
50              return;
51          String[] atoms = fmt.split(",");
52          for (int i=0;i<atoms.length;i++)
53          {
54              String s = atoms[i].trim();
55              switch (i)
56              {
57                  case 0: 
58                  {
59                      if (!"".equals(s))
60                          groupId = s;
61                      break;
62                  }
63                  case 1:
64                  {
65                      if (!"".equals(s))
66                          artifactId = s;
67                      break;
68                  }
69                  case 2:
70                  {
71                      if (!"".equals(s))
72                          classifier = s;
73                      break;
74                  }
75                  case 3: 
76                  { 
77                      if (!"".equals(s))
78                          targetPath = s;
79                      break;
80                  }
81                  case 4:
82                  {
83                      if ("".equals(s))
84                          skip = false;
85                      else
86                          skip = Boolean.valueOf(s);
87                      break;
88                  }
89                  case 5:
90                  {
91                      if ("".equals(s))
92                          filtered = false;
93                      else
94                          filtered = Boolean.valueOf(s);
95                      break;
96                  }
97                  case 6:
98                  {
99                      if ("".equals(s))
100                         break;
101                     String[] incs = s.split(";");
102                     if (incs.length > 0)
103                         includes = Arrays.asList(incs);
104                     break;
105                 }
106                 case 7:
107                 { 
108                     if ("".equals(s))
109                         break;
110                     String[] exs = s.split(";");
111                     if (exs.length > 0)
112                         excludes = Arrays.asList(exs);
113                     break;
114                 }
115             }
116         }
117     }
118 
119     public OverlayConfig(Xpp3Dom root, List<String> defaultIncludes, List<String> defaultExcludes)
120     {
121         Xpp3Dom node = root.getChild("groupId");
122         setGroupId(node==null?null:node.getValue());
123         
124         node = root.getChild("artifactId");
125         setArtifactId(node==null?null:node.getValue());   
126         
127         node = root.getChild("classifier");
128         setClassifier(node==null?null:node.getValue());
129        
130         node = root.getChild("targetPath");
131         setTargetPath(node==null?null:node.getValue());
132         
133         node = root.getChild("skip");
134         setSkip(node==null?false:Boolean.valueOf(node.getValue()));
135 
136         node = root.getChild("filtered");
137         setFiltered(node==null?false:Boolean.valueOf(node.getValue()));
138 
139         node = root.getChild("includes");
140         List<String> includes = null;
141         if (node != null && node.getChildCount() > 0)
142         {
143             Xpp3Dom[] list = node.getChildren("include");
144             for (int j=0; list != null && j < list.length;j++)
145             {
146                 if (includes == null)
147                     includes = new ArrayList<String>();
148                 includes.add(list[j].getValue());
149             }
150         }
151         if (includes == null && defaultIncludes != null)
152         {
153             includes = new ArrayList<String>();
154             includes.addAll(defaultIncludes);
155         }
156         setIncludes(includes);
157         
158        
159         node = root.getChild("excludes");
160         List<String> excludes = null;
161         if (node != null && node.getChildCount() > 0)
162         {
163             Xpp3Dom[] list = node.getChildren("exclude");
164             for (int j=0; list != null && j < list.length;j++)
165             {
166                 if (excludes == null)
167                     excludes = new ArrayList<String>();
168                 excludes.add(list[j].getValue());
169             }
170         }
171         if (excludes == null && defaultExcludes != null)
172         {
173             excludes = new ArrayList<String>();
174             excludes.addAll(defaultExcludes);
175         }
176         setExcludes(excludes);
177     }
178     
179     public String getTargetPath()
180     {
181         return targetPath;
182     }
183 
184     public void setTargetPath(String targetPath)
185     {
186         this.targetPath = targetPath;
187     }
188 
189     public String getGroupId()
190     {
191         return groupId;
192     }
193 
194     public void setGroupId(String groupId)
195     {
196         this.groupId = groupId;
197     }
198 
199     public String getArtifactId()
200     {
201         return artifactId;
202     }
203 
204     public void setArtifactId(String artifactId)
205     {
206         this.artifactId = artifactId;
207     }
208 
209     public String getClassifier()
210     {
211         return classifier;
212     }
213 
214     public void setClassifier(String classifier)
215     {
216         this.classifier = classifier;
217     }
218 
219     public List<String> getIncludes()
220     {
221         return includes;
222     }
223 
224     public void setIncludes(List<String> includes)
225     {
226         this.includes = includes;
227     }
228 
229     public List<String> getExcludes()
230     {
231         return excludes;
232     }
233 
234     public void setExcludes(List<String> excludes)
235     {
236         this.excludes = excludes;
237     }
238 
239     public boolean isSkip()
240     {
241         return skip;
242     }
243 
244     public void setSkip(boolean skip)
245     {
246         this.skip = skip;
247     }
248 
249     public boolean isFiltered()
250     {
251         return filtered;
252     }
253 
254     public void setFiltered(boolean filtered)
255     {
256         this.filtered = filtered;
257     }
258     
259     public boolean isCurrentProject()
260     {
261         if (this.groupId == null && this.artifactId == null)
262             return true;
263         return false;
264     }
265     
266 
267     /**
268      * Check if this overlay configuration matches an Artifact's info
269      * 
270      * @param gid Artifact groupId
271      * @param aid Artifact artifactId
272      * @param cls Artifact classifier
273      * @return
274      */
275     public boolean matchesArtifact (String gid, String aid, String cls)
276     {
277         if (((getGroupId() == null && gid == null) || (getGroupId() != null && getGroupId().equals(gid)))
278            &&((getArtifactId() == null && aid == null) || (getArtifactId() != null && getArtifactId().equals(aid)))
279            &&((getClassifier() == null) || (getClassifier().equals(cls))))
280             return true;
281 
282         return false;
283     }
284     
285     /**
286      * Check if this overlay configuration matches an Artifact's info
287      * 
288      * @param gid
289      * @param aid
290      * @return
291      */
292     public boolean matchesArtifact (String gid, String aid)
293     {
294         if (((getGroupId() == null && gid == null) || (getGroupId() != null && getGroupId().equals(gid)))
295            &&((getArtifactId() == null && aid == null) || (getArtifactId() != null && getArtifactId().equals(aid))))
296             return true;
297 
298         return false;
299     }
300     
301     
302     
303     
304     public String toString()
305     {
306         StringBuffer strbuff = new StringBuffer();
307         strbuff.append((groupId != null ? groupId : "")+",");
308         strbuff.append((artifactId != null ? artifactId : "")+",");
309         strbuff.append((classifier != null ? classifier : "")+",");
310         strbuff.append((targetPath != null ? targetPath : "")+",");
311         strbuff.append(""+skip+",");
312         strbuff.append(""+filtered+",");
313      
314         if (includes != null)
315         {
316             Iterator<String> itor = includes.iterator();
317             while (itor.hasNext())
318             {
319                 strbuff.append(itor.next());
320                 if (itor.hasNext())
321                     strbuff.append(";");
322             }
323         }
324         
325         strbuff.append(", ");
326 
327         if (excludes != null)
328         {
329             Iterator<String> itor = excludes.iterator();
330             while (itor.hasNext())
331             {
332                 strbuff.append(itor.next());
333                 if (itor.hasNext())
334                     strbuff.append(";");
335             }
336         }
337   
338         return strbuff.toString();
339     }
340 }
341