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.Iterator; |
37 | |
import java.util.List; |
38 | |
import java.util.Map; |
39 | |
|
40 | 0 | public class ExpressionDocumenter |
41 | |
{ |
42 | |
|
43 | 1 | private static final String[] EXPRESSION_ROOTS = { "project", "settings", "session", "plugin", "rootless" }; |
44 | |
|
45 | |
private static final String EXPRESSION_DOCO_ROOTPATH = "META-INF/maven/plugin-expressions/"; |
46 | |
|
47 | |
private static Map expressionDocumentation; |
48 | |
|
49 | |
public static Map load() |
50 | |
throws ExpressionDocumentationException |
51 | |
{ |
52 | 7 | if ( expressionDocumentation == null ) |
53 | |
{ |
54 | 1 | expressionDocumentation = new HashMap(); |
55 | |
|
56 | 1 | ClassLoader docLoader = initializeDocLoader(); |
57 | |
|
58 | 6 | for ( int i = 0; i < EXPRESSION_ROOTS.length; i++ ) |
59 | |
{ |
60 | 5 | InputStream docStream = null; |
61 | |
try |
62 | |
{ |
63 | 5 | docStream = docLoader |
64 | |
.getResourceAsStream( EXPRESSION_DOCO_ROOTPATH + EXPRESSION_ROOTS[i] + ".paramdoc.xml" ); |
65 | |
|
66 | 5 | if ( docStream != null ) |
67 | |
{ |
68 | 0 | Map doco = parseExpressionDocumentation( docStream ); |
69 | |
|
70 | 0 | expressionDocumentation.putAll( doco ); |
71 | |
} |
72 | |
} |
73 | 0 | catch ( IOException e ) |
74 | |
{ |
75 | 0 | throw new ExpressionDocumentationException( "Failed to read documentation for expression root: " + EXPRESSION_ROOTS[i], e ); |
76 | |
} |
77 | 0 | catch ( XmlPullParserException e ) |
78 | |
{ |
79 | 0 | throw new ExpressionDocumentationException( "Failed to parse documentation for expression root: " + EXPRESSION_ROOTS[i], e ); |
80 | |
} |
81 | |
finally |
82 | |
{ |
83 | 5 | IOUtil.close( docStream ); |
84 | 5 | } |
85 | |
} |
86 | |
} |
87 | |
|
88 | 7 | return expressionDocumentation; |
89 | |
} |
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 | |
private static Map parseExpressionDocumentation( InputStream docStream ) |
117 | |
throws IOException, XmlPullParserException |
118 | |
{ |
119 | 0 | Reader reader = new BufferedReader( ReaderFactory.newXmlReader( docStream ) ); |
120 | |
|
121 | 0 | ParamdocXpp3Reader paramdocReader = new ParamdocXpp3Reader(); |
122 | |
|
123 | 0 | ExpressionDocumentation documentation = paramdocReader.read( reader, true ); |
124 | |
|
125 | 0 | List expressions = documentation.getExpressions(); |
126 | |
|
127 | 0 | Map bySyntax = new HashMap(); |
128 | |
|
129 | 0 | if ( expressions != null && !expressions.isEmpty() ) |
130 | |
{ |
131 | 0 | for ( Iterator it = expressions.iterator(); it.hasNext(); ) |
132 | |
{ |
133 | 0 | Expression expr = (Expression) it.next(); |
134 | |
|
135 | 0 | bySyntax.put( expr.getSyntax(), expr ); |
136 | 0 | } |
137 | |
} |
138 | |
|
139 | 0 | return bySyntax; |
140 | |
} |
141 | |
|
142 | |
private static ClassLoader initializeDocLoader() |
143 | |
throws ExpressionDocumentationException |
144 | |
{ |
145 | 1 | String myResourcePath = ExpressionDocumenter.class.getName().replace( '.', '/' ) + ".class"; |
146 | |
|
147 | 1 | URL myResource = ExpressionDocumenter.class.getClassLoader().getResource( myResourcePath ); |
148 | |
|
149 | 1 | String myClasspathEntry = myResource.getPath(); |
150 | |
|
151 | 1 | myClasspathEntry = myClasspathEntry.substring( 0, myClasspathEntry.length() - ( myResourcePath.length() + 2 ) ); |
152 | |
|
153 | 1 | if ( myClasspathEntry.startsWith( "file:" ) ) |
154 | |
{ |
155 | 0 | myClasspathEntry = myClasspathEntry.substring( "file:".length() ); |
156 | |
} |
157 | |
|
158 | |
URL docResource; |
159 | |
try |
160 | |
{ |
161 | 1 | docResource = new File( myClasspathEntry ).toURL(); |
162 | |
} |
163 | 0 | catch ( MalformedURLException e ) |
164 | |
{ |
165 | 0 | throw new ExpressionDocumentationException( |
166 | |
"Cannot construct expression documentation classpath resource base.", |
167 | |
e ); |
168 | 1 | } |
169 | |
|
170 | 1 | return new URLClassLoader( new URL[] { docResource } ); |
171 | |
} |
172 | |
|
173 | |
} |