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