001: /*
002: * @(#)SelectNode.java 1.11 06/10/25
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.tools.jdwpgen;
028:
029: import java.util.*;
030: import java.io.*;
031:
032: class SelectNode extends AbstractGroupNode implements TypeNode {
033:
034: AbstractSimpleTypeNode typeNode;
035:
036: void prune() {
037: super .prune();
038: Iterator it = components.iterator();
039:
040: if (it.hasNext()) {
041: Node typeNode = (Node) it.next();
042:
043: if (typeNode instanceof ByteTypeNode
044: || typeNode instanceof IntTypeNode) {
045: this .typeNode = (AbstractSimpleTypeNode) typeNode;
046: it.remove();
047: } else {
048: error("Select must be based on 'int' or 'byte'");
049: }
050: } else {
051: error("empty");
052: }
053: }
054:
055: void constrain(Context ctx) {
056: super .constrain(ctx);
057: if (components.size() < 2) {
058: error("Select must have at least two options");
059: }
060: }
061:
062: void constrainComponent(Context ctx, Node node) {
063: node.constrain(ctx);
064: if (!(node instanceof AltNode)) {
065: error("Select must consist of selector followed by Alt items");
066: }
067: }
068:
069: void document(PrintWriter writer) {
070: typeNode.document(writer);
071: super .document(writer);
072: }
073:
074: String docType() {
075: // should never call this
076: error("Internal - called SelectNode.docType()");
077: return null;
078: }
079:
080: String commonBaseClass() {
081: return name() + "Common";
082: }
083:
084: private String commonVar() {
085: return " a" + commonBaseClass();
086: }
087:
088: void genJavaClassSpecifics(PrintWriter writer, int depth) {
089: indent(writer, depth);
090: writer.println("abstract static class " + commonBaseClass()
091: + " {");
092: if (context.isWritingCommand()) {
093: indent(writer, depth + 1);
094: writer.println("abstract void write(PacketStream ps);");
095: } else {
096: indent(writer, depth + 1);
097: writer.println("abstract " + typeNode.javaParam() + "();");
098: }
099: indent(writer, depth);
100: writer.println("}");
101: typeNode.genJavaDeclaration(writer, depth);
102: indent(writer, depth);
103: writer.println(commonBaseClass() + commonVar() + ";");
104: super .genJavaClassSpecifics(writer, depth);
105: }
106:
107: void genJavaClassBodyComponents(PrintWriter writer, int depth) {
108: // don't naively include alt components
109: }
110:
111: void genJavaWritingClassBody(PrintWriter writer, int depth,
112: String className) {
113: writer.println();
114: indent(writer, depth);
115: writer.print(className + "(" + typeNode.javaParam() + ", ");
116: writer.print(commonBaseClass() + commonVar());
117: writer.println(") {");
118: indent(writer, depth + 1);
119: writer.println("this." + typeNode.name() + " = "
120: + typeNode.name() + ";");
121: indent(writer, depth + 1);
122: writer
123: .println("this." + commonVar() + " =" + commonVar()
124: + ";");
125: indent(writer, depth);
126: writer.println("}");
127: }
128:
129: void genJavaWrites(PrintWriter writer, int depth) {
130: typeNode.genJavaWrite(writer, depth, typeNode.name());
131: indent(writer, depth);
132: writer.println(commonVar() + ".write(ps);");
133: }
134:
135: void genJavaReads(PrintWriter writer, int depth) {
136: typeNode.genJavaRead(writer, depth, typeNode.name());
137: indent(writer, depth);
138: writer.println("switch (" + typeNode.name() + ") {");
139: for (Iterator it = components.iterator(); it.hasNext();) {
140: AltNode alt = (AltNode) it.next();
141: alt.genJavaReadsSelectCase(writer, depth + 1, commonVar());
142: }
143: indent(writer, depth);
144: writer.println("}");
145: }
146:
147: public void genJavaDeclaration(PrintWriter writer, int depth) {
148: typeNode.genJavaDeclaration(writer, depth);
149: super .genJavaDeclaration(writer, depth);
150: }
151:
152: public String javaParam() {
153: return typeNode.javaParam() + ", " + name() + " a" + name();
154: }
155: }
|