Clover coverage report -
Coverage timestamp: Sun Nov 16 2008 23:05:30 GMT
file stats: LOC: 1,042   Methods: 79
NCLOC: 455   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Configuration.java 64.1% 50.7% 40.5% 50.9%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: Configuration.java 541508 2007-05-25 01:54:12Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.util;
 21   
 22    import org.apache.commons.logging.Log;
 23    import org.apache.commons.logging.LogFactory;
 24   
 25    import org.w3c.dom.Attr;
 26    import org.w3c.dom.Document;
 27    import org.w3c.dom.Element;
 28    import org.w3c.dom.NamedNodeMap;
 29    import org.w3c.dom.Node;
 30    import org.w3c.dom.NodeList;
 31    import org.w3c.dom.Text;
 32   
 33    import java.util.ArrayList;
 34    import java.util.List;
 35   
 36    /**
 37    * Configuration is a utility class that allows Configurable objects to
 38    * easily manage configuration information in a simple and consistent
 39    * fashion.
 40    *
 41    * @see org.apache.xindice.util.Configurable
 42    * @version $Revision: 541508 $, $Date: 2007-05-24 18:54:12 -0700 (Thu, 24 May 2007) $
 43    */
 44    public final class Configuration {
 45   
 46    private static final Log log = LogFactory.getLog(Configuration.class);
 47   
 48    private static final Configuration[] EMPTY = new Configuration[0];
 49   
 50    private Element config;
 51    private boolean readOnly;
 52    private boolean isDirty;
 53    private Configuration root;
 54   
 55   
 56  1503 public Configuration(Element config, boolean readOnly) {
 57  1503 this.config = config;
 58  1503 this.readOnly = readOnly;
 59    }
 60   
 61  802 public Configuration(Document config, boolean readOnly) {
 62  802 this(config.getDocumentElement(), readOnly);
 63    }
 64   
 65    /**
 66    * Create read only configuration out of Element
 67    * @param config Configuration element
 68    */
 69  288 public Configuration(Element config) {
 70  288 this(config, true);
 71    }
 72   
 73    /**
 74    * Create read only configuration out of Document
 75    * @param config Configuration document
 76    */
 77  288 public Configuration(Document config) {
 78  288 this(config.getDocumentElement());
 79    }
 80   
 81  6027 private Configuration(Configuration root, Element config, boolean readOnly) {
 82  6027 this.root = root;
 83  6027 this.config = config;
 84  6027 this.readOnly = readOnly;
 85    }
 86   
 87  13794 private void setDirty() {
 88  13794 if (root != null) {
 89  11111 root.setDirty();
 90    } else {
 91  2683 this.isDirty = true;
 92    }
 93    }
 94   
 95  1791 public void resetDirty() {
 96    // Only call on root configuration will actually reset dirty flag
 97  1791 this.isDirty = false;
 98    }
 99   
 100  5980 public boolean isDirty() {
 101  5980 if (root != null) {
 102  1 return root.isDirty();
 103    } else {
 104  5979 return isDirty;
 105    }
 106    }
 107   
 108    /**
 109    * getElement returns the Element being managed by this Configuration.
 110    *
 111    * Returned Element should not be modified, use Configuration's methods
 112    * to modify managed Element.
 113    *
 114    * @return The Configuration Element
 115    * @throws ReadOnlyException If the Configuration is Read-only
 116    */
 117  1918 public Element getElement() throws ReadOnlyException {
 118  1918 if (readOnly) {
 119  0 throw new ReadOnlyException();
 120    }
 121  1918 return config;
 122    }
 123   
 124    /**
 125    * getName returns the name of the Configuration node.
 126    *
 127    * @return The Node name
 128    */
 129  945 public String getName() {
 130  945 return config.getNodeName();
 131    }
 132   
 133    /**
 134    * hasAttributes returns whether or not the Configuration node has any
 135    * attributes associated with it.
 136    *
 137    * @return Whether or not attributes exist
 138    */
 139  17 public boolean hasAttributes() {
 140  17 return config.getAttributes().getLength() > 0;
 141    }
 142   
 143    /**
 144    * getAttribute returns an attribute from the Configuration node.
 145    *
 146    * @param name The attribute name
 147    * @param defValue The default value
 148    * @return The attribute value
 149    */
 150  11113 public String getAttribute(String name, String defValue) {
 151  11113 String value = config.getAttribute(name);
 152  11113 if (value == null || value.length() == 0) {
 153  3457 value = defValue;
 154    }
 155  11113 return value;
 156    }
 157   
 158    /**
 159    * getAttribute returns an attribute from the Configuration node.
 160    *
 161    * @param name The attribute name
 162    * @return The attribute value
 163    */
 164  5996 public String getAttribute(String name) {
 165  5996 return getAttribute(name, "");
 166    }
 167   
 168    /**
 169    * getBooleanAttribute returns an attribute from the Configuration node.
 170    *
 171    * @param name The attribute name
 172    * @param defValue The default value
 173    * @return The attribute value
 174    */
 175  5038 public boolean getBooleanAttribute(String name, boolean defValue) {
 176  5038 try {
 177  5038 String attr = getAttribute(name, defValue ? "1" : "0").toLowerCase();
 178  5038 return "[true][yes][1][y][on]".indexOf("[" + attr + "]") != -1;
 179    } catch (Exception e) {
 180  0 return defValue;
 181    }
 182    }
 183   
 184    /**
 185    * getBooleanAttribute returns an attribute from the Configuration node.
 186    *
 187    * @param name The attribute name
 188    * @return The attribute value
 189    */
 190  1 public boolean getBooleanAttribute(String name) {
 191  1 return getBooleanAttribute(name, false);
 192    }
 193   
 194    /**
 195    * getIntAttribute returns an attribute from the Configuration node.
 196    *
 197    * @param name The attribute name
 198    * @param defValue The default value
 199    * @return The attribute value
 200    */
 201  2269 public int getIntAttribute(String name, int defValue) {
 202  2269 try {
 203  2269 return Integer.parseInt(config.getAttribute(name));
 204    } catch (Exception e) {
 205  1840 return defValue;
 206    }
 207    }
 208   
 209    /**
 210    * getIntAttribute returns an attribute from the Configuration node.
 211    *
 212    * @param name The attribute name
 213    * @return The attribute value
 214    */
 215  1 public int getIntAttribute(String name) {
 216  1 return getIntAttribute(name, 0);
 217    }
 218   
 219    /**
 220    * getShortAttribute returns an attribute from the Configuration node.
 221    *
 222    * @param name The attribute name
 223    * @param defValue The default value
 224    * @return The attribute value
 225    */
 226  3141 public short getShortAttribute(String name, short defValue) {
 227  3141 try {
 228  3141 return Short.parseShort(config.getAttribute(name));
 229    } catch (Exception e) {
 230  3141 return defValue;
 231    }
 232    }
 233   
 234    /**
 235    * getShortAttribute returns an attribute from the Configuration node.
 236    *
 237    * @param name The attribute name
 238    * @return The attribute value
 239    */
 240  0 public short getShortAttribute(String name) {
 241  0 return getShortAttribute(name, (short) 0);
 242    }
 243   
 244    /**
 245    * getLongAttribute returns an attribute from the Configuration node.
 246    *
 247    * @param name The attribute name
 248    * @param defValue The default value
 249    * @return The attribute value
 250    */
 251  1134 public long getLongAttribute(String name, long defValue) {
 252  1134 try {
 253  1134 return Long.parseLong(config.getAttribute(name));
 254    } catch (Exception e) {
 255  973 return defValue;
 256    }
 257    }
 258   
 259    /**
 260    * getLongAttribute returns an attribute from the Configuration node.
 261    *
 262    * @param name The attribute name
 263    * @return The attribute value
 264    */
 265  0 public long getLongAttribute(String name) {
 266  0 return getLongAttribute(name, (long) 0);
 267    }
 268   
 269    /**
 270    * getFloatAttribute returns an attribute from the Configuration node.
 271    *
 272    * @param name The attribute name
 273    * @param defValue The default value
 274    * @return The attribute value
 275    */
 276  0 public float getFloatAttribute(String name, float defValue) {
 277  0 try {
 278  0 return Float.parseFloat(config.getAttribute(name));
 279    } catch (Exception e) {
 280  0 return defValue;
 281    }
 282    }
 283   
 284    /**
 285    * getFloatAttribute returns an attribute from the Configuration node.
 286    *
 287    * @param name The attribute name
 288    * @return The attribute value
 289    */
 290  0 public float getFloatAttribute(String name) {
 291  0 return getFloatAttribute(name, (float) 0.0);
 292    }
 293   
 294    /**
 295    * getDoubleAttribute returns an attribute from the Configuration node.
 296    *
 297    * @param name The attribute name
 298    * @param defValue The default value
 299    * @return The attribute value
 300    */
 301  0 public double getDoubleAttribute(String name, double defValue) {
 302  0 try {
 303  0 return Double.parseDouble(config.getAttribute(name));
 304    } catch (Exception e) {
 305  0 return defValue;
 306    }
 307    }
 308   
 309    /**
 310    * getDoubleAttribute returns an attribute from the Configuration node.
 311    *
 312    * @param name The attribute name
 313    * @return The attribute value
 314    */
 315  0 public double getDoubleAttribute(String name) {
 316  0 return getDoubleAttribute(name, 0.0D);
 317    }
 318   
 319    /**
 320    * getByteAttribute returns an attribute from the Configuration node.
 321    *
 322    * @param name The attribute name
 323    * @param defValue The default value
 324    * @return The attribute value
 325    */
 326  0 public byte getByteAttribute(String name, byte defValue) {
 327  0 try {
 328  0 return Byte.parseByte(config.getAttribute(name));
 329    } catch (Exception e) {
 330  0 return defValue;
 331    }
 332    }
 333   
 334    /**
 335    * getByteAttribute returns an attribute from the Configuration node.
 336    *
 337    * @param name The attribute name
 338    * @return The attribute value
 339    */
 340  0 public byte getByteAttribute(String name) {
 341  0 return getByteAttribute(name, (byte) 0);
 342    }
 343   
 344    /**
 345    * getCharAttribute returns an attribute from the Configuration node.
 346    *
 347    * @param name The attribute name
 348    * @param defValue The default value
 349    * @return The attribute value
 350    */
 351  0 public char getCharAttribute(String name, char defValue) {
 352  0 try {
 353  0 return config.getAttribute(name).charAt(0);
 354    } catch (Exception e) {
 355  0 return defValue;
 356    }
 357    }
 358   
 359    /**
 360    * getCharAttribute returns an attribute from the Configuration node.
 361    *
 362    * @param name The attribute name
 363    * @return The attribute value
 364    */
 365  0 public char getCharAttribute(String name) {
 366  0 return getCharAttribute(name, '\0');
 367    }
 368   
 369    /**
 370    * listAttributes returns a list of the attribute names that exist for
 371    * this Configuration node.
 372    *
 373    * @return The Attribute names
 374    */
 375  10 public String[] listAttributes() {
 376  10 NamedNodeMap map = config.getAttributes();
 377  10 String[] result = new String[map.getLength()];
 378  10 int size = map.getLength();
 379  10 for (int i = 0; i < size; i++) {
 380  20 result[i] = ((Attr) map.item(i)).getName();
 381    }
 382  10 return result;
 383    }
 384   
 385    /**
 386    * hasValue returns whether or not the Configuration node has a textual
 387    * value associated with it.
 388    *
 389    * @return Whether or not the node has a text value
 390    */
 391  17 public boolean hasValue() {
 392  17 NodeList list = config.getChildNodes();
 393  17 return (list.getLength() > 0 && list.item(0).getNodeType() == Node.TEXT_NODE);
 394    }
 395   
 396    /**
 397    * getValue returns the value from the Configuration node.
 398    *
 399    * @param defValue The default value
 400    * @return The value
 401    */
 402  0 public String getValue(String defValue) {
 403  0 NodeList list = config.getChildNodes();
 404  0 if (list.getLength() == 1 && list.item(0).getNodeType() == Node.TEXT_NODE) {
 405  0 return list.item(0).getNodeValue();
 406    } else {
 407  0 return defValue;
 408    }
 409    }
 410   
 411    /**
 412    * getValue returns the value from the Configuration node.
 413    *
 414    * @return The value
 415    */
 416  0 public String getValue() {
 417  0 return getValue("");
 418    }
 419   
 420    /**
 421    * getBooleanValue returns the value from the Configuration node.
 422    *
 423    * @param defValue The default value
 424    * @return The value
 425    */
 426  0 public boolean getBooleanValue(boolean defValue) {
 427  0 try {
 428  0 return "[true][yes][1][y][on]".indexOf("[" + getValue().toLowerCase() + "]") != -1;
 429    } catch (Exception e) {
 430  0 return defValue;
 431    }
 432    }
 433   
 434    /**
 435    * getBooleanValue returns the value from the Configuration node.
 436    *
 437    * @return The value
 438    */
 439  0 public boolean getBooleanValue() {
 440  0 return getBooleanValue(false);
 441    }
 442   
 443    /**
 444    * getIntValue returns the value from the Configuration node.
 445    *
 446    * @param defValue The default value
 447    * @return The value
 448    */
 449  0 public int getIntValue(int defValue) {
 450  0 try {
 451  0 return Integer.parseInt(getValue());
 452    } catch (Exception e) {
 453  0 return defValue;
 454    }
 455    }
 456   
 457    /**
 458    * getIntValue returns the value from the Configuration node.
 459    *
 460    * @return The value
 461    */
 462  0 public int getIntValue() {
 463  0 return getIntValue(0);
 464    }
 465   
 466    /**
 467    * getShortValue returns the value from the Configuration node.
 468    *
 469    * @param defValue The default value
 470    * @return The value
 471    */
 472  0 public short getShortValue(short defValue) {
 473  0 try {
 474  0 return Short.parseShort(getValue());
 475    } catch (Exception e) {
 476  0 return defValue;
 477    }
 478    }
 479   
 480    /**
 481    * getShortValue returns the value from the Configuration node.
 482    *
 483    * @return The value
 484    */
 485  0 public short getShortValue() {
 486  0 return getShortValue((short) 0);
 487    }
 488   
 489    /**
 490    * getLongValue returns the value from the Configuration node.
 491    *
 492    * @param defValue The default value
 493