1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.internal.xjc.generator.bean.field;
  27 
  28 import java.util.ArrayList;
  29 
  30 import com.sun.tools.internal.xjc.generator.bean.ClassOutlineImpl;
  31 import com.sun.tools.internal.xjc.model.CPropertyInfo;
  32 import com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
  33 import com.sun.tools.internal.xjc.outline.FieldOutline;
  34 import java.io.Serializable;
  35 
  36 /**
  37  * Default implementation of the FieldRendererFactory
  38  * that faithfully implements the semantics demanded by the JAXB spec.
  39  *
  40  * <p>
  41  * This class is just a facade --- it just determines which
  42  * {@link FieldRenderer} to use and just delegate the work.
  43  *
  44  * @author
  45  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  46  */
  47 final class DefaultFieldRenderer implements FieldRenderer {
  48 
  49     private final FieldRendererFactory frf;
  50 
  51     /**
  52      * Use {@link FieldRendererFactory#getDefault()}.
  53      */
  54     DefaultFieldRenderer(FieldRendererFactory frf) {
  55         this.frf = frf;
  56     }
  57 
  58     public DefaultFieldRenderer(FieldRendererFactory frf, FieldRenderer defaultCollectionFieldRenderer ) {
  59         this.frf = frf;
  60         this.defaultCollectionFieldRenderer = defaultCollectionFieldRenderer;
  61     }
  62 
  63     private FieldRenderer defaultCollectionFieldRenderer;
  64 
  65 
  66     public FieldOutline generate(ClassOutlineImpl outline, CPropertyInfo prop) {
  67         return decideRenderer(outline,prop).generate(outline,prop);
  68     }
  69 
  70     private FieldRenderer decideRenderer(ClassOutlineImpl outline, CPropertyInfo prop) {
  71 
  72         if (prop instanceof CReferencePropertyInfo) {
  73             CReferencePropertyInfo p = (CReferencePropertyInfo)prop;
  74             if (p.isDummy()) {
  75                 return frf.getDummyList(outline.parent().getCodeModel().ref(ArrayList.class));
  76             }
  77             if (p.isContent() && (p.isMixedExtendedCust())) {
  78                 return frf.getContentList(outline.parent().getCodeModel().ref(ArrayList.class).narrow(Serializable.class));
  79             }
  80         }
  81 
  82         if(!prop.isCollection()) {
  83             // non-collection field
  84 
  85             // TODO: check for bidning info for optionalPrimitiveType=boxed or
  86             // noHasMethod=false and noDeletedMethod=false
  87             if(prop.isUnboxable())
  88                 // this one uses a primitive type as much as possible
  89                 return frf.getRequiredUnboxed();
  90             else
  91                 // otherwise use the default non-collection field
  92                 return frf.getSingle();
  93         }
  94 
  95         if( defaultCollectionFieldRenderer==null ) {
  96             return frf.getList(outline.parent().getCodeModel().ref(ArrayList.class));
  97         }
  98 
  99         // this field is a collection field.
 100         // use untyped list as the default. This is consistent
 101         // to the JAXB spec.
 102         return defaultCollectionFieldRenderer;
 103     }
 104 }