001: /*
002: * Copyright 2004 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 sun.tools.jstat;
027:
028: import java.util.*;
029:
030: /**
031: * A class to represent the format for a column of data.
032: *
033: * @author Brian Doherty
034: * @version 1.8, 05/05/07
035: * @since 1.5
036: */
037: public class ColumnFormat extends OptionFormat {
038: private int number;
039: private int width;
040: private Alignment align = Alignment.CENTER;
041: private Scale scale = Scale.RAW;
042: private String format;
043: private String header;
044: private Expression expression;
045: private Object previousValue;
046:
047: public ColumnFormat(int number) {
048: super ("Column" + number);
049: this .number = number;
050: }
051:
052: /*
053: * method to apply various validation rules to the ColumnFormat object.
054: */
055: public void validate() throws ParserException {
056:
057: // if we allow column spanning, then this method must change. it
058: // should allow null data statments
059:
060: if (expression == null) {
061: // current policy is that a data statment must be specified
062: throw new ParserException(
063: "Missing data statement in column " + number);
064: }
065: if (header == null) {
066: // current policy is that if a header is not specified, then we
067: // will use the last component of the name as the header and
068: // insert the default anchor characters for center alignment..
069: throw new ParserException(
070: "Missing header statement in column " + number);
071: }
072: if (format == null) {
073: // if no formating is specified, then the format is set to output
074: // the raw data.
075: format = "0";
076: }
077: }
078:
079: public void setWidth(int width) {
080: this .width = width;
081: }
082:
083: public void setAlignment(Alignment align) {
084: this .align = align;
085: }
086:
087: public void setScale(Scale scale) {
088: this .scale = scale;
089: }
090:
091: public void setFormat(String format) {
092: this .format = format;
093: }
094:
095: public void setHeader(String header) {
096: this .header = header;
097: }
098:
099: public String getHeader() {
100: return header;
101: }
102:
103: public String getFormat() {
104: return format;
105: }
106:
107: public int getWidth() {
108: return width;
109: }
110:
111: public Alignment getAlignment() {
112: return align;
113: }
114:
115: public Scale getScale() {
116: return scale;
117: }
118:
119: public Expression getExpression() {
120: return expression;
121: }
122:
123: public void setExpression(Expression e) {
124: this .expression = e;
125: }
126:
127: public void setPreviousValue(Object o) {
128: this .previousValue = o;
129: }
130:
131: public Object getPreviousValue() {
132: return previousValue;
133: }
134:
135: public void printFormat(int indentLevel) {
136: String indentAmount = " ";
137:
138: StringBuilder indent = new StringBuilder("");
139: for (int j = 0; j < indentLevel; j++) {
140: indent.append(indentAmount);
141: }
142:
143: System.out.println(indent + name + " {");
144: System.out.println(indent + indentAmount + "name=" + name
145: + ";data=" + expression.toString() + ";header="
146: + header + ";format=" + format + ";width=" + width
147: + ";scale=" + scale.toString() + ";align="
148: + align.toString());
149:
150: for (Iterator i = children.iterator(); i.hasNext(); /* empty */) {
151: OptionFormat of = (OptionFormat) i.next();
152: of.printFormat(indentLevel + 1);
153: }
154:
155: System.out.println(indent + "}");
156: }
157:
158: public String getValue() {
159: return null;
160: }
161: }
|