1 package com.sun.tools.xjc.runtime; 2 3 import java.util.ArrayList ; 4 import java.util.StringTokenizer ; 5 6 import javax.xml.bind.JAXBContext; 7 import javax.xml.bind.JAXBException; 8 9 import com.sun.xml.bind.GrammarImpl; 10 import com.sun.xml.bind.Messages; 11 import com.sun.xml.bind.ProxyGroup; 12 13 19 class GrammarInfoFacade implements GrammarInfo { 20 21 private GrammarInfo[] grammarInfos = null; 22 23 24 25 public GrammarInfoFacade( GrammarInfo[] items ) throws JAXBException { 26 grammarInfos = items; 27 28 detectRootElementCollisions( getProbePoints() ); 30 } 32 33 38 public UnmarshallingEventHandler createUnmarshaller(String namespaceUri, String localName, UnmarshallingContext context) { 39 for( int i = 0; i < grammarInfos.length; i++ ) { 41 UnmarshallingEventHandler ueh = grammarInfos[i].createUnmarshaller( namespaceUri, localName, context ); 42 if( ueh != null ) { 43 return ueh; 44 } 45 } 46 47 return null; 49 } 50 51 public Class getRootElement(String namespaceUri, String localName) { 52 for( int i = 0; i < grammarInfos.length; i++ ) { 54 Class c = grammarInfos[i].getRootElement( namespaceUri, localName ); 55 if( c != null ) { 56 return c; 57 } 58 } 59 60 return null; 62 } 63 64 public boolean recognize( String nsUri, String localName ) { 65 for( int i = 0; i < grammarInfos.length; i++ ) 66 if( grammarInfos[i].recognize(nsUri, localName) ) 67 return true; 68 return false; 69 } 70 71 77 public String [] getProbePoints() { 78 ArrayList probePointList = new ArrayList (); 79 80 for( int i = 0; i < grammarInfos.length; i++ ) { 81 String [] points = grammarInfos[i].getProbePoints(); 82 for( int j = 0; j < points.length; j++ ) { 83 probePointList.add( points[j] ); 84 } 85 } 86 87 return (String [])probePointList.toArray( new String [ probePointList.size() ] ); 89 } 90 91 92 97 private void detectRootElementCollisions( String [] points ) 98 throws JAXBException { 99 100 for( int i = 0; i < points.length; i += 2 ) { 102 boolean elementFound = false; 105 for( int j = grammarInfos.length-1; j >= 0; j -- ) { 106 if( grammarInfos[j].recognize( points[i], points[i+1] ) ) { 107 if( elementFound == false ) { 108 elementFound = true; 109 } else { 110 throw new JAXBException( 111 Messages.format( Messages.COLLISION_DETECTED, 112 points[i], points[i+1] ) ); 113 } 114 } 115 } 116 } 117 } 118 120 124 static GrammarInfo createGrammarInfoFacade( String contextPath, 125 ClassLoader classLoader ) 126 throws JAXBException { 127 128 String version=null; 129 130 ArrayList gis = new ArrayList (); 132 133 StringTokenizer st = new StringTokenizer ( contextPath, ":;" ); 134 135 while( st.hasMoreTokens() ) { 137 String targetPackage = st.nextToken(); 138 String objectFactoryName = targetPackage + ".ObjectFactory"; 139 140 try { 141 JAXBContext c = (JAXBContext)Class.forName( 142 objectFactoryName, true, classLoader ).newInstance(); 143 144 if( version==null ) version = getVersion(c); 146 else 147 if( !version.equals(getVersion(c)) ) 148 throw new JAXBException( Messages.format( 149 Messages.INCOMPATIBLE_VERSION, new Object []{ 150 version, 151 c.getClass().getName(), 152 getVersion(c) } ) ); 153 154 Object grammarInfo = c.getClass().getField("grammarInfo").get(null); 156 157 grammarInfo = ProxyGroup.blindWrap( 159 grammarInfo, GrammarInfo.class, 160 new Class []{ 161 GrammarInfo.class, 162 UnmarshallingContext.class, 164 UnmarshallingEventHandler.class, 165 XMLSerializer.class, 168 XMLSerializable.class, 169 NamespaceContext2.class, 170 ValidatableObject.class 173 } ); 175 176 gis.add( grammarInfo ); 177 } catch( ClassNotFoundException e ) { 178 throw new NoClassDefFoundError (e.getMessage()); 179 } catch( Exception e ) { 180 throw new JAXBException(e); 181 } 182 } 183 184 if( gis.size()==1 ) 185 return (GrammarInfo)gis.get(0); 187 188 return new GrammarInfoFacade( 189 (GrammarInfo[])(gis.toArray( new GrammarInfo[ gis.size() ] ) ) ); 190 } 191 192 202 private static String getVersion(JAXBContext c) throws JAXBException { 203 try { 204 Class jaxbBersionClass = (Class )c.getClass().getField("version").get(null); 205 return (String )jaxbBersionClass.getField("version").get(null); 206 } catch( Throwable t ) { 207 return null; 208 } 209 } 210 211 public Class getDefaultImplementation( Class javaContentInterface ) { 212 for( int i=0; i<grammarInfos.length; i++ ) { 213 Class c = grammarInfos[i].getDefaultImplementation( javaContentInterface ); 214 if(c!=null) return c; 215 } 216 return null; 217 } 218 219 private com.sun.msv.grammar.Grammar bgm = null; 221 222 public com.sun.msv.grammar.Grammar getGrammar() throws JAXBException { 223 if(bgm==null) { 224 com.sun.msv.grammar.Grammar[] grammars = new com.sun.msv.grammar.Grammar[grammarInfos.length]; 225 226 for( int i=0; i<grammarInfos.length; i++ ) 228 grammars[i] = grammarInfos[i].getGrammar(); 229 230 for( int i=0; i<grammarInfos.length; i++ ) 232 if( grammars[i] instanceof GrammarImpl ) 233 ((GrammarImpl)grammars[i]).connect(grammars); 234 235 for( int i=0; i<grammarInfos.length; i++ ) { 237 com.sun.msv.grammar.Grammar n = grammars[i]; 238 if( bgm == null ) bgm = n; 239 else bgm = union( bgm, n ); 240 } 241 } 242 return bgm; 243 } 244 245 246 249 private com.sun.msv.grammar.Grammar union( com.sun.msv.grammar.Grammar g1, com.sun.msv.grammar.Grammar g2 ) { 250 final com.sun.msv.grammar.ExpressionPool pool = g1.getPool(); 253 final com.sun.msv.grammar.Expression top = pool.createChoice(g1.getTopLevel(),g2.getTopLevel()); 254 255 return new com.sun.msv.grammar.Grammar() { 256 public com.sun.msv.grammar.ExpressionPool getPool() { 257 return pool; 258 } 259 public com.sun.msv.grammar.Expression getTopLevel() { 260 return top; 261 } 262 }; 263 } 264 266 267 271 public XMLSerializable castToXMLSerializable(Object o) { 272 XMLSerializable result = null; 273 for( int i = 0; i < grammarInfos.length; i++ ) { 274 result = grammarInfos[i].castToXMLSerializable( o ); 275 if( result != null ) { 276 return result; 277 } 278 } 279 return null; 280 } 281 283 287 public ValidatableObject castToValidatableObject(Object o) { 288 ValidatableObject result = null; 289 for( int i = 0; i < grammarInfos.length; i++ ) { 290 result = grammarInfos[i].castToValidatableObject( o ); 291 if( result != null ) { 292 return result; 293 } 294 } 295 return null; 296 } 297 } 299 | Popular Tags |