1 package com.sun.tools.internal.xjc.runtime; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import java.util.Map ; 6 import java.util.StringTokenizer ; 7 8 import javax.xml.bind.JAXBContext; 9 import javax.xml.bind.JAXBException; 10 11 49 public class JAXBContextFactory { 50 private static final String DOT_OBJECT_FACTORY = ".ObjectFactory"; 51 private static final String IMPL_DOT_OBJECT_FACTORY = ".impl.ObjectFactory"; 52 53 56 public static JAXBContext createContext( Class [] classes, Map properties ) throws JAXBException { 57 Class [] r = new Class [classes.length]; 58 boolean modified = false; 59 60 for( int i=0; i<r.length; i++ ) { 63 Class c = classes[i]; 64 String name = c.getName(); 65 if(name.endsWith(DOT_OBJECT_FACTORY) 66 && !name.endsWith(IMPL_DOT_OBJECT_FACTORY)) { 67 69 name = name.substring(0,name.length()-DOT_OBJECT_FACTORY.length())+IMPL_DOT_OBJECT_FACTORY; 72 73 try { 74 c = c.getClassLoader().loadClass(name); 75 } catch (ClassNotFoundException e) { 76 throw new JAXBException(e); 77 } 78 79 modified = true; 80 } 81 82 r[i] = c; 83 } 84 85 if(!modified) { 86 90 95 throw new JAXBException("Unable to find a JAXB implementation to delegate"); 99 } 100 101 return JAXBContext.newInstance(r,properties); 103 } 104 105 106 109 public static JAXBContext createContext( String contextPath, 110 ClassLoader classLoader, Map properties ) throws JAXBException { 111 112 List <Class > classes = new ArrayList <Class >(); 113 StringTokenizer tokens = new StringTokenizer (contextPath,":"); 114 115 try { 120 while(tokens.hasMoreTokens()) { 121 String pkg = tokens.nextToken(); 122 classes.add(classLoader.loadClass(pkg+IMPL_DOT_OBJECT_FACTORY)); 123 } 124 } catch (ClassNotFoundException e) { 125 throw new JAXBException(e); 126 } 127 128 return JAXBContext.newInstance(classes.toArray(new Class [classes.size()]),properties); 130 } 131 } 132 | Popular Tags |