001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.xjc.reader.relaxng;
038:
039: import java.util.HashSet;
040: import java.util.Set;
041: import java.util.Stack;
042:
043: import org.kohsuke.rngom.digested.DAttributePattern;
044: import org.kohsuke.rngom.digested.DChoicePattern;
045: import org.kohsuke.rngom.digested.DDefine;
046: import org.kohsuke.rngom.digested.DListPattern;
047: import org.kohsuke.rngom.digested.DMixedPattern;
048: import org.kohsuke.rngom.digested.DOneOrMorePattern;
049: import org.kohsuke.rngom.digested.DOptionalPattern;
050: import org.kohsuke.rngom.digested.DPatternWalker;
051: import org.kohsuke.rngom.digested.DRefPattern;
052: import org.kohsuke.rngom.digested.DZeroOrMorePattern;
053:
054: /**
055: * Fumigate the named patterns that can be bound to inheritance.
056: *
057: * @author Kohsuke Kawaguchi
058: */
059: final class TypePatternBinder extends DPatternWalker {
060: private boolean canInherit;
061: private final Stack<Boolean> stack = new Stack<Boolean>();
062:
063: /**
064: * Patterns that are determined not to be bindable to inheritance.
065: */
066: private final Set<DDefine> cannotBeInherited = new HashSet<DDefine>();
067:
068: void reset() {
069: canInherit = true;
070: stack.clear();
071: }
072:
073: public Void onRef(DRefPattern p) {
074: if (!canInherit) {
075: cannotBeInherited.add(p.getTarget());
076: } else {
077: // if the whole pattern is like "A,B", we can only inherit from
078: // either A or B. For now, always derive from A.
079: // it might be worthwhile to have a smarter binding logic where
080: // we pick A and B based on their 'usefulness' --- by taking into
081: // account how many other paterns are derived from those.
082: canInherit = false;
083: }
084: return null;
085: }
086:
087: /*
088: Set the flag to false if we hit a pattern that cannot include
089: a <ref> to be bound as an inheritance.
090:
091: All the following code are the same
092: */
093: public Void onChoice(DChoicePattern p) {
094: push(false);
095: super .onChoice(p);
096: pop();
097: return null;
098: }
099:
100: public Void onAttribute(DAttributePattern p) {
101: push(false);
102: super .onAttribute(p);
103: pop();
104: return null;
105: }
106:
107: public Void onList(DListPattern p) {
108: push(false);
109: super .onList(p);
110: pop();
111: return null;
112: }
113:
114: public Void onMixed(DMixedPattern p) {
115: push(false);
116: super .onMixed(p);
117: pop();
118: return null;
119: }
120:
121: public Void onOneOrMore(DOneOrMorePattern p) {
122: push(false);
123: super .onOneOrMore(p);
124: pop();
125: return null;
126: }
127:
128: public Void onZeroOrMore(DZeroOrMorePattern p) {
129: push(false);
130: super .onZeroOrMore(p);
131: pop();
132: return null;
133: }
134:
135: public Void onOptional(DOptionalPattern p) {
136: push(false);
137: super .onOptional(p);
138: pop();
139: return null;
140: }
141:
142: private void push(boolean v) {
143: stack.push(canInherit);
144: canInherit = v;
145: }
146:
147: private void pop() {
148: canInherit = stack.pop();
149: }
150: }
|