src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/ExpressionParticleBinder.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

     1 /*
     2  * Copyright (c) 1997, 2011, 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  */
    26 package com.sun.tools.internal.xjc.reader.xmlschema;
    28 import java.util.Collection;
    30 import com.sun.tools.internal.xjc.model.CPropertyInfo;
    31 import com.sun.tools.internal.xjc.model.Multiplicity;
    32 import com.sun.tools.internal.xjc.reader.RawTypeSet;
    33 import com.sun.tools.internal.xjc.reader.gbind.ConnectedComponent;
    34 import com.sun.tools.internal.xjc.reader.gbind.Element;
    35 import com.sun.tools.internal.xjc.reader.gbind.Expression;
    36 import com.sun.tools.internal.xjc.reader.gbind.Graph;
    37 import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIProperty;
    38 import com.sun.xml.internal.bind.v2.model.core.WildcardMode;
    39 import com.sun.xml.internal.xsom.XSParticle;
    41 /**
    42  * {@link ParticleBinder} that uses {@link ExpressionBuilder} et al
    43  * for better, more intuitive (but non spec-conforming) binding.
    44  *
    45  * @author Kohsuke Kawaguchi
    46  */
    47 final class ExpressionParticleBinder extends ParticleBinder {
    48     public void build(XSParticle p, Collection<XSParticle> forcedProps) {
    49         // this class isn't about spec conformance, but
    50         // for the ease of use.
    51         // so we don't give a damn about 'forcedProps'.
    52         // although, for a future note, it's conceivable to expand
    53         // the binding algorithm to cover this notion.
    55         Expression tree = ExpressionBuilder.createTree(p);
    56         Graph g = new Graph(tree);
    57         for (ConnectedComponent cc : g) {
    58             buildProperty(cc);
    59         }
    60     }
    62     /**
    63      * Builds a property ouf ot a connected component.
    64      */
    65     private void buildProperty(ConnectedComponent cc) {
    66         StringBuilder propName = new StringBuilder(); // property name
    67         int nameTokenCount = 0; // combine only up to 3
    69         RawTypeSetBuilder rtsb = new RawTypeSetBuilder();
    70         for (Element e : cc) {
    71             GElement ge = (GElement)e;
    73             if(nameTokenCount<3) {
    74                 if(nameTokenCount!=0)
    75                     propName.append("And");
    76                 propName.append(makeJavaName(cc.isCollection(),ge.getPropertyNameSeed()));
    77                 nameTokenCount++;
    78             }
    80             if(e instanceof GElementImpl) {
    81                 GElementImpl ei = (GElementImpl) e;
    82                 rtsb.elementDecl(ei.decl);
    83                 continue;
    84             }
    85             if(e instanceof GWildcardElement) {
    86                 GWildcardElement w = (GWildcardElement)e;
    87                 rtsb.getRefs().add(new RawTypeSetBuilder.WildcardRef(
    88                     w.isStrict() ? WildcardMode.STRICT : WildcardMode.SKIP));
    89                 continue;
    90             }
    91             assert false : e;   // no other kind should be here
    92         }
    94         Multiplicity m = Multiplicity.ONE;
    95         if(cc.isCollection())
    96             m = m.makeRepeated();
    97         if(!cc.isRequired())
    98             m = m.makeOptional();
   100         RawTypeSet rts = new RawTypeSet(rtsb.getRefs(),m);
   102         XSParticle p = findSourceParticle(cc);
   104         BIProperty cust = BIProperty.getCustomization(p);
   105         CPropertyInfo prop = cust.createElementOrReferenceProperty(
   106             propName.toString(), false, p, rts );
   107         getCurrentBean().addProperty(prop);
   108     }
   110     /**
   111      * Finds a {@link XSParticle} that can serve as the representative property of
   112      * the given {@link ConnectedComponent}.
   113      *
   114      * The representative property is used for reporting an error location and
   115      * taking {@link BIProperty} customization. Right now, the algorithm is just pick
   116      * the first one with {@link BIProperty}, but one can think of a better algorithm,
   117      * such as taking a choice of (A|B) if CC={A,B}.
   118      */
   119     private XSParticle findSourceParticle(ConnectedComponent cc) {
   120         XSParticle first = null;
   122         for (Element e : cc) {
   123             GElement ge = (GElement)e;
   124             for (XSParticle p : ge.particles) {
   125                 if(first==null)
   126                     first = p;
   127                 if(getLocalPropCustomization(p)!=null)
   128                     return p;
   129             }
   130             // if there are multiple property customizations,
   131             // all but one will be left unused, which will be detected as an error
   132             // later, so no need to check that now.
   133         }
   135         // if no customization was found, just use the first one.
   136         return first;
   137     }
   139     public boolean checkFallback(XSParticle p) {
   140         // this algorithm never falls back to 'getContent'.
   141         return false;
   142     }
   143 }

mercurial