    1   /* Element
    2   *
    3   * $Id: Element.java 4545 2006-08-26 00:33:38Z stack-sf $
    4   *
    5   * Created on July 26, 2006.
    6   *
    7   * Copyright (C) 2006 Internet Archive.e.
    8   *
    9   * This file is part of the Heritrix web crawler (crawler.archive.org).
    10  *
    11  * Heritrix is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU Lesser Public License as published by
    13  * the Free Software Foundation; either version 2.1 of the License, or
    14  * any later version.
    15  *
    16  * Heritrix is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU Lesser Public License for more details.
    20  *
    21  * You should have received a copy of the GNU Lesser Public License
    22  * along with Heritrix; if not, write to the Free Software
    23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24  */
    25  package org.archive.util.anvl;
    26
    27
    28  /***
    29   * ANVL 'data element'.
    30   * Made of a lone {@link Label}, or a {@link Label} plus {@link Value}.
    31   *
    32   * @author stack
    33   * @see <a
    34   * href="http://www.cdlib.org/inside/diglib/ark/anvlspec.pdf">A Name-Value
    35   * Language (ANVL)</a>
    36   */
    37  class Element {
    38      private final SubElement [] subElements;
    39
    40      public Element(final Label l) {
    41          this.subElements = new SubElement [] {l};
    42      }
    43
    44      public Element(final Label l, final Value v) {
    45          this.subElements = new SubElement [] {l, v};
    46      }
    47
    48      public boolean isValue() {
    49          return this.subElements.length > 1;
    50      }
    51
    52      public Label getLabel() {
    53          return (Label)this.subElements[0];
    54      }
    55
    56      public Value getValue() {
    57          if (!isValue()) {
    58              return null;
    59          }
    60          return (Value)this.subElements[1];
    61      }
    62
    63      @Override
    64      public String toString() {
    65          StringBuilder sb = new StringBuilder();
    66          for (int i = 0; i < subElements.length; i++) {
    67              sb.append(subElements[i].toString());
    68              if (i == 0) {
    69                  // Add colon after Label.
    70                  sb.append(':');
    71                  if (isValue()) {
    72                      // Add space to intro the value.
    73                      sb.append(' ');
    74                  }
    75              }
    76          }
    77          return sb.toString();
    78      }
    79  }