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