| 1 | 0 | package jp.co.y_net.amm.common; |
| 2 | |
|
| 3 | |
import java.io.FileNotFoundException; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.InputStream; |
| 6 | |
import java.io.InputStreamReader; |
| 7 | |
import java.nio.charset.Charset; |
| 8 | |
import java.util.ArrayList; |
| 9 | |
import java.util.Enumeration; |
| 10 | |
import java.util.List; |
| 11 | |
import java.util.Locale; |
| 12 | |
import java.util.ResourceBundle; |
| 13 | |
|
| 14 | 0 | public class ResourceReader { |
| 15 | |
|
| 16 | |
private static final String DEFAULT_PROP = "App"; |
| 17 | |
|
| 18 | |
public static Integer getIntegerQuick(String key) { |
| 19 | 0 | String str = getStringQuick(key); |
| 20 | |
try { |
| 21 | 0 | return Integer.parseInt(str); |
| 22 | 0 | } catch (NumberFormatException e) { |
| 23 | 0 | return -1; |
| 24 | |
} |
| 25 | |
} |
| 26 | |
public static boolean getBooleanQuick(String key) { |
| 27 | 0 | String str = getStringQuick(key).toLowerCase(); |
| 28 | 0 | return str.equals("yes") |
| 29 | 0 | || str.equals("y") |
| 30 | 0 | || str.equals("true") |
| 31 | 0 | || str.equals("t") |
| 32 | 0 | || str.equals("1"); |
| 33 | |
} |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public static final String getStringQuick(String key, String def) { |
| 40 | 0 | String value = getStringQuick(key); |
| 41 | 0 | if(value == null) { |
| 42 | 0 | return def; |
| 43 | |
} else { |
| 44 | 0 | return value; |
| 45 | |
} |
| 46 | |
} |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public static final String getStringQuick(String key) { |
| 55 | 0 | String value = null; |
| 56 | |
try { |
| 57 | 0 | ResourceBundle bundle = ResourceBundle.getBundle(DEFAULT_PROP, RESOURCE_BUNDLE_CONTROL); |
| 58 | 0 | value = bundle.getString(key); |
| 59 | |
|
| 60 | 0 | } catch (Exception e) { |
| 61 | 0 | AppLogger.info(DEFAULT_PROP + ".properties から[" + key + "]が取得できませんでした。"); |
| 62 | |
} |
| 63 | 0 | return value; |
| 64 | |
} |
| 65 | 0 | private static final ResourceBundle.Control RESOURCE_BUNDLE_CONTROL = new ResourceBundle.Control() { |
| 66 | |
public long getTimeToLive(String baseName, Locale locale) { |
| 67 | 0 | return 60000L; |
| 68 | |
} |
| 69 | 0 | }; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public static List<String> getLines(String resourceName) { |
| 78 | |
Enumeration<String> keys; |
| 79 | |
try { |
| 80 | 0 | ResourceBundle bundle = ResourceBundle.getBundle(resourceName, RESOURCE_BUNDLE_CONTROL); |
| 81 | 0 | keys = bundle.getKeys(); |
| 82 | |
|
| 83 | 0 | } catch (Exception e) { |
| 84 | 0 | AppLogger.info(DEFAULT_PROP + ".properties が参照できませんでした。"); |
| 85 | 0 | return new ArrayList<String>(); |
| 86 | |
} |
| 87 | 0 | List<String> lines = new ArrayList<String>(); |
| 88 | 0 | while(keys.hasMoreElements()) { |
| 89 | 0 | lines.add(keys.nextElement()); |
| 90 | |
} |
| 91 | 0 | return lines; |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
public static String getText(String name) { |
| 118 | 0 | StringBuilder sb = new StringBuilder(); |
| 119 | 0 | InputStream is = null; |
| 120 | 0 | InputStreamReader isr = null; |
| 121 | |
try { |
| 122 | |
|
| 123 | 0 | is = ClassLoader.getSystemResourceAsStream(name); |
| 124 | 0 | if(is==null) { |
| 125 | |
|
| 126 | 0 | is = ResourceReader.class.getResourceAsStream("/" + name); |
| 127 | |
} |
| 128 | 0 | if(is==null) { |
| 129 | 0 | AppLogger.info(name + "が取得できませんでした。"); |
| 130 | |
} |
| 131 | 0 | isr = new InputStreamReader(is, Charset.forName("UTF-8")); |
| 132 | |
int contents; |
| 133 | 0 | while ((contents = isr.read()) != -1) { |
| 134 | 0 | sb.append((char)contents); |
| 135 | |
|
| 136 | |
} |
| 137 | 0 | } catch (FileNotFoundException e) { |
| 138 | 0 | throw new RuntimeException(e); |
| 139 | 0 | } catch (IOException e) { |
| 140 | 0 | throw new RuntimeException(e); |
| 141 | 0 | } finally { |
| 142 | |
try { |
| 143 | 0 | if (isr != null) isr.close(); |
| 144 | 0 | if (is != null) is.close(); |
| 145 | 0 | } catch (IOException e) { |
| 146 | 0 | throw new RuntimeException(e); |
| 147 | |
} |
| 148 | 0 | } |
| 149 | 0 | String text = sb.toString(); |
| 150 | 0 | return text; |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | 0 | public static class QuickeProperty extends ResourceReader { |
| 158 | |
|
| 159 | |
} |
| 160 | |
|
| 161 | |
} |