376196f340ac1430f42088d3c940f016c199a294
[mir.git] / source / mir / producer / reader / ReaderTool.java
1 package mir.producer.reader;
2
3 import java.util.*;
4
5 public class ReaderTool {
6
7   public static void checkValidIdentifier(String anIdentifier) throws ProducerConfigExc {
8   }
9
10   public static String getStringAttributeWithDefault(Map anAttributes, String aKey, String aDefault) {
11     if (anAttributes.containsKey(aKey))
12       return (String) anAttributes.get(aKey);
13     else
14       return aDefault;
15   }
16
17   public static void checkIntegerAttribute(Map anAttributes, String aKey) throws ProducerConfigExc {
18     try {
19       Integer.parseInt((String) anAttributes.get(aKey));
20     }
21     catch (Throwable t) {
22       throw new ProducerConfigExc("attribute '"+aKey+"' is not an integer" );
23     }
24   }
25
26   public static int getIntegerAttributeWithDefault(Map anAttributes, String aKey, int aDefault) throws ProducerConfigExc  {
27     String value;
28
29     if (anAttributes.containsKey(aKey)) {
30       checkIntegerAttribute(anAttributes, aKey);
31       return Integer.parseInt((String) anAttributes.get(aKey));
32     }
33     else
34       return aDefault;
35   }
36
37   public static void checkAttributes(Map anAttributes, String[] aRequiredAttributes, String[] anOptionalAttributes)  throws ProducerConfigExc {
38     checkAttributeSet(anAttributes.keySet(),
39        new HashSet(Arrays.asList(aRequiredAttributes)),
40        new HashSet(Arrays.asList(anOptionalAttributes)));
41   }
42
43   public static void checkAttributeSet(Set aSet, Set aRequiredElements, Set anOptionalElements) throws ProducerConfigExc{
44     Iterator i;
45
46     i = aSet.iterator();
47     while (i.hasNext()) {
48       Object item = i.next();
49
50       if (!(aRequiredElements.contains(item) || anOptionalElements.contains(item)))
51         throw new ProducerConfigExc("unknown attribute '" + item + "'" );
52     }
53
54     i = aRequiredElements.iterator();
55     while (i.hasNext()) {
56       Object item = i.next();
57
58       if (!(aSet.contains(item)))
59         throw new ProducerConfigExc("missing required attribute '" + item + "'" );
60     }
61
62   }
63 }