View Javadoc
1   package org.codehaus.mojo.gwt;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.codehaus.mojo.gwt.utils.GwtModuleReaderException;
34  import org.codehaus.plexus.util.xml.Xpp3Dom;
35  
36  
37  
38  
39  /**
40   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
41   */
42  public class GwtModule
43  {
44      private Xpp3Dom xml;
45  
46      private String name;
47  
48      private Set<GwtModule> inherits;
49  
50      private GwtModuleReader reader;
51      
52      private File sourceFile;
53  
54      public GwtModule( String name, Xpp3Dom xml, GwtModuleReader reader )
55      {
56          this.name = name;
57          this.xml = xml;
58          this.reader = reader;
59      }
60  
61      private String getRenameTo()
62      {
63          return xml.getAttribute( "rename-to" );
64      }
65  
66      public String getPublic()
67      {
68          Xpp3Dom node = xml.getChild( "public" );
69          return ( node == null ? "public" : node.getAttribute( "path" ) );
70      }
71  
72      public String[] getSuperSources()
73      {
74          Xpp3Dom nodes[] = xml.getChildren( "super-source" );
75          if ( nodes == null )
76          {
77              return new String[0];
78          }
79          String[] superSources = new String[nodes.length];
80          int i = 0;
81          for ( Xpp3Dom node : nodes )
82          {
83              String path = node.getAttribute( "path" );
84              if ( path == null )
85              {
86                  path = "";
87              }
88              superSources[i++] = path;
89          }
90          return superSources;
91      }
92  
93      public String[] getSources()
94      {
95          Xpp3Dom nodes[] = xml.getChildren( "source" );
96          if ( nodes == null || nodes.length == 0 )
97          {
98              return new String[] { "client" };
99          }
100         String[] sources = new String[nodes.length];
101         int i = 0;
102         for ( Xpp3Dom node : nodes )
103         {
104             sources[i++] = node.getAttribute( "path" );
105         }
106         return sources;
107     }
108 
109     public List<String> getEntryPoints()
110         throws GwtModuleReaderException
111     {
112         List<String> entryPoints = new ArrayList<String>();
113         entryPoints.addAll( getLocalEntryPoints() );
114         for ( GwtModule module : getInherits() )
115         {
116             entryPoints.addAll( module.getLocalEntryPoints() );
117         }
118         return entryPoints;
119     }
120 
121     private List<String> getLocalEntryPoints()
122     {
123         Xpp3Dom nodes[] = xml.getChildren( "entry-point" );
124         if ( nodes == null )
125         {
126             return Collections.emptyList();
127         }
128         List<String> entryPoints = new ArrayList<String>( nodes.length );
129         for ( Xpp3Dom node : nodes )
130         {
131             entryPoints.add( node.getAttribute( "class" ) );
132         }
133         return entryPoints;
134     }
135 
136     /**
137      * Build the set of inhertied modules. Due to xml inheritence mecanism, there may be cicles in the inheritence
138      * graph, so we build a set of inherited modules
139      */
140     public Set<GwtModule> getInherits()
141 		throws GwtModuleReaderException
142     {
143         if ( inherits != null )
144         {
145             return inherits;
146         }
147 
148         inherits = new HashSet<GwtModule>();
149         addInheritedModules( inherits, getLocalInherits() );
150 
151         return inherits;
152     }
153 
154     /**
155      * 
156      * @param set
157      * @param modules
158      * @throws MojoExecutionException
159      */
160     private void addInheritedModules( Set<GwtModule> set, Set<GwtModule> modules )
161         throws GwtModuleReaderException
162     {
163         for ( GwtModule module : modules )
164         {
165             if ( set.add( module ) )
166             {
167                 // if module is allready in the set, don't re-parse it's inherits
168                 addInheritedModules( set, module.getLocalInherits() );
169             }
170         }
171 
172     }
173 
174     private Set<GwtModule> getLocalInherits()
175         throws GwtModuleReaderException
176     {
177         Xpp3Dom nodes[] = xml.getChildren( "inherits" );
178         if ( nodes == null )
179         {
180             return Collections.emptySet();
181         }
182         Set<GwtModule> modules = new HashSet<GwtModule>();
183         for ( Xpp3Dom node : nodes )
184         {
185             String moduleName = node.getAttribute( "name" );
186             // exclude modules from gwt-dev/gwt-user
187             if ( !moduleName.startsWith( "com.google.gwt." ) )
188             {
189                 modules.add( reader.readModule( moduleName ) );
190             }
191         }
192         return modules;
193     }
194 
195     public Map<String, String> getServlets()
196         throws GwtModuleReaderException
197     {
198         return getServlets( getPath() );
199     }
200 
201     public Map<String, String> getServlets( String path )
202         throws GwtModuleReaderException
203     {
204         Map<String, String> servlets = getLocalServlets( path );
205         for ( GwtModule module : getInherits() )
206         {
207             servlets.putAll( module.getLocalServlets( path ) );
208         }
209         return servlets;
210     }
211 
212     private Map<String, String> getLocalServlets( String path )
213     {
214         Map<String, String> servlets = new HashMap<String, String>();
215         Xpp3Dom nodes[] = xml.getChildren( "servlet" );
216         if ( nodes != null )
217         {
218             for ( Xpp3Dom node : nodes )
219             {
220                 servlets.put( StringUtils.isBlank( path ) ? node.getAttribute( "path" ) : path + node.getAttribute( "path" ),
221                               node.getAttribute( "class" ) );
222             }
223         }
224         return servlets;
225     }
226 
227     public String getName()
228     {
229         return name;
230     }
231 
232     public String getPackage()
233     {
234         int index = name.lastIndexOf( '.' );
235         return ( index < 0 ) ? "" : name.substring( 0, index );
236     }
237 
238     public String getPath()
239     {
240         if ( getRenameTo() != null )
241         {
242             return getRenameTo();
243         }
244         return name;
245     }
246 
247     public File getSourceFile() {
248     	return sourceFile;
249     }
250     
251     public void setSourceFile(File file) {
252 		this.sourceFile = file;
253 	}
254 	
255     @Override
256     public boolean equals( Object obj )
257     {
258         return name.equals( ( (GwtModule) obj ).name );
259     }
260 
261     @Override
262     public int hashCode()
263     {
264         return name.hashCode();
265     }
266 
267 }