|
1 /* |
|
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.internal.xjc.model; |
|
27 |
|
28 import javax.activation.MimeType; |
|
29 import javax.xml.bind.annotation.adapters.XmlAdapter; |
|
30 |
|
31 import com.sun.codemodel.internal.JExpr; |
|
32 import com.sun.codemodel.internal.JExpression; |
|
33 import com.sun.codemodel.internal.JStringLiteral; |
|
34 import com.sun.tools.internal.xjc.outline.Outline; |
|
35 import com.sun.xml.internal.bind.v2.ClassFactory; |
|
36 import com.sun.xml.internal.bind.v2.model.core.ID; |
|
37 import com.sun.xml.internal.xsom.XmlString; |
|
38 |
|
39 |
|
40 /** |
|
41 * General-purpose {@link TypeUse} implementation. |
|
42 * |
|
43 * @author Kohsuke Kawaguchi |
|
44 */ |
|
45 final class TypeUseImpl implements TypeUse { |
|
46 private final CNonElement coreType; |
|
47 private final boolean collection; |
|
48 private final CAdapter adapter; |
|
49 private final ID id; |
|
50 private final MimeType expectedMimeType; |
|
51 |
|
52 |
|
53 public TypeUseImpl(CNonElement itemType, boolean collection, ID id, MimeType expectedMimeType, CAdapter adapter) { |
|
54 this.coreType = itemType; |
|
55 this.collection = collection; |
|
56 this.id = id; |
|
57 this.expectedMimeType = expectedMimeType; |
|
58 this.adapter = adapter; |
|
59 } |
|
60 |
|
61 public boolean isCollection() { |
|
62 return collection; |
|
63 } |
|
64 |
|
65 public CNonElement getInfo() { |
|
66 return coreType; |
|
67 } |
|
68 |
|
69 public CAdapter getAdapterUse() { |
|
70 return adapter; |
|
71 } |
|
72 |
|
73 public ID idUse() { |
|
74 return id; |
|
75 } |
|
76 |
|
77 public MimeType getExpectedMimeType() { |
|
78 return expectedMimeType; |
|
79 } |
|
80 |
|
81 public boolean equals(Object o) { |
|
82 if (this == o) return true; |
|
83 if (!(o instanceof TypeUseImpl)) return false; |
|
84 |
|
85 final TypeUseImpl that = (TypeUseImpl) o; |
|
86 |
|
87 if (collection != that.collection) return false; |
|
88 if (this.id != that.id ) return false; |
|
89 if (adapter != null ? !adapter.equals(that.adapter) : that.adapter != null) return false; |
|
90 if (coreType != null ? !coreType.equals(that.coreType) : that.coreType != null) return false; |
|
91 |
|
92 return true; |
|
93 } |
|
94 |
|
95 public int hashCode() { |
|
96 int result; |
|
97 result = (coreType != null ? coreType.hashCode() : 0); |
|
98 result = 29 * result + (collection ? 1 : 0); |
|
99 result = 29 * result + (adapter != null ? adapter.hashCode() : 0); |
|
100 return result; |
|
101 } |
|
102 |
|
103 |
|
104 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
105 if(isCollection()) return null; |
|
106 |
|
107 if(adapter==null) return coreType.createConstant(outline, lexical); |
|
108 |
|
109 // [RESULT] new Adapter().unmarshal(CONSTANT); |
|
110 JExpression cons = coreType.createConstant(outline, lexical); |
|
111 Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown(); |
|
112 |
|
113 // try to run the adapter now rather than later. |
|
114 if(cons instanceof JStringLiteral && atype!=null) { |
|
115 JStringLiteral scons = (JStringLiteral) cons; |
|
116 XmlAdapter a = ClassFactory.create(atype); |
|
117 try { |
|
118 Object value = a.unmarshal(scons.str); |
|
119 if(value instanceof String) { |
|
120 return JExpr.lit((String)value); |
|
121 } |
|
122 } catch (Exception e) { |
|
123 // assume that we can't eagerly bind this |
|
124 } |
|
125 } |
|
126 |
|
127 return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons); |
|
128 } |
|
129 } |