1 /*
   2  * Copyright (c) 2004, 2018, 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 boolean required = false;
  45     private Object previousValue;
  46 
  47     public ColumnFormat(int number) {
  48         super("Column" + number);
  49         this.number = number;
  50     }
  51 
  52     /*
  53      * method to apply various validation rules to the ColumnFormat object.
  54      */
  55     public void validate() throws ParserException {
  56 
  57         // if we allow column spanning, then this method must change. it
  58         // should allow null data statments
  59 
  60         if (expression == null) {
  61             // current policy is that a data statement must be specified
  62             throw new ParserException("Missing data statement in column " + number);
  63         }
  64         if (header == null) {
  65             // current policy is that if a header is not specified, then we
  66             // will use the last component of the name as the header and
  67             // insert the default anchor characters for center alignment..
  68             throw new ParserException("Missing header statement in column " + number);
  69         }
  70         if (format == null) {
  71             // if no formating is specified, then the format is set to output
  72             // the raw data.
  73             format="0";
  74         }
  75 
  76         // Adjust required flag
  77         expression.setRequired(required);
  78     }
  79 
  80     public void setWidth(int width) {
  81         this.width = width;
  82     }
  83 
  84     public void setAlignment(Alignment align) {
  85         this.align = align;
  86     }
  87 
  88     public void setScale(Scale scale) {
  89         this.scale = scale;
  90     }
  91 
  92     public void setFormat(String format) {
  93         this.format = format;
  94     }
  95 
  96     public void setHeader(String header) {
  97         this.header = header;
  98     }
  99 
 100     public String getHeader() {
 101         return header;
 102     }
 103 
 104     public String getFormat() {
 105         return format;
 106     }
 107 
 108     public int getWidth() {
 109         return width;
 110     }
 111 
 112     public Alignment getAlignment() {
 113         return align;
 114     }
 115 
 116     public Scale getScale() {
 117         return scale;
 118     }
 119 
 120     public Expression getExpression() {
 121         return expression;
 122     }
 123 
 124     public void setExpression(Expression e) {
 125         this.expression = e;
 126     }
 127 
 128     public void setRequired(boolean r) {
 129         this.required = r;
 130     }
 131 
 132     public boolean isRequired() {
 133         return this.required;
 134     }
 135 
 136     public void setPreviousValue(Object o) {
 137         this.previousValue = o;
 138     }
 139 
 140     public Object getPreviousValue() {
 141         return previousValue;
 142     }
 143 
 144     public void printFormat(int indentLevel) {
 145         String indentAmount = "  ";
 146 
 147         StringBuilder indent = new StringBuilder("");
 148         for (int j = 0; j < indentLevel; j++) {
 149             indent.append(indentAmount);
 150         }
 151 
 152         System.out.println(indent + name + " {");
 153         System.out.println(indent + indentAmount + "name=" + name
 154                 + ";data=" + expression.toString() + ";header=" + header
 155                 + ";format=" + format + ";width=" + width
 156                 + ";scale=" + scale.toString() + ";align=" + align.toString()
 157                 + ";required=" + required);
 158 
 159         for (Iterator<OptionFormat> i = children.iterator();  i.hasNext(); /* empty */) {
 160             OptionFormat of = i.next();
 161             of.printFormat(indentLevel+1);
 162         }
 163 
 164         System.out.println(indent + "}");
 165     }
 166 
 167     public String getValue() {
 168         return null;
 169     }
 170 }