01: package net.matuschek.util;
02:
03: import java.net.URLEncoder;
04:
05: /*********************************************
06: Copyright (c) 2001 by Daniel Matuschek
07: *********************************************/
08:
09: /**
10: * A simple class to store attribute value pairs
11: *
12: * @author Daniel Matuschek
13: * @version $Id: AttribValuePair.java,v 1.4 2002/05/31 14:45:56 matuschd Exp $*/
14: public class AttribValuePair {
15:
16: public void setIgnoreAttribCase(boolean ignore) {
17: this .ignoreAttribCase = ignore;
18: }
19:
20: public boolean getIgnoreAttribCase() {
21: return ignoreAttribCase;
22: }
23:
24: /**
25: * empty constructor that does nothing
26: */
27: public AttribValuePair() {
28: }
29:
30: /**
31: * initializes object using an attribute and its values
32: */
33: public AttribValuePair(String attrib, String value) {
34: this .attrib = attrib;
35: this .value = value;
36: }
37:
38: /**
39: * inializes object using attrib=value string
40: */
41: public AttribValuePair(String attribAndValue) {
42: setAttribAndValue(attribAndValue);
43: }
44:
45: /**
46: * set the attrib and value using an attrib=value string
47: */
48: protected void setAttribAndValue(String attribAndValue) {
49: int pos = 0;
50: pos = attribAndValue.indexOf("=");
51: if (pos == -1) {
52: attrib = attribAndValue;
53: } else {
54: attrib = attribAndValue.substring(0, pos).trim();
55: value = attribAndValue.substring(pos + 1).trim();
56: if (value.startsWith("\"") || value.startsWith("'")) {
57: value = value.substring(1);
58: }
59: if (value.endsWith("\"") || value.endsWith("'")) {
60: value = value.substring(0, value.length() - 1);
61: }
62: }
63: }
64:
65: public String getAttrib() {
66: if (ignoreAttribCase) {
67: return attrib.toLowerCase();
68: } else {
69: return attrib;
70: }
71: }
72:
73: public String getValue() {
74: return value;
75: }
76:
77: public String toEncodedString() {
78: return URLEncoder.encode(attrib) + "="
79: + URLEncoder.encode(value);
80: }
81:
82: public String toString() {
83: return attrib + "=\"" + value + "\"";
84: }
85:
86: private String attrib;
87: private String value;
88: private boolean ignoreAttribCase = false;
89: }
|