View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.common.xml;
22  
23  import java.util.Enumeration;
24  import java.util.Hashtable;
25  import java.util.Set;
26  
27  /**
28   * XmlHash Hashtable for XmlValue utility.
29   * Hash all values (including subXml) but only root for Multiple
30   * 
31   * @author Frederic Bregier
32   * 
33   */
34  public class XmlHash {
35      private final Hashtable<String, XmlValue> hashtable;
36  
37      public XmlHash(XmlValue[] values) {
38          hashtable = new Hashtable<String, XmlValue>();
39          for (XmlValue xmlValue: values) {
40              if (xmlValue.isMultiple()) {
41                  hashtable.put(xmlValue.getName(), xmlValue);
42              } else if (xmlValue.isSubXml()) {
43                  this.put(xmlValue);
44              } else {
45                  hashtable.put(xmlValue.getName(), xmlValue);
46              }
47          }
48      }
49      
50      public XmlValue get(String name) {
51          return hashtable.get(name);
52      }
53  
54      public XmlValue put(XmlValue value) {
55          if (value.isMultiple()) {
56              return hashtable.put(value.getName(), value);
57          } else if (value.isSubXml()) {
58              XmlValue ret = hashtable.put(value.getName(), value);
59              if (! value.isEmpty()) {
60                  for (XmlValue subvalue: value.getSubXml()) {
61                      if (subvalue != null) {
62                          this.put(subvalue);
63                      }
64                  }
65              }
66              return ret;
67          } else {
68              return hashtable.put(value.getName(), value);
69          }
70      }
71  
72      public int size() {
73          return hashtable.size();
74      }
75  
76      public boolean isEmpty() {
77          return hashtable.isEmpty();
78      }
79  
80      public Enumeration<String> keys() {
81          return hashtable.keys();
82      }
83  
84      public Enumeration<XmlValue> elements() {
85          return hashtable.elements();
86      }
87  
88      public boolean contains(XmlValue value) {
89          return hashtable.contains(value);
90      }
91  
92      public boolean containsValue(XmlValue value) {
93          return hashtable.containsValue(value);
94      }
95  
96      public boolean containsKey(String key) {
97          return hashtable.containsKey(key);
98      }
99  
100     public XmlValue remove(String key) {
101         return hashtable.remove(key);
102     }
103 
104     public void clear() {
105         hashtable.clear();
106     }
107 
108     public Set<String> keySet() {
109         return hashtable.keySet();
110     }
111 
112 }