KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > commons > utils > ExoProperties


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.commons.utils;
6
7 import java.util.*;
8 import org.exoplatform.commons.xml.ExoXPPParser;
9
10 /**
11  * Jul 20, 2004
12  * @author: Tuan Nguyen
13  * @email: tuan08@users.sourceforge.net
14  * @version: $Id: ExoProperties.java,v 1.1 2004/09/11 14:08:53 tuan08 Exp $
15  */

16 public class ExoProperties extends HashMap {
17     public ExoProperties() { }
18   
19   public ExoProperties(int size) {
20     super(size) ;
21   }
22   
23   public String JavaDoc getProperty(String JavaDoc key) { return (String JavaDoc) get(key) ; }
24   
25   public void setProperty(String JavaDoc key, String JavaDoc value) { put(key, value) ; }
26   
27   public void addPropertiesFromText(String JavaDoc text) {
28     String JavaDoc[] temp = text.split("\n") ;
29     for(int i = 0; i < temp.length; i++ ) {
30       temp[i] = temp[i].trim() ;
31       if(temp[i].length() > 0) {
32         String JavaDoc[] value = temp[i].split("=") ;
33         if(value.length == 2) put(value[0].trim(), value[1].trim()) ;
34       }
35     }
36   }
37   
38   public String JavaDoc toText() {
39     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
40     Set set = entrySet() ;
41     Iterator i = set.iterator() ;
42     while(i.hasNext()) {
43       Map.Entry entry = (Map.Entry) i.next() ;
44       b.append(entry.getKey()).append("=").append(entry.getValue()).append("\n") ;
45     }
46     return b.toString() ;
47   }
48   
49   public void addPropertiesFromXml(String JavaDoc xml) {
50     try {
51       ExoXPPParser xpp = ExoXPPParser.getInstance() ;
52       xpp.setInput(new java.io.StringReader JavaDoc(xml)) ;
53       xpp.mandatoryNode("properties") ;
54       while(xpp.node("property")) {
55         put(xpp.getNodeAttributeValue("key"), xpp.getNodeAttributeValue("value")) ;
56       }
57     } catch (Exception JavaDoc ex) {
58       ex.printStackTrace() ;
59     }
60   }
61   
62   public String JavaDoc toXml() {
63     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
64     b.append("<properties>") ;
65     Set set = entrySet() ;
66     Iterator i = set.iterator() ;
67     while(i.hasNext()) {
68       Map.Entry entry = (Map.Entry) i.next() ;
69       b.append("<property key=\"").append(entry.getKey()).append("\" value=\"").
70         append(entry.getValue()).append("\"/>");
71     }
72     b.append("</properties>") ;
73     return b.toString() ;
74   }
75 }
Popular Tags