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