Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.bind.v2.schemagen; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.ArrayList; |
aoqi@0 | 29 | import java.util.Arrays; |
aoqi@0 | 30 | import java.util.List; |
aoqi@0 | 31 | |
aoqi@0 | 32 | import com.sun.xml.internal.bind.v2.schemagen.xmlschema.ContentModelContainer; |
aoqi@0 | 33 | import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Particle; |
aoqi@0 | 34 | import com.sun.xml.internal.bind.v2.schemagen.xmlschema.TypeDefParticle; |
aoqi@0 | 35 | import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Occurs; |
aoqi@0 | 36 | |
aoqi@0 | 37 | /** |
aoqi@0 | 38 | * Normalized representation of the content model. |
aoqi@0 | 39 | * |
aoqi@0 | 40 | * <p> |
aoqi@0 | 41 | * This is built from bottom up so that we can eliminate redundant constructs, |
aoqi@0 | 42 | * and produce the most concise content model definition in XML. |
aoqi@0 | 43 | * |
aoqi@0 | 44 | * @author Kohsuke Kawaguchi |
aoqi@0 | 45 | */ |
aoqi@0 | 46 | abstract class Tree { |
aoqi@0 | 47 | |
aoqi@0 | 48 | /** |
aoqi@0 | 49 | * Returns "T?" from "T". |
aoqi@0 | 50 | * |
aoqi@0 | 51 | * @param really |
aoqi@0 | 52 | * if false this method becomes no-op. This is so that we can write |
aoqi@0 | 53 | * the caller fluently. |
aoqi@0 | 54 | */ |
aoqi@0 | 55 | Tree makeOptional(boolean really) { |
aoqi@0 | 56 | return really?new Optional(this) :this; |
aoqi@0 | 57 | } |
aoqi@0 | 58 | |
aoqi@0 | 59 | /** |
aoqi@0 | 60 | * Returns "T+" from "T". |
aoqi@0 | 61 | * |
aoqi@0 | 62 | * @param really |
aoqi@0 | 63 | * if false this method becomes no-op. This is so that we can write |
aoqi@0 | 64 | * the caller fluently. |
aoqi@0 | 65 | */ |
aoqi@0 | 66 | Tree makeRepeated(boolean really) { |
aoqi@0 | 67 | return really?new Repeated(this) :this; |
aoqi@0 | 68 | } |
aoqi@0 | 69 | |
aoqi@0 | 70 | /** |
aoqi@0 | 71 | * Returns a group tree. |
aoqi@0 | 72 | */ |
aoqi@0 | 73 | static Tree makeGroup(GroupKind kind, List<Tree> children ) { |
aoqi@0 | 74 | // pointless binary operator. |
aoqi@0 | 75 | if(children.size()==1) |
aoqi@0 | 76 | return children.get(0); |
aoqi@0 | 77 | |
aoqi@0 | 78 | // we neither have epsilon or emptySet, so can't handle children.length==0 nicely |
aoqi@0 | 79 | |
aoqi@0 | 80 | // eliminated nesting groups of the same kind. |
aoqi@0 | 81 | // this is where binary tree would have shined. |
aoqi@0 | 82 | List<Tree> normalizedChildren = new ArrayList<Tree>(children.size()); |
aoqi@0 | 83 | for (Tree t : children) { |
aoqi@0 | 84 | if (t instanceof Group) { |
aoqi@0 | 85 | Group g = (Group) t; |
aoqi@0 | 86 | if(g.kind==kind) { |
aoqi@0 | 87 | normalizedChildren.addAll(Arrays.asList(g.children)); |
aoqi@0 | 88 | continue; |
aoqi@0 | 89 | } |
aoqi@0 | 90 | } |
aoqi@0 | 91 | normalizedChildren.add(t); |
aoqi@0 | 92 | } |
aoqi@0 | 93 | |
aoqi@0 | 94 | return new Group(kind,normalizedChildren.toArray(new Tree[normalizedChildren.size()])); |
aoqi@0 | 95 | } |
aoqi@0 | 96 | |
aoqi@0 | 97 | /** |
aoqi@0 | 98 | * Returns true if this tree accepts empty sequence. |
aoqi@0 | 99 | */ |
aoqi@0 | 100 | abstract boolean isNullable(); |
aoqi@0 | 101 | |
aoqi@0 | 102 | /** |
aoqi@0 | 103 | * Returns true if the top node of this tree can |
aoqi@0 | 104 | * appear as a valid top-level content model in XML Schema. |
aoqi@0 | 105 | * |
aoqi@0 | 106 | * <p> |
aoqi@0 | 107 | * Model groups and occurrences that have model group in it can. |
aoqi@0 | 108 | */ |
aoqi@0 | 109 | boolean canBeTopLevel() { return false; } |
aoqi@0 | 110 | |
aoqi@0 | 111 | /** |
aoqi@0 | 112 | * Writes out the content model. |
aoqi@0 | 113 | * |
aoqi@0 | 114 | * Normall this runs recursively until we write out the whole content model. |
aoqi@0 | 115 | */ |
aoqi@0 | 116 | protected abstract void write(ContentModelContainer parent, boolean isOptional, boolean repeated); |
aoqi@0 | 117 | |
aoqi@0 | 118 | /** |
aoqi@0 | 119 | * Writes inside the given complex type. |
aoqi@0 | 120 | */ |
aoqi@0 | 121 | protected void write(TypeDefParticle ct) { |
aoqi@0 | 122 | if(canBeTopLevel()) |
aoqi@0 | 123 | write(ct._cast(ContentModelContainer.class), false, false); |
aoqi@0 | 124 | else |
aoqi@0 | 125 | // need a dummy wrapper |
aoqi@0 | 126 | new Group(GroupKind.SEQUENCE,this).write(ct); |
aoqi@0 | 127 | } |
aoqi@0 | 128 | |
aoqi@0 | 129 | /** |
aoqi@0 | 130 | * Convenience method to write occurrence constraints. |
aoqi@0 | 131 | */ |
aoqi@0 | 132 | protected final void writeOccurs(Occurs o, boolean isOptional, boolean repeated) { |
aoqi@0 | 133 | if(isOptional) |
aoqi@0 | 134 | o.minOccurs(0); |
aoqi@0 | 135 | if(repeated) |
aoqi@0 | 136 | o.maxOccurs("unbounded"); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | /** |
aoqi@0 | 140 | * Represents a terminal tree node, such as element, wildcard, etc. |
aoqi@0 | 141 | */ |
aoqi@0 | 142 | abstract static class Term extends Tree { |
aoqi@0 | 143 | boolean isNullable() { |
aoqi@0 | 144 | return false; |
aoqi@0 | 145 | } |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | /** |
aoqi@0 | 149 | * "T?" |
aoqi@0 | 150 | */ |
aoqi@0 | 151 | private static final class Optional extends Tree { |
aoqi@0 | 152 | private final Tree body; |
aoqi@0 | 153 | |
aoqi@0 | 154 | private Optional(Tree body) { |
aoqi@0 | 155 | this.body = body; |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | @Override |
aoqi@0 | 159 | boolean isNullable() { |
aoqi@0 | 160 | return true; |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | @Override |
aoqi@0 | 164 | Tree makeOptional(boolean really) { |
aoqi@0 | 165 | return this; |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | @Override |
aoqi@0 | 169 | protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) { |
aoqi@0 | 170 | body.write(parent,true,repeated); |
aoqi@0 | 171 | } |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | /** |
aoqi@0 | 175 | * "T+" |
aoqi@0 | 176 | */ |
aoqi@0 | 177 | private static final class Repeated extends Tree { |
aoqi@0 | 178 | private final Tree body; |
aoqi@0 | 179 | |
aoqi@0 | 180 | private Repeated(Tree body) { |
aoqi@0 | 181 | this.body = body; |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | @Override |
aoqi@0 | 185 | boolean isNullable() { |
aoqi@0 | 186 | return body.isNullable(); |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | @Override |
aoqi@0 | 190 | Tree makeRepeated(boolean really) { |
aoqi@0 | 191 | return this; |
aoqi@0 | 192 | } |
aoqi@0 | 193 | |
aoqi@0 | 194 | @Override |
aoqi@0 | 195 | protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) { |
aoqi@0 | 196 | body.write(parent,isOptional,true); |
aoqi@0 | 197 | } |
aoqi@0 | 198 | } |
aoqi@0 | 199 | |
aoqi@0 | 200 | /** |
aoqi@0 | 201 | * "S|T", "S,T", and "S&T". |
aoqi@0 | 202 | */ |
aoqi@0 | 203 | private static final class Group extends Tree { |
aoqi@0 | 204 | private final GroupKind kind; |
aoqi@0 | 205 | private final Tree[] children; |
aoqi@0 | 206 | |
aoqi@0 | 207 | private Group(GroupKind kind, Tree... children) { |
aoqi@0 | 208 | this.kind = kind; |
aoqi@0 | 209 | this.children = children; |
aoqi@0 | 210 | } |
aoqi@0 | 211 | |
aoqi@0 | 212 | @Override |
aoqi@0 | 213 | boolean canBeTopLevel() { |
aoqi@0 | 214 | return true; |
aoqi@0 | 215 | } |
aoqi@0 | 216 | |
aoqi@0 | 217 | @Override |
aoqi@0 | 218 | boolean isNullable() { |
aoqi@0 | 219 | if(kind== GroupKind.CHOICE) { |
aoqi@0 | 220 | for (Tree t : children) { |
aoqi@0 | 221 | if(t.isNullable()) |
aoqi@0 | 222 | return true; |
aoqi@0 | 223 | } |
aoqi@0 | 224 | return false; |
aoqi@0 | 225 | } else { |
aoqi@0 | 226 | for (Tree t : children) { |
aoqi@0 | 227 | if(!t.isNullable()) |
aoqi@0 | 228 | return false; |
aoqi@0 | 229 | } |
aoqi@0 | 230 | return true; |
aoqi@0 | 231 | } |
aoqi@0 | 232 | } |
aoqi@0 | 233 | |
aoqi@0 | 234 | @Override |
aoqi@0 | 235 | protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) { |
aoqi@0 | 236 | Particle c = kind.write(parent); |
aoqi@0 | 237 | writeOccurs(c,isOptional,repeated); |
aoqi@0 | 238 | |
aoqi@0 | 239 | for (Tree child : children) { |
aoqi@0 | 240 | child.write(c,false,false); |
aoqi@0 | 241 | } |
aoqi@0 | 242 | } |
aoqi@0 | 243 | } |
aoqi@0 | 244 | } |