View Javadoc

1   package org.apache.maven.archetype.source;
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 org.apache.maven.archetype.catalog.Archetype;
23  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
24  import org.apache.maven.archetype.source.ArchetypeDataSource;
25  import org.apache.maven.archetype.source.ArchetypeDataSourceException;
26  import org.codehaus.plexus.component.annotations.Component;
27  import org.codehaus.plexus.util.IOUtil;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  
32  import java.net.URL;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  import java.util.Properties;
37  import java.util.regex.Matcher;
38  import java.util.regex.Pattern;
39  
40  /**
41   * An archetype data source getting its content from a Confluence Wiki page.
42   * By default, <a href="http://docs.codehaus.org/display/MAVENUSER/Archetypes+List">MAVENUSER/Archetypes List</a>
43   * is used.
44   *
45   * @author            Jason van Zyl
46   */
47  @Component( role = ArchetypeDataSource.class, hint = "wiki" )
48  public class WikiArchetypeDataSource
49      implements ArchetypeDataSource
50  {
51      private static String DEFAULT_ARCHETYPE_INVENTORY_PAGE =
52          "http://docs.codehaus.org/pages/viewpagesrc.action?pageId=48400";
53  
54      static String cleanup( String val )
55      {
56          val = val.replaceAll( "\\r|\\n|\\s{2,}|\\[|\\|[^\\]]+]|\\]", "" );
57          return val;
58      }
59  
60      static String cleanupUrl( String val )
61      {
62          return val.replaceAll( "\\r|\\n|\\s{2,}|\\[|\\]|\\&nbsp;", "" );
63      }
64  
65      public ArchetypeCatalog getArchetypeCatalog( Properties properties )
66          throws ArchetypeDataSourceException
67      {
68          ArchetypeCatalog ac = new ArchetypeCatalog();
69          ac.setArchetypes( getArchetypes( properties ) );
70          return ac;
71      }
72  
73      public List<Archetype> getArchetypes( Properties properties )
74          throws ArchetypeDataSourceException
75      {
76          String url = properties.getProperty( "url" );
77  
78          if ( url == null )
79          {
80              url = DEFAULT_ARCHETYPE_INVENTORY_PAGE;
81          }
82  
83          List<Archetype> archetypes = new ArrayList<Archetype>();
84  
85          String pageSource = "";
86          InputStream in = null;
87          try
88          {
89              in = new URL( cleanupUrl( url ) ).openStream();
90  
91              pageSource = IOUtil.toString( in );
92          }
93          catch ( IOException e )
94          {
95              throw new ArchetypeDataSourceException( "Error retrieving list of archetypes from " + url );
96          }
97          finally
98          {
99              IOUtil.close( in );
100         }
101 
102         // | ArtifactId | GroupId | Version | Repository | Description |
103         Pattern ptn =
104             Pattern.compile(
105                 "<br>\\|([-a-zA-Z0-9_. ]+)\\|([-a-zA-Z0-9_. ]+)\\|([-a-zA-Z0-9_. ]+)\\|([-a-zA-Z0-9_.:/ \\[\\],]+)\\|(.*) \\|"
106             );
107 
108         Matcher m = ptn.matcher( pageSource );
109 
110         while ( m.find() )
111         {
112             Archetype archetype = new Archetype();
113 
114             archetype.setArtifactId( m.group( 1 ).trim() );
115 
116             archetype.setGroupId( m.group( 2 ).trim() );
117 
118             String version = m.group( 3 ).trim();
119             if ( version.equals( "" ) )
120             {
121                 version = "RELEASE";
122             }
123             archetype.setVersion( version );
124 
125             archetype.setRepository( cleanupUrl( m.group( 4 ).trim() ) );
126 
127             archetype.setDescription( cleanup( m.group( 5 ).trim() ) );
128 
129             archetypes.add( archetype );
130         }
131         return archetypes;
132     }
133 
134     public void updateCatalog( Properties properties, Archetype archetype )
135         throws ArchetypeDataSourceException
136     {
137         throw new UnsupportedOperationException( "Not supported yet." );
138     }
139 }