KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > NamedValueList


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.geronimo.interop.properties.PropertyMap;
25
26
27 public class NamedValueList extends LinkedList JavaDoc {
28     public NamedValueList() {
29     }
30
31     public NamedValueList(String JavaDoc namedValueList) {
32         List JavaDoc csvList = ListUtil.getCommaSeparatedList(namedValueList);
33         for (Iterator JavaDoc i = csvList.iterator(); i.hasNext();) {
34             String JavaDoc item = (String JavaDoc) i.next();
35             int eqPos = item.indexOf("=");
36             if (eqPos == -1) {
37                 badList(namedValueList);
38             }
39             String JavaDoc name = item.substring(0, eqPos).trim();
40             if (name.length() == 0) {
41                 badList(namedValueList);
42             }
43             String JavaDoc value = item.substring(eqPos + 1).trim();
44             add(new NamedValue(name, value));
45         }
46     }
47
48     // public methods
49

50     public PropertyMap getProperties() {
51         PropertyMap props = new PropertyMap();
52         for (Iterator JavaDoc i = this.iterator(); i.hasNext();) {
53             NamedValue nv = (NamedValue) i.next();
54             props.put(nv.name, nv.value);
55         }
56         return props;
57     }
58
59     public String JavaDoc getValue(String JavaDoc name) {
60         return getValue(name, null);
61     }
62
63     public String JavaDoc getValue(String JavaDoc name, String JavaDoc defaultValue) {
64         return (String JavaDoc) getProperties().getProperty(name, defaultValue);
65     }
66
67     public String JavaDoc toString() {
68         return ListUtil.formatCommaSeparatedList(this);
69     }
70
71     // protected methods
72

73     protected void badList(String JavaDoc namedValueList) {
74         throw new IllegalArgumentException JavaDoc("namedValueList = " + namedValueList);
75     }
76 }
77
Popular Tags