1 8 package com.sun.japex.testsuite.impl.runtime; 9 10 import java.io.InputStream ; 11 import java.io.ObjectInputStream ; 12 import java.util.ArrayList ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 17 import javax.xml.bind.JAXBException; 18 import javax.xml.namespace.QName ; 19 20 import com.sun.xml.bind.Messages; 21 22 30 public class GrammarInfoImpl implements GrammarInfo 31 { 32 36 private final Map rootTagMap; 37 38 41 private final Class objectFactoryClass; 42 43 48 private final Map defaultImplementationMap; 49 50 53 private final ClassLoader classLoader; 54 55 public GrammarInfoImpl( Map _rootTagMap, Map _defaultImplementationMap, Class _objectFactoryClass ) { 56 this.rootTagMap = _rootTagMap; 57 this.defaultImplementationMap = _defaultImplementationMap; 58 this.objectFactoryClass = _objectFactoryClass; 59 this.classLoader = objectFactoryClass.getClassLoader(); 62 } 63 64 68 private final Class lookupRootMap( String nsUri, String localName ) { 69 QName qn; 71 72 qn = new QName (nsUri,localName); 73 if(rootTagMap.containsKey(qn)) return (Class )rootTagMap.get(qn); 74 75 qn = new QName (nsUri,"*"); 76 if(rootTagMap.containsKey(qn)) return (Class )rootTagMap.get(qn); 77 78 qn = new QName ("*","*"); 79 return (Class )rootTagMap.get(qn); 80 } 81 82 public final Class getRootElement(String namespaceUri, String localName) { 83 Class intfCls = lookupRootMap(namespaceUri,localName); 84 if(intfCls==null) return null; 85 else return getDefaultImplementation(intfCls); 86 } 87 88 public final UnmarshallingEventHandler createUnmarshaller( 89 String namespaceUri, String localName, UnmarshallingContext context ) { 90 91 Class impl = getRootElement(namespaceUri,localName); 92 if(impl==null) return null; 93 94 try { 95 return ((UnmarshallableObject)impl.newInstance()).createUnmarshaller(context); 96 } catch (InstantiationException e) { 97 throw new InstantiationError (e.toString()); 98 } catch (IllegalAccessException e) { 99 throw new IllegalAccessError (e.toString()); 100 } 101 } 102 103 public final String [] getProbePoints() { 104 List r = new ArrayList (); 105 for (Iterator itr = rootTagMap.keySet().iterator(); itr.hasNext();) { 106 QName qn = (QName ) itr.next(); 107 r.add(qn.getNamespaceURI()); 108 r.add(qn.getLocalPart()); 109 } 110 return (String []) r.toArray(new String [r.size()]); 111 } 112 113 public final boolean recognize( String nsUri, String localName ) { 114 return lookupRootMap(nsUri,localName)!=null; 115 } 116 117 public final Class getDefaultImplementation( Class javaContentInterface ) { 118 try { 119 return Class.forName((String )defaultImplementationMap.get(javaContentInterface), true, classLoader ); 121 } catch (ClassNotFoundException e) { 122 throw new NoClassDefFoundError (e.toString()); 123 } 124 } 125 126 130 public final com.sun.msv.grammar.Grammar getGrammar() throws JAXBException { 131 try { 132 InputStream is = objectFactoryClass.getResourceAsStream("bgm.ser"); 133 134 if( is==null ) { 135 String name = objectFactoryClass.getName(); 137 int idx = name.lastIndexOf('.'); 138 name = '/'+name.substring(0,idx+1).replace('.','/')+"bgm.ser"; 139 throw new JAXBException( 140 Messages.format( Messages.NO_BGM, name ) ); 141 } 142 143 ObjectInputStream ois = new ObjectInputStream ( is ); 145 com.sun.xml.bind.GrammarImpl g = (com.sun.xml.bind.GrammarImpl)ois.readObject(); 146 ois.close(); 147 148 g.connect(new com.sun.msv.grammar.Grammar[]{g}); 150 return g; 151 } catch( Exception e ) { 152 throw new JAXBException( 153 Messages.format( Messages.UNABLE_TO_READ_BGM ), 154 e ); 155 } 156 } 157 158 161 public XMLSerializable castToXMLSerializable(Object o) { 162 if( o instanceof XMLSerializable ) { 163 return (XMLSerializable)o; 164 } else { 165 return null; 166 } 167 } 168 169 172 public ValidatableObject castToValidatableObject(Object o) { 173 if( o instanceof ValidatableObject ) { 174 return (ValidatableObject)o; 175 } else { 176 return null; 177 } 178 } 179 } 180 | Popular Tags |