1 package org.apache.maven.archetype.source;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41
42
43
44
45
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,}|\\[|\\]|\\ ", "" );
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
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 }