1 5 package org.exoplatform.commons.utils; 6 7 import java.util.*; 8 import org.exoplatform.commons.xml.ExoXPPParser; 9 10 16 public class ExoProperties extends HashMap { 17 public ExoProperties() { } 18 19 public ExoProperties(int size) { 20 super(size) ; 21 } 22 23 public String getProperty(String key) { return (String ) get(key) ; } 24 25 public void setProperty(String key, String value) { put(key, value) ; } 26 27 public void addPropertiesFromText(String text) { 28 String [] 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 [] value = temp[i].split("=") ; 33 if(value.length == 2) put(value[0].trim(), value[1].trim()) ; 34 } 35 } 36 } 37 38 public String toText() { 39 StringBuffer b = new StringBuffer () ; 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 xml) { 50 try { 51 ExoXPPParser xpp = ExoXPPParser.getInstance() ; 52 xpp.setInput(new java.io.StringReader (xml)) ; 53 xpp.mandatoryNode("properties") ; 54 while(xpp.node("property")) { 55 put(xpp.getNodeAttributeValue("key"), xpp.getNodeAttributeValue("value")) ; 56 } 57 } catch (Exception ex) { 58 ex.printStackTrace() ; 59 } 60 } 61 62 public String toXml() { 63 StringBuffer b = new StringBuffer () ; 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 |