1 /**
2 *
3 * Copyright 2003-2004 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 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.geronimo.common.propertyeditor;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.util.Properties;
23
24 /**
25 * A property editor for indirect property bundles. This editor
26 * transforms the text value of the propery into a Property resource bundle.
27 *
28 * @version $Rev: 405651 $
29 */
30 public class PropertiesEditor extends TextPropertyEditorSupport {
31 /**
32 * Treats the text value of this property as an input stream that
33 * is converted into a Property bundle.
34 *
35 * @return a Properties object
36 * @throws PropertyEditorException An error occurred creating the Properties object.
37 */
38 public Object getValue() {
39 Object currentValue = super.getValue();
40 if (currentValue instanceof Properties) {
41 return (Properties) currentValue;
42 } else {
43
44
45 ByteArrayInputStream stream = new ByteArrayInputStream(currentValue.toString().getBytes());
46
47 Properties bundle = new Properties();
48 try {
49 bundle.load(stream);
50 } catch (IOException e) {
51
52 throw new PropertyEditorException(e);
53 }
54 return bundle;
55 }
56 }
57
58 /**
59 * Provides a String version of a Properties object suitable
60 * for loading into a Properties table using the load method.
61 *
62 * @return The String value of the Properties object as created
63 * by the store method.
64 * @throws PropertyEditorException An error occurred converting the Properties object
65 * @see Properties#store(java.io.OutputStream, String)
66 */
67 public String getAsText() {
68 Object value = getValue();
69 if (value instanceof Properties) {
70 ByteArrayOutputStream baos = new ByteArrayOutputStream();
71 try {
72 ((Properties) value).store(baos, null);
73 } catch (IOException e) {
74
75 throw new PropertyEditorException(e);
76 }
77 return baos.toString();
78 }
79 return ("" + value);
80 }
81
82 }