KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > provisional > SiteOptimizerApplication


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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  * Chris Aniszczyk (IBM Corp.) - NL-enabled the site optimizer
11  *******************************************************************************/

12 package org.eclipse.update.internal.provisional;
13
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.io.PrintStream JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.jar.JarFile JavaDoc;
30 import java.util.jar.JarOutputStream JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32
33 import org.eclipse.core.runtime.CoreException;
34 import org.eclipse.core.runtime.IPlatformRunnable;
35 import org.eclipse.core.runtime.IStatus;
36 import org.eclipse.core.runtime.Platform;
37 import org.eclipse.core.runtime.Status;
38 import org.eclipse.osgi.util.NLS;
39 import org.eclipse.update.core.IIncludedFeatureReference;
40 import org.eclipse.update.core.IncludedFeatureReference;
41 import org.eclipse.update.core.model.DefaultSiteParser;
42 import org.eclipse.update.core.model.FeatureModel;
43 import org.eclipse.update.core.model.FeatureModelFactory;
44 import org.eclipse.update.core.model.FeatureReferenceModel;
45 import org.eclipse.update.core.model.ImportModel;
46 import org.eclipse.update.core.model.PluginEntryModel;
47 import org.eclipse.update.core.model.SiteModel;
48 import org.eclipse.update.core.model.URLEntryModel;
49 import org.eclipse.update.internal.core.ExtendedSiteURLFactory;
50 import org.eclipse.update.internal.core.Messages;
51 import org.eclipse.update.internal.core.UpdateManagerUtils;
52 import org.eclipse.update.internal.jarprocessor.JarProcessor;
53 import org.eclipse.update.internal.jarprocessor.JarProcessorExecutor;
54 import org.eclipse.update.internal.jarprocessor.Main;
55 import org.xml.sax.SAXException JavaDoc;
56
57 /**
58  * The application class used to perform update site optimizations.
59  * <p>
60  * This class can only be referenced from <code>org.eclipse.runtime.applications</code>
61  * extension point. It should not be exteded or instantiated.
62  * <p>
63  * <b>Note:</b> This class/interface is part of an interim API that is still
64  * under development and expected to change significantly before reaching
65  * stability. It is being made available at this early stage to solicit feedback
66  * from pioneering adopters on the understanding that any code that uses this
67  * API will almost certainly be broken (repeatedly) as the API evolves.
68  * </p>
69  *
70  * @since 3.2
71  */

