01: /* Element
02: *
03: * $Id: Element.java 4545 2006-08-26 00:33:38Z stack-sf $
04: *
05: * Created on July 26, 2006.
06: *
07: * Copyright (C) 2006 Internet Archive.e.
08: *
09: * 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: * ANVL 'data element'.
29: * Made of a lone {@link Label}, or a {@link Label} plus {@link Value}.
30: *
31: * @author stack
32: * @see <a
33: * href="http://www.cdlib.org/inside/diglib/ark/anvlspec.pdf">A Name-Value
34: * Language (ANVL)</a>
35: */
36: class Element {
37: private final SubElement[] subElements;
38:
39: public Element(final Label l) {
40: this .subElements = new SubElement[] { l };
41: }
42:
43: public Element(final Label l, final Value v) {
44: this .subElements = new SubElement[] { l, v };
45: }
46:
47: public boolean isValue() {
48: return this .subElements.length > 1;
49: }
50:
51: public Label getLabel() {
52: return (Label) this .subElements[0];
53: }
54:
55: public Value getValue() {
56: if (!isValue()) {
57: return null;
58: }
59: return (Value) this .subElements[1];
60: }
61:
62: @Override
63: public String toString() {
64: StringBuilder sb = new StringBuilder();
65: for (int i = 0; i < subElements.length; i++) {
66: sb.append(subElements[i].toString());
67: if (i == 0) {
68: // Add colon after Label.
69: sb.append(':');
70: if (isValue()) {
71: // Add space to intro the value.
72: sb.append(' ');
73: }
74: }
75: }
76: return sb.toString();
77: }
78: }
|