001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.model.impl;
038:
039: import java.util.Collections;
040: import java.util.HashMap;
041: import java.util.Iterator;
042: import java.util.LinkedHashMap;
043: import java.util.Map;
044:
045: import javax.xml.bind.JAXBContext;
046: import javax.xml.bind.JAXBException;
047: import javax.xml.bind.Marshaller;
048: import javax.xml.bind.annotation.XmlNs;
049: import javax.xml.bind.annotation.XmlNsForm;
050: import javax.xml.bind.annotation.XmlRegistry;
051: import javax.xml.bind.annotation.XmlSchema;
052: import javax.xml.bind.annotation.XmlTransient;
053: import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
054: import javax.xml.namespace.QName;
055: import javax.xml.transform.Result;
056:
057: import com.sun.xml.bind.v2.model.annotation.AnnotationReader;
058: import com.sun.xml.bind.v2.model.core.BuiltinLeafInfo;
059: import com.sun.xml.bind.v2.model.core.ClassInfo;
060: import com.sun.xml.bind.v2.model.core.LeafInfo;
061: import com.sun.xml.bind.v2.model.core.NonElement;
062: import com.sun.xml.bind.v2.model.core.Ref;
063: import com.sun.xml.bind.v2.model.core.TypeInfo;
064: import com.sun.xml.bind.v2.model.core.TypeInfoSet;
065: import com.sun.xml.bind.v2.model.nav.Navigator;
066: import com.sun.xml.bind.v2.runtime.IllegalAnnotationException;
067: import com.sun.xml.bind.v2.runtime.RuntimeUtil;
068: import com.sun.xml.bind.v2.util.FlattenIterator;
069:
070: /**
071: * Set of {@link TypeInfo}s.
072: *
073: * <p>
074: * This contains a fixed set of {@link LeafInfo}s and arbitrary set of {@link ClassInfo}s.
075: *
076: * <p>
077: * Members are annotated with JAXB annotations so that we can dump it easily.
078: *
079: * @author Kohsuke Kawaguchi
080: */
081: class TypeInfoSetImpl<T, C, F, M> implements TypeInfoSet<T, C, F, M> {
082:
083: @XmlTransient
084: public final Navigator<T, C, F, M> nav;
085:
086: @XmlTransient
087: public final AnnotationReader<T, C, F, M> reader;
088:
089: /**
090: * All the leaves.
091: */
092: private final Map<T, BuiltinLeafInfo<T, C>> builtins = new LinkedHashMap<T, BuiltinLeafInfo<T, C>>();
093:
094: /** All {@link EnumLeafInfoImpl}s. */
095: private final Map<C, EnumLeafInfoImpl<T, C, F, M>> enums = new LinkedHashMap<C, EnumLeafInfoImpl<T, C, F, M>>();
096:
097: /** All {@link ArrayInfoImpl}s. */
098: private final Map<T, ArrayInfoImpl<T, C, F, M>> arrays = new LinkedHashMap<T, ArrayInfoImpl<T, C, F, M>>();
099:
100: /**
101: * All the user-defined classes.
102: *
103: * Using {@link LinkedHashMap} allows us to process classes
104: * in the order they are given to us. When the user incorrectly
105: * puts an unexpected class into a reference graph, this causes
106: * an error to be reported on a class closer to the user's code.
107: */
108: @XmlJavaTypeAdapter(RuntimeUtil.ToStringAdapter.class)
109: private final Map<C, ClassInfoImpl<T, C, F, M>> beans = new LinkedHashMap<C, ClassInfoImpl<T, C, F, M>>();
110:
111: @XmlTransient
112: private final Map<C, ClassInfoImpl<T, C, F, M>> beansView = Collections
113: .unmodifiableMap(beans);
114:
115: /**
116: * The element mapping.
117: */
118: private final Map<C, Map<QName, ElementInfoImpl<T, C, F, M>>> elementMappings = new LinkedHashMap<C, Map<QName, ElementInfoImpl<T, C, F, M>>>();
119:
120: private final Iterable<? extends ElementInfoImpl<T, C, F, M>> allElements = new Iterable<ElementInfoImpl<T, C, F, M>>() {
121: public Iterator<ElementInfoImpl<T, C, F, M>> iterator() {
122: return new FlattenIterator<ElementInfoImpl<T, C, F, M>>(
123: elementMappings.values());
124: }
125: };
126:
127: /**
128: * {@link TypeInfo} for <tt>xs:anyType</tt>.
129: *
130: * anyType is the only {@link TypeInfo} that works with an interface,
131: * and accordingly it requires a lot of special casing.
132: */
133: private final NonElement<T, C> anyType;
134:
135: /**
136: * Lazily parsed set of {@link XmlNs}s.
137: *
138: * @see #getXmlNs(String)
139: */
140: private Map<String, Map<String, String>> xmlNsCache;
141:
142: public TypeInfoSetImpl(Navigator<T, C, F, M> nav,
143: AnnotationReader<T, C, F, M> reader,
144: Map<T, ? extends BuiltinLeafInfoImpl<T, C>> leaves) {
145: this .nav = nav;
146: this .reader = reader;
147: this .builtins.putAll(leaves);
148:
149: this .anyType = createAnyType();
150:
151: // register primitive types.
152: for (Map.Entry<Class, Class> e : RuntimeUtil.primitiveToBox
153: .entrySet()) {
154: this .builtins.put(nav.getPrimitive(e.getKey()), leaves
155: .get(nav.ref(e.getValue())));
156: }
157:
158: // make sure at lease we got a map for global ones.
159: elementMappings
160: .put(
161: null,
162: new LinkedHashMap<QName, ElementInfoImpl<T, C, F, M>>());
163: }
164:
165: protected NonElement<T, C> createAnyType() {
166: return new AnyTypeImpl<T, C>(nav);
167: }
168:
169: public Navigator<T, C, F, M> getNavigator() {
170: return nav;
171: }
172:
173: /**
174: * Adds a new {@link ClassInfo} to the set.
175: */
176: public void add(ClassInfoImpl<T, C, F, M> ci) {
177: beans.put(ci.getClazz(), ci);
178: }
179:
180: /**
181: * Adds a new {@link LeafInfo} to the set.
182: */
183: public void add(EnumLeafInfoImpl<T, C, F, M> li) {
184: enums.put(li.clazz, li);
185: }
186:
187: public void add(ArrayInfoImpl<T, C, F, M> ai) {
188: arrays.put(ai.getType(), ai);
189: }
190:
191: /**
192: * Returns a {@link TypeInfo} for the given type.
193: *
194: * @return
195: * null if the specified type cannot be bound by JAXB, or
196: * not known to this set.
197: */
198: public NonElement<T, C> getTypeInfo(T type) {
199: type = nav.erasure(type); // replace type variables by their bounds
200:
201: LeafInfo<T, C> l = builtins.get(type);
202: if (l != null)
203: return l;
204:
205: if (nav.isArray(type)) {
206: return arrays.get(type);
207: }
208:
209: C d = nav.asDecl(type);
210: if (d == null)
211: return null;
212: return getClassInfo(d);
213: }
214:
215: public NonElement<T, C> getAnyTypeInfo() {
216: return anyType;
217: }
218:
219: /**
220: * This method is used to add a root reference to a model.
221: */
222: public NonElement<T, C> getTypeInfo(Ref<T, C> ref) {
223: // TODO: handle XmlValueList
224: assert !ref.valueList;
225: C c = nav.asDecl(ref.type);
226: if (c != null
227: && reader
228: .getClassAnnotation(XmlRegistry.class, c, null/*TODO: is this right?*/) != null) {
229: return null; // TODO: is this correct?
230: } else
231: return getTypeInfo(ref.type);
232: }
233:
234: /**
235: * Returns all the {@link ClassInfo}s known to this set.
236: */
237: public Map<C, ? extends ClassInfoImpl<T, C, F, M>> beans() {
238: return beansView;
239: }
240:
241: public Map<T, ? extends BuiltinLeafInfo<T, C>> builtins() {
242: return builtins;
243: }
244:
245: public Map<C, ? extends EnumLeafInfoImpl<T, C, F, M>> enums() {
246: return enums;
247: }
248:
249: public Map<? extends T, ? extends ArrayInfoImpl<T, C, F, M>> arrays() {
250: return arrays;
251: }
252:
253: /**
254: * Returns a {@link ClassInfo} for the given bean.
255: *
256: * <p>
257: * This method is almost like refinement of {@link #getTypeInfo(Object)} except
258: * our C cannot derive from T.
259: *
260: * @return
261: * null if the specified type is not bound by JAXB or otherwise
262: * unknown to this set.
263: */
264: public NonElement<T, C> getClassInfo(C type) {
265: LeafInfo<T, C> l = builtins.get(nav.use(type));
266: if (l != null)
267: return l;
268:
269: l = enums.get(type);
270: if (l != null)
271: return l;
272:
273: if (nav.asDecl(Object.class).equals(type))
274: return anyType;
275:
276: return beans.get(type);
277: }
278:
279: public ElementInfoImpl<T, C, F, M> getElementInfo(C scope,
280: QName name) {
281: while (scope != null) {
282: Map<QName, ElementInfoImpl<T, C, F, M>> m = elementMappings
283: .get(scope);
284: if (m != null) {
285: ElementInfoImpl<T, C, F, M> r = m.get(name);
286: if (r != null)
287: return r;
288: }
289: scope = nav.getSuperClass(scope);
290: }
291: return elementMappings.get(null).get(name);
292: }
293:
294: /**
295: * @param builder
296: * used for reporting errors.
297: */
298: public final void add(ElementInfoImpl<T, C, F, M> ei,
299: ModelBuilder<T, C, F, M> builder) {
300: C scope = null;
301: if (ei.getScope() != null)
302: scope = ei.getScope().getClazz();
303:
304: Map<QName, ElementInfoImpl<T, C, F, M>> m = elementMappings
305: .get(scope);
306: if (m == null)
307: elementMappings
308: .put(
309: scope,
310: m = new LinkedHashMap<QName, ElementInfoImpl<T, C, F, M>>());
311:
312: ElementInfoImpl<T, C, F, M> existing = m.put(ei
313: .getElementName(), ei);
314:
315: if (existing != null) {
316: QName en = ei.getElementName();
317: builder.reportError(new IllegalAnnotationException(
318: Messages.CONFLICTING_XML_ELEMENT_MAPPING.format(en
319: .getNamespaceURI(), en.getLocalPart()), ei,
320: existing));
321: }
322: }
323:
324: public Map<QName, ? extends ElementInfoImpl<T, C, F, M>> getElementMappings(
325: C scope) {
326: return elementMappings.get(scope);
327: }
328:
329: public Iterable<? extends ElementInfoImpl<T, C, F, M>> getAllElements() {
330: return allElements;
331: }
332:
333: public Map<String, String> getXmlNs(String namespaceUri) {
334: if (xmlNsCache == null) {
335: xmlNsCache = new HashMap<String, Map<String, String>>();
336:
337: for (ClassInfoImpl<T, C, F, M> ci : beans().values()) {
338: XmlSchema xs = reader.getPackageAnnotation(
339: XmlSchema.class, ci.getClazz(), null);
340: if (xs == null)
341: continue;
342:
343: String uri = xs.namespace();
344: Map<String, String> m = xmlNsCache.get(uri);
345: if (m == null)
346: xmlNsCache.put(uri,
347: m = new HashMap<String, String>());
348:
349: for (XmlNs xns : xs.xmlns()) {
350: m.put(xns.prefix(), xns.namespaceURI());
351: }
352: }
353: }
354:
355: Map<String, String> r = xmlNsCache.get(namespaceUri);
356: if (r != null)
357: return r;
358: else
359: return Collections.emptyMap();
360: }
361:
362: public Map<String, String> getSchemaLocations() {
363: Map<String, String> r = new HashMap<String, String>();
364: for (ClassInfoImpl<T, C, F, M> ci : beans().values()) {
365: XmlSchema xs = reader.getPackageAnnotation(XmlSchema.class,
366: ci.getClazz(), null);
367: if (xs == null)
368: continue;
369:
370: String loc = xs.location();
371: if (loc.equals(XmlSchema.NO_LOCATION))
372: continue; // unspecified
373:
374: r.put(xs.namespace(), loc);
375: }
376: return r;
377: }
378:
379: public final XmlNsForm getElementFormDefault(String nsUri) {
380: for (ClassInfoImpl<T, C, F, M> ci : beans().values()) {
381: XmlSchema xs = reader.getPackageAnnotation(XmlSchema.class,
382: ci.getClazz(), null);
383: if (xs == null)
384: continue;
385:
386: if (!xs.namespace().equals(nsUri))
387: continue;
388:
389: XmlNsForm xnf = xs.elementFormDefault();
390: if (xnf != XmlNsForm.UNSET)
391: return xnf;
392: }
393: return XmlNsForm.UNSET;
394: }
395:
396: public final XmlNsForm getAttributeFormDefault(String nsUri) {
397: for (ClassInfoImpl<T, C, F, M> ci : beans().values()) {
398: XmlSchema xs = reader.getPackageAnnotation(XmlSchema.class,
399: ci.getClazz(), null);
400: if (xs == null)
401: continue;
402:
403: if (!xs.namespace().equals(nsUri))
404: continue;
405:
406: XmlNsForm xnf = xs.attributeFormDefault();
407: if (xnf != XmlNsForm.UNSET)
408: return xnf;
409: }
410: return XmlNsForm.UNSET;
411: }
412:
413: /**
414: * Dumps this model into XML.
415: *
416: * For debug only.
417: *
418: * TODO: not sure if this actually works. We don't really know what are T,C.
419: */
420: public void dump(Result out) throws JAXBException {
421: JAXBContext context = JAXBContext.newInstance(this .getClass());
422: Marshaller m = context.createMarshaller();
423: m.marshal(this, out);
424: }
425: }
|