KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > JBossXBTestDelegate


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.xml;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.net.URL JavaDoc;
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 /**
35  * JBossXBTestDelegate.
36  *
37  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
38  * @version $Revision: 40492 $
39  */

40 public class JBossXBTestDelegate extends AbstractTestDelegate
41 {
42    /** Whether initialization has been done */
43    private static boolean done = false;
44
45    /** The unmarshaller factory */
46    protected UnmarshallerFactory unmarshallerFactory;
47
48    /** The resolver */
49    protected SchemaBindingResolver defaultResolver;
50    
51    /**
52     * Initialize
53     */

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 JavaDoc 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    /**
70     * Create a new JBossXBTestDelegate.
71     *
72     * @param clazz the test class
73     */

74    public JBossXBTestDelegate(Class JavaDoc clazz)
75    {
76       super(clazz);
77    }
78
79    public void setUp() throws Exception JavaDoc
80    {
81       super.setUp();
82       init();
83       unmarshallerFactory = UnmarshallerFactory.newInstance();
84       initResolver();
85    }
86    
87    protected void initResolver() throws Exception JavaDoc
88    {
89       try
90       {
91          Method JavaDoc method = clazz.getMethod("initResolver", null);
92          defaultResolver = (SchemaBindingResolver) method.invoke(null, null);
93       }
94       catch (NoSuchMethodException JavaDoc ignored)
95       {
96          defaultResolver = new DefaultSchemaResolver();
97       }
98    }
99    
100    /**
101     * Unmarshal an object
102     *
103     * @param url the url
104     * @param resolver the resolver
105     * @return the object
106     * @throws Exception for any error
107     */

108    public Object JavaDoc unmarshal(String JavaDoc url, SchemaBindingResolver resolver) throws Exception JavaDoc
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 JavaDoc result = unmarshaller.unmarshal(url, resolver);
119          log.debug("Total parse for " + url + " took " + (System.currentTimeMillis() - start) + "ms");
120          return result;
121       }
122       catch (Exception JavaDoc e)
123       {
124          log.debug("Error during parsing: " + url, e);
125          throw e;
126       }
127    }
128 }
129
Popular Tags