KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > content > packager > PackageParameters


1 /*
2  * PackageParameters.java
3  *
4  * Version: $Revision: 1.1 $
5  *
6  * Date: $Date: 2006/02/13 10:33:49 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40
41 package org.dspace.content.packager;
42
43 import java.util.Enumeration JavaDoc;
44 import java.util.Properties JavaDoc;
45
46 import javax.servlet.ServletRequest JavaDoc;
47
48 /**
49  * Parameter list for SIP and DIP packagers. It's really just
50  * a Java Properties object extended so each parameter can have
51  * multiple values. This was necessary so it can represent Servlet
52  * parameters, which have multiple values. It is also helpful to
53  * indicate e.g. metadata choices for package formats like METS that
54  * allow many different metadata segments.
55  *
56  * @author Larry Stone
57  * @version $Revision: 1.1 $
58  */

59
60 public class PackageParameters extends Properties JavaDoc
61 {
62     // Use non-printing FS (file separator) as arg-sep token, like Perl $;
63
private static final String JavaDoc SEPARATOR = "\034";
64
65     // Regular expression to match the separator token:
66
private static final String JavaDoc SEPARATOR_REGEX = "\\034";
67
68     public PackageParameters()
69     {
70         super();
71     }
72
73     public PackageParameters(Properties JavaDoc defaults)
74     {
75         super(defaults);
76     }
77
78     /**
79      * Creates new parameters object with the parameter values from
80      * a servlet request object.
81      *
82      * @param request - the request from which to take the values
83      * @return new parameters object.
84      */

85     public static PackageParameters create(ServletRequest JavaDoc request)
86     {
87         PackageParameters result = new PackageParameters();
88
89         Enumeration JavaDoc pe = request.getParameterNames();
90         while (pe.hasMoreElements())
91         {
92             String JavaDoc name = (String JavaDoc)pe.nextElement();
93             String JavaDoc v[] = request.getParameterValues(name);
94             if (v.length == 0)
95                 result.setProperty(name, "");
96             else if (v.length == 1)
97                 result.setProperty(name, v[0]);
98             else
99             {
100                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
101                 for (int i = 0; i < v.length; ++i)
102                 {
103                     if (i > 0)
104                         sb.append(SEPARATOR);
105                     sb.append(v[i]);
106                 }
107                 result.setProperty(name, sb.toString());
108             }
109         }
110         return result;
111     }
112
113
114     /**
115      * Adds a value to a property; if property already has value(s),
116      * this is tacked onto the end, otherwise it acts like setProperty().
117      *
118      * @param key - the key to be placed into this property list.
119      * @param value - the new value to add, corresponding to this key.
120      * @return the previous value of the specified key in this property list, or
121      * null if it did not have one.
122      */

123     public Object JavaDoc addProperty(String JavaDoc key, String JavaDoc value)
124     {
125         String JavaDoc oldVal = getProperty(key);
126         if (oldVal == null)
127             setProperty(key, value);
128         else
129             setProperty(key, oldVal + SEPARATOR + value);
130         return oldVal;
131     }
132
133     /**
134      * Returns multiple property values in an array.
135      *
136      * @param key - the key to look for in this property list.
137      * @return all values in an array, or null if this property is unset.
138      */

139     public String JavaDoc[] getProperties(String JavaDoc key)
140     {
141         String JavaDoc val = getProperty(key);
142         if (val == null)
143             return null;
144         else
145             return val.split(SEPARATOR_REGEX);
146     }
147
148     /**
149      * Returns boolean form of property with selectable default
150      * @param key the key to look for in this property list.
151      * @param default default to return if there is no such property
152      * @return the boolean derived from the value of property, or default
153      * if it was not specified.
154      */

155     public boolean getBooleanProperty(String JavaDoc key, boolean defaultAnswer)
156     {
157         String JavaDoc stringValue = getProperty(key);
158
159         if (stringValue == null)
160             return defaultAnswer;
161         else
162             return stringValue.equalsIgnoreCase("true") ||
163                    stringValue.equalsIgnoreCase("on") ||
164                    stringValue.equalsIgnoreCase("yes");
165     }
166 }
167
Popular Tags