KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > suite > BrandingSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.suite;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.jar.JarEntry JavaDoc;
38 import java.util.jar.JarFile JavaDoc;
39 import org.netbeans.modules.apisupport.project.ManifestManager;
40 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteProperties;
41 import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
42 import org.netbeans.modules.apisupport.project.universe.ModuleList;
43 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
44 import org.netbeans.spi.project.support.ant.EditableProperties;
45 import org.netbeans.spi.project.support.ant.PropertyUtils;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.util.Utilities;
48
49 /**
50  * Provide set of helper methods for branding purposes.
51  * @author Radek Matous
52  */

53 public final class BrandingSupport {
54     
55     private final SuiteProject suiteProject;
56     private final SuiteProperties suiteProperties;
57     private Set JavaDoc brandedModules;
58     private Set JavaDoc brandedBundleKeys;
59     private Set JavaDoc brandedFiles;
60     
61     private NbPlatform platform;
62     private final File JavaDoc brandingDir;
63     
64     public static final String JavaDoc BRANDING_DIR_PROPERTY = "branding.dir"; // NOI18N
65
private static final String JavaDoc BUNDLE_NAME = "Bundle.properties"; //NOI18N
66

67     public static BrandingSupport getInstance(final SuiteProperties suiteProperties) throws IOException JavaDoc {
68         return new BrandingSupport(suiteProperties);
69     }
70         
71     private BrandingSupport(final SuiteProperties suiteProperties) throws IOException JavaDoc {
72         this.suiteProperties = suiteProperties;
73         this.suiteProject = suiteProperties.getProject();
74         File JavaDoc suiteDir = suiteProject.getProjectDirectoryFile();
75         assert suiteDir != null && suiteDir.exists();
76         brandingDir = new File JavaDoc(suiteDir, getNameOfBrandingFolder());//NOI18N
77
init();
78     }
79     
80     /**
81      * @return the project directory beneath which everything in the project lies
82      */

83     public File JavaDoc getProjectDirectory() {
84         return suiteProject.getProjectDirectoryFile();
85     }
86     
87     /**
88      * @return the top-level branding directory
89      */

90     public File JavaDoc getBrandingRoot() {
91         return new File JavaDoc(getProjectDirectory(), getNameOfBrandingFolder());
92     }
93     
94     /**
95      * @return the branding directory for NetBeans module represented as
96      * <code>ModuleEntry</code>
97      */

98     public File JavaDoc getModuleEntryDirectory(ModuleEntry mEntry) {
99         String JavaDoc relativePath;
100         relativePath = PropertyUtils.relativizeFile( mEntry.getClusterDirectory(),
101                 mEntry.getJarLocation());
102         return new File JavaDoc(getBrandingRoot(),relativePath);
103     }
104     
105     /**
106      * @return the file representing localizing bundle for NetBeans module
107      */

108     public File JavaDoc getLocalizingBundle(final ModuleEntry mEntry) {
109         ManifestManager mfm = ManifestManager.getInstanceFromJAR(mEntry.getJarLocation());
110         File JavaDoc bundle = null;
111         if (mfm != null) {
112             String JavaDoc bundlePath = mfm.getLocalizingBundle();
113             if (bundlePath != null) {
114                 bundle = new File JavaDoc(getModuleEntryDirectory(mEntry),bundlePath);
115             }
116         }
117         return bundle;
118     }
119     
120     public boolean isBranded(final BundleKey key) {
121         boolean retval = getBrandedBundleKeys().contains(key);
122         return retval;
123         
124     }
125     
126     public boolean isBranded(final BrandedFile bFile) {
127         boolean retval = getBrandedFiles().contains(bFile);
128         return retval;
129         
130     }
131     
132     
133     /**
134      * @return true if NetBeans module is already branded
135      */

136     public boolean isBranded(final ModuleEntry entry) {
137         boolean retval = getBrandedModules().contains(entry);
138         assert (retval == getModuleEntryDirectory(entry).exists());
139         return retval;
140     }
141     
142     public Set JavaDoc getBrandedModules() {
143         return brandedModules;
144     }
145     
146     public Set JavaDoc getBrandedBundleKeys() {
147         return brandedBundleKeys;
148     }
149     
150     public Set JavaDoc getBrandedFiles() {
151         return brandedFiles;
152     }
153     
154     public Set JavaDoc getLocalizingBundleKeys(final String JavaDoc moduleCodeNameBase, final Set JavaDoc keys) {
155         ModuleEntry foundEntry = getModuleEntry(moduleCodeNameBase);
156         return (foundEntry != null) ? getLocalizingBundleKeys(foundEntry, keys) : null;
157     }
158     
159     public Set JavaDoc getLocalizingBundleKeys(final ModuleEntry moduleEntry, final Set JavaDoc keys) {
160         Set JavaDoc retval = new HashSet JavaDoc();
161         for (Iterator JavaDoc it = getBrandedBundleKeys().iterator();
162         it.hasNext() && retval.size() != keys.size();) {
163             BundleKey bKey = (BundleKey)it.next();
164             if (keys.contains(bKey.getKey())) {
165                 retval.add(bKey);
166             }
167         }
168         
169         if (retval.size() != keys.size()) {
170             loadLocalizedBundlesFromPlatform(moduleEntry, keys, retval);
171         }
172         return (retval.size() != keys.size()) ? null : retval;
173     }
174     
175     public BrandedFile getBrandedFile(final String JavaDoc moduleCodeNameBase, final String JavaDoc entryPath) {
176         ModuleEntry foundEntry = getModuleEntry(moduleCodeNameBase);
177         return (foundEntry != null) ? getBrandedFile(foundEntry,entryPath) : null;
178     }
179     
180     public BrandedFile getBrandedFile(final ModuleEntry moduleEntry, final String JavaDoc entryPath) {
181         BrandedFile retval = null;
182         try {
183             retval = new BrandedFile(moduleEntry, entryPath);
184             for (Iterator JavaDoc it = getBrandedFiles().iterator();it.hasNext() ;) {
185                 BrandedFile bFile = (BrandedFile)it.next();
186                 
187                 if (retval.equals(bFile)) {
188                     retval = bFile;
189                     
190                 }
191             }
192         } catch (MalformedURLException JavaDoc ex) {
193             retval = null;
194         }
195         return retval;
196     }
197     
198     public BundleKey getBundleKey(final String JavaDoc moduleCodeNameBase,
199             final String JavaDoc bundleEntry,final String JavaDoc key) {
200         Set JavaDoc keys = new HashSet JavaDoc();
201         keys.add(key);
202         keys = getBundleKeys(moduleCodeNameBase,bundleEntry, keys);
203         return (keys == null) ? null : (BrandingSupport.BundleKey) keys.toArray()[0];
204     }
205     
206     public Set JavaDoc getBundleKeys(final String JavaDoc moduleCodeNameBase, final String JavaDoc bundleEntry,final Set JavaDoc keys) {
207         ModuleEntry foundEntry = getModuleEntry(moduleCodeNameBase);
208         return (foundEntry != null) ? getBundleKeys(foundEntry,bundleEntry, keys) : null;
209     }
210     
211     public Set JavaDoc getBundleKeys(final ModuleEntry moduleEntry, final String JavaDoc bundleEntry, final Set JavaDoc keys) {
212         Set JavaDoc retval = new HashSet JavaDoc();
213         for (Iterator JavaDoc it = getBrandedBundleKeys().iterator();
214         it.hasNext() && retval.size() != keys.size();) {
215             BundleKey bKey = (BundleKey)it.next();
216             if (keys.contains(bKey.getKey())) {
217                 retval.add(bKey);
218             }
219         }
220         
221         if (retval.size() != keys.size()) {
222             try {
223                 loadLocalizedBundlesFromPlatform(moduleEntry, bundleEntry, keys, retval);
224             } catch (IOException JavaDoc ex) {
225                 //ex.printStackTrace();
226
throw new IllegalStateException JavaDoc();
227             }
228         }
229                     
230         return (retval.size() != keys.size()) ? null : retval;
231     }
232     
233     private ModuleEntry getModuleEntry(final String JavaDoc moduleCodeNameBase) {
234         NbPlatform platform;
235         platform = getActivePlatform();
236         for (Iterator JavaDoc it = Arrays.asList(platform.getModules()).iterator(); it.hasNext();) {
237             ModuleEntry entry = (ModuleEntry)it.next();
238             if (entry.getCodeNameBase().equals(moduleCodeNameBase)) {
239                 return entry;
240             }
241         }
242         
243         return null;
244     }
245
246     private NbPlatform getActivePlatform() {
247         NbPlatform retval = suiteProperties.getActivePlatform();
248         if (retval != null) {
249             return retval;
250         } else {
251             return NbPlatform.getDefaultPlatform();
252         }
253     }
254     
255     public void brandFile(final BrandedFile bFile) throws IOException JavaDoc {
256         if (!bFile.isModified()) return;
257         
258         File JavaDoc target = bFile.getFileLocation();
259         if (!target.exists()) {
260             target.getParentFile().mkdirs();
261             target.createNewFile();
262         }
263         
264         assert target.exists();
265         
266         InputStream JavaDoc is = null;
267         OutputStream JavaDoc os = null;
268         try {
269             is = bFile.getBrandingSource().openStream();
270             os = new FileOutputStream JavaDoc(target);
271             FileUtil.copy(is, os);
272         } finally {
273             if (is != null) {
274                 is.close();
275             }
276             
277             if (os != null) {
278                 os.close();
279             }
280             
281             brandedFiles.add(bFile);
282             bFile.modified = false;
283         }
284     }
285     
286     public void brandFile(final BrandedFile bFile, final Runnable JavaDoc saveTask) throws IOException JavaDoc {
287         if (!bFile.isModified()) return;
288         
289         saveTask.run();
290         brandedFiles.add(bFile);
291         bFile.modified = false;
292     }
293     
294     public void brandBundleKey(final BundleKey bundleKey) throws IOException JavaDoc {
295         if (bundleKey == null) {
296             return;
297         }
298         Set JavaDoc keys = new HashSet JavaDoc();
299         keys.add(bundleKey);
300         brandBundleKeys(keys);
301     }
302     
303     public void brandBundleKeys(final Set JavaDoc bundleKeys) throws IOException JavaDoc {
304         init();
305         Map JavaDoc mentryToEditProp = new HashMap JavaDoc();
306         for (Iterator JavaDoc it = bundleKeys.iterator();it.hasNext();) {
307             BundleKey bKey = (BundleKey)it.next();
308             if (bKey.isModified()) {
309                 EditableProperties ep = (EditableProperties)mentryToEditProp.get(bKey.getBrandingBundle());
310                 if (ep == null) {
311                     File JavaDoc bundle = bKey.getBrandingBundle();
312                     if (!bundle.exists()) {
313                         bundle.getParentFile().mkdirs();
314                         bundle.createNewFile();
315                     }
316                     ep = getEditableProperties(bundle);
317                     mentryToEditProp.put(bKey.getBrandingBundle(), ep);
318                 }
319                 ep.setProperty(bKey.getKey(), bKey.getValue());
320             }
321         }
322         
323         for (Iterator JavaDoc it = mentryToEditProp.entrySet().iterator();it.hasNext();) {
324             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
325             File JavaDoc bundle = (File JavaDoc) entry.getKey();
326             assert bundle.exists();
327             storeEditableProperties((EditableProperties) entry.getValue(), bundle);
328             for (Iterator JavaDoc it2 = bundleKeys.iterator();it2.hasNext();) {
329                 BundleKey bKey = (BundleKey)it2.next();
330                 File JavaDoc bundle2 = bKey.getBrandingBundle();
331                 if (bundle2.equals(bundle)) {
332                     brandedBundleKeys.add(bKey);
333                     bKey.modified = false;
334                     brandedModules.add(bKey.getModuleEntry());
335                 }
336             }
337         }
338     }
339     
340     private void init() throws IOException JavaDoc {
341         NbPlatform newPlatform = getActivePlatform();
342         
343         if (brandedModules == null || !newPlatform.equals(platform)) {
344             brandedModules = new HashSet JavaDoc();
345             brandedBundleKeys = new HashSet JavaDoc();
346             brandedFiles = new HashSet JavaDoc();
347             platform = newPlatform;
348             
349             if (brandingDir.exists()) {
350                 assert brandingDir.isDirectory();
351                 scanModulesInBrandingDir(brandingDir, platform.getModules());
352             }
353         }
354     }
355     
356     private void scanModulesInBrandingDir(final File JavaDoc srcDir, final ModuleEntry[] platformModules) throws IOException JavaDoc {
357         if (srcDir.getName().endsWith(".jar")) {//NOI18N
358
ModuleEntry foundEntry = null;
359             for (int i = 0; i < platformModules.length; i++){
360                 if (isBrandingForModuleEntry(srcDir, platformModules[i])) {
361                     scanBrandedFiles(srcDir, platformModules[i]);
362                     
363                     foundEntry = platformModules[i];
364                     break;
365                 }
366             }
367             if (foundEntry != null) {
368                 brandedModules.add(foundEntry);
369             }
370         } else {
371             String JavaDoc[] kids = srcDir.list();
372             assert (kids != null);
373             
374             for (int i = 0; i < kids.length; i++) {
375                 File JavaDoc kid = new File JavaDoc(srcDir, kids[i]);
376                 if (!kid.isDirectory()) {
377                     continue;
378                 }
379                 scanModulesInBrandingDir(kid, platformModules);
380             }
381         }
382     }
383     
384     private void scanBrandedFiles(final File JavaDoc srcDir, final ModuleEntry mEntry) throws IOException JavaDoc {
385         String JavaDoc[] kids = srcDir.list();
386         assert (kids != null);
387         
388         for (int i = 0; i < kids.length; i++) {
389             File JavaDoc kid = new File JavaDoc(srcDir, kids[i]);
390             if (!kid.isDirectory()) {
391                 if (kid.getName().endsWith(BUNDLE_NAME)) {
392                     loadBundleKeys(mEntry, kid);
393                 } else {
394                     loadBrandedFiles(mEntry, kid);
395                 }
396                 
397                 continue;
398             }
399             scanBrandedFiles(kid, mEntry);
400         }
401     }
402     
403     private void loadBundleKeys(final ModuleEntry mEntry,
404             final File JavaDoc bundle) throws IOException JavaDoc {
405         
406         EditableProperties p = getEditableProperties(bundle);
407         
408         for (Iterator JavaDoc it = p.entrySet().iterator(); it.hasNext();) {
409             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
410             brandedBundleKeys.add(new BundleKey(mEntry, bundle,(String JavaDoc)entry.getKey(), (String JavaDoc)entry.getValue()));
411         }
412     }
413     
414     private void loadBrandedFiles(final ModuleEntry mEntry,
415             final File JavaDoc file) throws IOException JavaDoc {
416         
417         String JavaDoc entryPath = PropertyUtils.relativizeFile(getModuleEntryDirectory(mEntry),file);
418         BrandedFile bf = new BrandedFile(mEntry, file.toURI().toURL(), entryPath);
419         brandedFiles.add(bf);
420     }
421     
422     
423     private static EditableProperties getEditableProperties(final File JavaDoc bundle) throws IOException JavaDoc {
424         EditableProperties p = new EditableProperties(true);
425         InputStream JavaDoc is;
426         is = new FileInputStream JavaDoc(bundle);
427         try {
428             p.load(is);
429         } finally {
430             is.close();
431         }
432         
433         
434         return p;
435     }
436     
437     private static void storeEditableProperties(final EditableProperties p, final File JavaDoc bundle) throws IOException JavaDoc {
438         OutputStream JavaDoc os;
439         os = new FileOutputStream JavaDoc(bundle);
440         try {
441             p.store(os);
442         } finally {
443             os.close();
444         }
445     }
446     
447     
448     private void loadLocalizedBundlesFromPlatform(final ModuleEntry moduleEntry, final Set JavaDoc keys, final Set JavaDoc bundleKeys) {
449         EditableProperties p;
450         p = ModuleList.loadBundleInfo(moduleEntry.getSourceLocation()).toEditableProperties();
451         for (Iterator JavaDoc it = p.keySet().iterator(); it.hasNext(); ) {
452             String JavaDoc key = (String JavaDoc)it.next();
453             if (keys.contains(key)) {
454                 String JavaDoc value = (String JavaDoc)p.getProperty(key);
455                 bundleKeys.add(new BundleKey(moduleEntry, key, value));
456             }
457         }
458     }
459     
460     private void loadLocalizedBundlesFromPlatform(final ModuleEntry moduleEntry,
461             final String JavaDoc bundleEntry, final Set JavaDoc keys, final Set JavaDoc bundleKeys) throws IOException JavaDoc {
462         Properties JavaDoc p = new Properties JavaDoc();
463         JarFile JavaDoc module = new JarFile JavaDoc(moduleEntry.getJarLocation());
464         JarEntry JavaDoc je = module.getJarEntry(bundleEntry);
465         InputStream JavaDoc is = module.getInputStream(je);
466         File JavaDoc bundle = new File JavaDoc(getModuleEntryDirectory(moduleEntry),bundleEntry);
467         try {
468             
469             p.load(is);
470         } finally {
471             is.close();
472         }
473         for (Iterator JavaDoc it = p.keySet().iterator(); it.hasNext(); ) {
474             String JavaDoc key = (String JavaDoc)it.next();
475             if (keys.contains(key)) {
476                 String JavaDoc value = (String JavaDoc)p.getProperty(key);
477                 bundleKeys.add(new BundleKey(moduleEntry, bundle, key, value));
478             }
479         }
480     }
481     
482     
483     private boolean isBrandingForModuleEntry(final File JavaDoc srcDir, final ModuleEntry mEntry) {
484         boolean retval = mEntry.getJarLocation().getName().equals(srcDir.getName());
485         if (retval) {
486             String JavaDoc relPath1 = PropertyUtils.relativizeFile( mEntry.getClusterDirectory(), mEntry.getJarLocation().getParentFile());
487             String JavaDoc relPath2 = PropertyUtils.relativizeFile(brandingDir, srcDir.getParentFile());
488             
489             retval = relPath1.equals(relPath2);
490         }
491         return retval;
492     }
493     
494     public final class BundleKey {
495         private final File JavaDoc brandingBundle;
496         private final ModuleEntry moduleEntry;
497         private final String JavaDoc key;
498         private String JavaDoc value;
499         private boolean modified = false;
500         
501         private BundleKey(final ModuleEntry moduleEntry, final File JavaDoc brandingBundle, final String JavaDoc key, final String JavaDoc value) {
502             this.moduleEntry = moduleEntry;
503             this.key = key;
504             this.value = value;
505             this.brandingBundle = brandingBundle;
506         }
507         
508         private BundleKey(final ModuleEntry mEntry, final String JavaDoc key, final String JavaDoc value) {
509             this(mEntry, getLocalizingBundle(mEntry), key,value);
510         }
511         
512         public ModuleEntry getModuleEntry() {
513             return moduleEntry;
514         }
515         
516         public String JavaDoc getKey() {
517             return key;
518         }
519         
520         public String JavaDoc getValue() {
521             return value;
522         }
523         
524         public void setValue(final String JavaDoc value) {
525             if (!this.value.equals(value)) {
526                 modified = true;
527             }
528             this.value = value;
529         }
530         
531         public boolean equals(Object JavaDoc obj) {
532             boolean retval = false;
533             
534             if (obj instanceof BundleKey) {
535                 BundleKey bKey = (BundleKey)obj;
536                 retval = getKey().equals(bKey.getKey())
537                 && getModuleEntry().equals(bKey.getModuleEntry())
538                 && getBrandingBundle().equals(bKey.getBrandingBundle());
539             }
540             
541             return retval;
542         }
543         
544         public int hashCode() {
545             return 0;
546         }
547         
548         boolean isModified() {
549             return modified;
550         }
551         
552         public File JavaDoc getBrandingBundle() {
553             return brandingBundle;
554         }
555
556     }
557     
558     public class BrandedFile {
559         private final ModuleEntry moduleEntry;
560         private final String JavaDoc entryPath;
561         private URL JavaDoc brandingSource;
562         private boolean modified = false;
563         
564         private BrandedFile(final ModuleEntry moduleEntry, final String JavaDoc entry) throws MalformedURLException JavaDoc {
565             this(moduleEntry, null, entry);
566         }
567         
568         private BrandedFile(final ModuleEntry moduleEntry, final URL JavaDoc source, final String JavaDoc entry) throws MalformedURLException JavaDoc {
569             this.moduleEntry = moduleEntry;
570             this.entryPath = entry;
571             if (source == null) {
572                 brandingSource = moduleEntry.getJarLocation().toURI().toURL();
573                 brandingSource = new URL JavaDoc("jar:" + brandingSource + "!/" + entryPath); // NOI18N
574
} else {
575                 brandingSource = source;
576             }
577             
578         }
579         
580         public ModuleEntry getModuleEntry() {
581             return moduleEntry;
582         }
583         
584         public String JavaDoc getEntryPath() {
585             return entryPath;
586         }
587         
588         public File JavaDoc getFileLocation() {
589             return new File JavaDoc(getModuleEntryDirectory(getModuleEntry()), getEntryPath());
590         }
591         
592         public URL JavaDoc getBrandingSource() {
593             return brandingSource;
594         }
595         
596         public void setBrandingSource(URL JavaDoc brandingSource) {
597             if (!Utilities.compareObjects(brandingSource, this.brandingSource)) {
598                 modified = true;
599             }
600             this.brandingSource = brandingSource;
601         }
602         
603         public void setBrandingSource(File JavaDoc brandingFile) throws MalformedURLException JavaDoc {
604             setBrandingSource(brandingFile.toURI().toURL());
605         }
606         
607         public boolean isModified() {
608             return modified;
609         }
610         
611         public boolean equals(Object JavaDoc obj) {
612             boolean retval = false;
613             
614             if (obj instanceof BrandedFile) {
615                 BrandedFile bFile = (BrandedFile)obj;
616                 retval = getModuleEntry().equals(bFile.getModuleEntry())
617                 && getFileLocation().equals(bFile.getFileLocation());
618             }
619             
620             //if ()
621
return retval;
622         }
623
624         public int hashCode() {
625             return 0;
626         }
627         
628     }
629
630     public String JavaDoc getNameOfBrandingFolder() {
631         return suiteProject.getEvaluator().getProperty(BRANDING_DIR_PROPERTY);
632     }
633     
634 }
635
Popular Tags