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 com.sun.tools.xjc.model.CBuiltinLeafInfo;
040: import com.sun.tools.xjc.model.TypeUse;
041: import com.sun.tools.xjc.model.TypeUseFactory;
042:
043: import org.kohsuke.rngom.digested.DAttributePattern;
044: import org.kohsuke.rngom.digested.DChoicePattern;
045: import org.kohsuke.rngom.digested.DContainerPattern;
046: import org.kohsuke.rngom.digested.DDataPattern;
047: import org.kohsuke.rngom.digested.DElementPattern;
048: import org.kohsuke.rngom.digested.DEmptyPattern;
049: import org.kohsuke.rngom.digested.DGrammarPattern;
050: import org.kohsuke.rngom.digested.DGroupPattern;
051: import org.kohsuke.rngom.digested.DInterleavePattern;
052: import org.kohsuke.rngom.digested.DListPattern;
053: import org.kohsuke.rngom.digested.DMixedPattern;
054: import org.kohsuke.rngom.digested.DNotAllowedPattern;
055: import org.kohsuke.rngom.digested.DOneOrMorePattern;
056: import org.kohsuke.rngom.digested.DOptionalPattern;
057: import org.kohsuke.rngom.digested.DPattern;
058: import org.kohsuke.rngom.digested.DPatternVisitor;
059: import org.kohsuke.rngom.digested.DRefPattern;
060: import org.kohsuke.rngom.digested.DTextPattern;
061: import org.kohsuke.rngom.digested.DValuePattern;
062: import org.kohsuke.rngom.digested.DZeroOrMorePattern;
063:
064: /**
065: * Walks the pattern tree and binds it to a {@link TypeUse}.
066: *
067: * The singleton instance is kept in {@link RELAXNGCompiler}.
068: *
069: * TODO: I should really normalize before process.
070: *
071: * @author Kohsuke Kawaguchi
072: */
073: final class TypeUseBinder implements DPatternVisitor<TypeUse> {
074: private final RELAXNGCompiler compiler;
075:
076: public TypeUseBinder(RELAXNGCompiler compiler) {
077: this .compiler = compiler;
078: }
079:
080: public TypeUse onGrammar(DGrammarPattern p) {
081: return CBuiltinLeafInfo.STRING;
082: }
083:
084: public TypeUse onChoice(DChoicePattern p) {
085: // can't support unions
086: return CBuiltinLeafInfo.STRING;
087: }
088:
089: public TypeUse onData(DDataPattern p) {
090: return onDataType(p.getDatatypeLibrary(), p.getType());
091: }
092:
093: public TypeUse onValue(DValuePattern p) {
094: return onDataType(p.getDatatypeLibrary(), p.getType());
095: }
096:
097: private TypeUse onDataType(String datatypeLibrary, String type) {
098: DatatypeLib lib = compiler.datatypes.get(datatypeLibrary);
099: if (lib != null) {
100: TypeUse use = lib.get(type);
101: if (use != null)
102: return use;
103: }
104:
105: // unknown
106: return CBuiltinLeafInfo.STRING;
107: }
108:
109: public TypeUse onInterleave(DInterleavePattern p) {
110: return onContainer(p);
111: }
112:
113: public TypeUse onGroup(DGroupPattern p) {
114: return onContainer(p);
115: }
116:
117: private TypeUse onContainer(DContainerPattern p) {
118: TypeUse t = null;
119: for (DPattern child : p) {
120: TypeUse s = child.accept(this );
121: if (t != null && t != s)
122: return CBuiltinLeafInfo.STRING; // heterogenous
123: t = s;
124: }
125: return t;
126: }
127:
128: public TypeUse onNotAllowed(DNotAllowedPattern p) {
129: // TODO
130: return error();
131: }
132:
133: public TypeUse onEmpty(DEmptyPattern p) {
134: return CBuiltinLeafInfo.STRING;
135: }
136:
137: public TypeUse onList(DListPattern p) {
138: return p.getChild().accept(this );
139: }
140:
141: public TypeUse onOneOrMore(DOneOrMorePattern p) {
142: return TypeUseFactory.makeCollection(p.getChild().accept(this ));
143: }
144:
145: public TypeUse onZeroOrMore(DZeroOrMorePattern p) {
146: return TypeUseFactory.makeCollection(p.getChild().accept(this ));
147: }
148:
149: public TypeUse onOptional(DOptionalPattern p) {
150: return CBuiltinLeafInfo.STRING;
151: }
152:
153: public TypeUse onRef(DRefPattern p) {
154: // TODO: check for enums
155: return p.getTarget().getPattern().accept(this );
156: }
157:
158: public TypeUse onText(DTextPattern p) {
159: return CBuiltinLeafInfo.STRING;
160: }
161:
162: //
163: //
164: // Not allowed in this context
165: //
166: //
167: public TypeUse onAttribute(DAttributePattern p) {
168: return error();
169: }
170:
171: public TypeUse onElement(DElementPattern p) {
172: return error();
173: }
174:
175: public TypeUse onMixed(DMixedPattern p) {
176: return error();
177: }
178:
179: private TypeUse error() {
180: throw new IllegalStateException();
181: }
182: }
|