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.tools.internal.txw2.model.prop.Prop;
030: import com.sun.xml.internal.txw2.TypedXmlWriter;
031:
032: import java.util.HashSet;
033: import java.util.Set;
034:
035: /**
036: * A named pattern.
037: *
038: * @author Kohsuke Kawaguchi
039: */
040: public class Define extends WriterNode {
041: public final Grammar scope;
042: public final String name;
043:
044: JDefinedClass clazz;
045:
046: public Define(Grammar scope, String name) {
047: super (null, null);
048: if (scope == null)
049: scope = (Grammar) this ; // hack for start pattern
050: this .scope = scope;
051: this .name = name;
052: assert name != null;
053: }
054:
055: /**
056: * Returns true if this define only contains
057: * one child (and thus considered inlinable.)
058: *
059: * A pattern definition is also inlineable if
060: * it's the start of the grammar (because "start" isn't a meaningful name)
061: */
062: public boolean isInline() {
063: return hasOneChild() || name == Grammar.START;
064: }
065:
066: void declare(NodeSet nset) {
067: if (isInline())
068: return;
069:
070: clazz = nset.createClass(name);
071: clazz._implements (TypedXmlWriter.class);
072: }
073:
074: void generate(NodeSet nset) {
075: if (clazz == null)
076: return;
077:
078: HashSet<Prop> props = new HashSet<Prop>();
079: for (Leaf l : this )
080: l.generate(clazz, nset, props);
081: }
082:
083: void generate(JDefinedClass clazz, NodeSet nset, Set<Prop> props) {
084: if (isInline()) {
085: for (Leaf l : this )
086: l.generate(clazz, nset, props);
087: } else {
088: assert this .clazz != null;
089: clazz._implements (this .clazz);
090: }
091: }
092:
093: void prepare(NodeSet nset) {
094: if (isInline() && leaf instanceof WriterNode
095: && !name.equals(Grammar.START))
096: ((WriterNode) leaf).alternativeName = name;
097: }
098:
099: public String toString() {
100: return "Define " + name;
101: }
102: }
|