KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > ProductFile


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.build;
13
14 import java.io.*;
15 import java.util.*;
16 import javax.xml.parsers.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19 import org.xml.sax.*;
20 import org.xml.sax.helpers.DefaultHandler JavaDoc;
21
22 /**
23  *
24  * @since 3.1
25  */

26 public class ProductFile extends DefaultHandler JavaDoc implements IPDEBuildConstants {
27     private final static SAXParserFactory parserFactory = SAXParserFactory.newInstance();
28
29     private static final String JavaDoc PROGRAM_ARGS = "programArgs"; //$NON-NLS-1$
30
private static final String JavaDoc PROGRAM_ARGS_LINUX = "programArgsLin"; //$NON-NLS-1$
31
private static final String JavaDoc PROGRAM_ARGS_MAC = "programArgsMac"; //$NON-NLS-1$
32
private static final String JavaDoc PROGRAM_ARGS_SOLARIS = "programArgsSol"; //$NON-NLS-1$
33
private static final String JavaDoc PROGRAM_ARGS_WIN = "programArgsWin"; //$NON-NLS-1$
34
private static final String JavaDoc VM_ARGS = "vmArgs"; //$NON-NLS-1$
35
private static final String JavaDoc VM_ARGS_LINUX = "vmArgsLin"; //$NON-NLS-1$
36
private static final String JavaDoc VM_ARGS_MAC = "vmArgsMac"; //$NON-NLS-1$
37
private static final String JavaDoc VM_ARGS_SOLARIS = "vmArgsSol"; //$NON-NLS-1$
38
private static final String JavaDoc VM_ARGS_WIN = "vmArgsWin"; //$NON-NLS-1$
39

40     private static final String JavaDoc SOLARIS_LARGE = "solarisLarge"; //$NON-NLS-1$
41
private static final String JavaDoc SOLARIS_MEDIUM = "solarisMedium"; //$NON-NLS-1$
42
private static final String JavaDoc SOLARIS_SMALL = "solarisSmall"; //$NON-NLS-1$
43
private static final String JavaDoc SOLARIS_TINY = "solarisTiny"; //$NON-NLS-1$
44
private static final String JavaDoc WIN32_16_LOW = "winSmallLow"; //$NON-NLS-1$
45
private static final String JavaDoc WIN32_16_HIGH = "winSmallHigh"; //$NON-NLS-1$
46
private static final String JavaDoc WIN32_24_LOW = "win24Low"; //$NON-NLS-1$
47
private static final String JavaDoc WIN32_32_LOW = "winMediumLow"; //$NON-NLS-1$
48
private static final String JavaDoc WIN32_32_HIGH = "winMediumHigh"; //$NON-NLS-1$
49
private static final String JavaDoc WIN32_48_LOW = "winLargeLow"; //$NON-NLS-1$
50
private static final String JavaDoc WIN32_48_HIGH = "winLargeHigh"; //$NON-NLS-1$
51

52     private static final String JavaDoc PRODUCT = "product"; //$NON-NLS-1$
53
private static final String JavaDoc CONFIG_INI = "configIni"; //$NON-NLS-1$
54
private static final String JavaDoc LAUNCHER = "launcher"; //$NON-NLS-1$
55
private static final String JavaDoc LAUNCHER_ARGS = "launcherArgs"; //$NON-NLS-1$
56
private static final String JavaDoc PLUGINS = "plugins"; //$NON-NLS-1$
57
private static final String JavaDoc FEATURES = "features"; //$NON-NLS-1$
58
private static final String JavaDoc SPLASH = "splash"; //$NON-NLS-1$
59
private static final String JavaDoc P_USE_ICO = "useIco"; //$NON-NLS-1$
60

61     //These constants form a small state machine to parse the .product file
62
private static final int STATE_START = 0;
63     private static final int STATE_PRODUCT = 1;
64     private static final int STATE_LAUNCHER = 2;
65     private static final int STATE_LAUNCHER_ARGS = 3;
66     private static final int STATE_PLUGINS = 4;
67     private static final int STATE_FEATURES = 5;
68     private static final int STATE_PROGRAM_ARGS = 6;
69     private static final int STATE_PROGRAM_ARGS_LINUX = 7;
70     private static final int STATE_PROGRAM_ARGS_MAC = 8;
71     private static final int STATE_PROGRAM_ARGS_SOLARIS = 9;
72     private static final int STATE_PROGRAM_ARGS_WIN = 10;
73     private static final int STATE_VM_ARGS = 11;
74     private static final int STATE_VM_ARGS_LINUX = 12;
75     private static final int STATE_VM_ARGS_MAC = 13;
76     private static final int STATE_VM_ARGS_SOLARIS = 14;
77     private static final int STATE_VM_ARGS_WIN = 15;
78     
79     private int state = STATE_START;
80
81     private SAXParser parser;
82     private String JavaDoc currentOS = null;
83     private boolean useIco = false;
84     private ArrayList result = new ArrayList(6);
85     private String JavaDoc launcherName = null;
86     private String JavaDoc icons[] = null;
87     private String JavaDoc configPath = null;
88     private String JavaDoc id = null;
89     private boolean useFeatures = false;
90     private List plugins = null;
91     private List fragments = null;
92     private List features = null;
93     private String JavaDoc splashLocation = null;
94     private String JavaDoc productName = null;
95     private String JavaDoc application = null;
96
97     private Properties launcherArgs = new Properties();
98     
99     private static String JavaDoc normalize(String JavaDoc text) {
100         if (text == null || text.trim().length() == 0)
101             return ""; //$NON-NLS-1$
102

103         text = text.replaceAll("\\r|\\n|\\f|\\t", " "); //$NON-NLS-1$ //$NON-NLS-2$
104
return text.replaceAll("\\s+", " "); //$NON-NLS-1$ //$NON-NLS-2$
105
}
106     
107     /**
108      * Constructs a feature parser.
109      */

110     public ProductFile(String JavaDoc location, String JavaDoc os) throws CoreException {
111         super();
112         this.currentOS = os;
113         try {
114             parserFactory.setNamespaceAware(true);
115             parser = parserFactory.newSAXParser();
116             InputStream in = new BufferedInputStream(new FileInputStream(location));
117             parser.parse(new InputSource(in), this);
118         } catch (ParserConfigurationException e) {
119             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PRODUCT_FORMAT, NLS.bind(Messages.exception_productParse, location), e));
120         } catch (SAXException e) {
121             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PRODUCT_FORMAT, NLS.bind(Messages.exception_productParse, location), e));
122         } catch (FileNotFoundException e) {
123             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PRODUCT_FILE, NLS.bind(Messages.exception_missingElement, location), null));
124         } catch (IOException e) {
125             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PRODUCT_FORMAT, NLS.bind(Messages.exception_productParse, location), e));
126         }
127     }
128
129     public String JavaDoc getLauncherName() {
130         return launcherName;
131     }
132     
133     public List getPlugins() {
134         return getPlugins(true);
135     }
136     
137     public List getPlugins(boolean includeFragments) {
138         List p = plugins != null ? plugins : Collections.EMPTY_LIST;
139         if(!includeFragments)
140             return p;
141         
142         List f = fragments != null ? fragments : Collections.EMPTY_LIST;
143         int size = p.size() + f.size();
144         if (size == 0)
145             return Collections.EMPTY_LIST;
146
147         List both = new ArrayList(size);
148         both.addAll(p);
149         both.addAll(f);
150         return both;
151     }
152     
153     public List getFragments() {
154         if(fragments == null)
155             return Collections.EMPTY_LIST;
156         return fragments;
157     }
158     
159     public List getFeatures() {
160         if(features == null)
161             return Collections.EMPTY_LIST;
162         return features;
163     }
164     
165     public boolean containsPlugin(String JavaDoc plugin) {
166         return (plugins != null && plugins.contains(plugin)) || (fragments != null && fragments.contains(plugin));
167     }
168
169     /**
170      * Parses the specified url and constructs a feature
171      */

