SimpleAttributes.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.xml.handler.common;
017 
018 import java.text.DateFormat;
019 import java.text.ParseException;
020 import java.text.SimpleDateFormat;
021 import java.util.Date;
022 import java.util.HashMap;
023 import java.util.Iterator;
024 import java.util.Map;
025 import java.util.SortedMap;
026 import java.util.TreeMap;
027 
028 
029 /**
030  * Value object that contains unsorted key value pairs. Both arguments are string type, but there
031  * are access methods for different data types. If the type conversion in not possible an
032  * appropriate {@link RuntimeException} is thrown.
033  <p>
034  * With {@link #add} another key value pair is added. An {@link IllegalArgumentException} is thrown,
035  * if the key is already known.
036  *
037  @author    Michael Meyling
038  */
039 public class SimpleAttributes {
040 
041     /** Key value storage. */
042     private Map map = new HashMap();
043 
044     /**
045      * Adds a key value pair. This key must be still unknown.
046      *
047      @param   key     Key.
048      @param   value   Value, maybe <code>null</code>.
049      */
050     public final void add(final String key, final String value) {
051         if (map.containsKey(key)) {
052             throw new IllegalArgumentException(
053                 "Key " + key + " already known with value: " + map.get(key));
054         }
055         map.put(key, value);
056     }
057 
058     /**
059      * Returns the value for a key. If the key dosn't exist <code>null</code> is returned.
060      *
061      @param   key     Key.
062      @return  Associated value.
063      */
064     public final String getString(final String key) {
065         return (Stringmap.get(key);
066     }
067 
068     /**
069      * Returns the value for a key as an Integer. If the key dosn't exist
070      <code>null</code> is returned.
071      * If the value must be transformable into an Integer value.
072      *
073      @param   key     Key.
074      @return  Associated value converted into an Integer.
075      */
076     public final Integer getInteger(final String key) {
077         String value = (Stringmap.get(key);
078         if (value != null) {
079             value = value.trim();
080         }
081         if (value == null || value.length() == 0) {
082             return null;
083         }
084         return new Integer(value);
085     }
086 
087     /**
088      * Returns the value for a key as an Boolean. If the key dosn't exist
089      <code>null</code> is returned.
090      * If the value must be transformable into an Boolean value.
091      *
092      @param   key     Key.
093      @return  Associated value converted into an Boolean.
094      */
095     public final Boolean getBoolean(final String key) {
096         String value = ((Stringmap.get(key));
097         if (value != null) {
098             value = value.trim();
099         }
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 = (Stringmap.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.Entryiterator.next();
147             buffer.append(entry.getKey() "=\"" + entry.getValue() "\" ");
148         }
149         return buffer.toString();
150     }
151 
152 }