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.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
42
43
44
45
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,}|\\[|\\]|\\ ", "" );
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
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 }