1 28 29 30 package org.jibx.binding; 31 32 import java.io.ByteArrayInputStream ; 33 import java.io.ByteArrayOutputStream ; 34 import java.io.File ; 35 import java.io.FileInputStream ; 36 import java.io.FileNotFoundException ; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.net.URL ; 40 import java.util.ArrayList ; 41 import java.util.jar.Attributes ; 42 import java.util.jar.JarFile ; 43 import java.util.jar.Manifest ; 44 45 import org.jibx.binding.classes.BoundClass; 46 import org.jibx.binding.classes.ClassFile; 47 import org.jibx.binding.def.BindingBuilder; 48 import org.jibx.binding.def.BindingDefinition; 49 import org.jibx.binding.def.MappingBase; 50 import org.jibx.binding.model.BindingElement; 51 import org.jibx.binding.model.MappingElement; 52 import org.jibx.binding.model.ValidationContext; 53 import org.jibx.runtime.JiBXException; 54 import org.jibx.runtime.impl.UnmarshallingContext; 55 56 63 64 public class Utility 65 { 66 private static final int COPY_BUFFER_SIZE = 1024; 68 69 private Utility() {} 71 72 79 private static byte[] getStreamData(InputStream is) throws IOException { 80 byte[] buff = new byte[COPY_BUFFER_SIZE]; 81 ByteArrayOutputStream os = new ByteArrayOutputStream (); 82 int count; 83 while ((count = is.read(buff)) >= 0) { 84 os.write(buff, 0, count); 85 } 86 return os.toByteArray(); 87 } 88 89 97 private static void recursePathJars(String path, ArrayList paths) { 98 try { 99 100 JarFile jfile = new JarFile (path, false); 102 Manifest mfst = jfile.getManifest(); 103 if (mfst != null) { 104 105 Attributes attrs = mfst.getMainAttributes(); 107 String cpath = (String )attrs.get(Attributes.Name.CLASS_PATH); 108 if (cpath != null) { 109 110 int split = path.lastIndexOf(File.separatorChar); 112 String base = (split >= 0) ? 113 path.substring(0, split+1) : ""; 114 115 while (cpath != null) { 117 split = cpath.indexOf(' '); 118 String item; 119 if (split >= 0) { 120 item = cpath.substring(0, split); 121 cpath = cpath.substring(split+1).trim(); 122 } else { 123 item = cpath; 124 cpath = null; 125 } 126 String ipath = base + item; 127 if (!paths.contains(ipath)) { 128 paths.add(ipath); 129 split = ipath.lastIndexOf('.'); 130 if (split >= 0 && "jar".equalsIgnoreCase 131 (ipath.substring(split+1))) { 132 recursePathJars(ipath, paths); 133 } 134 } 135 } 136 } 137 } 138 } catch (IOException ex) { } 139 } 140 141 146 public static String [] getClassPaths() { 147 148 String path = System.getProperty("java.class.path"); 150 ArrayList paths = new ArrayList (); 151 int start = 0; 152 int mark; 153 while (path != null) { 154 mark = path.indexOf(File.pathSeparatorChar, start); 155 String item; 156 if (mark >= 0) { 157 item = path.substring(start, mark); 158 } else { 159 item = path.substring(start); 160 path = null; 161 } 162 if (!paths.contains(item)) { 163 paths.add(item); 164 int split = item.lastIndexOf('.'); 165 if (split >= 0 && 166 "jar".equalsIgnoreCase(item.substring(split+1))) { 167 recursePathJars(item, paths); 168 } 169 } 170 start = mark + 1; 171 } 172 paths.add("."); 173 String [] clsspths = new String [paths.size()]; 174 paths.toArray(clsspths); 175 return clsspths; 176 } 177 178 186 public static String convertName(String name) { 187 188 StringBuffer buff = new StringBuffer (name); 190 if (!Character.isJavaIdentifierStart(buff.charAt(0))) { 191 buff.insert(0, 'X'); 192 } 193 for (int i = 1; i < buff.length(); i++) { 194 if (!Character.isJavaIdentifierPart(buff.charAt(i))) { 195 buff.setCharAt(i, '_'); 196 } 197 } 198 return buff.toString(); 199 } 200 201 207 public static String fileName(String path) { 208 int split = path.lastIndexOf(File.separatorChar); 209 return path.substring(split+1); 210 } 211 212 223 public static BindingElement validateBinding(String name, URL url, 224 InputStream is) { 225 try { 226 ValidationContext vctx = BindingElement.newValidationContext(); 227 BindingElement root = 228 BindingElement.validateBinding(name, url, is, vctx); 229 if (vctx.getErrorCount() == 0 && vctx.getFatalCount() == 0) { 230 return root; 231 } 232 } catch (JiBXException ex) { 233 System.err.println("Unable to process binding " + name); 234 ex.printStackTrace(); 235 } 236 return null; 237 } 238 239 256 public static BindingDefinition loadBinding(String fname, String sname, 257 InputStream istrm, URL url, boolean test) 258 throws JiBXException, IOException { 259 260 byte[] data = getStreamData(istrm); 262 263 boolean valid = true; 265 ClassFile cf = null; 266 String tpack = null; 267 if (test) { 268 BindingElement root = validateBinding(fname, url, 269 new ByteArrayInputStream (data)); 270 if (root == null) { 271 valid = false; 272 } else { 273 274 ArrayList childs = root.topChildren(); 276 if (childs != null) { 277 278 for (int i = 0; i < childs.size(); i++) { 280 Object child = childs.get(i); 281 if (child instanceof MappingElement) { 282 283 MappingElement map = (MappingElement)child; 285 cf = map.getHandledClass().getClassFile(); 286 if (!cf.isInterface() && cf.isModifiable()) { 287 break; 288 } 289 } 290 } 291 } 292 tpack = root.getTargetPackage(); 293 if (tpack == null && cf != null) { 294 tpack = cf.getPackage(); 295 } 296 } 297 } 298 if (valid) { 299 try { 300 UnmarshallingContext uctx = new UnmarshallingContext(0, 302 new String [0], new String [0], new String [0], new String [0]); 303 uctx.setDocument(new ByteArrayInputStream (data), fname, null); 304 if (cf != null) { 305 306 BoundClass.setModify(cf.getRoot(), tpack); 308 } 309 BindingDefinition bdef = 310 BindingBuilder.unmarshalBindingDefinition(uctx, sname, url); 311 312 if (!test) { 314 315 ArrayList maps = bdef.getDefinitionContext().getMappings(); 317 if (maps != null) { 318 319 for (int i = 0; i < maps.size(); i++) { 321 Object child = maps.get(i); 322 if (child instanceof MappingBase) { 323 324 MappingBase mapbase = (MappingBase)child; 326 cf = mapbase.getBoundClass().getMungedFile(); 327 if (mapbase.getBoundClass().isDirectAccess()) { 328 break; 329 } 330 331 } 332 } 333 } 334 } 335 336 if (cf == null) { 338 throw new JiBXException("One or more <mapping> elements " + 339 "must be defined in <binding>"); 340 } else { 341 342 if (tpack == null) { 344 tpack = bdef.getDefaultPackage(); 345 if (tpack == null) { 346 tpack = cf.getPackage(); 347 } 348 } 349 bdef.setFactoryLocation(tpack, cf.getRoot()); 350 return bdef; 351 } 352 353 } catch (JiBXException e) { 354 throw new JiBXException 355 ("\n*** Error during code generation - please report " + 356 "this error on the JiBX users list so that the condition " + 357 "can be caught during validation ***\n", e); 358 } 359 360 } else { 361 throw new JiBXException("Binding " + fname + 362 " is unusable because of validation errors"); 363 } 364 } 365 366 375 public static BindingDefinition loadFileBinding(String path, boolean valid) 376 throws JiBXException, IOException { 377 378 String fname = fileName(path); 380 String sname = fname; 381 int split = sname.indexOf('.'); 382 if (split > 0) { 383 sname = sname.substring(0, split); 384 } 385 sname = convertName(sname); 386 387 File file = new File (path); 389 return loadBinding(fname, sname, new FileInputStream (file), 390 new URL ("file://" + file.getAbsolutePath()), valid); 391 } 392 } | Popular Tags |