01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.tools.internal.xjc.reader.xmlschema;
27:
28: import com.sun.tools.internal.xjc.model.Multiplicity;
29: import com.sun.xml.internal.xsom.XSElementDecl;
30: import com.sun.xml.internal.xsom.XSModelGroup;
31: import com.sun.xml.internal.xsom.XSModelGroupDecl;
32: import com.sun.xml.internal.xsom.XSParticle;
33: import com.sun.xml.internal.xsom.XSWildcard;
34: import com.sun.xml.internal.xsom.visitor.XSTermFunction;
35:
36: import static com.sun.tools.internal.xjc.model.Multiplicity.ONE;
37: import static com.sun.tools.internal.xjc.model.Multiplicity.ZERO;
38:
39: /**
40: * Counts {@link Multiplicity} for a particle/term.
41: *
42: * @author Kohsuke Kawaguchi
43: */
44: public final class MultiplicityCounter implements
45: XSTermFunction<Multiplicity> {
46:
47: public static final MultiplicityCounter theInstance = new MultiplicityCounter();
48:
49: private MultiplicityCounter() {
50: }
51:
52: public Multiplicity particle(XSParticle p) {
53: Multiplicity m = p.getTerm().apply(this );
54:
55: Integer max;
56: if (m.max == null || p.getMaxOccurs() == XSParticle.UNBOUNDED)
57: max = null;
58: else
59: max = p.getMaxOccurs();
60:
61: return Multiplicity.multiply(m, Multiplicity.create(p
62: .getMinOccurs(), max));
63: }
64:
65: public Multiplicity wildcard(XSWildcard wc) {
66: return ONE;
67: }
68:
69: public Multiplicity modelGroupDecl(XSModelGroupDecl decl) {
70: return modelGroup(decl.getModelGroup());
71: }
72:
73: public Multiplicity modelGroup(XSModelGroup group) {
74: boolean isChoice = group.getCompositor() == XSModelGroup.CHOICE;
75:
76: Multiplicity r = ZERO;
77:
78: for (XSParticle p : group.getChildren()) {
79: Multiplicity m = particle(p);
80:
81: if (r == null) {
82: r = m;
83: continue;
84: }
85: if (isChoice) {
86: r = Multiplicity.choice(r, m);
87: } else {
88: r = Multiplicity.group(r, m);
89: }
90: }
91: return r;
92: }
93:
94: public Multiplicity elementDecl(XSElementDecl decl) {
95: return ONE;
96: }
97: }
|