KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > usability > plugin > ExpressionDocumenter


1 package org.apache.maven.usability.plugin;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.usability.plugin.io.xpp3.ParamdocXpp3Reader;
20 import org.codehaus.plexus.util.IOUtil;
21 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.net.URLClassLoader JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36
37 public class ExpressionDocumenter
38 {
39
40     private static final String JavaDoc[] EXPRESSION_ROOTS = { "project", "settings", "session", "plugin", "rootless" };
41
42     private static final String JavaDoc EXPRESSION_DOCO_ROOTPATH = "META-INF/maven/plugin-expressions/";
43
44     private static Map JavaDoc expressionDocumentation;
45
46     public static Map JavaDoc load()
47         throws ExpressionDocumentationException
48     {
49         if ( expressionDocumentation == null )
50         {
51             expressionDocumentation = new HashMap JavaDoc();
52             
53             ClassLoader JavaDoc docLoader = initializeDocLoader();
54
55             for ( int i = 0; i < EXPRESSION_ROOTS.length; i++ )
56             {
57                 InputStream JavaDoc docStream = null;
58                 try
59                 {
60                     docStream = docLoader
61                         .getResourceAsStream( EXPRESSION_DOCO_ROOTPATH + EXPRESSION_ROOTS[i] + ".paramdoc.xml" );
62
63                     if ( docStream != null )
64                     {
65                         Map JavaDoc doco = parseExpressionDocumentation( docStream );
66
67                         expressionDocumentation.putAll( doco );
68                     }
69                 }
70                 catch ( IOException JavaDoc e )
71                 {
72                     throw new ExpressionDocumentationException( "Failed to read documentation for expression root: " + EXPRESSION_ROOTS[i], e );
73                 }
74                 catch ( XmlPullParserException e )
75                 {
76                     throw new ExpressionDocumentationException( "Failed to parse documentation for expression root: " + EXPRESSION_ROOTS[i], e );
77                 }
78                 finally
79                 {
80                     IOUtil.close( docStream );
81                 }
82             }
83         }
84         
85         return expressionDocumentation;
86     }
87
88     /**
89      * <expressions>
90      * <expression>
91      * <syntax>project.distributionManagementArtifactRepository</syntax>
92      * <origin><![CDATA[
93      * <distributionManagement>
94      * <repository>
95      * <id>some-repo</id>
96      * <url>scp://host/path</url>
97      * </repository>
98      * <snapshotRepository>
99      * <id>some-snap-repo</id>
100      * <url>scp://host/snapshot-path</url>
101      * </snapshotRepository>
102      * </distributionManagement>
103      * ]]></origin>
104      * <usage><![CDATA[
105      * The repositories onto which artifacts should be deployed.
106      * One is for releases, the other for snapshots.
107      * ]]></usage>
108      * </expression>
109      * <expressions>
110      * @throws IOException
111      * @throws XmlPullParserException
112      */

113     private static Map JavaDoc parseExpressionDocumentation( InputStream JavaDoc docStream )
114         throws IOException JavaDoc, XmlPullParserException
115     {
116         Reader reader = new BufferedReader JavaDoc( new InputStreamReader JavaDoc( docStream ) );
117         
118         ParamdocXpp3Reader paramdocReader = new ParamdocXpp3Reader();
119         
120         ExpressionDocumentation documentation = paramdocReader.read( reader, true );
121         
122         List JavaDoc expressions = documentation.getExpressions();
123         
124         Map JavaDoc bySyntax = new HashMap JavaDoc();
125         
126         if ( expressions != null && !expressions.isEmpty() )
127         {
128             for ( Iterator JavaDoc it = expressions.iterator(); it.hasNext(); )
129             {
130                 Expression expr = (Expression) it.next();
131                 
132                 bySyntax.put( expr.getSyntax(), expr );
133             }
134         }
135         
136         return bySyntax;
137     }
138
139     private static ClassLoader JavaDoc initializeDocLoader()
140         throws ExpressionDocumentationException
141     {
142         String JavaDoc myResourcePath = ExpressionDocumenter.class.getName().replace( '.', '/' ) + ".class";
143         
144         URL JavaDoc myResource = ExpressionDocumenter.class.getClassLoader().getResource( myResourcePath );
145
146         String JavaDoc myClasspathEntry = myResource.getPath();
147         
148         myClasspathEntry = myClasspathEntry.substring( 0, myClasspathEntry.length() - ( myResourcePath.length() + 2 ) );
149         
150         if ( myClasspathEntry.startsWith( "file:" ) )
151         {
152             myClasspathEntry = myClasspathEntry.substring( "file:".length() );
153         }
154
155         URL JavaDoc docResource;
156         try
157         {
158             docResource = new File JavaDoc( myClasspathEntry ).toURL();
159         }
160         catch ( MalformedURLException JavaDoc e )
161         {
162             throw new ExpressionDocumentationException(
163                                                         "Cannot construct expression documentation classpath resource base.",
164                                                         e );
165         }
166
167         return new URLClassLoader JavaDoc( new URL JavaDoc[] { docResource } );
168     }
169
170 }
171
Popular Tags