001: /*
002: * @(#)Node.java 1.13 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: abstract class Node {
033:
034: String kind;
035: List components;
036: int lineno;
037: List commentList = new ArrayList();
038: Node parent = null;
039: Context context;
040:
041: static final int maxStructIndent = 5;
042: static int structIndent = 0;
043:
044: abstract void document(PrintWriter writer);
045:
046: void set(String kind, List components, int lineno) {
047: this .kind = kind;
048: this .components = components;
049: this .lineno = lineno;
050: }
051:
052: void parentAndExtractComments() {
053: for (Iterator it = components.iterator(); it.hasNext();) {
054: Node node = (Node) it.next();
055: if (node instanceof CommentNode) {
056: it.remove();
057: commentList.add(((CommentNode) node).text());
058: } else {
059: node.parent = this ;
060: node.parentAndExtractComments();
061: }
062: }
063: }
064:
065: void prune() {
066: for (Iterator it = components.iterator(); it.hasNext();) {
067: Node node = (Node) it.next();
068: node.prune();
069: }
070: }
071:
072: void constrain(Context ctx) {
073: context = ctx;
074: for (Iterator it = components.iterator(); it.hasNext();) {
075: Node node = (Node) it.next();
076: constrainComponent(ctx, node);
077: }
078: }
079:
080: void constrainComponent(Context ctx, Node node) {
081: node.constrain(ctx);
082: }
083:
084: void indent(PrintWriter writer, int depth) {
085: for (int i = depth; i > 0; --i) {
086: writer.print(" ");
087: }
088: }
089:
090: void documentIndex(PrintWriter writer) {
091: }
092:
093: void docRowStart(PrintWriter writer) {
094: writer.println("<tr>");
095: if (structIndent > 0) {
096: writer.println("<td colspan=" + structIndent + ">");
097: }
098: }
099:
100: String comment() {
101: StringBuffer comment = new StringBuffer();
102: for (Iterator it = commentList.iterator(); it.hasNext();) {
103: comment.append((String) it.next());
104: }
105: return comment.toString();
106: }
107:
108: void genJavaComment(PrintWriter writer, int depth) {
109: if (commentList.size() > 0) {
110: indent(writer, depth);
111: writer.println("/**");
112: for (Iterator it = commentList.iterator(); it.hasNext();) {
113: indent(writer, depth);
114: writer.println(" * " + (String) it.next());
115: }
116: indent(writer, depth);
117: writer.println(" */");
118: }
119: }
120:
121: String javaType() {
122: return "-- WRONG ---";
123: }
124:
125: void genJava(PrintWriter writer, int depth) {
126: for (Iterator it = components.iterator(); it.hasNext();) {
127: Node node = (Node) it.next();
128: node.genJava(writer, depth);
129: }
130: }
131:
132: void genCInclude(PrintWriter writer) {
133: for (Iterator it = components.iterator(); it.hasNext();) {
134: Node node = (Node) it.next();
135: node.genCInclude(writer);
136: }
137: }
138:
139: String debugValue(String label) {
140: return label;
141: }
142:
143: void genJavaDebugWrite(PrintWriter writer, int depth,
144: String writeLabel) {
145: genJavaDebugWrite(writer, depth, writeLabel,
146: debugValue(writeLabel));
147: }
148:
149: void genJavaDebugWrite(PrintWriter writer, int depth,
150: String writeLabel, String displayValue) {
151: if (!Main.genDebug) {
152: return;
153: }
154: indent(writer, depth);
155: writer
156: .println("if ((ps.vm.traceFlags & VirtualMachineImpl.TRACE_SENDS) != 0) {");
157: indent(writer, depth + 1);
158: writer.print("ps.vm.printTrace(\"Sending: ");
159: indent(writer, depth); // this is inside the quotes
160: writer.print(writeLabel + "(" + javaType() + "): \" + ");
161: writer.println(displayValue + ");");
162: indent(writer, depth);
163: writer.println("}");
164: }
165:
166: public void genJavaRead(PrintWriter writer, int depth,
167: String readLabel) {
168: error("Internal - Should not call Node.genJavaRead()");
169: }
170:
171: void genJavaDebugRead(PrintWriter writer, int depth,
172: String readLabel, String displayValue) {
173: if (!Main.genDebug) {
174: return;
175: }
176: indent(writer, depth);
177: writer.println("if (vm.traceReceives) {");
178: indent(writer, depth + 1);
179: writer.print("vm.printReceiveTrace(" + depth + ", \"");
180: writer.print(readLabel + "(" + javaType() + "): \" + ");
181: writer.println(displayValue + ");");
182: indent(writer, depth);
183: writer.println("}");
184: }
185:
186: void genJavaPreDef(PrintWriter writer, int depth) {
187: for (Iterator it = components.iterator(); it.hasNext();) {
188: Node node = (Node) it.next();
189: node.genJavaPreDef(writer, depth);
190: }
191: }
192:
193: void error(String errmsg) {
194: System.err.println();
195: System.err.println(Main.specSource + ":" + lineno + ": " + kind
196: + " - " + errmsg);
197: System.err.println();
198: System.exit(1);
199: }
200: }
|