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  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       * <expressions>
87       * <expression>
88       * <syntax>project.distributionManagementArtifactRepository</syntax>
89       * <origin><![CDATA[
90       * <distributionManagement>
91       * <repository>
92       * <id>some-repo</id>
93       * <url>scp://host/path</url>
94       * </repository>
95       * <snapshotRepository>
96       * <id>some-snap-repo</id>
97       * <url>scp://host/snapshot-path</url>
98       * </snapshotRepository>
99       * </distributionManagement>
100      * ]]></origin>
101      * <usage><![CDATA[
102      * The repositories onto which artifacts should be deployed.
103      * One is for releases, the other for snapshots.
104      * ]]></usage>
105      * </expression>
106      * <expressions>
107      *
108      * @throws IOException
109      * @throws XmlPullParserException
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 }