aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.schemagen; aoqi@0: aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.List; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.schemagen.xmlschema.ContentModelContainer; aoqi@0: import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Particle; aoqi@0: import com.sun.xml.internal.bind.v2.schemagen.xmlschema.TypeDefParticle; aoqi@0: import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Occurs; aoqi@0: aoqi@0: /** aoqi@0: * Normalized representation of the content model. aoqi@0: * aoqi@0: *

aoqi@0: * This is built from bottom up so that we can eliminate redundant constructs, aoqi@0: * and produce the most concise content model definition in XML. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: abstract class Tree { aoqi@0: aoqi@0: /** aoqi@0: * Returns "T?" from "T". aoqi@0: * aoqi@0: * @param really aoqi@0: * if false this method becomes no-op. This is so that we can write aoqi@0: * the caller fluently. aoqi@0: */ aoqi@0: Tree makeOptional(boolean really) { aoqi@0: return really?new Optional(this) :this; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns "T+" from "T". aoqi@0: * aoqi@0: * @param really aoqi@0: * if false this method becomes no-op. This is so that we can write aoqi@0: * the caller fluently. aoqi@0: */ aoqi@0: Tree makeRepeated(boolean really) { aoqi@0: return really?new Repeated(this) :this; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a group tree. aoqi@0: */ aoqi@0: static Tree makeGroup(GroupKind kind, List children ) { aoqi@0: // pointless binary operator. aoqi@0: if(children.size()==1) aoqi@0: return children.get(0); aoqi@0: aoqi@0: // we neither have epsilon or emptySet, so can't handle children.length==0 nicely aoqi@0: aoqi@0: // eliminated nesting groups of the same kind. aoqi@0: // this is where binary tree would have shined. aoqi@0: List normalizedChildren = new ArrayList(children.size()); aoqi@0: for (Tree t : children) { aoqi@0: if (t instanceof Group) { aoqi@0: Group g = (Group) t; aoqi@0: if(g.kind==kind) { aoqi@0: normalizedChildren.addAll(Arrays.asList(g.children)); aoqi@0: continue; aoqi@0: } aoqi@0: } aoqi@0: normalizedChildren.add(t); aoqi@0: } aoqi@0: aoqi@0: return new Group(kind,normalizedChildren.toArray(new Tree[normalizedChildren.size()])); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns true if this tree accepts empty sequence. aoqi@0: */ aoqi@0: abstract boolean isNullable(); aoqi@0: aoqi@0: /** aoqi@0: * Returns true if the top node of this tree can aoqi@0: * appear as a valid top-level content model in XML Schema. aoqi@0: * aoqi@0: *

aoqi@0: * Model groups and occurrences that have model group in it can. aoqi@0: */ aoqi@0: boolean canBeTopLevel() { return false; } aoqi@0: aoqi@0: /** aoqi@0: * Writes out the content model. aoqi@0: * aoqi@0: * Normall this runs recursively until we write out the whole content model. aoqi@0: */ aoqi@0: protected abstract void write(ContentModelContainer parent, boolean isOptional, boolean repeated); aoqi@0: aoqi@0: /** aoqi@0: * Writes inside the given complex type. aoqi@0: */ aoqi@0: protected void write(TypeDefParticle ct) { aoqi@0: if(canBeTopLevel()) aoqi@0: write(ct._cast(ContentModelContainer.class), false, false); aoqi@0: else aoqi@0: // need a dummy wrapper aoqi@0: new Group(GroupKind.SEQUENCE,this).write(ct); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Convenience method to write occurrence constraints. aoqi@0: */ aoqi@0: protected final void writeOccurs(Occurs o, boolean isOptional, boolean repeated) { aoqi@0: if(isOptional) aoqi@0: o.minOccurs(0); aoqi@0: if(repeated) aoqi@0: o.maxOccurs("unbounded"); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Represents a terminal tree node, such as element, wildcard, etc. aoqi@0: */ aoqi@0: abstract static class Term extends Tree { aoqi@0: boolean isNullable() { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * "T?" aoqi@0: */ aoqi@0: private static final class Optional extends Tree { aoqi@0: private final Tree body; aoqi@0: aoqi@0: private Optional(Tree body) { aoqi@0: this.body = body; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: boolean isNullable() { aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: Tree makeOptional(boolean really) { aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) { aoqi@0: body.write(parent,true,repeated); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * "T+" aoqi@0: */ aoqi@0: private static final class Repeated extends Tree { aoqi@0: private final Tree body; aoqi@0: aoqi@0: private Repeated(Tree body) { aoqi@0: this.body = body; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: boolean isNullable() { aoqi@0: return body.isNullable(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: Tree makeRepeated(boolean really) { aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) { aoqi@0: body.write(parent,isOptional,true); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * "S|T", "S,T", and "S&T". aoqi@0: */ aoqi@0: private static final class Group extends Tree { aoqi@0: private final GroupKind kind; aoqi@0: private final Tree[] children; aoqi@0: aoqi@0: private Group(GroupKind kind, Tree... children) { aoqi@0: this.kind = kind; aoqi@0: this.children = children; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: boolean canBeTopLevel() { aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: boolean isNullable() { aoqi@0: if(kind== GroupKind.CHOICE) { aoqi@0: for (Tree t : children) { aoqi@0: if(t.isNullable()) aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } else { aoqi@0: for (Tree t : children) { aoqi@0: if(!t.isNullable()) aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) { aoqi@0: Particle c = kind.write(parent); aoqi@0: writeOccurs(c,isOptional,repeated); aoqi@0: aoqi@0: for (Tree child : children) { aoqi@0: child.write(c,false,false); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }