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.xml.bind.annotation.adapters.CollapsedStringAdapter;
040: import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
041: import javax.xml.bind.annotation.adapters.XmlAdapter;
042:
043: import com.sun.codemodel.JClass;
044: import com.sun.tools.xjc.model.nav.EagerNClass;
045: import com.sun.tools.xjc.model.nav.NClass;
046: import com.sun.tools.xjc.model.nav.NType;
047: import com.sun.tools.xjc.model.nav.NavigatorImpl;
048: import com.sun.tools.xjc.outline.Aspect;
049: import com.sun.tools.xjc.outline.Outline;
050: import com.sun.xml.bind.v2.model.core.Adapter;
051:
052: /**
053: * Extended {@link Adapter} for use within XJC.
054: *
055: * @author Kohsuke Kawaguchi
056: */
057: public final class CAdapter extends Adapter<NType, NClass> {
058:
059: /**
060: * If non-null, the same as {@link #adapterType} but more conveniently typed.
061: */
062: private JClass adapterClass1;
063:
064: /**
065: * If non-null, the same as {@link #adapterType} but more conveniently typed.
066: */
067: private Class<? extends XmlAdapter> adapterClass2;
068:
069: /**
070: * When the adapter class is statically known to us.
071: *
072: * @param copy
073: * true to copy the adapter class into the user package,
074: * or otherwise just refer to the class specified via the
075: * adapter parameter.
076: */
077: public CAdapter(Class<? extends XmlAdapter> adapter, boolean copy) {
078: super (getRef(adapter, copy), NavigatorImpl.theInstance);
079: this .adapterClass1 = null;
080: this .adapterClass2 = adapter;
081: }
082:
083: static NClass getRef(final Class<? extends XmlAdapter> adapter,
084: boolean copy) {
085: if (copy) {
086: // TODO: this is a hack. the code generation should be defered until
087: // the backend. (right now constant generation happens in the front-end)
088: return new EagerNClass(adapter) {
089: @Override
090: public JClass toType(Outline o, Aspect aspect) {
091: return o.addRuntime(adapter);
092: }
093:
094: public String fullName() {
095: // TODO: implement this method later
096: throw new UnsupportedOperationException();
097: }
098: };
099: } else {
100: return NavigatorImpl.theInstance.ref(adapter);
101: }
102: }
103:
104: public CAdapter(JClass adapter) {
105: super (NavigatorImpl.theInstance.ref(adapter),
106: NavigatorImpl.theInstance);
107: this .adapterClass1 = adapter;
108: this .adapterClass2 = null;
109: }
110:
111: public JClass getAdapterClass(Outline o) {
112: if (adapterClass1 == null)
113: adapterClass1 = o.getCodeModel().ref(adapterClass2);
114: return adapterType.toType(o, Aspect.EXPOSED);
115: }
116:
117: /**
118: * Returns true if the adapter is for whitespace normalization.
119: * Such an adapter can be ignored when producing a list.
120: */
121: public boolean isWhitespaceAdapter() {
122: return adapterClass2 == CollapsedStringAdapter.class
123: || adapterClass2 == NormalizedStringAdapter.class;
124: }
125:
126: /**
127: * Returns the adapter class if the adapter type is statically known to XJC.
128: * <p>
129: * This method is mostly for enabling certain optimized code generation.
130: */
131: public Class<? extends XmlAdapter> getAdapterIfKnown() {
132: return adapterClass2;
133: }
134: }
|