aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/usability/plugin/ExpressionDocumenter.java
blob: 0e98b0cb297a1a6d78f9f5b2e7ecc96ef68ec026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package org.apache.maven.usability.plugin;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.maven.usability.plugin.io.xpp3.ParamdocXpp3Reader;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExpressionDocumenter
{

    private static final String[] EXPRESSION_ROOTS = { "project", "settings", "session", "plugin", "rootless" };

    private static final String EXPRESSION_DOCO_ROOTPATH = "META-INF/maven/plugin-expressions/";

    private static Map expressionDocumentation;

    public static Map load()
        throws ExpressionDocumentationException
    {
        if ( expressionDocumentation == null )
        {
            expressionDocumentation = new HashMap();

            ClassLoader docLoader = initializeDocLoader();

            for ( String EXPRESSION_ROOT : EXPRESSION_ROOTS )
            {
                InputStream docStream = null;
                try
                {
                    docStream =
                        docLoader.getResourceAsStream( EXPRESSION_DOCO_ROOTPATH + EXPRESSION_ROOT + ".paramdoc.xml" );

                    if ( docStream != null )
                    {
                        Map doco = parseExpressionDocumentation( docStream );

                        expressionDocumentation.putAll( doco );
                    }
                }
                catch ( IOException e )
                {
                    throw new ExpressionDocumentationException(
                        "Failed to read documentation for expression root: " + EXPRESSION_ROOT, e );
                }
                catch ( XmlPullParserException e )
                {
                    throw new ExpressionDocumentationException(
                        "Failed to parse documentation for expression root: " + EXPRESSION_ROOT, e );
                }
                finally
                {
                    IOUtil.close( docStream );
                }
            }
        }

        return expressionDocumentation;
    }

    /**
     * <expressions>
     *   <expression>
     *     <syntax>project.distributionManagementArtifactRepository</syntax>
     *     <origin><![CDATA[
     *   <distributionManagement>
     *     <repository>
     *       <id>some-repo</id>
     *       <url>scp://host/path</url>
     *     </repository>
     *     <snapshotRepository>
     *       <id>some-snap-repo</id>
     *       <url>scp://host/snapshot-path</url>
     *     </snapshotRepository>
     *   </distributionManagement>
     *   ]]></origin>
     *     <usage><![CDATA[
     *   The repositories onto which artifacts should be deployed.
     *   One is for releases, the other for snapshots.
     *   ]]></usage>
     *   </expression>
     * <expressions>
     * @throws IOException
     * @throws XmlPullParserException
     */
    private static Map parseExpressionDocumentation( InputStream docStream )
        throws IOException, XmlPullParserException
    {
        Reader reader = new BufferedReader( ReaderFactory.newXmlReader( docStream ) );

        ParamdocXpp3Reader paramdocReader = new ParamdocXpp3Reader();

        ExpressionDocumentation documentation = paramdocReader.read( reader, true );

        List expressions = documentation.getExpressions();

        Map bySyntax = new HashMap();

        if ( expressions != null && !expressions.isEmpty() )
        {
            for ( Object expression : expressions )
            {
                Expression expr = (Expression) expression;

                bySyntax.put( expr.getSyntax(), expr );
            }
        }

        return bySyntax;
    }

    private static ClassLoader initializeDocLoader()
        throws ExpressionDocumentationException
    {
        String myResourcePath = ExpressionDocumenter.class.getName().replace( '.', '/' ) + ".class";

        URL myResource = ExpressionDocumenter.class.getClassLoader().getResource( myResourcePath );

        assert myResource != null : "The resource is this class itself loaded by its own classloader and must exist";

        String myClasspathEntry = myResource.getPath();

        myClasspathEntry = myClasspathEntry.substring( 0, myClasspathEntry.length() - ( myResourcePath.length() + 2 ) );

        if ( myClasspathEntry.startsWith( "file:" ) )
        {
            myClasspathEntry = myClasspathEntry.substring( "file:".length() );
        }

        URL docResource;
        try
        {
            docResource = new File( myClasspathEntry ).toURL();
        }
        catch ( MalformedURLException e )
        {
            throw new ExpressionDocumentationException( "Cannot construct expression documentation classpath"
                + " resource base.", e );
        }

        return new URLClassLoader( new URL[] { docResource } );
    }

}