remove some warnings by using java 5 features
[captive-validator.git] / src / com / versign / tat / dnssec / Util.java
1 /*
2  * $Id$
3  * 
4  * Copyright (c) 2005 VeriSign. All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer. 2. Redistributions in
11  * binary form must reproduce the above copyright notice, this list of
12  * conditions and the following disclaimer in the documentation and/or other
13  * materials provided with the distribution. 3. The name of the author may not
14  * be used to endorse or promote products derived from this software without
15  * specific prior written permission.
16  * 
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
20  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *  
28  */
29
30 package com.versign.tat.dnssec;
31
32 import java.util.*;
33
34 import org.xbill.DNS.Name;
35
36 /**
37  * Some basic utility functions.
38  */
39 public class Util
40 {
41
42   /**
43    * Convert a DNS name into a string suitable for use as a cache key.
44    * 
45    * @param name The name to convert.
46    * @return A string representing the name. This isn't ever meant to be
47    *         converted back into a DNS name.
48    */
49   public static String nameToString(Name name)
50   {
51     if (name.equals(Name.root)) return ".";
52
53     String n = name.toString().toLowerCase();
54     if (n.endsWith(".")) n = n.substring(0, n.length() - 1);
55
56     return n;
57   }
58
59   public static int parseInt(String s, int def)
60   {
61     if (s == null) return def;
62     try
63     {
64       return Integer.parseInt(s);
65     }
66     catch (NumberFormatException e)
67     {
68       return def;
69     }
70   }
71
72   public static long parseLong(String s, long def)
73   {
74     if (s == null) return def;
75     try
76     {
77       return Long.parseLong(s);
78     }
79     catch (NumberFormatException e)
80     {
81       return def;
82     }
83   }
84   
85   public static class ConfigEntry
86   {
87     public String key;
88     public String value;
89     
90     public ConfigEntry(String key, String value)
91     {
92       this.key = key; this.value = value;
93     }
94   }
95   
96   public static List<ConfigEntry> parseConfigPrefix(Properties config, String prefix)
97   {
98     if (! prefix.endsWith("."))
99     {
100       prefix = prefix + ".";
101     }
102     
103     List<ConfigEntry> res = new ArrayList<ConfigEntry>();
104     
105     for (Map.Entry<Object, Object> entry : config.entrySet()) {
106         String key = (String) entry.getKey();
107         if (key.startsWith(prefix)) {
108             key = key.substring(prefix.length());
109             res.add(new ConfigEntry(key, (String) entry.getValue()));
110         }
111     }
112     
113     return res;
114   }
115 }