View Javadoc

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