KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > NamedValueList


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

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

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

82     protected void badList(String namedValueList)
83     {
84         throw new IllegalArgumentException("namedValueList = " + namedValueList);
85     }
86 }
87
Popular Tags