KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > updater > ModuleUpdate


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.updater;
21
22 import java.io.File JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.*;
26 import java.util.jar.*;
27
28 import org.w3c.dom.*;
29 import org.xml.sax.*;
30 import javax.xml.parsers.*;
31
32 /** This class represents one module update available on the web
33  *
34  * @author Ales Kemr
35  * @version
36  */

37 class ModuleUpdate extends Object JavaDoc {
38
39     // Constants
40
private static final String JavaDoc ATTR_CODENAMEBASE = "codenamebase"; // NOI18N
41

42     /** Holds value of property codenamebase. */
43     private String JavaDoc codenamebase = null;
44     /** Holds value of sv */
45     private String JavaDoc specification_version = null;
46     
47     private boolean pError = false;
48
49     private boolean l10n = false;
50     private boolean fromInstall = false;
51     
52     /** Creates new ModuleUpdate for downloaded .nbm file */
53     ModuleUpdate( File JavaDoc nbmFile, boolean fromInstall ) {
54         this.fromInstall = fromInstall;
55         createFromDistribution( nbmFile );
56     }
57
58     /** Creates module from downloaded .nbm file */
59     private void createFromDistribution( File JavaDoc nbmFile ) {
60
61         Document document = null;
62         Node node = null;
63         Element documentElement = null;
64         
65         // Try to parse the info file
66
JarFile jf = null;
67         InputStream JavaDoc is = null;
68         boolean exit = false;
69         String JavaDoc errorMessage = null;
70         try {
71             jf = new JarFile(nbmFile);
72             is = jf.getInputStream(jf.getEntry("Info/info.xml")); // NOI18N
73

74             InputSource xmlInputSource = new InputSource( is );
75             document = XMLUtil.parse( xmlInputSource, false, false, new ErrorCatcher(), XMLUtil.createAUResolver() );
76             
77             documentElement = document.getDocumentElement();
78             node = documentElement;
79             if (is != null)
80                 is.close();
81         }
82         catch ( org.xml.sax.SAXException JavaDoc e ) {
83             errorMessage = "Bad info : " + nbmFile.getAbsolutePath (); // NOI18N
84
System.out.println(errorMessage);
85             e.printStackTrace ();
86             exit = true;
87         }
88         catch ( java.io.IOException JavaDoc e ) {
89             errorMessage = "Missing info : " + nbmFile.getAbsolutePath (); // NOI18N
90
System.out.println(errorMessage);
91             e.printStackTrace ();
92             exit = true;
93         }
94         finally {
95             try {
96                 if (is != null)
97                     is.close();
98                 if (jf != null)
99                     jf.close();
100             } catch ( IOException JavaDoc ioe ) {
101                 ioe.printStackTrace();
102                 exit = true;
103             }
104         }
105         
106         if (exit) {
107             throw new RuntimeException JavaDoc (errorMessage);
108         }
109
110         setCodenamebase( getAttribute( node, ATTR_CODENAMEBASE ) );
111         NodeList nodeList = ((Element)node).getElementsByTagName( "l10n" ); // NOI18N
112

113         if ( nodeList.getLength() > 0 ) {
114             l10n = true;
115             Node n = nodeList.item( 0 );
116             setSpecification_version( getAttribute( n, "module_spec_version" ) );
117         } else {
118             nodeList = ((Element)node).getElementsByTagName( "manifest" ); // NOI18N
119

120             for( int i = 0; i < nodeList.getLength(); i++ ) {
121
122                 if ( nodeList.item( i ).getNodeType() != Node.ELEMENT_NODE ||
123                         !( nodeList.item( i ) instanceof Element ) ) {
124                     break;
125                 }
126
127                 // ((Element)nodeList.item( i )).normalize();
128
NamedNodeMap attrList = nodeList.item( i ).getAttributes();
129                 for( int j = 0; j < attrList.getLength(); j++ ) {
130                     Attr attr = (Attr) attrList.item( j );
131                     if (attr.getName().equals("OpenIDE-Module")) // NOI18N
132
setCodenamebase(attr.getValue());
133                     else if (attr.getName().equals("OpenIDE-Module-Specification-Version")) // NOI18N
134
setSpecification_version(attr.getValue());
135                 }
136             }
137         }
138     }
139     
140     private String JavaDoc getAttribute(Node n, String JavaDoc attribute) {
141         Node attr = n.getAttributes().getNamedItem( attribute );
142         return attr == null ? null : attr.getNodeValue();
143     }
144
145     /** Getter for property codeNameBase.
146      *@return Value of property codeNameBase.
147      */

148     String JavaDoc getCodenamebase() {
149         return codenamebase;
150     }
151
152     /** Setter for property Codenamebase.
153      *@param manufacturer New value of property Codenamebase.
154      */

155     void setCodenamebase(String JavaDoc codenamebase) {
156         this.codenamebase = codenamebase;
157     }
158
159     /** Getter for property specification_version.
160      *@return Value of property specification_version.
161      */

162     String JavaDoc getSpecification_version() {
163         return specification_version;
164     }
165
166     /** Setter for property specification_version.
167      *@param notification New value of property specification_version.
168      */

169     void setSpecification_version(String JavaDoc specification_version) {
170         this.specification_version = specification_version;
171     }
172     
173     /** Getter for property l10n.
174      * @return Value of property l10n.
175      *
176      */

177     public boolean isL10n() {
178         return l10n;
179     }
180     
181     /** Getter for property fromInstall.
182      * @return Value of property fromInstall.
183      *
184      */

185     public boolean isFromInstall() {
186         return fromInstall;
187     }
188     
189     class ErrorCatcher implements org.xml.sax.ErrorHandler JavaDoc {
190         private void message (String JavaDoc level, org.xml.sax.SAXParseException JavaDoc e) {
191             pError = true;
192         }
193
194         public void error (org.xml.sax.SAXParseException JavaDoc e) {
195             // normally a validity error
196
pError = true;
197         }
198
199         public void warning (org.xml.sax.SAXParseException JavaDoc e) {
200             //parseFailed = true;
201
}
202
203         public void fatalError (org.xml.sax.SAXParseException JavaDoc e) {
204             pError = true;
205         }
206     } //end of inner class ErrorPrinter
207

208 }
209
Popular Tags