KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > NamedPropertySet


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

26
27 package com.opensugar.cube;
28
29 import java.util.Hashtable JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.Vector JavaDoc;
32 import java.util.Enumeration JavaDoc;
33
34 // This class represents a named property set, i.e. a set of properties and a name.
35
// It is used to hold parts of a bundle manifest header entry.
36
// A bundle manifest header entry takes the form:
37
// headerTitle: name1; propName11=propValue11; propName12=propValue12,
38
// name2; propName21=propValue21,
39
// name3, ...
40
// Thus a bundle manifest header entry is made up of a title and one or more value parts
41
// each of which contains a name and possibly a set of properties.
42
// This class is used to hold the data of one of the parts of the entry, i.e. one name and
43
// its set of properties.
44
// Thus a bundle manifest header entry can be represented by a title and one or more instances
45
// of NamedPropertySet.
46
public class NamedPropertySet {
47
48    // A special property that is found is bundle manifest header entries is the specification
49
// version of exported and imported packages.
50
public static final String JavaDoc SPECIFICATION_VERSION = "specification-version";
51
52    // the name of the property set
53
private String JavaDoc name;
54    // the set of properties
55
private Hashtable JavaDoc propertySet;
56
57    // Create a list of named property sets from a string. The string that is passed
58
// here could be the part of a bundle manifest header entry after the header name
59
// and colon.
60
// The string must have the form that is used for bundle manifest header entries:
61
// SET (, SET)*
62
// where SET = name (;PARAM)*
63
// PARAM = paramName '=' paramValue
64
public static NamedPropertySet[] createNamedPropertySets( String JavaDoc description ) {
65       // If the string is null, return null.
66
if ( description == null ) {
67          return null;
68       }
69
70       // Decompose the bundle manifest entry string into its parts
71
StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc( description, "," );
72       Vector JavaDoc tmp = new Vector JavaDoc();
73       String JavaDoc token;
74       while ( tokens.hasMoreTokens() ) {
75          token = tokens.nextToken();
76          // For each part, create a NamedPropertySet.
77
tmp.addElement( createNamedPropertySet( token ) );
78       }
79       // Create the array of named property sets and return it.
80
NamedPropertySet[] ret = new NamedPropertySet[ tmp.size() ];
81       tmp.copyInto( ret );
82       return ret;
83    }
84
85    // Create a named property set from a string. The string that is passed
86
// here could be a sub-part of a bundle manifest header entry.
87
// The string must have the form that is used for bundle manifest header entry parts:
88
// name (; PARAM)*
89
// where PARAM = paramName '=' paramValue
90
public static NamedPropertySet createNamedPropertySet( String JavaDoc description ) {
91       // If the string is null, return null.
92
if ( description == null ) {
93          return null;
94       }
95
96       String JavaDoc name;
97       Hashtable JavaDoc propertySet = new Hashtable JavaDoc();
98
99       // Decompose the description into its parts.
100
StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc( description, ";" );
101       // The name is the first item found in the string.
102
name = tokens.nextToken().trim();
103       // The name should not contain an equals sign; otherwise it is a property.
104
int n = name.indexOf( "=" );
105       if ( n != -1 ) {
106          throw new IllegalArgumentException JavaDoc( "= sign found in name" );
107       }
108
109       String JavaDoc token;
110       // All the parts after the name are properties.
111
// We find the equals sign, and then determine the name and the value of the
112
// property.
113
while ( tokens.hasMoreTokens() ) {
114          token = tokens.nextToken().trim();
115          n = token.indexOf( "=" );
116          if ( n < 0 ) {
117             throw new IllegalArgumentException JavaDoc( "= expected but not found" );
118          }
119          if ( n < 1 ) {
120             throw new IllegalArgumentException JavaDoc( "No property name found" );
121          }
122          if ( n == token.length() ) {
123             throw new IllegalArgumentException JavaDoc( "No property value found" );
124          }
125          propertySet.put( token.substring( 0, n ), token.substring( n + 1, token.length() ) );
126       }
127
128       // Create the named property set and return it.
129
return new NamedPropertySet( name, propertySet );
130    }
131
132    public NamedPropertySet( String JavaDoc name, Hashtable JavaDoc propertySet ) {
133       this.name = name;
134       this.propertySet = propertySet;
135    }
136
137    public String JavaDoc getName() {
138       return name;
139    }
140
141    public Enumeration JavaDoc getPropertyNames() {
142       return propertySet.keys();
143    }
144
145    public Object JavaDoc getProperty( String JavaDoc key ) {
146       return propertySet.get( key );
147    }
148
149 }
150
Popular Tags