001: /*
002: * @(#)RepeatNode.java 1.12 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 RepeatNode extends AbstractTypeNode {
033:
034: Node member;
035:
036: void constrain(Context ctx) {
037: super .constrain(ctx);
038: if (components.size() != 1) {
039: error("Repeat must have exactly one member, use Group for more");
040: }
041: member = (Node) (components.get(0));
042: if (!(member instanceof TypeNode)) {
043: error("Repeat member must be type specifier");
044: }
045: }
046:
047: void document(PrintWriter writer) {
048: docRowStart(writer);
049: writer.println("<td colspan="
050: + (maxStructIndent - structIndent) + ">");
051: writer.println("int<td><i>" + name + "</i><td>" + comment()
052: + " ");
053: docRowStart(writer);
054: writer.println("<td colspan="
055: + (maxStructIndent - structIndent + 2) + ">");
056: writer.println("Repeated <i>" + name + "</i> times:");
057: ++structIndent;
058: member.document(writer);
059: --structIndent;
060: }
061:
062: String docType() {
063: return "-BOGUS-"; // should never call this
064: }
065:
066: String javaType() {
067: return member.javaType() + "[]";
068: }
069:
070: public void genJavaWrite(PrintWriter writer, int depth,
071: String writeLabel) {
072: genJavaDebugWrite(writer, depth, writeLabel, "\"\"");
073: indent(writer, depth);
074: writer.println("ps.writeInt(" + writeLabel + ".length);");
075: indent(writer, depth);
076: writer.println("for (int i = 0; i < " + writeLabel
077: + ".length; i++) {;");
078: ((TypeNode) member).genJavaWrite(writer, depth + 1, writeLabel
079: + "[i]");
080: indent(writer, depth);
081: writer.println("}");
082: }
083:
084: String javaRead() {
085: error("Internal - Should not call RepeatNode.javaRead()");
086: return "";
087: }
088:
089: public void genJavaRead(PrintWriter writer, int depth,
090: String readLabel) {
091: genJavaDebugRead(writer, depth, readLabel, "\"\"");
092: String cntLbl = readLabel + "Count";
093: indent(writer, depth);
094: writer.println("int " + cntLbl + " = ps.readInt();");
095: indent(writer, depth);
096: writer.println(readLabel + " = new " + member.javaType() + "["
097: + cntLbl + "];");
098: indent(writer, depth);
099: writer.println("for (int i = 0; i < " + cntLbl + "; i++) {;");
100: member.genJavaRead(writer, depth + 1, readLabel + "[i]");
101: indent(writer, depth);
102: writer.println("}");
103: }
104: }
|