001: //
002: // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.4-b18-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: 2005.03.04 at 10:20:40 PST
006: //
007:
008: package com.nabhinc.portal.config.impl.runtime;
009:
010: import java.util.ArrayList;
011: import java.util.StringTokenizer;
012:
013: import javax.xml.bind.JAXBContext;
014: import javax.xml.bind.JAXBException;
015:
016: import com.sun.xml.bind.GrammarImpl;
017: import com.sun.xml.bind.Messages;
018: import com.sun.xml.bind.ProxyGroup;
019:
020: /**
021: * This class is a facade to a collection of GrammarInfo objects. It
022: * dispatches rootElement requests to the underlying GrammarInfo objects.
023: *
024: * @version $Revision: 1.1 $
025: */
026: class GrammarInfoFacade implements GrammarInfo {
027:
028: private GrammarInfo[] grammarInfos = null;
029:
030: public GrammarInfoFacade(GrammarInfo[] items) throws JAXBException {
031: grammarInfos = items;
032:
033: detectRootElementCollisions(getProbePoints());
034: }
035:
036: /*
037: * Gets a generated implementation class for the specified root element.
038: * This method is used to determine the first object to be unmarshalled.
039: */
040: public UnmarshallingEventHandler createUnmarshaller(
041: String namespaceUri, String localName,
042: UnmarshallingContext context) {
043: // find the root element among the GrammarInfos
044: for (int i = 0; i < grammarInfos.length; i++) {
045: UnmarshallingEventHandler ueh = grammarInfos[i]
046: .createUnmarshaller(namespaceUri, localName,
047: context);
048: if (ueh != null) {
049: return ueh;
050: }
051: }
052:
053: // the element was not located in any of the grammar infos...
054: return null;
055: }
056:
057: public Class getRootElement(String namespaceUri, String localName) {
058: // find the root element among the GrammarInfos
059: for (int i = 0; i < grammarInfos.length; i++) {
060: Class c = grammarInfos[i].getRootElement(namespaceUri,
061: localName);
062: if (c != null) {
063: return c;
064: }
065: }
066:
067: // the element was not located in any of the grammar infos...
068: return null;
069: }
070:
071: public boolean recognize(String nsUri, String localName) {
072: for (int i = 0; i < grammarInfos.length; i++)
073: if (grammarInfos[i].recognize(nsUri, localName))
074: return true;
075: return false;
076: }
077:
078: /*
079: * Return the probe points for this GrammarInfo, which are used to detect
080: * {namespaceURI,localName} collisions across the GrammarInfo's on the
081: * schemaPath. This is a slightly more complex implementation than a simple
082: * hashmap, but it is more flexible in supporting additional schema langs.
083: */
084: public String[] getProbePoints() {
085: ArrayList probePointList = new ArrayList();
086:
087: for (int i = 0; i < grammarInfos.length; i++) {
088: String[] points = grammarInfos[i].getProbePoints();
089: for (int j = 0; j < points.length; j++) {
090: probePointList.add(points[j]);
091: }
092: }
093:
094: // once per JAXBContext creation, so it may not be worth it.
095: return (String[]) probePointList
096: .toArray(new String[probePointList.size()]);
097: }
098:
099: /**
100: * Iterate through the probe points looking for root element collisions.
101: * If a duplicate is detected, then multiple root element componenets
102: * exist with the same uri:localname
103: */
104: private void detectRootElementCollisions(String[] points)
105: throws JAXBException {
106:
107: // the array of probe points contain uri:localname pairs
108: for (int i = 0; i < points.length; i += 2) {
109: // iterate over GrammarInfos - if more than one GI returns
110: // a class from getRootElement, then there is a collision
111: boolean elementFound = false;
112: for (int j = grammarInfos.length - 1; j >= 0; j--) {
113: if (grammarInfos[j].recognize(points[i], points[i + 1])) {
114: if (elementFound == false) {
115: elementFound = true;
116: } else {
117: throw new JAXBException(Messages.format(
118: Messages.COLLISION_DETECTED, points[i],
119: points[i + 1]));
120: }
121: }
122: }
123: }
124: }
125:
126: /*
127: * This static method is used to setup the GrammarInfoFacade. It
128: * is invoked by the DefaultJAXBContextImpl constructor
129: */
130: static GrammarInfo createGrammarInfoFacade(String contextPath,
131: ClassLoader classLoader) throws JAXBException {
132:
133: String version = null;
134:
135: // array of GrammarInfo objs
136: ArrayList gis = new ArrayList();
137:
138: StringTokenizer st = new StringTokenizer(contextPath, ":;");
139:
140: // instantiate all of the specified JAXBContextImpls
141: while (st.hasMoreTokens()) {
142: String targetPackage = st.nextToken();
143: String objectFactoryName = targetPackage + ".ObjectFactory";
144:
145: try {
146: JAXBContext c = (JAXBContext) Class.forName(
147: objectFactoryName, true, classLoader)
148: .newInstance();
149:
150: // check version
151: if (version == null)
152: version = getVersion(c);
153: else if (!version.equals(getVersion(c)))
154: throw new JAXBException(Messages.format(
155: Messages.INCOMPATIBLE_VERSION,
156: new Object[] { version,
157: c.getClass().getName(),
158: getVersion(c) }));
159:
160: // use reflection to get GrammarInfo
161: Object grammarInfo = c.getClass().getField(
162: "grammarInfo").get(null);
163:
164: // wrap the grammarInfo into a proxy if necessary
165: grammarInfo = ProxyGroup.blindWrap(grammarInfo,
166: GrammarInfo.class, new Class[] {
167: GrammarInfo.class,
168: UnmarshallingContext.class,
169: UnmarshallingEventHandler.class,
170: XMLSerializer.class,
171: XMLSerializable.class,
172: NamespaceContext2.class,
173: ValidatableObject.class });
174:
175: gis.add(grammarInfo);
176: } catch (ClassNotFoundException e) {
177: throw new NoClassDefFoundError(e.getMessage());
178: } catch (Exception e) {
179: throw new JAXBException(e);
180: }
181: }
182:
183: if (gis.size() == 1)
184: // if there's only one path, no need to use a facade.
185: return (GrammarInfo) gis.get(0);
186:
187: return new GrammarInfoFacade((GrammarInfo[]) (gis
188: .toArray(new GrammarInfo[gis.size()])));
189: }
190:
191: /**
192: * Obtains a version number of the JAXB RI that has generated
193: * the specified context, or null if it fails (for example
194: * because it's not generated by JAXB RI.)
195: *
196: * @param c
197: * an instance of a generated ObjectFactory class.
198: * This will return the version number written into
199: * the corresponding JAXBVersion class.
200: */
201: private static String getVersion(JAXBContext c)
202: throws JAXBException {
203: try {
204: Class jaxbBersionClass = (Class) c.getClass().getField(
205: "version").get(null);
206: return (String) jaxbBersionClass.getField("version").get(
207: null);
208: } catch (Throwable t) {
209: return null;
210: }
211: }
212:
213: public Class getDefaultImplementation(Class javaContentInterface) {
214: for (int i = 0; i < grammarInfos.length; i++) {
215: Class c = grammarInfos[i]
216: .getDefaultImplementation(javaContentInterface);
217: if (c != null)
218: return c;
219: }
220: return null;
221: }
222:
223: private com.sun.msv.grammar.Grammar bgm = null;
224:
225: public com.sun.msv.grammar.Grammar getGrammar()
226: throws JAXBException {
227: if (bgm == null) {
228: com.sun.msv.grammar.Grammar[] grammars = new com.sun.msv.grammar.Grammar[grammarInfos.length];
229:
230: // load al the grammars individually
231: for (int i = 0; i < grammarInfos.length; i++)
232: grammars[i] = grammarInfos[i].getGrammar();
233:
234: // connect them to each other
235: for (int i = 0; i < grammarInfos.length; i++)
236: if (grammars[i] instanceof GrammarImpl)
237: ((GrammarImpl) grammars[i]).connect(grammars);
238:
239: // take union of them
240: for (int i = 0; i < grammarInfos.length; i++) {
241: com.sun.msv.grammar.Grammar n = grammars[i];
242: if (bgm == null)
243: bgm = n;
244: else
245: bgm = union(bgm, n);
246: }
247: }
248: return bgm;
249: }
250:
251: /**
252: * Computes the union of two grammars.
253: */
254: private com.sun.msv.grammar.Grammar union(
255: com.sun.msv.grammar.Grammar g1,
256: com.sun.msv.grammar.Grammar g2) {
257: // either g1.getPool() or g2.getPool() is OK.
258: // this is just a metter of performance problem.
259: final com.sun.msv.grammar.ExpressionPool pool = g1.getPool();
260: final com.sun.msv.grammar.Expression top = pool.createChoice(g1
261: .getTopLevel(), g2.getTopLevel());
262:
263: return new com.sun.msv.grammar.Grammar() {
264: public com.sun.msv.grammar.ExpressionPool getPool() {
265: return pool;
266: }
267:
268: public com.sun.msv.grammar.Expression getTopLevel() {
269: return top;
270: }
271: };
272: }
273:
274: /**
275: * @see com.sun.tools.xjc.runtime.GrammarInfo#castToXMLSerializable(java.lang.Object)
276: */
277: public XMLSerializable castToXMLSerializable(Object o) {
278: XMLSerializable result = null;
279: for (int i = 0; i < grammarInfos.length; i++) {
280: result = grammarInfos[i].castToXMLSerializable(o);
281: if (result != null) {
282: return result;
283: }
284: }
285: return null;
286: }
287:
288: /**
289: * @see com.sun.tools.xjc.runtime.GrammarInfo#castToValidatableObject(java.lang.Object)
290: */
291: public ValidatableObject castToValidatableObject(Object o) {
292: ValidatableObject result = null;
293: for (int i = 0; i < grammarInfos.length; i++) {
294: result = grammarInfos[i].castToValidatableObject(o);
295: if (result != null) {
296: return result;
297: }
298: }
299: return null;
300: }
301: }
|