Source Code Cross Referenced for TypeInfoSetImpl.java in  » 6.0-JDK-Modules » jaxb-impl » com » sun » xml » bind » v2 » model » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » jaxb impl » com.sun.xml.bind.v2.model.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
w___ww.j__a___v__a_2s_.__c_o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.