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 javax.xml.namespace.QName;
040:
041: import com.sun.tools.xjc.model.CAttributePropertyInfo;
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.Multiplicity;
046: import com.sun.tools.xjc.reader.RawTypeSet;
047: import com.sun.xml.bind.v2.model.core.ID;
048:
049: import org.kohsuke.rngom.digested.DAttributePattern;
050: import org.kohsuke.rngom.digested.DChoicePattern;
051: import org.kohsuke.rngom.digested.DMixedPattern;
052: import org.kohsuke.rngom.digested.DOneOrMorePattern;
053: import org.kohsuke.rngom.digested.DOptionalPattern;
054: import org.kohsuke.rngom.digested.DPattern;
055: import org.kohsuke.rngom.digested.DPatternWalker;
056: import org.kohsuke.rngom.digested.DZeroOrMorePattern;
057:
058: import static com.sun.tools.xjc.model.CElementPropertyInfo.CollectionMode.REPEATED_ELEMENT;
059:
060: /**
061: * Recursively visits {@link DPattern} and
062: * decides which patterns to map to properties.
063: *
064: * @author Kohsuke Kawaguchi
065: */
066: final class ContentModelBinder extends DPatternWalker {
067: private final RELAXNGCompiler compiler;
068: private final CClassInfo clazz;
069:
070: private boolean insideOptional = false;
071: private int iota = 1;
072:
073: public ContentModelBinder(RELAXNGCompiler compiler, CClassInfo clazz) {
074: this .compiler = compiler;
075: this .clazz = clazz;
076: }
077:
078: public Void onMixed(DMixedPattern p) {
079: throw new UnsupportedOperationException();
080: }
081:
082: public Void onChoice(DChoicePattern p) {
083: boolean old = insideOptional;
084: insideOptional = true;
085: super .onChoice(p);
086: insideOptional = old;
087: return null;
088: }
089:
090: public Void onOptional(DOptionalPattern p) {
091: boolean old = insideOptional;
092: insideOptional = true;
093: super .onOptional(p);
094: insideOptional = old;
095: return null;
096: }
097:
098: public Void onZeroOrMore(DZeroOrMorePattern p) {
099: return onRepeated(p, true);
100: }
101:
102: public Void onOneOrMore(DOneOrMorePattern p) {
103: return onRepeated(p, insideOptional);
104:
105: }
106:
107: private Void onRepeated(DPattern p, boolean optional) {
108: RawTypeSet rts = RawTypeSetBuilder.build(compiler, p,
109: optional ? Multiplicity.STAR : Multiplicity.PLUS);
110: if (rts.canBeTypeRefs == RawTypeSet.Mode.SHOULD_BE_TYPEREF) {
111: CElementPropertyInfo prop = new CElementPropertyInfo(
112: calcName(p), REPEATED_ELEMENT, ID.NONE, null, null,
113: null, p.getLocation(), !optional);
114: rts.addTo(prop);
115: clazz.addProperty(prop);
116: } else {
117: CReferencePropertyInfo prop = new CReferencePropertyInfo(
118: calcName(p), true, false/*TODO*/, null, null, p
119: .getLocation());
120: rts.addTo(prop);
121: clazz.addProperty(prop);
122: }
123:
124: return null;
125: }
126:
127: public Void onAttribute(DAttributePattern p) {
128: // TODO: support multiple names
129: QName name = p.getName().listNames().iterator().next();
130:
131: CAttributePropertyInfo ap = new CAttributePropertyInfo(
132: calcName(p), null, null/*TODO*/, p.getLocation(),
133: name, p.getChild().accept(compiler.typeUseBinder),
134: null, !insideOptional);
135: clazz.addProperty(ap);
136:
137: return null;
138: }
139:
140: private String calcName(DPattern p) {
141: // TODO
142: return "field" + (iota++);
143: }
144: }
|