001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.updater;
043:
044: import java.io.File;
045: import java.io.InputStream;
046: import java.io.IOException;
047: import java.util.*;
048: import java.util.jar.*;
049:
050: import org.w3c.dom.*;
051: import org.xml.sax.*;
052: import javax.xml.parsers.*;
053:
054: /** This class represents one module update available on the web
055: *
056: * @author Ales Kemr
057: * @version
058: */
059: class ModuleUpdate extends Object {
060:
061: // Constants
062: private static final String ATTR_CODENAMEBASE = "codenamebase"; // NOI18N
063:
064: /** Holds value of property codenamebase. */
065: private String codenamebase = null;
066: /** Holds value of sv */
067: private String specification_version = null;
068:
069: private boolean pError = false;
070:
071: private boolean l10n = false;
072:
073: /** Creates new ModuleUpdate for downloaded .nbm file */
074: ModuleUpdate(File nbmFile) {
075: createFromDistribution(nbmFile);
076: }
077:
078: /** Creates module from downloaded .nbm file */
079: private void createFromDistribution(File nbmFile) {
080:
081: Document document = null;
082: Node node = null;
083: Element documentElement = null;
084:
085: // Try to parse the info file
086: JarFile jf = null;
087: InputStream is = null;
088: boolean exit = false;
089: String errorMessage = null;
090: try {
091: jf = new JarFile(nbmFile);
092: is = jf.getInputStream(jf.getEntry("Info/info.xml")); // NOI18N
093:
094: InputSource xmlInputSource = new InputSource(is);
095: document = XMLUtil.parse(xmlInputSource, false, false,
096: new ErrorCatcher(), XMLUtil.createAUResolver());
097:
098: documentElement = document.getDocumentElement();
099: node = documentElement;
100: if (is != null)
101: is.close();
102: } catch (org.xml.sax.SAXException e) {
103: errorMessage = "Bad info : " + nbmFile.getAbsolutePath(); // NOI18N
104: System.out.println(errorMessage);
105: e.printStackTrace();
106: exit = true;
107: } catch (java.io.IOException e) {
108: errorMessage = "Missing info : "
109: + nbmFile.getAbsolutePath(); // NOI18N
110: System.out.println(errorMessage);
111: e.printStackTrace();
112: exit = true;
113: } finally {
114: try {
115: if (is != null)
116: is.close();
117: if (jf != null)
118: jf.close();
119: } catch (IOException ioe) {
120: ioe.printStackTrace();
121: exit = true;
122: }
123: }
124:
125: if (exit) {
126: throw new RuntimeException(errorMessage);
127: }
128:
129: setCodenamebase(getAttribute(node, ATTR_CODENAMEBASE));
130: NodeList nodeList = ((Element) node)
131: .getElementsByTagName("l10n"); // NOI18N
132:
133: if (nodeList.getLength() > 0) {
134: l10n = true;
135: Node n = nodeList.item(0);
136: setSpecification_version(getAttribute(n,
137: "module_spec_version"));
138: } else {
139: nodeList = ((Element) node)
140: .getElementsByTagName("manifest"); // NOI18N
141:
142: for (int i = 0; i < nodeList.getLength(); i++) {
143:
144: if (nodeList.item(i).getNodeType() != Node.ELEMENT_NODE
145: || !(nodeList.item(i) instanceof Element)) {
146: break;
147: }
148:
149: // ((Element)nodeList.item( i )).normalize();
150: NamedNodeMap attrList = nodeList.item(i)
151: .getAttributes();
152: for (int j = 0; j < attrList.getLength(); j++) {
153: Attr attr = (Attr) attrList.item(j);
154: if (attr.getName().equals("OpenIDE-Module")) // NOI18N
155: setCodenamebase(attr.getValue());
156: else if (attr.getName().equals(
157: "OpenIDE-Module-Specification-Version")) // NOI18N
158: setSpecification_version(attr.getValue());
159: }
160: }
161: }
162: }
163:
164: private String getAttribute(Node n, String attribute) {
165: Node attr = n.getAttributes().getNamedItem(attribute);
166: return attr == null ? null : attr.getNodeValue();
167: }
168:
169: /** Getter for property codeNameBase.
170: *@return Value of property codeNameBase.
171: */
172: String getCodenamebase() {
173: return codenamebase;
174: }
175:
176: /** Setter for property Codenamebase.
177: *@param manufacturer New value of property Codenamebase.
178: */
179: void setCodenamebase(String codenamebase) {
180: this .codenamebase = codenamebase;
181: }
182:
183: /** Getter for property specification_version.
184: *@return Value of property specification_version.
185: */
186: String getSpecification_version() {
187: return specification_version;
188: }
189:
190: /** Setter for property specification_version.
191: *@param notification New value of property specification_version.
192: */
193: void setSpecification_version(String specification_version) {
194: this .specification_version = specification_version;
195: }
196:
197: /** Getter for property l10n.
198: * @return Value of property l10n.
199: *
200: */
201: public boolean isL10n() {
202: return l10n;
203: }
204:
205: class ErrorCatcher implements org.xml.sax.ErrorHandler {
206: private void message(String level,
207: org.xml.sax.SAXParseException e) {
208: pError = true;
209: }
210:
211: public void error(org.xml.sax.SAXParseException e) {
212: // normally a validity error
213: pError = true;
214: }
215:
216: public void warning(org.xml.sax.SAXParseException e) {
217: //parseFailed = true;
218: }
219:
220: public void fatalError(org.xml.sax.SAXParseException e) {
221: pError = true;
222: }
223: } //end of inner class ErrorPrinter
224:
225: }
|