1
2
3
4
5
6
7
8 package de.nierbeck.timeTrack.model.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 /***
23 * Keeps the information about the grammar as a whole.
24 *
25 * This object is immutable and thread-safe.
26 *
27 * @author <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
28 */
29 public class GrammarInfoImpl implements GrammarInfo {
30 /***
31 * Map from {@link QName}s (root tag names) to {@link Class}es of the
32 * content interface that should be instanciated.
33 */
34 private final Map rootTagMap;
35
36 /***
37 * Enclosing ObjectFactory class. Used to load resources.
38 */
39 private final Class objectFactoryClass;
40
41 /***
42 * Map from {@link Class}es that represent content interfaces to
43 * {@link String}s that represent names of the corresponding implementation
44 * classes.
45 */
46 private final Map defaultImplementationMap;
47
48 /***
49 * ClassLoader that should be used to load impl classes.
50 */
51 private final ClassLoader classLoader;
52
53 public GrammarInfoImpl(Map _rootTagMap, Map _defaultImplementationMap,
54 Class _objectFactoryClass) {
55 this.rootTagMap = _rootTagMap;
56 this.defaultImplementationMap = _defaultImplementationMap;
57 this.objectFactoryClass = _objectFactoryClass;
58
59
60 this.classLoader = objectFactoryClass.getClassLoader();
61 }
62
63 /***
64 * @return the name of the content interface that is registered with the
65 * specified element name.
66 */
67 private final Class lookupRootMap(String nsUri, String localName) {
68
69 QName qn;
70
71 qn = new QName(nsUri, localName);
72 if (rootTagMap.containsKey(qn))
73 return (Class) rootTagMap.get(qn);
74
75 qn = new QName(nsUri, "*");
76 if (rootTagMap.containsKey(qn))
77 return (Class) rootTagMap.get(qn);
78
79 qn = new QName("*", "*");
80 return (Class) rootTagMap.get(qn);
81 }
82
83 public final Class getRootElement(String namespaceUri, String localName) {
84 Class intfCls = lookupRootMap(namespaceUri, localName);
85 if (intfCls == null)
86 return null;
87 else
88 return getDefaultImplementation(intfCls);
89 }
90
91 public final UnmarshallingEventHandler createUnmarshaller(
92 String namespaceUri, String localName, UnmarshallingContext context) {
93
94 Class impl = getRootElement(namespaceUri, localName);
95 if (impl == null)
96 return null;
97
98 try {
99 return ((UnmarshallableObject) impl.newInstance())
100 .createUnmarshaller(context);
101 } catch (InstantiationException e) {
102 throw new InstantiationError(e.toString());
103 } catch (IllegalAccessException e) {
104 throw new IllegalAccessError(e.toString());
105 }
106 }
107
108 public final String[] getProbePoints() {
109 List r = new ArrayList();
110 for (Iterator itr = rootTagMap.keySet().iterator(); itr.hasNext();) {
111 QName qn = (QName) itr.next();
112 r.add(qn.getNamespaceURI());
113 r.add(qn.getLocalPart());
114 }
115 return (String[]) r.toArray(new String[r.size()]);
116 }
117
118 public final boolean recognize(String nsUri, String localName) {
119 return lookupRootMap(nsUri, localName) != null;
120 }
121
122 public final Class getDefaultImplementation(Class javaContentInterface) {
123 try {
124
125 return Class.forName((String) defaultImplementationMap
126 .get(javaContentInterface), true, classLoader);
127 } catch (ClassNotFoundException e) {
128 throw new NoClassDefFoundError(e.toString());
129 }
130 }
131
132 /***
133 * Gets the MSV AGM which can be used to validate XML during
134 * marshalling/unmarshalling.
135 */
136 public final com.sun.msv.grammar.Grammar getGrammar() throws JAXBException {
137 try {
138 InputStream is = objectFactoryClass.getResourceAsStream("bgm.ser");
139
140 if (is == null) {
141
142 String name = objectFactoryClass.getName();
143 int idx = name.lastIndexOf('.');
144 name = '/' + name.substring(0, idx + 1).replace('.', '/')
145 + "bgm.ser";
146 throw new JAXBException(Messages.format(Messages.NO_BGM, name));
147 }
148
149
150 ObjectInputStream ois = new ObjectInputStream(is);
151 com.sun.xml.bind.GrammarImpl g = (com.sun.xml.bind.GrammarImpl) ois
152 .readObject();
153 ois.close();
154
155 g.connect(new com.sun.msv.grammar.Grammar[] { g });
156
157
158 return g;
159 } catch (Exception e) {
160 throw new JAXBException(Messages
161 .format(Messages.UNABLE_TO_READ_BGM), e);
162 }
163 }
164
165 /***
166 * @see com.sun.tools.xjc.runtime.GrammarInfo#castToXMLSerializable(java.lang.Object)
167 */
168 public XMLSerializable castToXMLSerializable(Object o) {
169 if (o instanceof XMLSerializable) {
170 return (XMLSerializable) o;
171 } else {
172 return null;
173 }
174 }
175
176 /***
177 * @see com.sun.tools.xjc.runtime.GrammarInfo#castToValidatableObject(java.lang.Object)
178 */
179 public ValidatableObject castToValidatableObject(Object o) {
180 if (o instanceof ValidatableObject) {
181 return (ValidatableObject) o;
182 } else {
183 return null;
184 }
185 }
186 }