1 19 20 package org.netbeans.updater; 21 22 import java.io.File ; 23 import java.io.InputStream ; 24 import java.io.IOException ; 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 37 class ModuleUpdate extends Object { 38 39 private static final String ATTR_CODENAMEBASE = "codenamebase"; 42 43 private String codenamebase = null; 44 45 private String specification_version = null; 46 47 private boolean pError = false; 48 49 private boolean l10n = false; 50 private boolean fromInstall = false; 51 52 53 ModuleUpdate( File nbmFile, boolean fromInstall ) { 54 this.fromInstall = fromInstall; 55 createFromDistribution( nbmFile ); 56 } 57 58 59 private void createFromDistribution( File nbmFile ) { 60 61 Document document = null; 62 Node node = null; 63 Element documentElement = null; 64 65 JarFile jf = null; 67 InputStream is = null; 68 boolean exit = false; 69 String errorMessage = null; 70 try { 71 jf = new JarFile(nbmFile); 72 is = jf.getInputStream(jf.getEntry("Info/info.xml")); 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 e ) { 83 errorMessage = "Bad info : " + nbmFile.getAbsolutePath (); System.out.println(errorMessage); 85 e.printStackTrace (); 86 exit = true; 87 } 88 catch ( java.io.IOException e ) { 89 errorMessage = "Missing info : " + nbmFile.getAbsolutePath (); 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 ioe ) { 101 ioe.printStackTrace(); 102 exit = true; 103 } 104 } 105 106 if (exit) { 107 throw new RuntimeException (errorMessage); 108 } 109 110 setCodenamebase( getAttribute( node, ATTR_CODENAMEBASE ) ); 111 NodeList nodeList = ((Element)node).getElementsByTagName( "l10n" ); 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" ); 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 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")) setCodenamebase(attr.getValue()); 133 else if (attr.getName().equals("OpenIDE-Module-Specification-Version")) setSpecification_version(attr.getValue()); 135 } 136 } 137 } 138 } 139 140 private String getAttribute(Node n, String attribute) { 141 Node attr = n.getAttributes().getNamedItem( attribute ); 142 return attr == null ? null : attr.getNodeValue(); 143 } 144 145 148 String getCodenamebase() { 149 return codenamebase; 150 } 151 152 155 void setCodenamebase(String codenamebase) { 156 this.codenamebase = codenamebase; 157 } 158 159 162 String getSpecification_version() { 163 return specification_version; 164 } 165 166 169 void setSpecification_version(String specification_version) { 170 this.specification_version = specification_version; 171 } 172 173 177 public boolean isL10n() { 178 return l10n; 179 } 180 181 185 public boolean isFromInstall() { 186 return fromInstall; 187 } 188 189 class ErrorCatcher implements org.xml.sax.ErrorHandler { 190 private void message (String level, org.xml.sax.SAXParseException e) { 191 pError = true; 192 } 193 194 public void error (org.xml.sax.SAXParseException e) { 195 pError = true; 197 } 198 199 public void warning (org.xml.sax.SAXParseException e) { 200 } 202 203 public void fatalError (org.xml.sax.SAXParseException e) { 204 pError = true; 205 } 206 } 208 } 209 | Popular Tags |