KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > text > template > MapFormater


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.text.template;
6
7 import java.util.Map JavaDoc ;
8 import java.util.Iterator JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.Writer JavaDoc;
11
12 /**
13  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
14  * @since Feb 3, 2005
15  * @version $Id$
16  */

17 public class MapFormater implements ObjectFormater {
18   private String JavaDoc starEntrySeparator_ ;
19   private String JavaDoc endEntrySeparator_ ;
20   private ObjectFormater keyFormater_ ;
21   private ObjectFormater valueFormater_ ;
22   
23   public MapFormater(String JavaDoc startItemSeparator, String JavaDoc endItemSeparator) {
24     starEntrySeparator_ = startItemSeparator ;
25     endEntrySeparator_ = endItemSeparator;
26   }
27   
28   public void format(Writer JavaDoc w, Object JavaDoc obj) throws IOException JavaDoc {
29     Map JavaDoc list = (Map JavaDoc) obj ;
30     Iterator JavaDoc i = list.entrySet().iterator() ;
31     while(i.hasNext()) {
32       Map.Entry JavaDoc entry = (Map.Entry JavaDoc) 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