KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > common > propertyeditor > PropertiesEditor


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Properties JavaDoc;
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: 476049 $
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 JavaDoc getValue() {
39         Object JavaDoc currentValue = super.getValue();
40         if (currentValue instanceof Properties JavaDoc) {
41             return (Properties JavaDoc) currentValue;
42         } else {
43             // convert the text value into an in-memory input stream we can used for
44
// property loading.
45
ByteArrayInputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(currentValue.toString().getBytes());
46             // load this into a properties instance.
47
Properties JavaDoc bundle = new Properties JavaDoc();
48             try {
49                 bundle.load(stream);
50             } catch (IOException JavaDoc e) {
51                 // any errors here are just a property exception
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 JavaDoc getAsText() {
68         Object JavaDoc value = getValue();
69         if (value instanceof Properties JavaDoc) {
70             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
71             try {
72                 ((Properties JavaDoc) value).store(baos, null);
73             } catch (IOException JavaDoc e) {
74                 // any errors here are just a property exception
75
throw new PropertyEditorException(e);
76             }
77             return baos.toString();
78         }
79         return ("" + value);
80     }
81
82 }
83
Popular Tags