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.List;
041:
042: import javax.xml.bind.JAXBElement;
043:
044: import com.sun.codemodel.JBlock;
045: import com.sun.codemodel.JClass;
046: import com.sun.codemodel.JCodeModel;
047: import com.sun.codemodel.JConditional;
048: import com.sun.codemodel.JExpr;
049: import com.sun.codemodel.JExpression;
050: import com.sun.codemodel.JForEach;
051: import com.sun.codemodel.JType;
052: import com.sun.codemodel.JVar;
053: import com.sun.tools.xjc.model.CElementInfo;
054: import static com.sun.tools.xjc.outline.Aspect.EXPOSED;
055: import com.sun.tools.xjc.outline.FieldAccessor;
056: import com.sun.tools.xjc.outline.FieldOutline;
057:
058: /**
059: * {@link ElementAdapter} that works with a collection
060: * of {@link JAXBElement}.
061: *
062: * @author Kohsuke Kawaguchi
063: */
064: final class ElementCollectionAdapter extends ElementAdapter {
065: public ElementCollectionAdapter(FieldOutline core, CElementInfo ei) {
066: super (core, ei);
067: }
068:
069: public JType getRawType() {
070: return codeModel().ref(List.class).narrow(itemType().boxify());
071: }
072:
073: private JType itemType() {
074: return ei.getContentInMemoryType().toType(outline(), EXPOSED);
075: }
076:
077: public FieldAccessor create(JExpression targetObject) {
078: return new FieldAccessorImpl(targetObject);
079: }
080:
081: final class FieldAccessorImpl extends
082: ElementAdapter.FieldAccessorImpl {
083: public FieldAccessorImpl(JExpression target) {
084: super (target);
085: }
086:
087: public void toRawValue(JBlock block, JVar $var) {
088: JCodeModel cm = outline().getCodeModel();
089: JClass elementType = ei.toType(outline(), EXPOSED).boxify();
090:
091: // [RESULT]
092: // $var = new ArrayList();
093: // for( JAXBElement e : [core.toRawValue] ) {
094: // if(e==null)
095: // $var.add(null);
096: // else
097: // $var.add(e.getValue());
098: // }
099:
100: block.assign($var, JExpr._new(cm.ref(ArrayList.class)
101: .narrow(itemType().boxify())));
102: JVar $col = block.decl(core.getRawType(), "col"
103: + hashCode());
104: acc.toRawValue(block, $col);
105: JForEach loop = block.forEach(elementType,
106: "v" + hashCode()/*unique string handling*/, $col);
107:
108: JConditional cond = loop.body()._if(
109: loop.var().eq(JExpr._null()));
110: cond._then().invoke($var, "add").arg(JExpr._null());
111: cond._else().invoke($var, "add").arg(
112: loop.var().invoke("getValue"));
113: }
114:
115: public void fromRawValue(JBlock block, String uniqueName,
116: JExpression $var) {
117: JCodeModel cm = outline().getCodeModel();
118: JClass elementType = ei.toType(outline(), EXPOSED).boxify();
119:
120: // [RESULT]
121: // $t = new ArrayList();
122: // for( Type e : $var ) {
123: // $var.add(new JAXBElement(e));
124: // }
125: // [core.fromRawValue]
126:
127: JClass col = cm.ref(ArrayList.class).narrow(elementType);
128: JVar $t = block.decl(col, uniqueName + "_col", JExpr
129: ._new(col));
130:
131: JForEach loop = block.forEach(itemType(),
132: uniqueName + "_i", $t);
133: loop.body().invoke($var, "add").arg(
134: createJAXBElement(loop.var()));
135:
136: acc.fromRawValue(block, uniqueName, $t);
137: }
138: }
139: }
|