KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > propertyeditor > PropertiesEditor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.propertyeditor;
23
24 import java.beans.PropertyEditorSupport JavaDoc;
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import org.jboss.util.NestedRuntimeException;
34 import org.jboss.util.StringPropertyReplacer;
35
36 /**
37  * A property editor for {@link java.util.Properties}.
38  *
39  * @author Jason Dillon
40  * @author Scott.Stark@jboss.org
41  * @version $Revision: 1958 $
42  */

43 public class PropertiesEditor extends PropertyEditorSupport JavaDoc
44 {
45
46    /**
47     * To be consistent with the Properties.load and .store methods.
48     */

49    private static final String JavaDoc ENC = "ISO-8859-1";
50
51    /**
52     * Initializes a Properties object with text value
53     * interpretted as a .properties file contents. This replaces any
54     * references of the form ${x} with the corresponding system property.
55     *
56     * @return a Properties object
57     *
58     * @throws NestedRuntimeException An IOException occured.
59     */

60    public void setAsText(String JavaDoc propsText)
61    {
62       try
63       {
64          // Load the current key=value properties into a Properties object
65
Properties JavaDoc rawProps = new Properties JavaDoc(System.getProperties());
66          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(propsText.getBytes(ENC));
67          rawProps.load(bais);
68          // Now go through the rawProps and replace any ${x} refs
69
Properties JavaDoc props = new Properties JavaDoc();
70          Iterator JavaDoc keys = rawProps.keySet().iterator();
71          while( keys.hasNext() )
72          {
73             String JavaDoc key = (String JavaDoc) keys.next();
74             String JavaDoc value = rawProps.getProperty(key);
75             String JavaDoc value2 = StringPropertyReplacer.replaceProperties(value, rawProps);
76             props.setProperty(key, value2);
77          }
78          rawProps.clear();
79
80          setValue(props);
81       }
82       catch (IOException JavaDoc e)
83       {
84          throw new NestedRuntimeException(e);
85       }
86    }
87
88    /**
89     * Returns the properties as text.
90     */

91    public String JavaDoc getAsText()
92    {
93       try
94       {
95          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
96          Properties JavaDoc p = (Properties JavaDoc)getValue();
97          p.store(baos, null);
98          return baos.toString(ENC);
99       }
100       catch (IOException JavaDoc e)
101       {
102          throw new NestedRuntimeException(e);
103       }
104    }
105
106 }
107
Popular Tags