72 public class SiteOptimizerApplication implements IPlatformRunnable {
73     public final static Integer JavaDoc EXIT_ERROR = new Integer JavaDoc(1);
74
75     public final static String JavaDoc JAR_PROCESSOR = "-jarProcessor"; //$NON-NLS-1$
76

77     public final static String JavaDoc DIGEST_BUILDER = "-digestBuilder"; //$NON-NLS-1$
78

79     public final static String JavaDoc INPUT = "input"; //$NON-NLS-1$
80

81     public final static String JavaDoc OUTPUT_DIR = "-outputDir"; //$NON-NLS-1$
82

83     public final static String JavaDoc VERBOSE = "-verbose"; //$NON-NLS-1$
84

85     public final static String JavaDoc JAR_PROCESSOR_PACK = "-pack"; //$NON-NLS-1$
86

87     public final static String JavaDoc JAR_PROCESSOR_UNPACK = "-unpack"; //$NON-NLS-1$
88

89     public final static String JavaDoc JAR_PROCESSOR_REPACK = "-repack"; //$NON-NLS-1$
90

91     public final static String JavaDoc JAR_PROCESSOR_SIGN = "-sign"; //$NON-NLS-1$
92

93     public final static String JavaDoc JAR_PROCESSOR_PROCESS_ALL = "-processAll"; //$NON-NLS-1$
94

95     public final static String JavaDoc SITE_XML = "-siteXML"; //$NON-NLS-1$
96

97     public final static String JavaDoc SITE_ATTRIBUTES_FILE = "siteAttributes.txt"; //$NON-NLS-1$
98

99     public final static String JavaDoc DIGEST_OUTPUT_DIR = "-digestOutputDir"; //$NON-NLS-1$
100

101     /*
102      * private final static String DESCRIPTION = "DESCRIPTION"; private final
103      * static String LICENCE = "LICENCE"; private final static String COPYRIGHT =
104      * "COPYRIGHT"; private final static String FEATURE_LABEL = "FEATURE_LABEL";
105      */

106
107     /**
108      * Parses the command line in the form: [-key [value]]* [inputvalue] If the
109      * last argument does not start with a "-" then it is taken as the input
110      * value and not the value for a preceding -key
111      *
112      * @param args
113      * @return
114      */

115     private Map JavaDoc parseCmdLine(String JavaDoc[] args) {
116         Map JavaDoc cmds = new HashMap JavaDoc();
117         for (int i = 0; i < args.length; i++) {
118             if (i == args.length - 1 && !args[i].startsWith("-")) { //$NON-NLS-1$
119
cmds.put(INPUT, args[i]);
120             } else {
121                 String JavaDoc key = args[i];
122                 String JavaDoc val = null;
123                 if (i < args.length - 2 && !args[i + 1].startsWith("-")) { //$NON-NLS-1$
124
val = args[++i];
125                 }
126
127                 if (key.startsWith(SITE_XML)) {
128                     // System.out.println(val.indexOf(":null"));
129
val = key.substring(key.indexOf("=") + 1); //$NON-NLS-1$
130
// System.out.println(key + ":" + val);
131
cmds.put(SITE_XML, val);
132                 } else if (key.startsWith(DIGEST_OUTPUT_DIR)) {
133                     val = key.substring(key.indexOf("=") + 1); //$NON-NLS-1$
134
// System.out.println(key + ":" + val);
135
cmds.put(DIGEST_OUTPUT_DIR, val);
136                 } else {
137
138                     // System.out.println(key + ":" + val);
139
cmds.put(key, val);
140                 }
141             }
142         }
143         return cmds;
144     }
145
146     private boolean runJarProcessor(Map JavaDoc params) {
147         Main.Options options = new Main.Options();
148         options.pack = params.containsKey(JAR_PROCESSOR_PACK);
149         options.unpack = params.containsKey(JAR_PROCESSOR_UNPACK);
150         options.repack = params.containsKey(JAR_PROCESSOR_REPACK);
151         options.processAll = params.containsKey(JAR_PROCESSOR_PROCESS_ALL);
152         options.verbose = params.containsKey(VERBOSE);
153         options.signCommand = (String JavaDoc) params.get(JAR_PROCESSOR_SIGN);
154         options.outputDir = (String JavaDoc) params.get(OUTPUT_DIR);
155
156         String JavaDoc problem = null;
157
158         String JavaDoc input = (String JavaDoc) params.get(INPUT);
159         if (input == null)
160             problem = Messages.SiteOptimizer_inputNotSpecified;
161         else {
162             File JavaDoc inputFile = new File JavaDoc(input);
163             if (inputFile.exists())
164                 options.input = inputFile;
165             else
166                 problem = NLS.bind(Messages.SiteOptimizer_inputFileNotFound,
167                         new String JavaDoc[] { input });
168         }
169
170         if (options.unpack) {
171             if (!JarProcessor.canPerformUnpack()) {
172                 problem = Messages.JarProcessor_unpackNotFound;
173             } else if (options.pack || options.repack
174                     || options.signCommand != null) {
175                 problem = Messages.JarProcessor_noPackUnpack;
176             }
177         } else if ((options.pack || options.repack)
178                 && !JarProcessor.canPerformPack()) {
179             problem = Messages.JarProcessor_packNotFound;
180         }
181
182         if (problem != null) {
183             System.out.println(problem);
184             return false;
185         }
186
187         new JarProcessorExecutor().runJarProcessor(options);
188         return true;
189     }
190
191     private boolean runDigestBuilder(Map JavaDoc params) {
192
193         List JavaDoc featureList = getFeatureList(params);
194
195         if ((featureList == null) || featureList.isEmpty()) {
196             System.out.println("no features to process"); //$NON-NLS-1$
197
return false;
198         }
199         Map JavaDoc perFeatureLocales = new HashMap JavaDoc();
200         Map JavaDoc availableLocales = getAvailableLocales(featureList,
201                 perFeatureLocales);
202         try {
203             openInputStremas(availableLocales);
204         } catch (IOException JavaDoc e1) {
205             e1.printStackTrace();
206             System.out.println("Can not create file in output direcotry"); //$NON-NLS-1$
207
return false;
208         }
209
210         for(int i = 0; i < featureList.size(); i++) {
211
212             String JavaDoc featureJarFileName = (String JavaDoc) featureList.get(i);
213
214             if (featureJarFileName.endsWith("jar")) { //$NON-NLS-1$
215
System.out.println("Processing... " + featureJarFileName); //$NON-NLS-1$
216
} else {
217                 System.out.println("Skipping... " + featureJarFileName); //$NON-NLS-1$
218
continue;
219             }
220
221             JarFile JavaDoc featureJar = null;
222             try {
223                 featureJar = new JarFile JavaDoc(featureJarFileName);
224             } catch (IOException JavaDoc e) {
225                 System.out.println("Problem with openning jar: " //$NON-NLS-1$
226
+ featureJarFileName);
227                 e.printStackTrace();
228                 return false;
229             }
230             FeatureModelFactory fmf = new FeatureModelFactory();
231
232             try {
233                 ZipEntry JavaDoc featureXMLEntry = featureJar.getEntry("feature.xml"); //$NON-NLS-1$
234
Map JavaDoc featureProperties = loadProperties(featureJar,
235                         featureJarFileName, perFeatureLocales);
236
237                 FeatureModel featureModel = fmf.parseFeature(featureJar
238                         .getInputStream(featureXMLEntry));
239
240                 featureList = addFeaturesToList( (String JavaDoc) params.get(SITE_XML), featureList, featureModel.getFeatureIncluded(), availableLocales, perFeatureLocales);
241
242                 Iterator JavaDoc availableLocalesIterator = availableLocales.values()
243                 .iterator();
244                 while (availableLocalesIterator.hasNext()) {
245                     ((AvailableLocale) availableLocalesIterator.next())
246                     .writeFeatureDigests(featureModel,
247                             featureProperties);
248                 }
249
250             } catch (SAXException JavaDoc e) {
251                 e.printStackTrace();
252                 return false;
253             } catch (IOException JavaDoc e) {
254                 e.printStackTrace();
255                 return false;
256             } catch (CoreException e) {
257                 e.printStackTrace();
258                 return false;
259             }
260         }
261         Iterator JavaDoc availableLocalesIterator = availableLocales.values()
262         .iterator();
263         String JavaDoc outputDirectory = (String JavaDoc) params.get(DIGEST_OUTPUT_DIR);
264
265         outputDirectory = outputDirectory.substring(outputDirectory
266                 .indexOf("=") + 1); //$NON-NLS-1$
267
if (!outputDirectory.endsWith(File.separator)) {
268             outputDirectory = outputDirectory + File.separator;
269         }
270         while (availableLocalesIterator.hasNext()) {
271             try {
272                 ((AvailableLocale) availableLocalesIterator.next())
273                 .finishDigest(outputDirectory);
274             } catch (IOException JavaDoc e) {
275                 System.out.println("Can not write in digest output directory: " //$NON-NLS-1$
276
+ outputDirectory);
277                 e.printStackTrace();
278                 return false;
279             }
280         }
281         System.out.println("Done"); //$NON-NLS-1$
282
return true;
283     }
284
285     private List JavaDoc addFeaturesToList( String JavaDoc siteXML, List JavaDoc featureList, IIncludedFeatureReference[] iIncludedFeatureReferences, Map JavaDoc availableLocales, Map JavaDoc perFeatureLocales ) throws CoreException {
286
287         String JavaDoc directoryName = (new File JavaDoc(siteXML)).getParent();
288         if (!directoryName.endsWith(File.separator)) {
289             directoryName = directoryName + File.separator;
290         }
291         directoryName = directoryName + "features" + File.separator; //$NON-NLS-1$
292

293         for (int i = 0; i < iIncludedFeatureReferences.length; i++) {
294             String JavaDoc featureURL = directoryName + iIncludedFeatureReferences[i].getVersionedIdentifier() + ".jar"; //$NON-NLS-1$
295
if (!(isFeatureAlreadyInList(featureList, featureURL))) {
296                 try {
297                     System.out.println("Extracting locales from inlcuded feature " + featureURL); //$NON-NLS-1$
298
processLocalesInJar(availableLocales, featureURL, perFeatureLocales, true);
299                 } catch (IOException JavaDoc e) {
300                     if (iIncludedFeatureReferences[i].isOptional())
301                         continue;
302                     System.out.println("Error while extracting locales from inlcuded feature " + featureURL);//$NON-NLS-1$
303
e.printStackTrace();
304                     throw new CoreException( new Status( IStatus.ERROR, "", IStatus.OK, "Error while extracting locales from inlcuded feature " + featureURL, e)); //$NON-NLS-1$ //$NON-NLS-2$
305
}
306                 featureList.add(featureURL);
307             }
308         }
309
310         return featureList;
311     }
312
313     private boolean isFeatureAlreadyInList(List JavaDoc featureList, String JavaDoc featureURL) {
314         for (int i = 0; i < featureList.size(); i++) {
315             String JavaDoc currentFeatureURL = (String JavaDoc)featureList.get(i);
316             if (currentFeatureURL.equals(featureURL)) {
317                 return true;
318             }
319         }
320         return false;
321     }
322
323     private Map JavaDoc loadProperties(JarFile JavaDoc featureJar, String JavaDoc featureJarFileName,
324             Map JavaDoc perFeatureLocales) {
325         // System.out.println(
326
// ((List)perFeatureLocales.get(featureJarFileName)).size());
327
Iterator JavaDoc it = ((List JavaDoc) perFeatureLocales.get(featureJarFileName))
328         .iterator();
329         Map JavaDoc result = new HashMap JavaDoc();
330         while (it.hasNext()) {
331             String JavaDoc propertyFileName = (String JavaDoc) it.next();
332
333             ZipEntry JavaDoc featurePropertiesEntry = featureJar
334             .getEntry(propertyFileName);
335             Properties JavaDoc featureProperties = new Properties JavaDoc();
336             if (featurePropertiesEntry != null) {
337                 try {
338                     featureProperties.load(featureJar
339                             .getInputStream(featurePropertiesEntry));
340                     String JavaDoc localeString = null;
341                     if (propertyFileName.endsWith("feature.properties")) { //$NON-NLS-1$
342
localeString = ""; //$NON-NLS-1$
343
} else {
344                         localeString = propertyFileName.substring(8,
345                                 propertyFileName.indexOf('.'));
346                     }
347                     result.put(localeString, featureProperties);
348                 } catch (IOException JavaDoc e) {
349                     e.printStackTrace();
350                 }
351             }
352         }
353         return result;
354     }
355
356     private void openInputStremas(Map JavaDoc availableLocales) throws IOException JavaDoc {
357         Iterator JavaDoc locales = availableLocales.values().iterator();
358         while (locales.hasNext()) {
359             AvailableLocale availableLocale = (AvailableLocale) locales.next();
360             availableLocale.openLocalizedOutputStream();
361         }
362     }
363
364     private Map JavaDoc getAvailableLocales(List JavaDoc featureList, Map JavaDoc perFeatureLocales) {
365         Iterator JavaDoc features = featureList.iterator();
366         Map JavaDoc locales = new HashMap JavaDoc();
367         while (features.hasNext()) {
368             String JavaDoc feature = (String JavaDoc) features.next();
369             try {
370                 System.out.println("Extracting locales from " + feature); //$NON-NLS-1$
371
processLocalesInJar(locales, feature, perFeatureLocales, false);
372             } catch (IOException JavaDoc e) {
373                 System.out.println("Error while extracting locales from " //$NON-NLS-1$
374
+ feature);
375                 e.printStackTrace();
376                 return null;
377             }
378         }
379         return locales;
380     }
381
382     private void processLocalesInJar(Map JavaDoc locales, String JavaDoc feature,
383             Map JavaDoc perFeatureLocales, boolean ignoreNewLocales) throws IOException JavaDoc {
384
385         JarFile JavaDoc jar = new JarFile JavaDoc(feature);
386         // System.out.println(feature);
387
Enumeration JavaDoc files = jar.entries();
388
389         List JavaDoc localesTemp = new ArrayList JavaDoc();
390         perFeatureLocales.put(feature, localesTemp);
391
392         while (files.hasMoreElements()) {
393             ZipEntry JavaDoc file = (ZipEntry JavaDoc) files.nextElement();
394             String JavaDoc localeString = null;
395             String JavaDoc name = file.getName();
396             // System.out.println("processLocalesInJar:"+name);
397
if (name.startsWith("feature") && name.endsWith(".properties")) { //$NON-NLS-1$ //$NON-NLS-2$
398
// System.out.println(name);
399
localesTemp.add(name);
400                 // System.out.println(name);
401
if (name.endsWith("feature.properties")) { //$NON-NLS-1$
402
localeString = ""; //$NON-NLS-1$
403
} else {
404                     localeString = name.substring(8, name.indexOf('.'));
405                 }
406                 // System.out.println(name +"::::\"" + localeString + "\"");
407
if ( !ignoreNewLocales && !locales.containsKey(localeString)) {
408                     locales.put(localeString, new AvailableLocale(localeString));
409                 }
410                 if (locales.containsKey(localeString)) {
411                     AvailableLocale currentLocale = (AvailableLocale) locales.get(localeString);
412                     currentLocale.addFeatures(feature);
413                 }
414             }
415         }
416
417     }
418
419     private List JavaDoc getFeatureList(Map JavaDoc params) {
420         if (params.containsKey(SITE_XML)
421                 && (fileExists((String JavaDoc) params.get(SITE_XML)))) {
422             return getFeatureListFromSiteXML((String JavaDoc) params.get(SITE_XML));
423         } else if (params.containsKey(INPUT)
424                 && isDirectory((String JavaDoc) params
425                         .get(SiteOptimizerApplication.INPUT))) {
426             return getFeatureListFromDirectory((String JavaDoc) params.get(INPUT));
427         }
428         return null;
429     }
430
431     private boolean fileExists(String JavaDoc fileName) {
432         // System.out.println("fileExists:"+fileName);
433
File JavaDoc file = new File JavaDoc(fileName);
434         if ((file != null) && file.exists())
435             return true;
436         return false;
437     }
438
439     private List JavaDoc getFeatureListFromDirectory(String JavaDoc directoryName) {
440         List JavaDoc featuresURLs = new ArrayList JavaDoc();
441         File JavaDoc directory = new File JavaDoc(directoryName);
442         String JavaDoc[] featureJarFileNames = directory.list();
443         for (int i = 0; i < featureJarFileNames.length; i++) {
444             featuresURLs.add(directoryName + File.separator
445                     + featureJarFileNames[i]);
446         }
447         return featuresURLs;
448     }
449
450     private boolean isDirectory(String JavaDoc fileName) {
451
452         File JavaDoc directory = new File JavaDoc(fileName);
453         if ((directory != null) && directory.exists()
454                 && directory.isDirectory())
455             return true;
456         return false;
457     }
458
459     private List JavaDoc getFeatureListFromSiteXML(String JavaDoc siteXML) {
460
461         List JavaDoc featuresURLs = new ArrayList JavaDoc();
462         String JavaDoc directoryName = (new File JavaDoc(siteXML)).getParent();
463         if (!directoryName.endsWith(File.separator)) {
464             directoryName = directoryName + File.separator;
465         }
466
467         DefaultSiteParser siteParser = new DefaultSiteParser();
468         siteParser.init(new ExtendedSiteURLFactory());
469
470         try {
471             SiteModel site = siteParser.parse(new FileInputStream JavaDoc(siteXML));
472             if(site.getFeatureReferenceModels().length > 0) {
473                 site.getFeatureReferenceModels()[0].getURLString();
474                 FeatureReferenceModel[] featureReferenceModel = site
475                 .getFeatureReferenceModels();
476                 for (int i = 0; i < featureReferenceModel.length; i++) {
477                     featuresURLs.add(directoryName
478                             + featureReferenceModel[i].getURLString());
479                 }
480             }
481             return featuresURLs;
482         } catch (FileNotFoundException JavaDoc e) {
483             System.out.println("File not found: " + e.getMessage()); //$NON-NLS-1$
484
e.printStackTrace();
485         } catch (SAXException JavaDoc e) {
486             System.out.println("Parsing problem: " + e.getMessage()); //$NON-NLS-1$
487
e.printStackTrace();
488         } catch (IOException JavaDoc e) {
489             System.out.println("Problem while parsing: " + e.getMessage()); //$NON-NLS-1$
490
e.printStackTrace();
491         }
492         return null;
493     }
494
495     /*
496      * (non-Javadoc)
497      *
498      * @see org.eclipse.core.runtime.IPlatformRunnable#run(java.lang.Object)
499      */

500     public Object JavaDoc run(Object JavaDoc args) throws Exception JavaDoc {
501         Platform.endSplash();
502         if (args == null)
503             return EXIT_ERROR;
504         if (args instanceof String JavaDoc[]) {
505             Map JavaDoc params = parseCmdLine((String JavaDoc[]) args);
506             if (params.containsKey(JAR_PROCESSOR)) {
507                 if (!runJarProcessor(params))
508                     return EXIT_ERROR;
509             }
510
511             if (params.containsKey(DIGEST_BUILDER)) {
512                 if (!runDigestBuilder(params))
513                     return EXIT_ERROR;
514             }
515         }
516         return IPlatformRunnable.EXIT_OK;
517     }
518
519     private class AvailableLocale {
520
521         private String JavaDoc PREFIX = "temp"; //$NON-NLS-1$
522

523         private String JavaDoc locale;
524
525         private Map JavaDoc /* VersionedIdentifier */features = new HashMap JavaDoc();
526
527         private PrintStream JavaDoc localizedPrintStream;
528
529         private File JavaDoc tempDigestDirectory;
530
531         public Map JavaDoc availableLocales;
532
533         public Map JavaDoc getAvailableLocales() {
534             return availableLocales;
535         }
536
537         public void finishDigest(String JavaDoc outputDirectory) throws IOException JavaDoc {
538             localizedPrintStream.println("</digest>"); //$NON-NLS-1$
539
if (localizedPrintStream != null) {
540                 localizedPrintStream.close();
541             }
542
543             File JavaDoc digest = new File JavaDoc(outputDirectory + File.separator + "digest" //$NON-NLS-1$
544
+ (locale == null || locale.equals("") ? "" : "_"+locale) + ".zip"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
545
System.out.println(digest.getAbsolutePath());
546             System.out.println(digest.getName());
547             if (digest.exists()) {
548                 digest.delete();
549             }
550             digest.createNewFile();
551             OutputStream JavaDoc os = new FileOutputStream JavaDoc(digest);
552             JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(os);
553             jos.putNextEntry(new ZipEntry JavaDoc("digest.xml")); //$NON-NLS-1$
554
InputStream JavaDoc is = new FileInputStream JavaDoc(tempDigestDirectory);
555             byte[] b = new byte[4096];
556             int bytesRead = 0;
557             do {
558                 bytesRead = is.read(b);
559                 if (bytesRead > 0) {
560                     jos.write(b, 0, bytesRead);
561                 }
562             } while (bytesRead > 0);
563
564             jos.closeEntry();
565             jos.close();
566             os.close();
567             is.close();
568             tempDigestDirectory.delete();
569
570         }
571
572         public void setAvailableLocales(Map JavaDoc availableLocales) {
573             this.availableLocales = availableLocales;
574         }
575
576         public AvailableLocale(String JavaDoc locale) {
577             this.locale = locale;
578         }
579
580         public Map JavaDoc getFeatures() {
581             return features;
582         }
583
584         public void addFeatures(String JavaDoc feature) {
585             features.put(feature, feature);
586         }
587
588         public String JavaDoc getLocale() {
589             return locale;
590         }
591
592         public PrintStream JavaDoc getLocalizedPrintStream() {
593             return localizedPrintStream;
594         }
595
596         public void openLocalizedOutputStream() throws IOException JavaDoc {
597             tempDigestDirectory = File.createTempFile(PREFIX, null);
598             FileOutputStream JavaDoc fstream = new FileOutputStream JavaDoc(tempDigestDirectory);
599             localizedPrintStream = new PrintStream JavaDoc(fstream);
600             localizedPrintStream
601             .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n <digest>"); //$NON-NLS-1$
602
tempDigestDirectory.deleteOnExit();
603         }
604
605         public int hashCode() {
606             return locale.hashCode();
607         }
608
609         public boolean equals(Object JavaDoc obj) {
610
611             if (this == obj)
612                 return true;
613             if (obj == null)
614                 return false;
615             if (getClass() != obj.getClass())
616                 return false;
617             final AvailableLocale other = (AvailableLocale) obj;
618             if (locale == null) {
619                 if (other.locale != null)
620                     return false;
621             } else if (!locale.equals(other.locale))
622                 return false;
623             return true;
624         }
625
626         public void writeFeatureDigests(FeatureModel featureModel,
627                 Map JavaDoc featureProperties) {
628
629             if (this.locale.equals("")) { //$NON-NLS-1$
630
writeFeatureDigest(localizedPrintStream, featureModel,
631                         (Properties JavaDoc) featureProperties.get("")); //$NON-NLS-1$
632
return;
633             }
634             Properties JavaDoc temp = new Properties JavaDoc();
635             if (locale.indexOf("_") < 0) { //$NON-NLS-1$
636
temp = combineProperties(
637                         (Properties JavaDoc) featureProperties.get(""), //$NON-NLS-1$
638
(Properties JavaDoc) featureProperties.get(locale), temp);
639                 writeFeatureDigest(localizedPrintStream, featureModel, temp);
640             } else {
641                 temp = combineProperties((Properties JavaDoc) featureProperties
642                         .get(locale.substring(locale.indexOf("_") + 1)), //$NON-NLS-1$
643
(Properties JavaDoc) featureProperties.get(locale), temp);
644                 writeFeatureDigest(localizedPrintStream, featureModel, temp);
645             }
646
647         }
648
649         private Properties JavaDoc combineProperties(Properties JavaDoc properties,
650                 Properties JavaDoc properties2, Properties JavaDoc properties3) {
651             return new CombinedProperties(properties3, properties2, properties);
652
653         }
654
655     }
656
657     public static void writeFeatureDigest(PrintStream JavaDoc digest,
658             FeatureModel featureModel, Properties JavaDoc featureProperties) {
659
660         String JavaDoc label = null;
661         String JavaDoc provider = null;
662         String JavaDoc description = null;
663         String JavaDoc license = null;
664         String JavaDoc copyright = null;
665
666         if ((featureProperties != null)
667                 && featureModel.getLabel().startsWith("%")) { //$NON-NLS-1$
668
label = featureProperties.getProperty(featureModel.getLabel()
669                     .substring(1));
670         } else {
671             label = featureModel.getLabel();
672         }
673         if ((featureProperties != null)
674                 && (featureModel.getDescriptionModel() != null)
675                 && featureModel.getDescriptionModel().getAnnotation()
676                 .startsWith("%")) { //$NON-NLS-1$
677
// System.out.println(featureProperties.getProperty(featureModel.getDescriptionModel().getAnnotation().substring(1)));
678
description = featureProperties.getProperty(featureModel
679                     .getDescriptionModel().getAnnotation().substring(1));
680         } else {
681             URLEntryModel descriptionModel = featureModel.getDescriptionModel();
682             if( descriptionModel == null )
683                 description = "";
684             else
685                 description = descriptionModel.getAnnotation();
686         }
687         String JavaDoc pvalue = featureModel.getProvider();
688         if ((featureProperties != null)
689                 && pvalue!=null && pvalue.startsWith("%")) { //$NON-NLS-1$
690
provider = featureProperties.getProperty(featureModel.getProvider()
691                     .substring(1));
692         } else {
693             provider = pvalue;
694         }
695         if (provider==null) provider = "";
696
697         if (((featureProperties != null) && featureModel.getCopyrightModel() != null)
698                 && featureModel.getCopyrightModel().getAnnotation().startsWith(
699                 "%")) { //$NON-NLS-1$
700
copyright = featureProperties.getProperty(featureModel
701                     .getCopyrightModel().getAnnotation().substring(1));
702         } else {
703             if (featureModel.getCopyrightModel() != null) {
704                 copyright = featureModel.getCopyrightModel().getAnnotation();
705             } else {
706                 copyright = null;
707             }
708         }
709
710         if ((featureProperties != null)
711                 && (featureModel.getLicenseModel() != null)
712                 && featureModel.getLicenseModel().getAnnotation().startsWith(
713                 "%")) { //$NON-NLS-1$
714
license = featureProperties.getProperty(featureModel
715                     .getLicenseModel().getAnnotation().substring(1));
716         } else {
717             license = featureModel.getLicenseModel().getAnnotation();
718         }
719
720         digest.print("<feature "); //$NON-NLS-1$
721
digest.print("label=\"" + label + "\" "); //$NON-NLS-1$//$NON-NLS-2$
722
digest.print("provider-name=\"" + provider + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
723
digest.print("id=\"" + featureModel.getFeatureIdentifier() + "\" "); //$NON-NLS-1$//$NON-NLS-2$
724
digest.print("version=\"" + featureModel.getFeatureVersion() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
725
if (featureModel.getOS() != null)
726             digest.print("os=\"" + featureModel.getOS() + "\" "); //$NON-NLS-1$//$NON-NLS-2$
727
if (featureModel.getNL() != null)
728             digest.print("nl=\"" + featureModel.getNL() + "\" "); //$NON-NLS-1$//$NON-NLS-2$
729
if (featureModel.getWS() != null)
730             digest.print("ws=\"" + featureModel.getWS() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
731
if (featureModel.getOSArch() != null)
732             digest.print("arch=\"" + featureModel.getOSArch() + "\" "); //$NON-NLS-1$//$NON-NLS-2$
733
if (featureModel.isExclusive())
734             digest.print("exclusive=\"" + featureModel.isExclusive() + "\" "); //$NON-NLS-1$//$NON-NLS-2$
735

736         if (((featureModel.getImportModels() == null) || (featureModel
737                 .getImportModels().length == 0))
738                 && ((featureModel.getDescriptionModel() == null)
739                         || (featureModel.getDescriptionModel().getAnnotation() == null) || (featureModel
740                                 .getDescriptionModel().getAnnotation().trim().length() == 0))
741                                 && ((featureModel.getCopyrightModel() == null)
742                                         || (featureModel.getCopyrightModel().getAnnotation() == null) || (featureModel
743                                                 .getCopyrightModel().getAnnotation().trim().length() == 0))
744                                                 && ((featureModel.getLicenseModel() == null)
745                                                         || (featureModel.getLicenseModel().getAnnotation() == null) || (featureModel
746                                                                 .getLicenseModel().getAnnotation().trim().length() == 0))
747                                                                 && ((featureModel.getFeatureIncluded() == null) || (featureModel
748                                                                         .getFeatureIncluded().length == 0))){
749             digest.println("/> "); //$NON-NLS-1$
750
} else {
751             digest.println("> "); //$NON-NLS-1$
752
if (featureModel.getImportModels().length > 0) {
753
754                 digest.println("\t<requires> "); //$NON-NLS-1$
755
ImportModel[] imports = featureModel.getImportModels();
756                 for (int j = 0; j < imports.length; j++) {
757                     digest.print("\t\t<import "); //$NON-NLS-1$
758
if (imports[j].isFeatureImport()) {
759                         digest.print("feature=\""); //$NON-NLS-1$
760
} else {
761                         digest.print("plugin=\""); //$NON-NLS-1$
762
}
763                     digest.print(imports[j].getIdentifier() + "\" "); //$NON-NLS-1$
764
digest.print("version=\""); //$NON-NLS-1$
765
digest.print(imports[j].getVersion() + "\" "); //$NON-NLS-1$
766
digest.print("match=\""); //$NON-NLS-1$
767
digest.print(imports[j].getMatchingRuleName() + "\" "); //$NON-NLS-1$
768
if (imports[j].isPatch()) {
769                         digest.print("patch=\"true\" "); //$NON-NLS-1$
770
}
771                     digest.println(" />"); //$NON-NLS-1$
772
}
773
774                 digest.println("\t</requires>"); //$NON-NLS-1$
775

776             }
777
778             if ((featureModel.getDescriptionModel() != null)
779                     && (featureModel.getDescriptionModel().getAnnotation() != null)
780                     && (featureModel.getDescriptionModel().getAnnotation()
781                             .trim().length() != 0)) {
782                 digest.println("\t<description>"); //$NON-NLS-1$
783
digest.println("\t\t" + UpdateManagerUtils.getWritableXMLString(description)); //$NON-NLS-1$
784
digest.println("\t</description>"); //$NON-NLS-1$
785
}
786
787             if (featureModel.getCopyrightModel() != null) {
788                 if (featureModel.getCopyrightModel().getAnnotation() != null) {
789                     // if
790
// (featureModel.getDescriptionModel().getAnnotation().length()
791
// != 0) {
792
digest.println("\t<copyright>"); //$NON-NLS-1$
793
digest.println("\t\t" + UpdateManagerUtils.getWritableXMLString(copyright)); //$NON-NLS-1$
794
digest.println("\t</copyright>"); //$NON-NLS-1$
795
// }
796
}
797             }
798
799             if ((featureModel.getLicenseModel() != null)
800                     && (featureModel.getLicenseModel().getAnnotation() != null)
801                     && (featureModel.getLicenseModel().getAnnotation()
802                             .trim().length() != 0)) {
803                 digest.println("\t<license>"); //$NON-NLS-1$
804
digest.println("\t\t" + UpdateManagerUtils.getWritableXMLString(license)); //$NON-NLS-1$
805
digest.println("\t</license>"); //$NON-NLS-1$
806
}
807
808             PluginEntryModel[] plugins = featureModel.getPluginEntryModels();
809             if ((plugins != null) && (plugins.length != 0)) {
810                 for (int i = 0; i < plugins.length; i++) {
811                     digest.print("\t<plugin "); //$NON-NLS-1$
812
digest.print("id=\"" + plugins[i].getPluginIdentifier() //$NON-NLS-1$
813
+ "\" "); //$NON-NLS-1$
814
digest.print("version=\"" + plugins[i].getPluginVersion() //$NON-NLS-1$
815
+ "\" "); //$NON-NLS-1$
816
if (plugins[i].getOS() != null)
817                         digest.print("os=\"" + plugins[i].getOS() + "\" "); //$NON-NLS-1$//$NON-NLS-2$
818
if (plugins[i].getNL() != null)
819                         digest.print("nl=\"" + plugins[i].getNL() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
820
if (plugins[i].getWS() != null)
821                         digest.print("ws=\"" + plugins[i].getWS() + "\" "); //$NON-NLS-1$//$NON-NLS-2$
822
if (plugins[i].getOSArch() != null)
823                         digest
824                         .print("arch=\"" + plugins[i].getOSArch() //$NON-NLS-1$
825
+ "\" "); //$NON-NLS-1$
826
if (plugins[i].getDownloadSize() > 0)
827                         digest.print("download-size=\"" //$NON-NLS-1$
828
+ plugins[i].getDownloadSize() + "\" "); //$NON-NLS-1$
829
if (plugins[i].getInstallSize() > 0)
830                         digest.print("install-size=\"" //$NON-NLS-1$
831
+ plugins[i].getInstallSize() + "\" "); //$NON-NLS-1$
832
if (!plugins[i].isUnpack())
833                         digest.print("unpack=\"" + plugins[i].isUnpack() //$NON-NLS-1$
834
+ "\" "); //$NON-NLS-1$
835

836                     digest.println("/> "); //$NON-NLS-1$
837
}
838             }
839
840             IIncludedFeatureReference[] inlcudedFeatures = featureModel.getFeatureIncluded();
841
842             if ((inlcudedFeatures != null) && (inlcudedFeatures.length != 0)) {
843                 for (int i = 0; i < inlcudedFeatures.length; i++) {
844                     try {
845                         digest.print("\t<includes "); //$NON-NLS-1$
846

847                         digest.print("id=\"" + inlcudedFeatures[i].getVersionedIdentifier().getIdentifier() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
848
digest.print("version=\"" + inlcudedFeatures[i].getVersionedIdentifier().getVersion() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
849
if (inlcudedFeatures[i].getOS() != null)
850                             digest.print("os=\"" + inlcudedFeatures[i].getOS() + "\" "); //$NON-NLS-1$//$NON-NLS-2$
851
if (inlcudedFeatures[i].getNL() != null)
852                             digest.print("nl=\"" + inlcudedFeatures[i].getNL() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
853
if (inlcudedFeatures[i].getWS() != null)
854                             digest.print("ws=\"" + inlcudedFeatures[i].getWS() + "\" "); //$NON-NLS-1$//$NON-NLS-2$
855
if (inlcudedFeatures[i].getOSArch() != null)
856                             digest.print("arch=\"" + inlcudedFeatures[i].getOSArch() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
857
if ( (inlcudedFeatures[i] instanceof IncludedFeatureReference) && (((IncludedFeatureReference)inlcudedFeatures[i]).getLabel() != null))
858                             digest.print("name=\"" + inlcudedFeatures[i].getName() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
859
if (inlcudedFeatures[i].isOptional())
860                             digest.print("optional=\"true\""); //$NON-NLS-1$
861
digest.print("search-location=\"" + inlcudedFeatures[i].getSearchLocation() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
862

863                         digest.println("/> "); //$NON-NLS-1$
864
} catch (CoreException e) {
865                         // TODO Auto-generated catch block
866
e.printStackTrace();
867                     }
868                 }
869             }
870             digest.println("</feature>"); //$NON-NLS-1$
871
}
872     }
873
874     private class CombinedProperties extends Properties JavaDoc {
875
876         private Properties JavaDoc properties1;
877
878         private Properties JavaDoc properties2;
879
880         private Properties JavaDoc properties3;
881
882         public CombinedProperties(Properties JavaDoc properties1,
883                 Properties JavaDoc properties2, Properties JavaDoc properties3) {
884             this.properties1 = properties1;
885             this.properties2 = properties2;
886             this.properties3 = properties3;
887         }
888
889         /**
890          *
891          */

892         private static final long serialVersionUID = 1L;
893
894         public String JavaDoc getProperty(String JavaDoc key) {
895             String JavaDoc result = null;
896             if (properties3 != null && properties3.containsKey(key))
897                 result = properties3.getProperty(key);
898             if (properties2 != null && properties2.containsKey(key))
899                 result = properties2.getProperty(key);
900             if (properties1 != null && properties1.containsKey(key))
901                 result = properties1.getProperty(key);
902             return result;
903         }
904
905     }
906
907 }
908
Popular Tags