View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * ExpressionDocumenter
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       * <expressions>
75       * <expression>
76       * <syntax>project.distributionManagementArtifactRepository</syntax>
77       * <origin><![CDATA[
78       * <distributionManagement>
79       * <repository>
80       * <id>some-repo</id>
81       * <url>scp://host/path</url>
82       * </repository>
83       * <snapshotRepository>
84       * <id>some-snap-repo</id>
85       * <url>scp://host/snapshot-path</url>
86       * </snapshotRepository>
87       * </distributionManagement>
88       * ]]></origin>
89       * <usage><![CDATA[
90       * The repositories onto which artifacts should be deployed.
91       * One is for releases, the other for snapshots.
92       * ]]></usage>
93       * </expression>
94       * <expressions>
95       *
96       * @throws IOException
97       * @throws XmlPullParserException
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 }