src/share/jaxws_classes/com/sun/xml/internal/bind/v2/schemagen/Tree.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/schemagen/Tree.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,244 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.schemagen;
    1.30 +
    1.31 +import java.util.ArrayList;
    1.32 +import java.util.Arrays;
    1.33 +import java.util.List;
    1.34 +
    1.35 +import com.sun.xml.internal.bind.v2.schemagen.xmlschema.ContentModelContainer;
    1.36 +import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Particle;
    1.37 +import com.sun.xml.internal.bind.v2.schemagen.xmlschema.TypeDefParticle;
    1.38 +import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Occurs;
    1.39 +
    1.40 +/**
    1.41 + * Normalized representation of the content model.
    1.42 + *
    1.43 + * <p>
    1.44 + * This is built from bottom up so that we can eliminate redundant constructs,
    1.45 + * and produce the most concise content model definition in XML.
    1.46 + *
    1.47 + * @author Kohsuke Kawaguchi
    1.48 + */
    1.49 +abstract class Tree {
    1.50 +
    1.51 +    /**
    1.52 +     * Returns "T?" from "T".
    1.53 +     *
    1.54 +     * @param really
    1.55 +     *      if false this method becomes no-op. This is so that we can write
    1.56 +     *      the caller fluently.
    1.57 +     */
    1.58 +    Tree makeOptional(boolean really) {
    1.59 +        return really?new Optional(this) :this;
    1.60 +    }
    1.61 +
    1.62 +    /**
    1.63 +     * Returns "T+" from "T".
    1.64 +     *
    1.65 +     * @param really
    1.66 +     *      if false this method becomes no-op. This is so that we can write
    1.67 +     *      the caller fluently.
    1.68 +     */
    1.69 +    Tree makeRepeated(boolean really) {
    1.70 +        return really?new Repeated(this) :this;
    1.71 +    }
    1.72 +
    1.73 +    /**
    1.74 +     * Returns a group tree.
    1.75 +     */
    1.76 +    static Tree makeGroup(GroupKind kind, List<Tree> children ) {
    1.77 +        // pointless binary operator.
    1.78 +        if(children.size()==1)
    1.79 +            return children.get(0);
    1.80 +
    1.81 +        // we neither have epsilon or emptySet, so can't handle children.length==0 nicely
    1.82 +
    1.83 +        // eliminated nesting groups of the same kind.
    1.84 +        // this is where binary tree would have shined.
    1.85 +        List<Tree> normalizedChildren = new ArrayList<Tree>(children.size());
    1.86 +        for (Tree t : children) {
    1.87 +            if (t instanceof Group) {
    1.88 +                Group g = (Group) t;
    1.89 +                if(g.kind==kind) {
    1.90 +                    normalizedChildren.addAll(Arrays.asList(g.children));
    1.91 +                    continue;
    1.92 +                }
    1.93 +            }
    1.94 +            normalizedChildren.add(t);
    1.95 +        }
    1.96 +
    1.97 +        return new Group(kind,normalizedChildren.toArray(new Tree[normalizedChildren.size()]));
    1.98 +    }
    1.99 +
   1.100 +    /**
   1.101 +     * Returns true if this tree accepts empty sequence.
   1.102 +     */
   1.103 +    abstract boolean isNullable();
   1.104 +
   1.105 +    /**
   1.106 +     * Returns true if the top node of this tree can
   1.107 +     * appear as a valid top-level content model in XML Schema.
   1.108 +     *
   1.109 +     * <p>
   1.110 +     * Model groups and occurrences that have model group in it can.
   1.111 +     */
   1.112 +    boolean canBeTopLevel() { return false; }
   1.113 +
   1.114 +    /**
   1.115 +     * Writes out the content model.
   1.116 +     *
   1.117 +     * Normall this runs recursively until we write out the whole content model.
   1.118 +     */
   1.119 +    protected abstract void write(ContentModelContainer parent, boolean isOptional, boolean repeated);
   1.120 +
   1.121 +    /**
   1.122 +     * Writes inside the given complex type.
   1.123 +     */
   1.124 +    protected void write(TypeDefParticle ct) {
   1.125 +        if(canBeTopLevel())
   1.126 +            write(ct._cast(ContentModelContainer.class), false, false);
   1.127 +        else
   1.128 +            // need a dummy wrapper
   1.129 +            new Group(GroupKind.SEQUENCE,this).write(ct);
   1.130 +    }
   1.131 +
   1.132 +    /**
   1.133 +     * Convenience method to write occurrence constraints.
   1.134 +     */
   1.135 +    protected final void writeOccurs(Occurs o, boolean isOptional, boolean repeated) {
   1.136 +        if(isOptional)
   1.137 +            o.minOccurs(0);
   1.138 +        if(repeated)
   1.139 +            o.maxOccurs("unbounded");
   1.140 +    }
   1.141 +
   1.142 +    /**
   1.143 +     * Represents a terminal tree node, such as element, wildcard, etc.
   1.144 +     */
   1.145 +    abstract static class Term extends Tree {
   1.146 +        boolean isNullable() {
   1.147 +            return false;
   1.148 +        }
   1.149 +    }
   1.150 +
   1.151 +    /**
   1.152 +     * "T?"
   1.153 +     */
   1.154 +    private static final class Optional extends Tree {
   1.155 +        private final Tree body;
   1.156 +
   1.157 +        private Optional(Tree body) {
   1.158 +            this.body = body;
   1.159 +        }
   1.160 +
   1.161 +        @Override
   1.162 +        boolean isNullable() {
   1.163 +            return true;
   1.164 +        }
   1.165 +
   1.166 +        @Override
   1.167 +        Tree makeOptional(boolean really) {
   1.168 +            return this;
   1.169 +        }
   1.170 +
   1.171 +        @Override
   1.172 +        protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) {
   1.173 +            body.write(parent,true,repeated);
   1.174 +        }
   1.175 +    }
   1.176 +
   1.177 +    /**
   1.178 +     * "T+"
   1.179 +     */
   1.180 +    private static final class Repeated extends Tree {
   1.181 +        private final Tree body;
   1.182 +
   1.183 +        private Repeated(Tree body) {
   1.184 +            this.body = body;
   1.185 +        }
   1.186 +
   1.187 +        @Override
   1.188 +        boolean isNullable() {
   1.189 +            return body.isNullable();
   1.190 +        }
   1.191 +
   1.192 +        @Override
   1.193 +        Tree makeRepeated(boolean really) {
   1.194 +            return this;
   1.195 +        }
   1.196 +
   1.197 +        @Override
   1.198 +        protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) {
   1.199 +            body.write(parent,isOptional,true);
   1.200 +        }
   1.201 +    }
   1.202 +
   1.203 +    /**
   1.204 +     * "S|T", "S,T", and "S&amp;T".
   1.205 +     */
   1.206 +    private static final class Group extends Tree {
   1.207 +        private final GroupKind kind;
   1.208 +        private final Tree[] children;
   1.209 +
   1.210 +        private Group(GroupKind kind, Tree... children) {
   1.211 +            this.kind = kind;
   1.212 +            this.children = children;
   1.213 +        }
   1.214 +
   1.215 +        @Override
   1.216 +        boolean canBeTopLevel() {
   1.217 +            return true;
   1.218 +        }
   1.219 +
   1.220 +        @Override
   1.221 +        boolean isNullable() {
   1.222 +            if(kind== GroupKind.CHOICE) {
   1.223 +                for (Tree t : children) {
   1.224 +                    if(t.isNullable())
   1.225 +                        return true;
   1.226 +                }
   1.227 +                return false;
   1.228 +            } else {
   1.229 +                for (Tree t : children) {
   1.230 +                    if(!t.isNullable())
   1.231 +                        return false;
   1.232 +                }
   1.233 +                return true;
   1.234 +            }
   1.235 +        }
   1.236 +
   1.237 +        @Override
   1.238 +        protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) {
   1.239 +            Particle c = kind.write(parent);
   1.240 +            writeOccurs(c,isOptional,repeated);
   1.241 +
   1.242 +            for (Tree child : children) {
   1.243 +                child.write(c,false,false);
   1.244 +            }
   1.245 +        }
   1.246 +    }
   1.247 +}

mercurial