1 package org.apache.maven.usability.plugin; 2 3 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 ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.Reader ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.net.URLClassLoader ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Map ; 36 37 public class ExpressionDocumenter 38 { 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 expressionDocumentation; 45 46 public static Map load() 47 throws ExpressionDocumentationException 48 { 49 if ( expressionDocumentation == null ) 50 { 51 expressionDocumentation = new HashMap (); 52 53 ClassLoader docLoader = initializeDocLoader(); 54 55 for ( int i = 0; i < EXPRESSION_ROOTS.length; i++ ) 56 { 57 InputStream 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 doco = parseExpressionDocumentation( docStream ); 66 67 expressionDocumentation.putAll( doco ); 68 } 69 } 70 catch ( IOException 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 113 private static Map parseExpressionDocumentation( InputStream docStream ) 114 throws IOException , XmlPullParserException 115 { 116 Reader reader = new BufferedReader ( new InputStreamReader ( docStream ) ); 117 118 ParamdocXpp3Reader paramdocReader = new ParamdocXpp3Reader(); 119 120 ExpressionDocumentation documentation = paramdocReader.read( reader, true ); 121 122 List expressions = documentation.getExpressions(); 123 124 Map bySyntax = new HashMap (); 125 126 if ( expressions != null && !expressions.isEmpty() ) 127 { 128 for ( Iterator 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 initializeDocLoader() 140 throws ExpressionDocumentationException 141 { 142 String myResourcePath = ExpressionDocumenter.class.getName().replace( '.', '/' ) + ".class"; 143 144 URL myResource = ExpressionDocumenter.class.getClassLoader().getResource( myResourcePath ); 145 146 String 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 docResource; 156 try 157 { 158 docResource = new File ( myClasspathEntry ).toURL(); 159 } 160 catch ( MalformedURLException e ) 161 { 162 throw new ExpressionDocumentationException( 163 "Cannot construct expression documentation classpath resource base.", 164 e ); 165 } 166 167 return new URLClassLoader ( new URL [] { docResource } ); 168 } 169 170 } 171 | Popular Tags |