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.tools.xjc.model;
038:
039: import javax.activation.MimeType;
040: import javax.xml.bind.annotation.adapters.XmlAdapter;
041:
042: import com.sun.codemodel.JExpr;
043: import com.sun.codemodel.JExpression;
044: import com.sun.codemodel.JStringLiteral;
045: import com.sun.tools.xjc.outline.Outline;
046: import com.sun.xml.bind.v2.ClassFactory;
047: import com.sun.xml.bind.v2.model.core.ID;
048: import com.sun.xml.xsom.XmlString;
049:
050: /**
051: * General-purpose {@link TypeUse} implementation.
052: *
053: * @author Kohsuke Kawaguchi
054: */
055: final class TypeUseImpl implements TypeUse {
056: private final CNonElement coreType;
057: private final boolean collection;
058: private final CAdapter adapter;
059: private final ID id;
060: private final MimeType expectedMimeType;
061:
062: public TypeUseImpl(CNonElement itemType, boolean collection, ID id,
063: MimeType expectedMimeType, CAdapter adapter) {
064: this .coreType = itemType;
065: this .collection = collection;
066: this .id = id;
067: this .expectedMimeType = expectedMimeType;
068: this .adapter = adapter;
069: }
070:
071: public boolean isCollection() {
072: return collection;
073: }
074:
075: public CNonElement getInfo() {
076: return coreType;
077: }
078:
079: public CAdapter getAdapterUse() {
080: return adapter;
081: }
082:
083: public ID idUse() {
084: return id;
085: }
086:
087: public MimeType getExpectedMimeType() {
088: return expectedMimeType;
089: }
090:
091: public boolean equals(Object o) {
092: if (this == o)
093: return true;
094: if (!(o instanceof TypeUseImpl))
095: return false;
096:
097: final TypeUseImpl that = (TypeUseImpl) o;
098:
099: if (collection != that.collection)
100: return false;
101: if (this .id != that.id)
102: return false;
103: if (adapter != null ? !adapter.equals(that.adapter)
104: : that.adapter != null)
105: return false;
106: if (coreType != null ? !coreType.equals(that.coreType)
107: : that.coreType != null)
108: return false;
109:
110: return true;
111: }
112:
113: public int hashCode() {
114: int result;
115: result = (coreType != null ? coreType.hashCode() : 0);
116: result = 29 * result + (collection ? 1 : 0);
117: result = 29 * result
118: + (adapter != null ? adapter.hashCode() : 0);
119: return result;
120: }
121:
122: public JExpression createConstant(Outline outline, XmlString lexical) {
123: if (isCollection())
124: return null;
125:
126: if (adapter == null)
127: return coreType.createConstant(outline, lexical);
128:
129: // [RESULT] new Adapter().unmarshal(CONSTANT);
130: JExpression cons = coreType.createConstant(outline, lexical);
131: Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown();
132:
133: // try to run the adapter now rather than later.
134: if (cons instanceof JStringLiteral && atype != null) {
135: JStringLiteral scons = (JStringLiteral) cons;
136: XmlAdapter a = ClassFactory.create(atype);
137: try {
138: Object value = a.unmarshal(scons.str);
139: if (value instanceof String) {
140: return JExpr.lit((String) value);
141: }
142: } catch (Exception e) {
143: // assume that we can't eagerly bind this
144: }
145: }
146:
147: return JExpr._new(adapter.getAdapterClass(outline)).invoke(
148: "unmarshal").arg(cons);
149: }
150: }
|