Source Code Cross Referenced for BIConversion.java in  » 6.0-JDK-Modules » jaxb-xjc » com » sun » tools » xjc » reader » xmlschema » bindinfo » 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 xjc » com.sun.tools.xjc.reader.xmlschema.bindinfo 
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:        package com.sun.tools.xjc.reader.xmlschema.bindinfo;
037:
038:        import javax.xml.bind.DatatypeConverter;
039:        import javax.xml.bind.annotation.XmlAttribute;
040:        import javax.xml.bind.annotation.XmlRootElement;
041:        import javax.xml.bind.annotation.adapters.XmlAdapter;
042:        import javax.xml.namespace.QName;
043:
044:        import com.sun.codemodel.JClass;
045:        import com.sun.codemodel.JClassAlreadyExistsException;
046:        import com.sun.codemodel.JCodeModel;
047:        import com.sun.codemodel.JDefinedClass;
048:        import com.sun.codemodel.JExpr;
049:        import com.sun.codemodel.JExpression;
050:        import com.sun.codemodel.JMethod;
051:        import com.sun.codemodel.JMod;
052:        import com.sun.codemodel.JPackage;
053:        import com.sun.codemodel.JType;
054:        import com.sun.codemodel.JVar;
055:        import com.sun.codemodel.JConditional;
056:        import com.sun.tools.xjc.ErrorReceiver;
057:        import com.sun.tools.xjc.model.CAdapter;
058:        import com.sun.tools.xjc.model.CBuiltinLeafInfo;
059:        import com.sun.tools.xjc.model.TypeUse;
060:        import com.sun.tools.xjc.model.TypeUseFactory;
061:        import com.sun.tools.xjc.reader.Const;
062:        import com.sun.tools.xjc.reader.Ring;
063:        import com.sun.tools.xjc.reader.TypeUtil;
064:        import com.sun.tools.xjc.reader.xmlschema.ClassSelector;
065:        import com.sun.xml.bind.v2.WellKnownNamespace;
066:        import com.sun.xml.xsom.XSSimpleType;
067:
068:        import org.xml.sax.Locator;
069:
070:        /**
071:         * Conversion declaration.
072:         * 
073:         * <p>
074:         * A conversion declaration specifies how an XML type gets mapped
075:         * to a Java type.
076:         * 
077:         * @author
078:         *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
079:         */
080:        public abstract class BIConversion extends AbstractDeclarationImpl {
081:            @Deprecated
082:            public BIConversion(Locator loc) {
083:                super (loc);
084:            }
085:
086:            protected BIConversion() {
087:            }
088:
089:            /**
090:             * Gets the {@link TypeUse} object that this conversion represents.
091:             * <p>
092:             * The returned {@link TypeUse} object is properly adapted.
093:             *
094:             * @param owner
095:             *      A {@link BIConversion} is always associated with one
096:             *      {@link XSSimpleType}, but that's not always available
097:             *      when a {@link BIConversion} is built. So we pass this
098:             *      as a parameter to this method.
099:             */
100:            public abstract TypeUse getTypeUse(XSSimpleType owner);
101:
102:            public QName getName() {
103:                return NAME;
104:            }
105:
106:            /** Name of the conversion declaration. */
107:            public static final QName NAME = new QName(Const.JAXB_NSURI,
108:                    "conversion");
109:
110:            /**
111:             * Implementation that returns a statically-determined constant {@link TypeUse}.
112:             */
113:            public static final class Static extends BIConversion {
114:                /**
115:                 * Always non-null.
116:                 */
117:                private final TypeUse transducer;
118:
119:                public Static(Locator loc, TypeUse transducer) {
120:                    super (loc);
121:                    this .transducer = transducer;
122:                }
123:
124:                public TypeUse getTypeUse(XSSimpleType owner) {
125:                    return transducer;
126:                }
127:            }
128:
129:            /**
130:             * User-specified &lt;javaType> customization.
131:             *
132:             * The parse/print methods are allowed to be null,
133:             * and their default values are determined based on the
134:             * owner of the token.
135:             */
136:            @XmlRootElement(name="javaType")
137:            public static class User extends BIConversion {
138:                @XmlAttribute
139:                private String parseMethod;
140:                @XmlAttribute
141:                private String printMethod;
142:                @XmlAttribute(name="name")
143:                private String type = "java.lang.String";
144:
145:                /**
146:                 * If null, computed from {@link #type}.
147:                 * Sometimes this can be set instead of {@link #type}.
148:                 */
149:                private JType inMemoryType;
150:
151:                public User(Locator loc, String parseMethod,
152:                        String printMethod, JType inMemoryType) {
153:                    super (loc);
154:                    this .parseMethod = parseMethod;
155:                    this .printMethod = printMethod;
156:                    this .inMemoryType = inMemoryType;
157:                }
158:
159:                public User() {
160:                }
161:
162:                /**
163:                 * Cache used by {@link #getTypeUse(XSSimpleType)} to improve the performance.
164:                 */
165:                private TypeUse typeUse;
166:
167:                public TypeUse getTypeUse(XSSimpleType owner) {
168:                    if (typeUse != null)
169:                        return typeUse;
170:
171:                    JCodeModel cm = getCodeModel();
172:
173:                    if (inMemoryType == null)
174:                        inMemoryType = TypeUtil.getType(cm, type, Ring
175:                                .get(ErrorReceiver.class), getLocation());
176:
177:                    JDefinedClass adapter = generateAdapter(
178:                            parseMethodFor(owner), printMethodFor(owner), owner);
179:
180:                    // XmlJavaType customization always converts between string and an user-defined type.
181:                    typeUse = TypeUseFactory.adapt(CBuiltinLeafInfo.STRING,
182:                            new CAdapter(adapter));
183:
184:                    return typeUse;
185:                }
186:
187:                /**
188:                 * generate the adapter class.
189:                 */
190:                private JDefinedClass generateAdapter(String parseMethod,
191:                        String printMethod, XSSimpleType owner) {
192:                    JDefinedClass adapter = null;
193:
194:                    int id = 1;
195:                    while (adapter == null) {
196:                        try {
197:                            JPackage pkg = Ring.get(ClassSelector.class)
198:                                    .getClassScope().getOwnerPackage();
199:                            adapter = pkg._class("Adapter" + id);
200:                        } catch (JClassAlreadyExistsException e) {
201:                            // try another name in search for an unique name.
202:                            // this isn't too efficient, but we expect people to usually use
203:                            // a very small number of adapters.
204:                            id++;
205:                        }
206:                    }
207:
208:                    JClass bim = inMemoryType.boxify();
209:
210:                    adapter._extends(getCodeModel().ref(XmlAdapter.class)
211:                            .narrow(String.class).narrow(bim));
212:
213:                    JMethod unmarshal = adapter.method(JMod.PUBLIC, bim,
214:                            "unmarshal");
215:                    JVar $value = unmarshal.param(String.class, "value");
216:
217:                    JExpression inv;
218:
219:                    if (parseMethod.equals("new")) {
220:                        // "new" indicates that the constructor of the target type
221:                        // will do the unmarshalling.
222:
223:                        // RESULT: new <type>()
224:                        inv = JExpr._new(bim).arg($value);
225:                    } else {
226:                        int idx = parseMethod.lastIndexOf('.');
227:                        if (idx < 0) {
228:                            // parseMethod specifies the static method of the target type
229:                            // which will do the unmarshalling.
230:
231:                            // because of an error check at the constructor,
232:                            // we can safely assume that this cast works.
233:                            inv = bim.staticInvoke(parseMethod).arg($value);
234:                        } else {
235:                            inv = JExpr.direct(parseMethod + "(value)");
236:                        }
237:                    }
238:                    unmarshal.body()._return(inv);
239:
240:                    JMethod marshal = adapter.method(JMod.PUBLIC, String.class,
241:                            "marshal");
242:                    $value = marshal.param(bim, "value");
243:
244:                    if (printMethod
245:                            .startsWith("javax.xml.bind.DatatypeConverter.")) {
246:                        // UGLY: if this conversion is the system-driven conversion,
247:                        // check for null
248:                        marshal.body()._if($value.eq(JExpr._null()))._then()
249:                                ._return(JExpr._null());
250:                    }
251:
252:                    int idx = printMethod.lastIndexOf('.');
253:                    if (idx < 0) {
254:                        // printMethod specifies a method in the target type
255:                        // which performs the serialization.
256:
257:                        // RESULT: <value>.<method>()
258:                        inv = $value.invoke(printMethod);
259:
260:                        // check value is not null ... if(value == null) return null;
261:                        JConditional jcon = marshal.body()._if(
262:                                $value.eq(JExpr._null()));
263:                        jcon._then()._return(JExpr._null());
264:                    } else {
265:                        // RESULT: <className>.<method>(<value>)
266:                        if (this .printMethod == null) {
267:                            // HACK HACK HACK
268:                            JType t = inMemoryType.unboxify();
269:                            inv = JExpr.direct(printMethod + "(("
270:                                    + findBaseConversion(owner).toLowerCase()
271:                                    + ")(" + t.fullName() + ")value)");
272:                        } else
273:                            inv = JExpr.direct(printMethod + "(value)");
274:                    }
275:                    marshal.body()._return(inv);
276:
277:                    return adapter;
278:                }
279:
280:                private String printMethodFor(XSSimpleType owner) {
281:                    if (printMethod != null)
282:                        return printMethod;
283:
284:                    if (inMemoryType.unboxify().isPrimitive()) {
285:                        String method = getConversionMethod("print", owner);
286:                        if (method != null)
287:                            return method;
288:                    }
289:
290:                    return "toString";
291:                }
292:
293:                private String parseMethodFor(XSSimpleType owner) {
294:                    if (parseMethod != null)
295:                        return parseMethod;
296:
297:                    if (inMemoryType.unboxify().isPrimitive()) {
298:                        String method = getConversionMethod("parse", owner);
299:                        if (method != null) {
300:                            // this cast is necessary for conversion between primitive Java types
301:                            return '(' + inMemoryType.unboxify().fullName()
302:                                    + ')' + method;
303:                        }
304:                    }
305:
306:                    return "new";
307:                }
308:
309:                private static final String[] knownBases = new String[] {
310:                        "Float", "Double", "Byte", "Short", "Int", "Long",
311:                        "Boolean" };
312:
313:                private String getConversionMethod(String methodPrefix,
314:                        XSSimpleType owner) {
315:                    String bc = findBaseConversion(owner);
316:                    if (bc == null)
317:                        return null;
318:
319:                    return DatatypeConverter.class.getName() + '.'
320:                            + methodPrefix + bc;
321:                }
322:
323:                private String findBaseConversion(XSSimpleType owner) {
324:                    // find the base simple type mapping.
325:                    for (XSSimpleType st = owner; st != null; st = st
326:                            .getSimpleBaseType()) {
327:                        if (!WellKnownNamespace.XML_SCHEMA.equals(st
328:                                .getTargetNamespace()))
329:                            continue; // user-defined type
330:
331:                        String name = st.getName().intern();
332:                        for (String s : knownBases)
333:                            if (name.equalsIgnoreCase(s))
334:                                return s;
335:                    }
336:
337:                    return null;
338:                }
339:
340:                public QName getName() {
341:                    return NAME;
342:                }
343:
344:                /** Name of the conversion declaration. */
345:                public static final QName NAME = new QName(Const.JAXB_NSURI,
346:                        "javaType");
347:            }
348:
349:            @XmlRootElement(name="javaType",namespace=Const.XJC_EXTENSION_URI)
350:            public static class UserAdapter extends BIConversion {
351:                @XmlAttribute(name="name")
352:                private String type = null;
353:
354:                @XmlAttribute
355:                private String adapter = null;
356:
357:                private TypeUse typeUse;
358:
359:                public TypeUse getTypeUse(XSSimpleType owner) {
360:                    if (typeUse != null)
361:                        return typeUse;
362:
363:                    JCodeModel cm = getCodeModel();
364:
365:                    JDefinedClass a;
366:                    try {
367:                        a = cm._class(adapter);
368:                        a.hide(); // we assume this is given by the user
369:                        a._extends(cm.ref(XmlAdapter.class)
370:                                .narrow(String.class).narrow(cm.ref(type)));
371:                    } catch (JClassAlreadyExistsException e) {
372:                        a = e.getExistingClass();
373:                    }
374:
375:                    // TODO: it's not correct to say that it adapts from String,
376:                    // but OTOH I don't think we can compute that.
377:                    typeUse = TypeUseFactory.adapt(CBuiltinLeafInfo.STRING,
378:                            new CAdapter(a));
379:
380:                    return typeUse;
381:                }
382:            }
383:        }
w___w___w._j___a_v_a2_s___.___c__o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.