Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * File created by Jimmy
  3.  * at 2004-mar-09 13:32:58
  4.  */
  5. package Macbeth.Utilities;
  6.  
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10.  
  11. /**
  12.  * A repository for string data structures. The repository is built upon named data
  13.  * lists, which can contain list items and/or several levels of sublists. Each list
  14.  * item contains one or more named string data fields.
  15.  * @author Jimmy
  16.  */
  17. public class DataRepository {
  18.     //a root level list, inside which all other lists and items reside
  19.     private DataList rootList;
  20.     //these are used while gradually building up the repository (see methods below)
  21.     private DataList currentList;
  22.     private DataListItem currentListItem;
  23.  
  24.     /**
  25.      * Creates a new instance of DataRepository.
  26.      */
  27.     public DataRepository() {
  28.         rootList = new DataList(null);
  29.         currentList = rootList;
  30.         currentListItem = null;
  31.     }
  32.  
  33.     /**
  34.      * Begins building a new sublist inside the current list. You
  35.      * can have any number of sublist. Each list/sublist contains its
  36.      * own list items and/or other sublists.
  37.      * @param name The name of the new list.
  38.      */
  39.     public void advancedBuild_BeginList(String name) {
  40.         //check if another list with the same name already exists inside the current list
  41.         if (currentList.subLists.containsKey(name)) {
  42.             //if so, let's use this as our current list (or else it would be overwritten)
  43.             currentList = (DataList)currentList.subLists.get(name);
  44.         }
  45.         //no conflicts...just add the new list
  46.         else {
  47.             DataList list = new DataList(currentList);
  48.             currentList.subLists.put(name, list);
  49.             currentList = list;
  50.         }
  51.     }
  52.  
  53.     /**
  54.      * Call this when you have finished adding list items to the
  55.      * current list. This means that control will return to its
  56.      * parent list. You need to call endList() the same number
  57.      * of times as beginList().
  58.      */
  59.     public void advancedBuild_EndList() {
  60.         if (currentList!=rootList) {
  61.             currentList = currentList.parentList;
  62.         }
  63.         else {
  64.             System.err.println("EndList() called too many times!");
  65.         }
  66.     }
  67.  
  68.     /**
  69.      * Begins building a new list item inside the current list. Each
  70.      * list item contains one or more data fields. Note that all data
  71.      * fields have to be wrapped inside list items, so you cannot add
  72.      * fields directly to a list.
  73.      */
  74.     public void advancedBuild_BeginListItem() {
  75.         DataListItem item = new DataListItem(currentList);
  76.         currentList.items.add(item);
  77.         currentListItem = item;
  78.     }
  79.  
  80.     /**
  81.      * Call this when you have finished adding data fields to the current
  82.      * list item.
  83.      */
  84.     public void advancedBuild_EndListItem() {
  85.         currentListItem = null;
  86.     }
  87.  
  88.     /**
  89.      * Adds a data field to the current list item.
  90.      * @param name The name of the data field.
  91.      * @param value The value of the data field.
  92.      */
  93.     public void advancedBuild_PutField(String name, String value) {
  94.         if (currentListItem!=null) {
  95.             currentListItem.putField(name, value);
  96.         }
  97.         else {
  98.             //TODO: throw exception when currentListItem==null
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Gets a data list from the root. The root list can be
  104.      * obtained as well by passing null or "root" as name.
  105.      * @param name The name of the list, or null or "root"
  106.      * for the root list.
  107.      * @return The list object.
  108.      */
  109.     public DataList getList(String name) {
  110.         if (name==null || name.equals("root")) {
  111.             return rootList;
  112.         }
  113.         else {
  114.             return (DataList)rootList.subLists.get(name);
  115.         }
  116.     }
  117.  
  118.     public Iterator lists() {
  119.         return rootList.subLists();
  120.     }
  121.  
  122.     /**
  123.      * Lists the contents in this data repository in a (multi-line) string.
  124.      * @return A string showing the contents of this data repository.
  125.      */
  126.     public String listContents() {
  127.         return rootList.listContents();
  128.     }
  129.  
  130.  
  131.  
  132.     public class DataList {
  133.         public DataList parentList;
  134.         private LinkedList items;
  135.         private HashMap subLists;
  136.  
  137.         public DataList(DataList parentList) {
  138.             this.parentList = parentList;
  139.             this.items = new LinkedList();
  140.             this.subLists = new HashMap();
  141.         }
  142.  
  143.         public Iterator items() {
  144.             return items.iterator();
  145.         }
  146.  
  147.         public DataListItem firstItem() {
  148.             return (DataListItem)items.getFirst();
  149.         }
  150.  
  151.         public Iterator subLists() {
  152.             return subLists.keySet().iterator();
  153.         }
  154.  
  155.         public DataList getSubList(String name) {
  156.             return (DataList) subLists.get(name);
  157.         }
  158.  
  159.         public String listContents() {
  160.             String tmp = "";
  161.             Iterator it = subLists();
  162.             //list contents in all sublists
  163.             while (it.hasNext()) {
  164.                 String sublist = (String) it.next();
  165.                 tmp += MySystem.lineBreak + "<LIST: '" + sublist + "'>" + MySystem.lineBreak;
  166.                 tmp += getSubList(sublist).listContents();
  167.                 tmp += "</LIST>" + MySystem.lineBreak;
  168.             }
  169.             //list all items
  170.             it = items();
  171.             while (it.hasNext()) {
  172.                 tmp += "   <ITEM>" + MySystem.lineBreak;
  173.                 tmp += ((DataListItem)it.next()).listContents();
  174.                 tmp += "   </ITEM>" + MySystem.lineBreak;
  175.             }
  176.             return tmp;
  177.         }
  178.     }
  179.  
  180.  
  181.     public class DataListItem {
  182.         public DataList parentList;
  183.         private HashMap fields;
  184.  
  185.         public DataListItem(DataList parentList) {
  186.             this.parentList = parentList;
  187.             this.fields = new HashMap();
  188.         }
  189.  
  190.         public boolean containsField(String name) {
  191.             return fields.containsKey(name);
  192.         }
  193.  
  194.         public String getField(String name) {
  195.             return (String)fields.get(name);
  196.         }
  197.  
  198.         public void putField(String name, String value) {
  199.             fields.put(name, value);
  200.         }
  201.  
  202.         public Iterator fields() {
  203.             return fields.keySet().iterator();
  204.         }
  205.  
  206.         public String listContents() {
  207.             String tmp = "";
  208.             Iterator it = fields();
  209.             while (it.hasNext()) {
  210.                 String field = (String) it.next();
  211.                 tmp += "      '" + field + "' = '" + getField(field) + "'" + MySystem.lineBreak;
  212.             }
  213.             return tmp;
  214.         }
  215.     }
  216.  
  217. }
  218.