KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > L10NUpdate


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.autoupdate;
21
22 import java.io.File JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.openide.modules.Dependency;
29 import org.openide.modules.ModuleInfo;
30 import org.openide.modules.SpecificationVersion;
31 import org.openide.util.NbBundle;
32 import org.w3c.dom.*;
33
34 /** This class represents one module l10n update available on the web
35  *
36  * @author Ales Kemr
37  * @version
38  */

39 class L10NUpdate extends ModuleUpdate {
40
41     private static final String JavaDoc ATTR_LONG_DESC = "OpenIDE-Module-Long-Description"; // NOI18N
42
/** Holds value of property langcode. */
43     private String JavaDoc langcode;
44     
45     /** Holds value of property brandingcode. */
46     private String JavaDoc brandingcode;
47     
48     /** Holds value of property specversion. */
49     private SpecificationVersion specversion;
50     
51     /** Holds value of property majorversion. */
52     private int majorversion;
53     
54     /** Holds value of property l10nname. */
55     private String JavaDoc l10nname;
56     
57     /** Holds value of property l10ndesc. */
58     private String JavaDoc l10ndesc;
59     
60     private L10NModuleInfo remoteinfo;
61     
62     L10NUpdate( URL JavaDoc xmlURL, Node node, Element documentElement ) {
63         super( xmlURL, node, documentElement );
64     }
65
66     /** Creates new L10NUpdate for downloaded .nbm file */
67     L10NUpdate( File JavaDoc nbmFile, Node node, Element documentElement ) {
68         super( nbmFile, node, documentElement );
69     }
70     
71     ModuleInfo readRemoteInfo() throws IllegalArgumentException JavaDoc {
72         NodeList nodeList = ((Element)getNode()).getElementsByTagName( ModuleUpdate.ELEMENT_L10N );
73
74         if ( nodeList.getLength() > 0 ) {
75             Node n = nodeList.item( 0 );
76             
77             langcode = getAttribute( n, "langcode" ); // NOI18N
78
brandingcode = getAttribute( n, "brandingcode" ); // NOI18N
79
specversion = new SpecificationVersion ( getAttribute( n, "module_spec_version" ) ); // NOI18N
80
String JavaDoc mvAttr = getAttribute( n, "module_major_version" ); // NOI18N
81
if ( mvAttr != null ) {
82                 majorversion = Integer.parseInt( mvAttr );
83             }
84             else {
85                 majorversion = -1;
86             }
87             l10nname = getAttribute( n, "OpenIDE-Module-Name" ); // NOI18N
88
l10ndesc = getAttribute( n, ATTR_LONG_DESC );
89             if ( getDescription() == null || getDescription().equals ("") ) // NOI18N
90
setDescription( l10ndesc );
91         }
92         
93         remoteinfo = new L10NModuleInfo();
94         remoteinfo.setName( l10nname );
95         remoteinfo.setCodeNameBase( getInfoCodenamebase() );
96         remoteinfo.setMajorversion( majorversion );
97         remoteinfo.setSpecificationVersion( specversion );
98         remoteinfo.setDependency( getInfoCodenamebase(), majorversion );
99         
100         return remoteinfo;
101     }
102     
103     ModuleInfo readLocalInfo() {
104         ModuleInfo localmodule = super.readLocalInfo();
105         L10NModuleInfo localinfo = null;
106         
107         if ( localmodule != null ) {
108             if ( l10nname == null || l10nname.equals ("") ) // NOI18N
109
remoteinfo.setName( localmodule.getDisplayName() );
110             if ( getDescription() == null || getDescription().equals ("") ) // NOI18N
111
setDescription( (String JavaDoc) localmodule.getLocalizedAttribute( ATTR_LONG_DESC ) );
112             
113             // get spec vers from update_tracking
114
SafeModule.ModuleStatus status = SafeModule.getModuleStatus( getInfoCodenamebase() );
115             if ( status != null ) {
116                 String JavaDoc jarpath = status.getJarPath() + "locale/" + status.getJarName(); // NOI18N
117
if ( brandingcode != null && brandingcode.length() > 0 ) {
118                     jarpath = jarpath + "_" + brandingcode; // NOI18N
119
}
120                 if ( langcode != null && langcode.length() > 0 ) {
121                     jarpath = jarpath + "_" + langcode; // NOI18N
122
}
123                 jarpath = jarpath + ".jar"; // NOI18N
124
SpecificationVersion specvers = getTrackingSpecVersion( jarpath );
125                 localinfo = new L10NModuleInfo();
126                 localinfo.setName( remoteinfo.getDisplayName() );
127                 localinfo.setCodeNameBase( getInfoCodenamebase() );
128                 if ( specvers != null ) {
129                     localinfo.setSpecificationVersion( specvers );
130                 }
131             }
132             markNamesWithL10N();
133         }
134         
135         return localinfo;
136     }
137     
138     private void markNamesWithL10N() {
139         String JavaDoc desc = "";
140         if ( langcode != null && langcode.length() > 0 ) {
141             String JavaDoc lang = new Locale JavaDoc( langcode, "" ).getDisplayLanguage(); // NOI18N
142
desc = NbBundle.getMessage( L10NUpdate.class, "TXT_L10N_Prefix", // NOI18N
143
lang );
144         }
145         if ( brandingcode != null && brandingcode.length() > 0 ) {
146             if ( langcode != null && langcode.length() > 0 )
147                 desc = desc + ", "; // NOI18N
148
desc = desc + NbBundle.getMessage( L10NUpdate.class, "TXT_L10N_Branding", // NOI18N
149
brandingcode );
150         }
151         if ( desc.length() > 0 ) {
152             desc = desc + NbBundle.getMessage (L10NUpdate.class, "TXT_L10N_DescEnd"); // NOI18N
153
}
154         
155         desc = desc + getDescription();
156         setDescription( desc );
157         
158         if ( l10nname == null || l10nname.equals ("") ) { // NOI18N
159
String JavaDoc dname = remoteinfo.getDisplayName() + " (";
160             if ( langcode != null && langcode.length() > 0 ) {
161                 dname = dname + langcode; // NOI18N
162
}
163             if ( brandingcode != null && brandingcode.length() > 0 ) {
164                 if ( langcode != null && langcode.length() > 0 )
165                     dname = dname + " "; // NOI18N
166
dname = dname + brandingcode;
167             }
168             dname = dname + ")";
169             remoteinfo.setName( dname ); // NOI18N
170
}
171     }
172     
173     private SpecificationVersion getTrackingSpecVersion(String JavaDoc jarpath) {
174         Iterator JavaDoc it = org.netbeans.updater.UpdateTracking.clusters (true).iterator ();
175         while (it.hasNext ()) {
176             File JavaDoc clusterDir = (File JavaDoc)it.next ();
177             org.netbeans.updater.UpdateTracking tracking;
178             tracking = org.netbeans.updater.UpdateTracking.getTracking (clusterDir, false);
179             if (tracking == null) {
180                 // skip directory without update_tracking info
181
continue;
182             }
183             
184             String JavaDoc specversU = tracking.getL10NSpecificationVersion( getInfoCodenamebase(), true, jarpath );
185             String JavaDoc specversI = tracking.getL10NSpecificationVersion( getInfoCodenamebase(), false, jarpath );
186             if ( specversU == null && specversI == null )
187                 continue;
188         
189             SpecificationVersion svU = ( specversU != null ) ? new SpecificationVersion( specversU ) : null;
190             SpecificationVersion svI = ( specversI != null ) ? new SpecificationVersion( specversI ) : null;
191
192             if ( svI != null && svU == null)
193                 return svI;
194             else if ( svI == null && svU != null )
195                 return svU;
196
197             if ( svI.compareTo( svU ) > 0 )
198                 return svI;
199             else
200                 return svU;
201         }
202         return null;
203     }
204     
205     private String JavaDoc getAttribute(Node n, String JavaDoc attribute) {
206         Node attr = n.getAttributes().getNamedItem( attribute );
207         return attr == null ? null : attr.getNodeValue();
208     }
209     
210     /** Getter for property langcode.
211      * @return Value of property langcode.
212      *
213      */

214     String JavaDoc getLangcode() {
215         return langcode;
216     }
217     
218     /** Getter for property brandingcode.
219      * @return Value of property brandingcode.
220      *
221      */

222     String JavaDoc getBrandingcode() {
223         return brandingcode;
224     }
225     
226     /** Getter for property majorversion.
227      * @return Value of property majorversion.
228      *
229      */

230     int getMajorversion() {
231         return majorversion;
232     }
233     
234     /** Getter for property l10nname.
235      * @return Value of property l10nname.
236      *
237      */

238     String JavaDoc getL10nname() {
239         return l10nname;
240     }
241     
242     /** Getter for property l10ndesc.
243      * @return Value of property l10ndesc.
244      *
245      */

246     String JavaDoc getL10ndesc() {
247         return l10ndesc;
248     }
249     
250     // overwritten from ModuleUpdate
251

252     /** Tests if there is an update available */
253     boolean isUpdateAvailable() {
254         if ( getLocalModule() != null &&
255                 ( getLocalModule().getSpecificationVersion() == null
256                  || getLocalModule().getSpecificationVersion().compareTo( remoteinfo.getSpecificationVersion() ) < 0
257                 ) ) {
258             return true;
259         }
260         
261         return false;
262     }
263     
264     boolean isRemoteModuleAvailable( List JavaDoc modules ) {
265         Iterator JavaDoc it = modules.iterator();
266         while ( it.hasNext() ) {
267             ModuleUpdate module = (ModuleUpdate) it.next();
268             if ( module.getCodeNameBase().equals( remoteinfo.getCodeNameBase() )
269                     && module.getRemoteModule().getSpecificationVersion().compareTo( remoteinfo.getSpecificationVersion()) <= 0) { // < bug 6174967/>
270
if ( remoteinfo.getDisplayName() == null || remoteinfo.getDisplayName().equals ("") ) // NOI18N
271
remoteinfo.setName( module.getRemoteModule().getDisplayName() );
272                 if ( getDescription() == null || getDescription().equals ("") ) // NOI18N
273
setDescription( (String JavaDoc) module.getRemoteModule().getLocalizedAttribute( ATTR_LONG_DESC ) );
274                 markNamesWithL10N();
275                 return true;
276             }
277         }
278         
279         return false;
280     }
281     
282     class L10NModuleInfo extends ModuleInfo {
283         
284         private SpecificationVersion specversion;
285         private String JavaDoc name;
286         private Set JavaDoc dep;
287         private String JavaDoc codenamebase;
288         private int majorversion;
289         
290         public L10NModuleInfo() {
291         }
292         
293         void setSpecificationVersion(SpecificationVersion specversion) {
294             this.specversion = specversion;
295         }
296         
297         void setName(String JavaDoc name) {
298             this.name = name;
299         }
300         
301         void setCodeNameBase(String JavaDoc codenamebase) {
302             this.codenamebase = codenamebase;
303         }
304         
305         void setMajorversion(int majorversion) {
306             this.majorversion = majorversion;
307         }
308         
309         void setDependency(String JavaDoc codenamebase, int majorversion) {
310             String JavaDoc body = codenamebase;
311             if ( majorversion > 0 )
312                 body = body + "/" + majorversion; // NOI18N
313
dep = Dependency.create( Dependency.TYPE_MODULE, body );
314         }
315         
316         // implement ModuleInfo
317

318         public SpecificationVersion getSpecificationVersion() {
319             return specversion;
320         }
321         
322         public String JavaDoc getDisplayName() {
323             return name;
324         }
325         
326         /** Get some attribute, for example OpenIDE-Module-Name.
327          * Not all manifest attributes need be supported here.
328          * Attributes not present in the manifest may be available.
329          *
330          */

331         public Object JavaDoc getAttribute(String JavaDoc attr) {
332             return null;
333         }
334         
335         /** The full code name, with release version after slash if defined. */
336         public String JavaDoc getCodeName() {
337             return codenamebase + "/" + majorversion; // NOI18N;
338
}
339         
340         /** The code name of the module, sans release version. */
341         public String JavaDoc getCodeNameBase() {
342             return codenamebase;
343         }
344         
345         /** The release version (-1 if undefined). */
346         public int getCodeNameRelease() {
347             return majorversion;
348         }
349         
350         /** Get a list of all dependencies this module has. */
351         public Set JavaDoc getDependencies() {
352             return dep;
353         }
354         
355         /** Get an attribute with localization.
356          * That is, if there is a suitable locale variant of the attribute
357          * name, return its value rather than the value of the base attribute.
358          *
359          */

360         public Object JavaDoc getLocalizedAttribute(String JavaDoc attr) {
361             return null;
362         }
363         
364         /** Whether the module is currently enabled. */
365         public boolean isEnabled() {
366             return false;
367         }
368         
369         /** Determine if the provided class
370          * was loaded as a part of this module, and thus will only be
371          * loadable later if this module is enabled.
372          * If in doubt, return <code>false</code>.
373          * @since 1.28
374          *
375          */

376         public boolean owns(Class JavaDoc clazz) {
377             return false;
378         }
379         
380     }
381     
382 }
383
Popular Tags