1 5 package org.exoplatform.text.template; 6 7 import java.util.Map ; 8 import java.util.Iterator ; 9 import java.io.IOException ; 10 import java.io.Writer ; 11 12 17 public class MapFormater implements ObjectFormater { 18 private String starEntrySeparator_ ; 19 private String endEntrySeparator_ ; 20 private ObjectFormater keyFormater_ ; 21 private ObjectFormater valueFormater_ ; 22 23 public MapFormater(String startItemSeparator, String endItemSeparator) { 24 starEntrySeparator_ = startItemSeparator ; 25 endEntrySeparator_ = endItemSeparator; 26 } 27 28 public void format(Writer w, Object obj) throws IOException { 29 Map list = (Map ) obj ; 30 Iterator i = list.entrySet().iterator() ; 31 while(i.hasNext()) { 32 Map.Entry entry = (Map.Entry ) i.next(); 33 if(starEntrySeparator_ != null) w.write(starEntrySeparator_); 34 if(keyFormater_ == null ) w.write(entry.getKey().toString()); 35 else keyFormater_.format(w, entry.getKey()) ; 36 w.write("="); 37 if(valueFormater_ == null ) w.write(entry.getValue().toString()); 38 else valueFormater_.format(w, entry.getValue()) ; 39 if(endEntrySeparator_ != null)w.write(endEntrySeparator_) ; 40 } 41 } 42 43 public ObjectFormater setValueFormater(ObjectFormater formater) { 44 valueFormater_ = formater ; 45 return this ; 46 } 47 48 public ObjectFormater setKeyFormater(ObjectFormater formater) { 49 keyFormater_ = formater ; 50 return this ; 51 } 52 } | Popular Tags |