1 package org.apache.maven.usability.plugin;
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.usability.plugin.io.xpp3.ParamdocXpp3Reader;
23 import org.codehaus.plexus.util.IOUtil;
24 import org.codehaus.plexus.util.ReaderFactory;
25 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
26
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.Reader;
32 import java.net.MalformedURLException;
33 import java.net.URL;
34 import java.net.URLClassLoader;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 public class ExpressionDocumenter
40 {
41
42 private static final String[] EXPRESSION_ROOTS = { "project", "settings", "session", "plugin", "rootless" };
43
44 private static final String EXPRESSION_DOCO_ROOTPATH = "META-INF/maven/plugin-expressions/";
45
46 private static Map expressionDocumentation;
47
48 public static Map load()
49 throws ExpressionDocumentationException
50 {
51 if ( expressionDocumentation == null )
52 {
53 expressionDocumentation = new HashMap();
54
55 ClassLoader docLoader = initializeDocLoader();
56
57 for ( String EXPRESSION_ROOT : EXPRESSION_ROOTS )
58 {
59 InputStream docStream = null;
60 try
61 {
62 docStream =
63 docLoader.getResourceAsStream( EXPRESSION_DOCO_ROOTPATH + EXPRESSION_ROOT + ".paramdoc.xml" );
64
65 if ( docStream != null )
66 {
67 Map doco = parseExpressionDocumentation( docStream );
68
69 expressionDocumentation.putAll( doco );
70 }
71 }
72 catch ( IOException e )
73 {
74 throw new ExpressionDocumentationException(
75 "Failed to read documentation for expression root: " + EXPRESSION_ROOT, e );
76 }
77 catch ( XmlPullParserException e )
78 {
79 throw new ExpressionDocumentationException(
80 "Failed to parse documentation for expression root: " + EXPRESSION_ROOT, e );
81 }
82 finally
83 {
84 IOUtil.close( docStream );
85 }
86 }
87 }
88
89 return expressionDocumentation;
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 private static Map parseExpressionDocumentation( InputStream docStream )
118 throws IOException, XmlPullParserException
119 {
120 Reader reader = new BufferedReader( ReaderFactory.newXmlReader( docStream ) );
121
122 ParamdocXpp3Reader paramdocReader = new ParamdocXpp3Reader();
123
124 ExpressionDocumentation documentation = paramdocReader.read( reader, true );
125
126 List expressions = documentation.getExpressions();
127
128 Map bySyntax = new HashMap();
129
130 if ( expressions != null && !expressions.isEmpty() )
131 {
132 for ( Object expression : expressions )
133 {
134 Expression expr = (Expression) expression;
135
136 bySyntax.put( expr.getSyntax(), expr );
137 }
138 }
139
140 return bySyntax;
141 }
142
143 private static ClassLoader initializeDocLoader()
144 throws ExpressionDocumentationException
145 {
146 String myResourcePath = ExpressionDocumenter.class.getName().replace( '.', '/' ) + ".class";
147
148 URL myResource = ExpressionDocumenter.class.getClassLoader().getResource( myResourcePath );
149
150 String myClasspathEntry = myResource.getPath();
151
152 myClasspathEntry = myClasspathEntry.substring( 0, myClasspathEntry.length() - ( myResourcePath.length() + 2 ) );
153
154 if ( myClasspathEntry.startsWith( "file:" ) )
155 {
156 myClasspathEntry = myClasspathEntry.substring( "file:".length() );
157 }
158
159 URL docResource;
160 try
161 {
162 docResource = new File( myClasspathEntry ).toURL();
163 }
164 catch ( MalformedURLException e )
165 {
166 throw new ExpressionDocumentationException( "Cannot construct expression documentation classpath"
167 + " resource base.", e );
168 }
169
170 return new URLClassLoader( new URL[] { docResource } );
171 }
172
173 }