1 22 package org.jboss.test.xml; 23 24 import java.lang.reflect.Method ; 25 import java.net.URL ; 26 27 import org.jboss.net.protocol.URLStreamHandlerFactory; 28 import org.jboss.test.AbstractTestDelegate; 29 import org.jboss.xb.binding.Unmarshaller; 30 import org.jboss.xb.binding.UnmarshallerFactory; 31 import org.jboss.xb.binding.sunday.unmarshalling.DefaultSchemaResolver; 32 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver; 33 34 40 public class JBossXBTestDelegate extends AbstractTestDelegate 41 { 42 43 private static boolean done = false; 44 45 46 protected UnmarshallerFactory unmarshallerFactory; 47 48 49 protected SchemaBindingResolver defaultResolver; 50 51 54 public synchronized static void init() 55 { 56 if (done) 57 return; 58 done = true; 59 URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory()); 60 URLStreamHandlerFactory.preload(); 61 String handlerPkgs = System.getProperty("java.protocol.handler.pkgs"); 62 if (handlerPkgs != null) 63 handlerPkgs += "|org.jboss.net.protocol"; 64 else 65 handlerPkgs = "org.jboss.net.protocol"; 66 System.setProperty("java.protocol.handler.pkgs", handlerPkgs); 67 } 68 69 74 public JBossXBTestDelegate(Class clazz) 75 { 76 super(clazz); 77 } 78 79 public void setUp() throws Exception 80 { 81 super.setUp(); 82 init(); 83 unmarshallerFactory = UnmarshallerFactory.newInstance(); 84 initResolver(); 85 } 86 87 protected void initResolver() throws Exception 88 { 89 try 90 { 91 Method method = clazz.getMethod("initResolver", null); 92 defaultResolver = (SchemaBindingResolver) method.invoke(null, null); 93 } 94 catch (NoSuchMethodException ignored) 95 { 96 defaultResolver = new DefaultSchemaResolver(); 97 } 98 } 99 100 108 public Object unmarshal(String url, SchemaBindingResolver resolver) throws Exception 109 { 110 if (resolver == null) 111 resolver = defaultResolver; 112 113 long start = System.currentTimeMillis(); 114 Unmarshaller unmarshaller = unmarshallerFactory.newUnmarshaller(); 115 log.debug("Initialized parsing in " + (System.currentTimeMillis() - start) + "ms"); 116 try 117 { 118 Object result = unmarshaller.unmarshal(url, resolver); 119 log.debug("Total parse for " + url + " took " + (System.currentTimeMillis() - start) + "ms"); 120 return result; 121 } 122 catch (Exception e) 123 { 124 log.debug("Error during parsing: " + url, e); 125 throw e; 126 } 127 } 128 } 129 | Popular Tags |