001// 002// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.6-01/24/2006 06:15 PM(kohsuke)-fcs 003// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 004// Any modifications to this file will be lost upon recompilation of the source schema. 005// Generated on: 2012.10.03 at 04:27:47 AM CEST 006// 007 008package org.jdtaus.mojo.resource.model.impl.runtime; 009 010import java.io.InputStream; 011import java.io.ObjectInputStream; 012import java.util.ArrayList; 013import java.util.Iterator; 014import java.util.List; 015import java.util.Map; 016 017import javax.xml.bind.JAXBException; 018import javax.xml.namespace.QName; 019 020import com.sun.xml.bind.Messages; 021 022/** 023 * Keeps the information about the grammar as a whole. 024 * 025 * This object is immutable and thread-safe. 026 * 027 * @author 028 * <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a> 029 */ 030public class GrammarInfoImpl implements GrammarInfo 031{ 032 /** 033 * Map from {@link QName}s (root tag names) to {@link Class}es of the 034 * content interface that should be instanciated. 035 */ 036 private final Map rootTagMap; 037 038 /** 039 * Enclosing ObjectFactory class. Used to load resources. 040 */ 041 private final Class objectFactoryClass; 042 043 /** 044 * Map from {@link Class}es that represent content interfaces 045 * to {@link String}s that represent names of the corresponding 046 * implementation classes. 047 */ 048 private final Map defaultImplementationMap; 049 050 /** 051 * ClassLoader that should be used to load impl classes. 052 */ 053 private final ClassLoader classLoader; 054 055 public GrammarInfoImpl( Map _rootTagMap, Map _defaultImplementationMap, Class _objectFactoryClass ) { 056 this.rootTagMap = _rootTagMap; 057 this.defaultImplementationMap = _defaultImplementationMap; 058 this.objectFactoryClass = _objectFactoryClass; 059 // the assumption is that the content interfaces and their impls 060 // are loaded from the same class loader. 061 this.classLoader = objectFactoryClass.getClassLoader(); 062 } 063 064 /** 065 * @return the name of the content interface that is registered with 066 * the specified element name. 067 */ 068 private final Class lookupRootMap( String nsUri, String localName ) { 069 // note that the value of rootTagMap could be null. 070 QName qn; 071 072 qn = new QName(nsUri,localName); 073 if(rootTagMap.containsKey(qn)) return (Class)rootTagMap.get(qn); 074 075 qn = new QName(nsUri,"*"); 076 if(rootTagMap.containsKey(qn)) return (Class)rootTagMap.get(qn); 077 078 qn = new QName("*","*"); 079 return (Class)rootTagMap.get(qn); 080 } 081 082 public final Class getRootElement(String namespaceUri, String localName) { 083 Class intfCls = lookupRootMap(namespaceUri,localName); 084 if(intfCls==null) return null; 085 else return getDefaultImplementation(intfCls); 086 } 087 088 public final UnmarshallingEventHandler createUnmarshaller( 089 String namespaceUri, String localName, UnmarshallingContext context ) { 090 091 Class impl = getRootElement(namespaceUri,localName); 092 if(impl==null) return null; 093 094 try { 095 return ((UnmarshallableObject)impl.newInstance()).createUnmarshaller(context); 096 } catch (InstantiationException e) { 097 throw new InstantiationError(e.toString()); 098 } catch (IllegalAccessException e) { 099 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 // by caching the obtained Class objects. 120 return Class.forName((String)defaultImplementationMap.get(javaContentInterface), true, classLoader ); 121 } catch (ClassNotFoundException e) { 122 throw new NoClassDefFoundError(e.toString()); 123 } 124 } 125 126 /** 127 * Gets the MSV AGM which can be used to validate XML during 128 * marshalling/unmarshalling. 129 */ 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 // unable to find bgm.ser 136 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 // deserialize the bgm 144 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}); // connect to itself 149 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 /** 159 * @see com.sun.tools.xjc.runtime.GrammarInfo#castToXMLSerializable(java.lang.Object) 160 */ 161 public XMLSerializable castToXMLSerializable(Object o) { 162 if( o instanceof XMLSerializable ) { 163 return (XMLSerializable)o; 164 } else { 165 return null; 166 } 167 } 168 169 /** 170 * @see com.sun.tools.xjc.runtime.GrammarInfo#castToValidatableObject(java.lang.Object) 171 */ 172 public ValidatableObject castToValidatableObject(Object o) { 173 if( o instanceof ValidatableObject ) { 174 return (ValidatableObject)o; 175 } else { 176 return null; 177 } 178 } 179}