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.api.impl.s2j;
038:
039: import java.util.ArrayList;
040: import java.util.Collection;
041: import java.util.List;
042:
043: import javax.xml.namespace.QName;
044:
045: import com.sun.tools.xjc.api.Mapping;
046: import com.sun.tools.xjc.api.Property;
047: import com.sun.tools.xjc.model.CClassInfo;
048: import com.sun.tools.xjc.model.CElement;
049: import com.sun.tools.xjc.model.CElementInfo;
050: import com.sun.tools.xjc.model.CElementPropertyInfo;
051: import com.sun.tools.xjc.model.CPropertyInfo;
052: import com.sun.tools.xjc.model.CReferencePropertyInfo;
053: import com.sun.tools.xjc.model.CTypeRef;
054: import com.sun.xml.bind.v2.model.core.ClassInfo;
055: import com.sun.xml.bind.v2.model.core.ReferencePropertyInfo;
056:
057: /**
058: * Partial common implementation between {@link ElementMappingImpl} and {@link BeanMappingImpl}
059: *
060: * @author Kohsuke Kawaguchi
061: */
062: abstract class AbstractMappingImpl<InfoT extends CElement> implements
063: Mapping {
064:
065: protected final JAXBModelImpl parent;
066: protected final InfoT clazz;
067:
068: /**
069: * Lazily computed.
070: *
071: * @see #getWrapperStyleDrilldown()
072: */
073: private List<Property> drilldown = null;
074: private boolean drilldownComputed = false;
075:
076: protected AbstractMappingImpl(JAXBModelImpl parent, InfoT clazz) {
077: this .parent = parent;
078: this .clazz = clazz;
079: }
080:
081: public final QName getElement() {
082: return clazz.getElementName();
083: }
084:
085: public final String getClazz() {
086: return clazz.getType().fullName();
087: }
088:
089: public final List<? extends Property> getWrapperStyleDrilldown() {
090: if (!drilldownComputed) {
091: drilldownComputed = true;
092: drilldown = calcDrilldown();
093: }
094: return drilldown;
095: }
096:
097: protected abstract List<Property> calcDrilldown();
098:
099: /**
100: * Derived classes can use this method to implement {@link #calcDrilldown}.
101: */
102: protected List<Property> buildDrilldown(CClassInfo typeBean) {
103: List<Property> result;
104:
105: CClassInfo bc = typeBean.getBaseClass();
106: if (bc != null) {
107: result = buildDrilldown(bc);
108: if (result == null)
109: return null; // aborted
110: } else
111: result = new ArrayList<Property>();
112:
113: for (CPropertyInfo p : typeBean.getProperties()) {
114: if (p instanceof CElementPropertyInfo) {
115: CElementPropertyInfo ep = (CElementPropertyInfo) p;
116: // wrong. A+,B,C is eligible for drill-down.
117: // if(ep.isCollection())
118: // // content model like A+,B,C is not eligible
119: // return null;
120:
121: List<? extends CTypeRef> ref = ep.getTypes();
122: if (ref.size() != 1)
123: // content model like (A|B),C is not eligible
124: return null;
125:
126: result.add(createPropertyImpl(ep, ref.get(0)
127: .getTagName()));
128: } else if (p instanceof ReferencePropertyInfo) {
129: CReferencePropertyInfo rp = (CReferencePropertyInfo) p;
130:
131: Collection<CElement> elements = rp.getElements();
132: if (elements.size() != 1)
133: return null;
134:
135: CElement ref = elements.iterator().next();
136: if (ref instanceof ClassInfo) {
137: result.add(createPropertyImpl(rp, ref
138: .getElementName()));
139: } else {
140: CElementInfo eref = (CElementInfo) ref;
141: if (!eref.getSubstitutionMembers().isEmpty())
142: return null; // elements with a substitution group isn't qualified for the wrapper style
143:
144: // JAX-WS doesn't want to see JAXBElement, so we have to hide it for them.
145: ElementAdapter fr;
146: if (rp.isCollection())
147: fr = new ElementCollectionAdapter(
148: parent.outline.getField(rp), eref);
149: else
150: fr = new ElementSingleAdapter(parent.outline
151: .getField(rp), eref);
152:
153: result.add(new PropertyImpl(this , fr, eref
154: .getElementName()));
155: }
156: } else
157: // to be eligible for the wrapper style, only elements are allowed.
158: // according to the JAX-RPC spec 2.3.1.2, element refs are disallowed
159: return null;
160:
161: }
162:
163: return result;
164: }
165:
166: private Property createPropertyImpl(CPropertyInfo p, QName tagName) {
167: return new PropertyImpl(this, parent.outline.getField(p),
168: tagName);
169: }
170: }
|