View Javadoc
1   package org.apache.maven.usability.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  /**
39   * ExpressionDocumenter
40   */
41  public class ExpressionDocumenter
42  {
43  
44      private static final String[] EXPRESSION_ROOTS =
45      {
46          "project", "settings", "session", "plugin", "rootless"
47      };
48  
49      private static final String EXPRESSION_DOCO_ROOTPATH = "META-INF/maven/plugin-expressions/";
50  
51      private static Map<String, Expression> expressionDocumentation;
52  
53      public static Map<String, Expression> load()
54          throws ExpressionDocumentationException
55      {
56          if ( expressionDocumentation == null )
57          {
58              expressionDocumentation = new HashMap<>();
59  
60              ClassLoader docLoader = initializeDocLoader();
61  
62              for ( String root : EXPRESSION_ROOTS )
63              {
64                  try ( InputStream docStream = docLoader.getResourceAsStream(
65                      EXPRESSION_DOCO_ROOTPATH + root + ".paramdoc.xml" ) )
66                  {
67                      if ( docStream != null )
68                      {
69                          Map<String, Expression> doco = parseExpressionDocumentation( docStream );
70  
71                          expressionDocumentation.putAll( doco );
72                      }
73                  }
74                  catch ( IOException e )
75                  {
76                      throw new ExpressionDocumentationException(
77                          "Failed to read documentation for expression root: " + root, e );
78                  }
79                  catch ( XmlPullParserException e )
80                  {
81                      throw new ExpressionDocumentationException(
82                          "Failed to parse documentation for expression root: " + root, e );
83                  }
84  
85              }
86          }
87  
88          return expressionDocumentation;
89      }
90  
91      /**
92       * <expressions>
93       * <expression>
94       * <syntax>project.distributionManagementArtifactRepository</syntax>
95       * <origin><![CDATA[
96       * <distributionManagement>
97       * <repository>
98       * <id>some-repo</id>
99       * <url>scp://host/path</url>
100      * </repository>
101      * <snapshotRepository>
102      * <id>some-snap-repo</id>
103      * <url>scp://host/snapshot-path</url>
104      * </snapshotRepository>
105      * </distributionManagement>
106      * ]]></origin>
107      * <usage><![CDATA[
108      * The repositories onto which artifacts should be deployed.
109      * One is for releases, the other for snapshots.
110      * ]]></usage>
111      * </expression>
112      * <expressions>
113      *
114      * @throws IOException
115      * @throws XmlPullParserException
116      */
117     private static Map<String, Expression> 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<Expression> expressions = documentation.getExpressions();
127 
128         Map<String, Expression> bySyntax = new HashMap<>();
129 
130         if ( expressions != null && !expressions.isEmpty() )
131         {
132             for ( Expression expression : expressions )
133             {
134                 bySyntax.put( expression.getSyntax(), expression );
135             }
136         }
137 
138         return bySyntax;
139     }
140 
141     private static ClassLoader initializeDocLoader()
142         throws ExpressionDocumentationException
143     {
144         String myResourcePath = ExpressionDocumenter.class.getName().replace( '.', '/' ) + ".class";
145 
146         URL myResource = ExpressionDocumenter.class.getClassLoader().getResource( myResourcePath );
147 
148         assert myResource != null : "The resource is this class itself loaded by its own classloader and must exist";
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(
167                 "Cannot construct expression documentation classpath" + " resource base.", e );
168         }
169 
170         return new URLClassLoader( new URL[]
171         {
172             docResource
173         } );
174     }
175 
176 }