update dnsjava library
[captive-validator.git] / src / com / verisign / tat / dnssec / Util.java
1 /***************************** -*- Java -*- ********************************\
2  *                                                                         *
3  *   Copyright (c) 2009 VeriSign, Inc. All rights reserved.                *
4  *                                                                         *
5  * This software is provided solely in connection with the terms of the    *
6  * license agreement.  Any other use without the prior express written     *
7  * permission of VeriSign is completely prohibited.  The software and      *
8  * documentation are "Commercial Items", as that term is defined in 48     *
9  * C.F.R.  section 2.101, consisting of "Commercial Computer Software" and *
10  * "Commercial Computer Software Documentation" as such terms are defined  *
11  * in 48 C.F.R. section 252.227-7014(a)(5) and 48 C.F.R. section           *
12  * 252.227-7014(a)(1), and used in 48 C.F.R. section 12.212 and 48 C.F.R.  *
13  * section 227.7202, as applicable.  Pursuant to the above and other       *
14  * relevant sections of the Code of Federal Regulations, as applicable,    *
15  * VeriSign's publications, commercial computer software, and commercial   *
16  * computer software documentation are distributed and licensed to United  *
17  * States Government end users with only those rights as granted to all    *
18  * other end users, according to the terms and conditions contained in the *
19  * license agreement(s) that accompany the products and software           *
20  * documentation.                                                          *
21  *                                                                         *
22 \***************************************************************************/
23
24 package com.verisign.tat.dnssec;
25
26 import org.xbill.DNS.Name;
27
28 import java.util.*;
29
30
31 /**
32  * Some basic utility functions.
33  */
34 public class Util {
35     /**
36      * Convert a DNS name into a string suitable for use as a cache key.
37      *
38      * @param name The name to convert.
39      * @return A string representing the name. This isn't ever meant to be
40      *         converted back into a DNS name.
41      */
42     public static String nameToString(Name name) {
43         if (name.equals(Name.root)) {
44             return ".";
45         }
46
47         String n = name.toString().toLowerCase();
48
49         if (n.endsWith(".")) {
50             n = n.substring(0, n.length() - 1);
51         }
52
53         return n;
54     }
55
56     public static int parseInt(String s, int def) {
57         if (s == null) {
58             return def;
59         }
60
61         try {
62             return Integer.parseInt(s);
63         } catch (NumberFormatException e) {
64             return def;
65         }
66     }
67
68     public static long parseLong(String s, long def) {
69         if (s == null) {
70             return def;
71         }
72
73         try {
74             return Long.parseLong(s);
75         } catch (NumberFormatException e) {
76             return def;
77         }
78     }
79
80     public static List<ConfigEntry> parseConfigPrefix(Properties config,
81         String prefix) {
82         if (!prefix.endsWith(".")) {
83             prefix = prefix + ".";
84         }
85
86         List<ConfigEntry> res = new ArrayList<ConfigEntry>();
87
88         for (Map.Entry<Object, Object> entry : config.entrySet()) {
89             String key = (String) entry.getKey();
90
91             if (key.startsWith(prefix)) {
92                 key = key.substring(prefix.length());
93                 res.add(new ConfigEntry(key, (String) entry.getValue()));
94             }
95         }
96
97         return res;
98     }
99
100     public static class ConfigEntry {
101         public String key;
102         public String value;
103
104         public ConfigEntry(String key, String value) {
105             this.key       = key;
106             this.value     = value;
107         }
108     }
109 }