001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.xjc.model;
027:
028: import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
029: import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
030: import javax.xml.bind.annotation.adapters.XmlAdapter;
031:
032: import com.sun.codemodel.internal.JClass;
033: import com.sun.tools.internal.xjc.model.nav.EagerNClass;
034: import com.sun.tools.internal.xjc.model.nav.NClass;
035: import com.sun.tools.internal.xjc.model.nav.NType;
036: import com.sun.tools.internal.xjc.model.nav.NavigatorImpl;
037: import com.sun.tools.internal.xjc.outline.Aspect;
038: import com.sun.tools.internal.xjc.outline.Outline;
039: import com.sun.xml.internal.bind.v2.model.core.Adapter;
040:
041: /**
042: * Extended {@link Adapter} for use within XJC.
043: *
044: * @author Kohsuke Kawaguchi
045: */
046: public final class CAdapter extends Adapter<NType, NClass> {
047:
048: /**
049: * If non-null, the same as {@link #adapterType} but more conveniently typed.
050: */
051: private JClass adapterClass1;
052:
053: /**
054: * If non-null, the same as {@link #adapterType} but more conveniently typed.
055: */
056: private Class<? extends XmlAdapter> adapterClass2;
057:
058: /**
059: * When the adapter class is statically known to us.
060: *
061: * @param copy
062: * true to copy the adapter class into the user package,
063: * or otherwise just refer to the class specified via the
064: * adapter parameter.
065: */
066: public CAdapter(Class<? extends XmlAdapter> adapter, boolean copy) {
067: super (getRef(adapter, copy), NavigatorImpl.theInstance);
068: this .adapterClass1 = null;
069: this .adapterClass2 = adapter;
070: }
071:
072: static NClass getRef(final Class<? extends XmlAdapter> adapter,
073: boolean copy) {
074: if (copy) {
075: // TODO: this is a hack. the code generation should be defered until
076: // the backend. (right now constant generation happens in the front-end)
077: return new EagerNClass(adapter) {
078: @Override
079: public JClass toType(Outline o, Aspect aspect) {
080: return o.addRuntime(adapter);
081: }
082:
083: public String fullName() {
084: // TODO: implement this method later
085: throw new UnsupportedOperationException();
086: }
087: };
088: } else {
089: return NavigatorImpl.theInstance.ref(adapter);
090: }
091: }
092:
093: public CAdapter(JClass adapter) {
094: super (NavigatorImpl.theInstance.ref(adapter),
095: NavigatorImpl.theInstance);
096: this .adapterClass1 = adapter;
097: this .adapterClass2 = null;
098: }
099:
100: public JClass getAdapterClass(Outline o) {
101: if (adapterClass1 == null)
102: adapterClass1 = o.getCodeModel().ref(adapterClass2);
103: return adapterType.toType(o, Aspect.EXPOSED);
104: }
105:
106: /**
107: * Returns true if the adapter is for whitespace normalization.
108: * Such an adapter can be ignored when producing a list.
109: */
110: public boolean isWhitespaceAdapter() {
111: return adapterClass2 == CollapsedStringAdapter.class
112: || adapterClass2 == NormalizedStringAdapter.class;
113: }
114:
115: /**
116: * Returns the adapter class if the adapter type is statically known to XJC.
117: * <p>
118: * This method is mostly for enabling certain optimized code generation.
119: */
120: public Class<? extends XmlAdapter> getAdapterIfKnown() {
121: return adapterClass2;
122: }
123: }
|