1 21 22 package com.izforge.izpack.util; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.InputStream ; 28 import java.util.Iterator ; 29 import java.util.Vector ; 30 31 import net.n3.nanoxml.NonValidator; 32 import net.n3.nanoxml.StdXMLBuilder; 33 import net.n3.nanoxml.StdXMLParser; 34 import net.n3.nanoxml.StdXMLReader; 35 import net.n3.nanoxml.XMLElement; 36 37 import com.izforge.izpack.installer.InstallerException; 38 import com.izforge.izpack.installer.ResourceManager; 39 40 46 public class SpecHelper 47 { 48 49 private String specFilename; 50 51 private XMLElement spec; 52 53 private boolean _haveSpec; 54 55 public static final String YES = "yes"; 56 57 public static final String NO = "no"; 58 59 private static final String PACK_KEY = "pack"; 60 61 private static final String PACK_NAME = "name"; 62 63 66 public SpecHelper() 67 { 68 super(); 69 } 70 71 72 77 78 public void readSpec(String specFileName) throws Exception 79 { 80 readSpec(specFileName, null); 81 } 82 83 84 89 90 public void readSpec(String specFileName, VariableSubstitutor substitutor) throws Exception 91 { 92 InputStream input = null; 94 try 95 { 96 input = getResource(specFileName); 97 } 98 catch (Exception exception) 99 { 100 _haveSpec = false; 101 return; 102 } 103 if (input == null) 104 { 105 _haveSpec = false; 106 return; 107 } 108 109 readSpec(input, substitutor); 110 111 input.close(); 113 this.specFilename = specFileName; 114 } 115 116 117 122 123 public void readSpec(InputStream input) throws Exception 124 { 125 readSpec(input, null); 126 } 127 128 129 134 135 public void readSpec(InputStream input, VariableSubstitutor substitutor) throws Exception 136 { 137 if (substitutor != null) 139 { 140 input = substituteVariables(input, substitutor); 141 } 142 143 StdXMLParser parser = new StdXMLParser(); 145 parser.setBuilder(new StdXMLBuilder()); 146 parser.setValidator(new NonValidator()); 147 parser.setReader(new StdXMLReader(input)); 148 149 spec = (XMLElement) parser.parse(); 151 _haveSpec = true; 152 } 153 154 160 public InputStream getResource(String res) 161 { 162 try 163 { 164 return ResourceManager.getInstance().getInputStream(res); 166 } 167 catch (Exception e) 168 { return null; 170 } 171 } 172 173 179 public XMLElement getPackForName(String packDestName) 180 { 181 Vector packs = getSpec().getChildrenNamed(PACK_KEY); 182 Iterator iter = null; 183 if (packs == null) return (null); 184 iter = packs.iterator(); 185 while (iter.hasNext()) 186 { 187 188 XMLElement pack = (XMLElement) iter.next(); 189 String packName = pack.getAttribute(PACK_NAME); 190 if (packName.equals(packDestName)) return (pack); 191 } 192 return (null); 193 194 } 195 196 203 public void parseError(XMLElement parent, String message) throws InstallerException 204 { 205 throw new InstallerException(specFilename + ":" + parent.getLineNr() + ": " + message); 206 } 207 208 213 public boolean haveSpec() 214 { 215 return _haveSpec; 216 } 217 218 223 public XMLElement getSpec() 224 { 225 return spec; 226 } 227 228 233 public void setSpec(XMLElement element) 234 { 235 spec = element; 236 } 237 238 246 public Vector getAllSubChildren(XMLElement root, String [] childdef) 247 { 248 return (getSubChildren(root, childdef, 0)); 249 } 250 251 261 private Vector getSubChildren(XMLElement root, String [] childdef, int depth) 262 { 263 Vector retval = null; 264 Vector retval2 = null; 265 Vector children = root != null ? root.getChildrenNamed(childdef[depth]) : null; 266 if (children == null) return (null); 267 if (depth < childdef.length - 1) 268 { 269 Iterator iter = children.iterator(); 270 while (iter.hasNext()) 271 { 272 retval2 = getSubChildren((XMLElement) iter.next(), childdef, depth + 1); 273 if (retval2 != null) 274 { 275 if (retval == null) retval = new Vector (); 276 retval.addAll(retval2); 277 } 278 } 279 } 280 else 281 return (children); 282 return (retval); 283 } 284 285 294 public InputStream substituteVariables(InputStream input, VariableSubstitutor substitutor) 295 throws Exception 296 { 297 File tempFile = File.createTempFile("izpacksubs", ""); 298 FileOutputStream fos = null; 299 tempFile.deleteOnExit(); 300 try 301 { 302 fos = new FileOutputStream (tempFile); 303 substitutor.substitute(input, fos, null, "UTF-8"); 304 } 305 finally 306 { 307 if (fos != null) fos.close(); 308 } 309 return new FileInputStream (tempFile); 310 } 311 312 321 public boolean isAttributeYes(XMLElement element, String attribute, boolean defaultValue) 322 { 323 String value = element.getAttribute(attribute, (defaultValue ? YES : NO)); 324 if (value.equalsIgnoreCase(YES)) return true; 325 if (value.equalsIgnoreCase(NO)) return false; 326 327 return defaultValue; 328 } 329 330 339 public String getRequiredAttribute(XMLElement element, String attrName) 340 throws InstallerException 341 { 342 String attr = element.getAttribute(attrName); 343 if (attr == null) 344 { 345 parseError(element, "<" + element.getName() + "> requires attribute '" + attrName 346 + "'."); 347 } 348 return (attr); 349 } 350 } 351 | Popular Tags |