Home | History | Annotate | Download | only in data
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.clearsilver.jsilver.data;
     18 
     19 import com.google.clearsilver.jsilver.autoescape.EscapeMode;
     20 
     21 import java.io.IOException;
     22 
     23 /**
     24  * This class is meant to hold implementation common to different instances of Data interface.
     25  */
     26 public abstract class AbstractData implements Data {
     27 
     28   protected EscapeMode escapeMode = EscapeMode.ESCAPE_NONE;
     29 
     30   public int getIntValue() {
     31     // If we ever use the integer value of a node to create the string
     32     // representation we must ensure that an empty node is not mistaken
     33     // for a node with the integer value '0'.
     34     return TypeConverter.asNumber(getValue());
     35   }
     36 
     37   public boolean getBooleanValue() {
     38     // If we ever use the boolean value of a node to create the string
     39     // representation we must ensure that an empty node is not mistaken
     40     // for a node with the boolean value 'false'.
     41     return TypeConverter.asBoolean(getValue());
     42   }
     43 
     44   // ******************* Convenience methods *******************
     45 
     46   /**
     47    * Retrieves the value at the specified path in this HDF node's subtree.
     48    *
     49    * Use {@link #getValue(String)} in preference to ensure ClearSilver compatibility.
     50    */
     51   public String getValue(String path, String defaultValue) {
     52     Data child = getChild(path);
     53     if (child == null) {
     54       return defaultValue;
     55     } else {
     56       String result = child.getValue();
     57       return result == null ? defaultValue : result;
     58     }
     59   }
     60 
     61   /**
     62    * Retrieves the integer value at the specified path in this HDF node's subtree. If the value does
     63    * not exist, or cannot be converted to an integer, default_value will be returned.
     64    *
     65    * Use {@link #getValue(String)} in preference to ensure ClearSilver compatibility.
     66    */
     67   public int getIntValue(String path, int defaultValue) {
     68     Data child = getChild(path);
     69     if (child == null) {
     70       return defaultValue;
     71     } else {
     72       String result = child.getValue();
     73       try {
     74         return result == null ? defaultValue : TypeConverter.parseNumber(result);
     75       } catch (NumberFormatException e) {
     76         return defaultValue;
     77       }
     78     }
     79   }
     80 
     81   /**
     82    * Retrieves the value at the specified path in this HDF node's subtree. If not found, returns
     83    * null.
     84    */
     85   public String getValue(String path) {
     86     return getValue(path, null);
     87   }
     88 
     89   /**
     90    * Retrieves the value at the specified path in this HDF node's subtree. If not found or invalid,
     91    * returns 0.
     92    */
     93   public int getIntValue(String path) {
     94     return TypeConverter.asNumber(getChild(path));
     95   }
     96 
     97   /**
     98    * Retrieves the value at the specified path in this HDF node's subtree. If not found or invalid,
     99    * returns false.
    100    */
    101   public boolean getBooleanValue(String path) {
    102     return TypeConverter.asBoolean(getChild(path));
    103   }
    104 
    105   /**
    106    * Sets the value at the specified path in this HDF node's subtree.
    107    */
    108   public void setValue(String path, String value) {
    109     Data child = createChild(path);
    110     child.setValue(value);
    111   }
    112 
    113   // ******************* String representation *******************
    114 
    115   @Override
    116   public String toString() {
    117     StringBuilder stringBuilder = new StringBuilder();
    118     toString(stringBuilder, 0);
    119     return stringBuilder.toString();
    120   }
    121 
    122   public void toString(StringBuilder out, int indent) {
    123     try {
    124       write(out, indent);
    125     } catch (IOException ioe) {
    126       throw new RuntimeException(ioe); // COV_NF_LINE
    127     }
    128   }
    129 
    130   @Override
    131   public void optimize() {
    132   // Do nothing.
    133   }
    134 
    135   @Override
    136   public void setEscapeMode(EscapeMode mode) {
    137     this.escapeMode = mode;
    138   }
    139 
    140   @Override
    141   public EscapeMode getEscapeMode() {
    142     return escapeMode;
    143   }
    144 }
    145