1 package org.apache.maven.project;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.jar.JarFile;
30 import java.util.zip.ZipEntry;
31
32 import org.codehaus.plexus.util.IOUtil;
33 import org.codehaus.plexus.util.ReaderFactory;
34 import org.codehaus.plexus.util.xml.Xpp3Dom;
35 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
36 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37
38
39
40
41
42
43 class ExtensionDescriptorBuilder
44 {
45
46 private String getExtensionDescriptorLocation()
47 {
48 return "META-INF/maven/extension.xml";
49 }
50
51
52
53
54
55
56
57
58 public ExtensionDescriptor build( File extensionJar )
59 throws IOException
60 {
61 ExtensionDescriptor extensionDescriptor = null;
62
63 if ( extensionJar.isFile() )
64 {
65 JarFile pluginJar = new JarFile( extensionJar, false );
66 try
67 {
68 ZipEntry pluginDescriptorEntry = pluginJar.getEntry( getExtensionDescriptorLocation() );
69
70 if ( pluginDescriptorEntry != null )
71 {
72 InputStream is = pluginJar.getInputStream( pluginDescriptorEntry );
73
74 extensionDescriptor = build( is );
75 }
76 }
77 finally
78 {
79 pluginJar.close();
80 }
81 }
82 else
83 {
84 File pluginXml = new File( extensionJar, getExtensionDescriptorLocation() );
85
86 if ( pluginXml.canRead() )
87 {
88 InputStream is = new BufferedInputStream( new FileInputStream( pluginXml ) );
89 try
90 {
91 extensionDescriptor = build( is );
92 }
93 finally
94 {
95 IOUtil.close( is );
96 }
97 }
98 }
99
100 return extensionDescriptor;
101 }
102
103 ExtensionDescriptor build( InputStream is )
104 throws IOException
105 {
106 ExtensionDescriptor extensionDescriptor = new ExtensionDescriptor();
107
108 Xpp3Dom dom;
109 try
110 {
111 dom = Xpp3DomBuilder.build( ReaderFactory.newXmlReader( is ) );
112 }
113 catch ( XmlPullParserException e )
114 {
115 throw (IOException) new IOException( e.getMessage() ).initCause( e );
116 }
117 finally
118 {
119 IOUtil.close( is );
120 }
121
122 if ( !"extension".equals( dom.getName() ) )
123 {
124 throw new IOException( "Unexpected root element \"" + dom.getName() + "\", expected \"extension\"" );
125 }
126
127 extensionDescriptor.setExportedPackages( parseStrings( dom.getChild( "exportedPackages" ) ) );
128
129 extensionDescriptor.setExportedArtifacts( parseStrings( dom.getChild( "exportedArtifacts" ) ) );
130
131 return extensionDescriptor;
132 }
133
134 private List<String> parseStrings( Xpp3Dom dom )
135 {
136 List<String> strings = null;
137
138 if ( dom != null )
139 {
140 strings = new ArrayList<String>();
141
142 for ( Xpp3Dom child : dom.getChildren() )
143 {
144 String string = child.getValue();
145 if ( string != null )
146 {
147 string = string.trim();
148 if ( string.length() > 0 )
149 {
150 strings.add( string );
151 }
152 }
153 }
154 }
155
156 return strings;
157 }
158
159 }