172     public String JavaDoc[] getIcons() {
173         if (icons != null)
174             return icons;
175         String JavaDoc[] temp = new String JavaDoc[result.size()];
176         int i = 0;
177         for (Iterator iter = result.iterator(); iter.hasNext();) {
178             String JavaDoc element = (String JavaDoc) iter.next();
179             if (element != null)
180                 temp[i++] = element;
181         }
182         icons = new String JavaDoc[i];
183         System.arraycopy(temp, 0, icons, 0, i);
184         return icons;
185     }
186
187     public String JavaDoc getConfigIniPath() {
188         return configPath;
189     }
190
191     public String JavaDoc getId() {
192         return id;
193     }
194     
195     public String JavaDoc getSplashLocation() {
196         return splashLocation;
197     }
198
199     public String JavaDoc getProductName() {
200         return productName;
201     }
202     
203     public String JavaDoc getApplication() {
204         return application;
205     }
206     
207     public boolean useFeatures() {
208         return useFeatures;
209     }
210
211     public String JavaDoc getVMArguments(String JavaDoc os) {
212         String JavaDoc key = null;
213         if( os.equals(Platform.OS_WIN32)){
214             key = VM_ARGS_WIN;
215         } else if( os.equals(Platform.OS_LINUX)) {
216             key = VM_ARGS_LINUX;
217         } else if( os.equals(Platform.OS_MACOSX)) {
218             key = VM_ARGS_MAC;
219         } else if(os.equals(Platform.OS_SOLARIS)) {
220             key = VM_ARGS_SOLARIS;
221         }
222         
223         String JavaDoc prefix = launcherArgs.getProperty(VM_ARGS);
224         String JavaDoc platform = null, args = null;
225         if (key != null)
226             platform = launcherArgs.getProperty(key);
227         if (prefix != null)
228             args = platform != null ? prefix + " " + platform : prefix; //$NON-NLS-1$
229
else
230             args = platform != null ? platform : ""; //$NON-NLS-1$
231
return normalize(args);
232     }
233     
234     public String JavaDoc getProgramArguments(String JavaDoc os) {
235         String JavaDoc key = null;
236         if( os.equals(Platform.OS_WIN32)){
237             key = PROGRAM_ARGS_WIN;
238         } else if( os.equals(Platform.OS_LINUX)) {
239             key = PROGRAM_ARGS_LINUX;
240         } else if( os.equals(Platform.OS_MACOSX)) {
241             key = PROGRAM_ARGS_MAC;
242         } else if(os.equals(Platform.OS_SOLARIS)) {
243             key = PROGRAM_ARGS_SOLARIS;
244         }
245         
246         String JavaDoc prefix = launcherArgs.getProperty(PROGRAM_ARGS);
247         String JavaDoc platform = null, args = null;
248         if (key != null)
249             platform = launcherArgs.getProperty(key);
250         if (prefix != null)
251             args = platform != null ? prefix + " " + platform : prefix; //$NON-NLS-1$
252
else
253             args = platform != null ? platform : ""; //$NON-NLS-1$
254
return normalize(args);
255     }
256     
257     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes attributes) {
258         switch (state) {
259             case STATE_START :
260                 if (PRODUCT.equals(localName)) {
261                     processProduct(attributes);
262                     state = STATE_PRODUCT;
263                 }
264                 break;
265
266             case STATE_PRODUCT :
267                 if (CONFIG_INI.equals(localName)) {
268                     processConfigIni(attributes);
269                 } else if (LAUNCHER.equals(localName)) {
270                     processLauncher(attributes);
271                     state = STATE_LAUNCHER;
272                 } else if (PLUGINS.equals(localName)) {
273                     state = STATE_PLUGINS;
274                 } else if (FEATURES.equals(localName)) {
275                     state = STATE_FEATURES;
276                 } else if (LAUNCHER_ARGS.equals(localName)) {
277                     state = STATE_LAUNCHER_ARGS;
278                 } else if (SPLASH.equals(localName)) {
279                     splashLocation = attributes.getValue("location"); //$NON-NLS-1$
280
}
281                 break;
282
283             case STATE_LAUNCHER :
284                 if (Platform.OS_SOLARIS.equals(localName)) {
285                     processSolaris(attributes);
286                 } else if ("win".equals(localName)) { //$NON-NLS-1$
287
processWin(attributes);
288                 } else if (Platform.OS_LINUX.equals(localName)) {
289                     processLinux(attributes);
290                 } else if (Platform.OS_MACOSX.equals(localName)) {
291                     processMac(attributes);
292                 }
293                 if ("ico".equals(localName)) { //$NON-NLS-1$
294
processIco(attributes);
295                 } else if ("bmp".equals(localName)) { //$NON-NLS-1$
296
processBmp(attributes);
297                 }
298                 break;
299
300             case STATE_LAUNCHER_ARGS :
301                 if (PROGRAM_ARGS.equals(localName)) {
302                     state = STATE_PROGRAM_ARGS;
303                 } else if (PROGRAM_ARGS_LINUX.equals(localName)) {
304                     state = STATE_PROGRAM_ARGS_LINUX;
305                 } else if (PROGRAM_ARGS_MAC.equals(localName)) {
306                     state = STATE_PROGRAM_ARGS_MAC;
307                 } else if (PROGRAM_ARGS_SOLARIS.equals(localName)) {
308                     state = STATE_PROGRAM_ARGS_SOLARIS;
309                 } else if (PROGRAM_ARGS_WIN.equals(localName)) {
310                     state = STATE_PROGRAM_ARGS_WIN;
311                 } else if (VM_ARGS.equals(localName)) {
312                     state = STATE_VM_ARGS;
313                 } else if (VM_ARGS_LINUX.equals(localName)) {
314                     state = STATE_VM_ARGS_LINUX;
315                 } else if (VM_ARGS_MAC.equals(localName)) {
316                     state = STATE_VM_ARGS_MAC;
317                 } else if (VM_ARGS_SOLARIS.equals(localName)) {
318                     state = STATE_VM_ARGS_SOLARIS;
319                 } else if (VM_ARGS_WIN.equals(localName)) {
320                     state = STATE_VM_ARGS_WIN;
321                 }
322                 break;
323
324             case STATE_PLUGINS :
325                 if ("plugin".equals(localName)) { //$NON-NLS-1$
326
processPlugin(attributes);
327                 }
328                 break;
329
330             case STATE_FEATURES :
331                 if ("feature".equals(localName)) { //$NON-NLS-1$
332
processFeature(attributes);
333                 }
334                 break;
335         }
336     }
337
338     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) {
339         switch (state) {
340             case STATE_PLUGINS :
341                 if (PLUGINS.equals(localName))
342                     state = STATE_PRODUCT;
343                 break;
344             case STATE_FEATURES :
345                 if (FEATURES.equals(localName))
346                     state = STATE_PRODUCT;
347                 break;
348             case STATE_LAUNCHER_ARGS :
349                 if (LAUNCHER_ARGS.equals(localName))
350                     state = STATE_PRODUCT;
351                 break;
352             case STATE_LAUNCHER :
353                 if (LAUNCHER.equals(localName))
354                     state = STATE_PRODUCT;
355                 break;
356
357             case STATE_PROGRAM_ARGS :
358             case STATE_PROGRAM_ARGS_LINUX :
359             case STATE_PROGRAM_ARGS_MAC :
360             case STATE_PROGRAM_ARGS_SOLARIS :
361             case STATE_PROGRAM_ARGS_WIN :
362             case STATE_VM_ARGS :
363             case STATE_VM_ARGS_LINUX :
364             case STATE_VM_ARGS_MAC :
365             case STATE_VM_ARGS_SOLARIS :
366             case STATE_VM_ARGS_WIN :
367                 state = STATE_LAUNCHER_ARGS;
368                 break;
369         }
370     }
371     
372     public void characters(char[] ch, int start, int length) {
373         switch (state) {
374             case STATE_PROGRAM_ARGS :
375                 addLaunchArgumentToMap(PROGRAM_ARGS, String.valueOf(ch, start, length));
376                 break;
377             case STATE_PROGRAM_ARGS_LINUX :
378                 addLaunchArgumentToMap(PROGRAM_ARGS_LINUX, String.valueOf(ch, start, length));
379                 break;
380             case STATE_PROGRAM_ARGS_MAC :
381                 addLaunchArgumentToMap(PROGRAM_ARGS_MAC, String.valueOf(ch, start, length));
382                 break;
383             case STATE_PROGRAM_ARGS_SOLARIS :
384                 addLaunchArgumentToMap(PROGRAM_ARGS_SOLARIS, String.valueOf(ch, start, length));
385                 break;
386             case STATE_PROGRAM_ARGS_WIN :
387                 addLaunchArgumentToMap(PROGRAM_ARGS_WIN, String.valueOf(ch, start, length));
388                 break;
389             case STATE_VM_ARGS :
390                 addLaunchArgumentToMap(VM_ARGS, String.valueOf(ch, start, length));
391                 break;
392             case STATE_VM_ARGS_LINUX :
393                 addLaunchArgumentToMap(VM_ARGS_LINUX, String.valueOf(ch, start, length));
394                 break;
395             case STATE_VM_ARGS_MAC :
396                 addLaunchArgumentToMap(VM_ARGS_MAC, String.valueOf(ch, start, length));
397                 break;
398             case STATE_VM_ARGS_SOLARIS :
399                 addLaunchArgumentToMap(VM_ARGS_SOLARIS, String.valueOf(ch, start, length));
400                 break;
401             case STATE_VM_ARGS_WIN :
402                 addLaunchArgumentToMap(VM_ARGS_WIN, String.valueOf(ch, start, length));
403                 break;
404         }
405     }
406     
407     private void addLaunchArgumentToMap(String JavaDoc key, String JavaDoc value) {
408         if (launcherArgs == null)
409             launcherArgs = new Properties();
410         
411         String JavaDoc oldValue = launcherArgs.getProperty(key);
412         if (oldValue != null)
413             launcherArgs.setProperty(key, oldValue + value);
414         else
415             launcherArgs.setProperty(key, value);
416     }
417     private void processPlugin(Attributes attributes) {
418         String JavaDoc fragment = attributes.getValue("fragment"); //$NON-NLS-1$
419
if (fragment != null && new Boolean JavaDoc(fragment).booleanValue()) {
420             if (fragments == null)
421                 fragments = new ArrayList();
422             fragments.add(attributes.getValue("id")); //$NON-NLS-1$
423
} else {
424             if (plugins == null)
425                 plugins = new ArrayList();
426             plugins.add(attributes.getValue("id")); //$NON-NLS-1$
427
}
428     }
429     
430     private void processFeature(Attributes attributes) {
431         if (features == null)
432             features = new ArrayList();
433         features.add(attributes.getValue("id")); //$NON-NLS-1$
434
}
435
436     private void processProduct(Attributes attributes) {
437         id = attributes.getValue("id"); //$NON-NLS-1$
438
productName = attributes.getValue("name"); //$NON-NLS-1$
439
application = attributes.getValue("application"); //$NON-NLS-1$
440
String JavaDoc use = attributes.getValue("useFeatures"); //$NON-NLS-1$
441
if (use != null)
442             useFeatures = IBuildPropertiesConstants.TRUE.equalsIgnoreCase(use);
443     }
444
445     private void processConfigIni(Attributes attributes) {
446         if (attributes.getValue("use").equals("custom")) { //$NON-NLS-1$//$NON-NLS-2$
447
configPath = attributes.getValue("path"); //$NON-NLS-1$
448
}
449     }
450
451     private void processLauncher(Attributes attributes) {
452         launcherName = attributes.getValue("name"); //$NON-NLS-1$
453
}
454
455     private boolean osMatch(String JavaDoc os) {
456         if (os == currentOS)
457             return true;
458         if (os == null)
459             return false;
460         return os.equals(currentOS);
461     }
462
463     private void processSolaris(Attributes attributes) {
464         if (!osMatch(Platform.OS_SOLARIS))
465             return;
466         result.add(attributes.getValue(SOLARIS_LARGE));
467         result.add(attributes.getValue(SOLARIS_MEDIUM));
468         result.add(attributes.getValue(SOLARIS_SMALL));
469         result.add(attributes.getValue(SOLARIS_TINY));
470     }
471
472     private void processWin(Attributes attributes) {
473         if (!osMatch(Platform.OS_WIN32))
474             return;
475         useIco = IBuildPropertiesConstants.TRUE.equalsIgnoreCase(attributes.getValue(P_USE_ICO));
476     }
477
478     private void processIco(Attributes attributes) {
479         if (!osMatch(Platform.OS_WIN32) || !useIco)
480             return;
481         result.add(attributes.getValue("path")); //$NON-NLS-1$
482
}
483
484     private void processBmp(Attributes attributes) {
485         if (!osMatch(Platform.OS_WIN32) || useIco)
486             return;
487         result.add(attributes.getValue(WIN32_16_HIGH));
488         result.add(attributes.getValue(WIN32_16_LOW));
489         result.add(attributes.getValue(WIN32_24_LOW));
490         result.add(attributes.getValue(WIN32_32_HIGH));
491         result.add(attributes.getValue(WIN32_32_LOW));
492         result.add(attributes.getValue(WIN32_48_HIGH));
493         result.add(attributes.getValue(WIN32_48_LOW));
494     }
495
496     private void processLinux(Attributes attributes) {
497         if (!osMatch(Platform.OS_LINUX))
498             return;
499         result.add(attributes.getValue("icon")); //$NON-NLS-1$
500
}
501
502     private void processMac(Attributes attributes) {
503         if (!osMatch(Platform.OS_MACOSX))
504             return;
505         result.add(attributes.getValue("icon")); //$NON-NLS-1$
506
}
507 }
508
Popular Tags