1 package org.apache.maven.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Properties;
29
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.parsers.SAXParser;
32 import javax.xml.parsers.SAXParserFactory;
33
34 import org.apache.commons.jelly.Script;
35 import org.apache.maven.MavenException;
36 import org.apache.maven.MavenUtils;
37 import org.apache.maven.jelly.MavenJellyContext;
38 import org.apache.maven.project.Project;
39 import org.xml.sax.InputSource;
40 import org.xml.sax.SAXException;
41
42 /**
43 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
44 *
45 * @version $Id: JellyScriptHousing.java 530196 2007-04-18 23:04:51Z aheritier $
46 */
47 public class JellyScriptHousing
48 {
49 /** Plug-in default properties name. */
50 private static final String PLUGIN_PROPERTIES_NAME = "plugin.properties";
51
52 /** Plugin properties. */
53 private Map pluginProperties = new HashMap();
54
55 /** Plugin Project. */
56 private Project project;
57
58 /** Jelly Script. */
59 private Script script;
60
61 /** the source of the jelly script. */
62 private File source;
63
64 private final String name;
65
66 private final File pluginDirectory;
67
68 private final MavenJellyContext parentContext;
69
70 public JellyScriptHousing()
71 {
72 this.name = null;
73 this.pluginDirectory = null;
74 this.parentContext = null;
75 }
76
77 public JellyScriptHousing( File pluginDir, MavenJellyContext parentContext ) throws IOException
78 {
79 this.pluginDirectory = pluginDir;
80 this.name = pluginDir.getName();
81 this.source = new File( pluginDir, "plugin.jelly" );
82 this.parentContext = parentContext;
83 this.pluginProperties = loadPluginProperties();
84 }
85
86
87
88
89
90 public File getSource()
91 {
92 return source;
93 }
94
95 public void setSource( File source )
96 {
97 this.source = source;
98 }
99
100 /**
101 *
102 * @return Project
103 */
104 public Project getProject() throws MavenException
105 {
106 if ( project == null )
107 {
108 project = MavenUtils.getProject( new File( pluginDirectory, "project.xml" ), parentContext, false );
109 }
110 return project;
111 }
112
113 /**
114 *
115 * @param project
116 */
117 public void setProject( Project project )
118 {
119 this.project = project;
120 }
121
122 public void setScript( Script script )
123 {
124 this.script = script;
125 }
126
127 /**
128 *
129 * @return Script
130 */
131 Script getScript()
132 {
133 return script;
134 }
135
136 public String toString()
137 {
138 return "\n source = " + getSource() + "\n project = " + project + "\n script = " + script;
139 }
140
141 /**
142 * @return String
143 */
144 public String getName()
145 {
146 return name;
147 }
148
149 void parse( PluginDefinitionHandler handler, String systemId, InputStream inStream ) throws MavenException
150 {
151 try
152 {
153 SAXParserFactory factory = SAXParserFactory.newInstance();
154 factory.setNamespaceAware( true );
155 SAXParser parser = factory.newSAXParser();
156 InputSource is = new InputSource( inStream );
157 is.setSystemId( systemId );
158 parser.parse( is, new PluginScriptParser( handler, this ) );
159 }
160 catch ( ParserConfigurationException e )
161 {
162 throw new MavenException( systemId + " : Error parsing plugin script", e );
163 }
164 catch ( SAXException e )
165 {
166 throw new MavenException( systemId + " : Error parsing plugin script", e );
167 }
168 catch ( IOException e )
169 {
170 throw new MavenException( systemId + " : Error reading plugin script", e );
171 }
172 }
173
174 void parse( PluginDefinitionHandler handler ) throws MavenException
175 {
176 FileInputStream fis = null;
177 try
178 {
179 fis = new FileInputStream( source );
180 parse( handler, source.getAbsolutePath(), fis );
181 }
182 catch ( FileNotFoundException e )
183 {
184 throw new MavenException( source + " : Error reading plugin script", e );
185 }
186 finally
187 {
188 if ( fis != null )
189 {
190 try
191 {
192 fis.close();
193 }
194 catch ( IOException e )
195 {
196
197 }
198 fis = null;
199 }
200 }
201 }
202
203 /**
204 * @return Map
205 */
206 public Map getPluginProperties()
207 {
208 return pluginProperties;
209 }
210
211 /**
212 * @return File
213 */
214 public File getPluginDirectory()
215 {
216 return pluginDirectory;
217 }
218
219 /**
220 * Retrieve the plugin's default properties.
221 *
222 * @param unpackedPluginDir
223 * The location of the unpacked plugin.
224 * @return The default properties file for the plugin, or <code>null</code> if no such plugin.
225 * @throws IOException
226 * If an IO error occurs while attempting to read the plugin's properties.
227 */
228 private Map loadPluginProperties() throws IOException
229 {
230 Map map = new HashMap();
231
232 File propsFile = new File( pluginDirectory, PLUGIN_PROPERTIES_NAME );
233
234 if ( propsFile.exists() )
235 {
236 Properties props = new Properties();
237 FileInputStream in = null;
238 try
239 {
240 in = new FileInputStream( propsFile );
241 props.load( in );
242 map.putAll( props );
243 }
244 finally
245 {
246 if ( in != null )
247 {
248 try
249 {
250 in.close();
251 }
252 catch ( IOException e )
253 {
254
255 }
256 in = null;
257 }
258 }
259 }
260
261 return map;
262 }
263 }