001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.txw2.model;
027:
028: import com.sun.codemodel.JDefinedClass;
029: import com.sun.codemodel.JMethod;
030: import com.sun.codemodel.JMod;
031: import com.sun.codemodel.JType;
032: import com.sun.tools.internal.txw2.model.prop.Prop;
033: import com.sun.tools.internal.txw2.model.prop.ValueProp;
034: import com.sun.xml.internal.txw2.annotation.XmlValue;
035: import org.kohsuke.rngom.ast.om.ParsedPattern;
036: import org.xml.sax.Locator;
037:
038: import java.util.Iterator;
039: import java.util.Set;
040:
041: /**
042: * {@link Leaf}s form a set (by a cyclic doubly-linked list.)
043: *
044: * @author Kohsuke Kawaguchi
045: */
046: public abstract class Leaf implements ParsedPattern {
047: private Leaf next;
048: private Leaf prev;
049:
050: /**
051: * Source location where this leaf was defined.
052: */
053: public Locator location;
054:
055: protected Leaf(Locator location) {
056: this .location = location;
057: prev = next = this ;
058: }
059:
060: public final Leaf getNext() {
061: assert next != null;
062: assert next.prev == this ;
063: return next;
064: }
065:
066: public final Leaf getPrev() {
067: assert prev != null;
068: assert prev.next == this ;
069: return prev;
070: }
071:
072: /**
073: * Combines two sets into one set.
074: *
075: * @return this
076: */
077: public final Leaf merge(Leaf that) {
078: Leaf n1 = this .next;
079: Leaf n2 = that.next;
080:
081: that.next = n1;
082: that.next.prev = that;
083: this .next = n2;
084: this .next.prev = this ;
085:
086: return this ;
087: }
088:
089: /**
090: * Returns the collection of all the siblings
091: * (including itself)
092: */
093: public final Iterable<Leaf> siblings() {
094: return new Iterable<Leaf>() {
095: public Iterator<Leaf> iterator() {
096: return new CycleIterator(Leaf.this );
097: }
098: };
099: }
100:
101: /**
102: * Populate the body of the writer class.
103: *
104: * @param props
105: * captures the generatesd {@link Prop}s to
106: */
107: abstract void generate(JDefinedClass clazz, NodeSet nset,
108: Set<Prop> props);
109:
110: /**
111: * Creates a prop of the data value method.
112: */
113: protected final void createDataMethod(JDefinedClass clazz,
114: JType valueType, NodeSet nset, Set<Prop> props) {
115: if (!props.add(new ValueProp(valueType)))
116: return;
117:
118: JMethod m = clazz.method(JMod.PUBLIC,
119: nset.opts.chainMethod ? (JType) clazz
120: : nset.codeModel.VOID, "_text");
121: m.annotate(XmlValue.class);
122: m.param(valueType, "value");
123: }
124: }
|