1 /*
   2  * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.jstat;
  27 
  28 import java.util.*;
  29 
  30 /**
  31  * A class to represent the format for a column of data.
  32  *
  33  * @author Brian Doherty
  34  * @since 1.5
  35  */
  36 public class ColumnFormat extends OptionFormat {
  37     private int number;
  38     private int width;
  39     private Alignment align = Alignment.CENTER;
  40     private Scale scale = Scale.RAW;
  41     private String format;
  42     private String header;
  43     private Expression expression;
  44     private Object previousValue;
  45 
  46     public ColumnFormat(int number) {
  47         super("Column" + number);
  48         this.number = number;
  49     }
  50 
  51     /*
  52      * method to apply various validation rules to the ColumnFormat object.
  53      */
  54     public void validate() throws ParserException {
  55 
  56         // if we allow column spanning, then this method must change. it
  57         // should allow null data statments
  58 
  59         if (expression == null) {
  60             // current policy is that a data statement must be specified
  61             throw new ParserException("Missing data statement in column " + number);
  62         }
  63         if (header == null) {
  64             // current policy is that if a header is not specified, then we
  65             // will use the last component of the name as the header and
  66             // insert the default anchor characters for center alignment..
  67             throw new ParserException("Missing header statement in column " + number);
  68         }
  69         if (format == null) {
  70             // if no formating is specified, then the format is set to output
  71             // the raw data.
  72             format="0";
  73         }
  74     }
  75 
  76     public void setWidth(int width) {
  77         this.width = width;
  78     }
  79 
  80     public void setAlignment(Alignment align) {
  81         this.align = align;
  82     }
  83 
  84     public void setScale(Scale scale) {
  85         this.scale = scale;
  86     }
  87 
  88     public void setFormat(String format) {
  89         this.format = format;
  90     }
  91 
  92     public void setHeader(String header) {
  93         this.header = header;
  94     }
  95 
  96     public String getHeader() {
  97         return header;
  98     }
  99 
 100     public String getFormat() {
 101         return format;
 102     }
 103 
 104     public int getWidth() {
 105         return width;
 106     }
 107 
 108     public Alignment getAlignment() {
 109         return align;
 110     }
 111 
 112     public Scale getScale() {
 113         return scale;
 114     }
 115 
 116     public Expression getExpression() {
 117         return expression;
 118     }
 119 
 120     public void setExpression(Expression e) {
 121         this.expression = e;
 122     }
 123 
 124     public void setPreviousValue(Object o) {
 125         this.previousValue = o;
 126     }
 127 
 128     public Object getPreviousValue() {
 129         return previousValue;
 130     }
 131 
 132     public void printFormat(int indentLevel) {
 133         String indentAmount = "  ";
 134 
 135         StringBuilder indent = new StringBuilder("");
 136         for (int j = 0; j < indentLevel; j++) {
 137             indent.append(indentAmount);
 138         }
 139 
 140         System.out.println(indent + name + " {");
 141         System.out.println(indent + indentAmount + "name=" + name
 142                 + ";data=" + expression.toString() + ";header=" + header
 143                 + ";format=" + format + ";width=" + width
 144                 + ";scale=" + scale.toString() + ";align=" + align.toString());
 145 
 146         for (Iterator<OptionFormat> i = children.iterator();  i.hasNext(); /* empty */) {
 147             OptionFormat of = i.next();
 148             of.printFormat(indentLevel+1);
 149         }
 150 
 151         System.out.println(indent + "}");
 152     }
 153 
 154     public String getValue() {
 155         return null;
 156     }
 157 }