View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.kernel.xml.handler.common;
17  
18  import java.text.DateFormat;
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.SortedMap;
26  import java.util.TreeMap;
27  
28  
29  /**
30   * Value object that contains unsorted key value pairs. Both arguments are string type, but there
31   * are access methods for different data types. If the type conversion in not possible an
32   * appropriate {@link RuntimeException} is thrown.
33   * <p>
34   * With {@link #add} another key value pair is added. An {@link IllegalArgumentException} is thrown,
35   * if the key is already known.
36   *
37   * @author    Michael Meyling
38   */
39  public class SimpleAttributes {
40  
41      /** Key value storage. */
42      private Map map = new HashMap();
43  
44      /**
45       * Adds a key value pair. This key must be still unknown.
46       *
47       * @param   key     Key.
48       * @param   value   Value, maybe <code>null</code>.
49       */
50      public final void add(final String key, final String value) {
51          if (map.containsKey(key)) {
52              throw new IllegalArgumentException(
53                  "Key " + key + " already known with value: " + map.get(key));
54          }
55          map.put(key, value);
56      }
57  
58      /**
59       * Returns the value for a key. If the key dosn't exist <code>null</code> is returned.
60       *
61       * @param   key     Key.
62       * @return  Associated value.
63       */
64      public final String getString(final String key) {
65          return (String) map.get(key);
66      }
67  
68      /**
69       * Returns the value for a key as an Integer. If the key dosn't exist
70       * <code>null</code> is returned.
71       * If the value must be transformable into an Integer value.
72       *
73       * @param   key     Key.
74       * @return  Associated value converted into an Integer.
75       */
76      public final Integer getInteger(final String key) {
77          String value = (String) map.get(key);
78          if (value != null) {
79              value = value.trim();
80          }
81          if (value == null || value.length() == 0) {
82              return null;
83          }
84          return new Integer(value);
85      }
86  
87      /**
88       * Returns the value for a key as an Boolean. If the key dosn't exist
89       * <code>null</code> is returned.
90       * If the value must be transformable into an Boolean value.
91       *
92       * @param   key     Key.
93       * @return  Associated value converted into an Boolean.
94       */
95      public final Boolean getBoolean(final String key) {
96          String value = ((String) map.get(key));
97          if (value != null) {
98              value = value.trim();
99          }
100         if (value == null || value.length() == 0) {
101             return null;
102         }
103         return Boolean.valueOf(value);
104     }
105 
106     /**
107      * Returns the value for a key as an Date. If the key dosn't exist
108      * <code>null</code> is returned.
109      * If the value must be transformable into an Date value.
110      * The expected date format is "yyyy-MM-dd'T'HH:mm:ss".
111      *
112      * @param   key     Key.
113      * @return  Associated value converted into an Date.
114      */
115     public final Date getDate(final String key) {
116         String value = (String) map.get(key);
117         if (value != null) {
118             value = value.trim();
119         }
120         if (value == null || value.length() == 0) {
121             return null;
122         }
123         try {
124             DateFormat formater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
125             Date date = formater.parse(value);
126             return date;
127         } catch (ParseException e) {
128             throw new IllegalArgumentException(e.toString());
129         }
130     }
131 
132     /**
133      * Get the attribute values, sorted by their keys.
134      *
135      * @return  Key sorted string values.
136      */
137     public final String[] getKeySortedStringValues() {
138         SortedMap sorted = new TreeMap(map);
139         return (String[]) sorted.values().toArray(new String[0]);
140     }
141 
142     public String toString() {
143         final StringBuffer buffer = new StringBuffer();
144         final Iterator iterator = map.entrySet().iterator();
145         while (iterator.hasNext()) {
146             Map.Entry entry = (Map.Entry) iterator.next();
147             buffer.append(entry.getKey() + "=\"" + entry.getValue() + "\" ");
148         }
149         return buffer.toString();
150     }
151 
152 }