KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > util > AttribValuePair


1 package net.matuschek.util;
2
3 import java.net.URLEncoder JavaDoc;
4 /*********************************************
5     Copyright (c) 2001 by Daniel Matuschek
6 *********************************************/

7             
8
9 /**
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   /**
32    * initializes object using an attribute and its values
33    */

34   public AttribValuePair(String JavaDoc attrib, String JavaDoc value) {
35     this.attrib=attrib;
36     this.value=value;
37   }
38
39   /**
40    * inializes object using attrib=value string
41    */

42   public AttribValuePair(String JavaDoc attribAndValue) {
43     setAttribAndValue(attribAndValue);
44   }
45
46   /**
47    * set the attrib and value using an attrib=value string
48    */

49   protected void setAttribAndValue(String JavaDoc attribAndValue) {
50     int pos=0;
51     pos=attribAndValue.indexOf("=");
52     if (pos==-1) {
53       attrib=attribAndValue;
54     } else {
55       attrib=attribAndValue.substring(0,pos).trim();
56       value=attribAndValue.substring(pos+1).trim();
57       if (value.startsWith("\"") || value.startsWith("'")) {
58     value=value.substring(1);
59       }
60       if (value.endsWith("\"") || value.endsWith("'")) {
61     value=value.substring(0,value.length()-1);
62       }
63     }
64   }
65   
66   public String JavaDoc getAttrib() {
67     if (ignoreAttribCase) {
68       return attrib.toLowerCase();
69     } else {
70       return attrib;
71     }
72   }
73   
74   public String JavaDoc getValue() {
75     return value;
76   }
77
78   public String JavaDoc toEncodedString() {
79     return
80       URLEncoder.encode(attrib)+
81       "="+
82       URLEncoder.encode(value);
83   }
84
85   public String JavaDoc toString() {
86     return attrib+"=\""+value+"\"";
87   }
88   
89   private String JavaDoc attrib;
90   private String JavaDoc value;
91   private boolean ignoreAttribCase=false;
92 }
93
94
95
Popular Tags