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:
042: import com.sun.tools.xjc.model.CClassInfo;
043: import com.sun.tools.xjc.model.CElementPropertyInfo;
044: import com.sun.tools.xjc.model.CReferencePropertyInfo;
045: import com.sun.tools.xjc.model.CTypeInfo;
046: import com.sun.tools.xjc.model.CTypeRef;
047: import com.sun.tools.xjc.model.Multiplicity;
048: import com.sun.tools.xjc.reader.RawTypeSet;
049: import com.sun.xml.bind.v2.model.core.ID;
050:
051: import org.kohsuke.rngom.digested.DAttributePattern;
052: import org.kohsuke.rngom.digested.DElementPattern;
053: import org.kohsuke.rngom.digested.DOneOrMorePattern;
054: import org.kohsuke.rngom.digested.DPattern;
055: import org.kohsuke.rngom.digested.DPatternWalker;
056: import org.kohsuke.rngom.digested.DZeroOrMorePattern;
057:
058: /**
059: * Builds {@link RawTypeSet} for RELAX NG.
060: *
061: * @author Kohsuke Kawaguchi
062: */
063: public final class RawTypeSetBuilder extends DPatternWalker {
064: public static RawTypeSet build(RELAXNGCompiler compiler,
065: DPattern contentModel, Multiplicity mul) {
066: RawTypeSetBuilder builder = new RawTypeSetBuilder(compiler, mul);
067: contentModel.accept(builder);
068: return builder.create();
069: }
070:
071: /**
072: * Multiplicity of the property.
073: */
074: private Multiplicity mul;
075:
076: /**
077: * Accumulates discovered {@link RawTypeSet.Ref}s.
078: */
079: private final Set<RawTypeSet.Ref> refs = new HashSet<RawTypeSet.Ref>();
080:
081: private final RELAXNGCompiler compiler;
082:
083: public RawTypeSetBuilder(RELAXNGCompiler compiler, Multiplicity mul) {
084: this .mul = mul;
085: this .compiler = compiler;
086: }
087:
088: private RawTypeSet create() {
089: return new RawTypeSet(refs, mul);
090: }
091:
092: public Void onAttribute(DAttributePattern p) {
093: // attributes form their own properties
094: return null;
095: }
096:
097: public Void onElement(DElementPattern p) {
098: CTypeInfo[] tis = compiler.classes.get(p);
099: if (tis != null) {
100: for (CTypeInfo ti : tis)
101: refs.add(new CClassInfoRef((CClassInfo) ti));
102: } else {
103: // TODO
104: assert false;
105: }
106: return null;
107: }
108:
109: public Void onZeroOrMore(DZeroOrMorePattern p) {
110: mul = mul.makeRepeated();
111: return super .onZeroOrMore(p);
112: }
113:
114: public Void onOneOrMore(DOneOrMorePattern p) {
115: mul = mul.makeRepeated();
116: return super .onOneOrMore(p);
117: }
118:
119: /**
120: * For {@link CClassInfo}s that map to elements.
121: */
122: private static final class CClassInfoRef extends RawTypeSet.Ref {
123: private final CClassInfo ci;
124:
125: CClassInfoRef(CClassInfo ci) {
126: this .ci = ci;
127: assert ci.isElement();
128: }
129:
130: protected ID id() {
131: return ID.NONE;
132: }
133:
134: protected boolean isListOfValues() {
135: return false;
136: }
137:
138: protected RawTypeSet.Mode canBeType(RawTypeSet parent) {
139: return RawTypeSet.Mode.SHOULD_BE_TYPEREF;
140: }
141:
142: protected void toElementRef(CReferencePropertyInfo prop) {
143: prop.getElements().add(ci);
144: }
145:
146: protected CTypeRef toTypeRef(CElementPropertyInfo ep) {
147: return new CTypeRef(ci, ci.getElementName(), ci
148: .getTypeName(), false, null);
149: }
150: }
151: }
|