fork download
  1. /*
  2.  * hosts ファイルの整理
  3.  *
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. //package myapp.tool;
  7.  
  8. import java.lang.Comparable;
  9. import java.lang.String;
  10. import java.lang.System;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.PriorityQueue;
  14. import java.util.Queue;
  15. import java.util.SortedMap;
  16. import java.util.TreeMap;
  17. import java.io.BufferedReader;
  18. import java.io.FileReader;
  19. import java.io.InputStreamReader;
  20. import java.io.PrintStream;
  21.  
  22. class Hosts
  23. {
  24. private static final String ARG_INPUT = "-i";
  25. private static final String ARG_TEMPLATE = "-t";
  26. private static final String ARG_OUTPUT = "-o";
  27.  
  28. public static void main (String[] args) throws java.lang.Exception
  29. {
  30. Map<String, String> argmap = parseArgs(args);
  31. BufferedReader in = null;
  32. PrintStream out = null;
  33.  
  34. try {
  35. if (argmap.containsKey(ARG_INPUT)) {
  36. in = new BufferedReader(new FileReader(argmap.get(ARG_INPUT)));
  37. } else {
  38. }
  39.  
  40. String line, ip;
  41. Map<String, String> ips = new HashMap<>();
  42. Tree tree = new Tree();
  43.  
  44. while ((line = in.readLine()) != null) {
  45. if (line.matches("^\\s*(\\d{1,3}\\.){3}\\d{1,3}(\\s+([^\\.]+\\.)+[^\\.]+)+") == false) {
  46. continue;
  47. }
  48. String[] tokens = line.split("\\s");
  49. ip = null;
  50. for (String s : tokens) {
  51. if (s.length() > 0) {
  52. if (ip == null) {
  53. ip = s;
  54. if (ips.containsKey(ip)) {
  55. ip = ips.get(ip);
  56. } else {
  57. ips.put(ip, ip);
  58. }
  59. } else {
  60. tree.add(s, ip);
  61. }
  62. }
  63. }
  64. }
  65.  
  66. if (argmap.containsKey(ARG_OUTPUT)) {
  67. out = new PrintStream(argmap.get(ARG_OUTPUT));
  68. } else {
  69. out = System.out;
  70. }
  71.  
  72. printHeader(out, argmap.get(ARG_TEMPLATE));
  73.  
  74. tree.print(out);
  75.  
  76. } catch (java.lang.Exception ex) {
  77. throw ex;
  78. } finally {
  79. if (in != null && argmap.containsKey(ARG_INPUT)) {
  80. in.close();
  81. }
  82. if (out != null && argmap.containsKey(ARG_OUTPUT)) {
  83. out.flush();
  84. out.close();
  85. }
  86. }
  87. }
  88.  
  89. private static void printHeader(PrintStream out, String filepath) throws java.lang.Exception
  90. {
  91. if (filepath == null) {
  92. out.println("# hosts");
  93. out.println();
  94. return;
  95. }
  96. BufferedReader in = null;
  97. try {
  98. in = new BufferedReader(new FileReader(filepath));
  99. String line;
  100. while ((line = in.readLine()) != null) {
  101. out.println(line);
  102. }
  103. } catch (java.lang.Exception ex) {
  104. throw ex;
  105. } finally {
  106. if (in != null) {
  107. in.close();
  108. }
  109. }
  110. }
  111.  
  112. private static enum Flag
  113. {
  114. ANY, INPUT, OUPUT, TEMPLATE
  115. }
  116.  
  117. private static Map<String, String> parseArgs(String args[])
  118. {
  119. Map<String, String> argmap = new HashMap<>();
  120. if (args == null) {
  121. return argmap;
  122. }
  123. Flag flag = Flag.ANY;
  124. for (String arg : args) {
  125. switch (flag) {
  126. case ANY:
  127. switch (arg) {
  128. case ARG_TEMPLATE: flag = Flag.TEMPLATE; break;
  129. case ARG_OUTPUT: flag = Flag.OUPUT; break;
  130. case ARG_INPUT: flag = Flag.INPUT; break;
  131. default:
  132. argmap.put(ARG_INPUT, arg); break;
  133. }
  134. break;
  135. case TEMPLATE:
  136. argmap.put(ARG_TEMPLATE, arg);
  137. flag = Flag.ANY;
  138. break;
  139. case OUPUT:
  140. argmap.put(ARG_OUTPUT, arg);
  141. flag = Flag.ANY;
  142. break;
  143. case INPUT:
  144. argmap.put(ARG_INPUT, arg);
  145. flag = Flag.ANY;
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. return argmap;
  152. }
  153. }
  154.  
  155. class Tree
  156. {
  157. private class Node implements Comparable<Node>
  158. {
  159. SortedMap<String, Node> map = null;
  160. final String name;
  161. String ip = null;
  162. int depth = 0;
  163.  
  164. Node(String name)
  165. {
  166. this.name = name;
  167. }
  168.  
  169. void setIP(String ip)
  170. {
  171. this.ip = ip;
  172. }
  173.  
  174. int calcDepth()
  175. {
  176. if (depth > 0) {
  177. return depth;
  178. }
  179. depth = 1;
  180. if (map != null) {
  181. int d = 1;
  182. for (Node node : map.values()) {
  183. int t = node.calcDepth();
  184. if (t > d) {
  185. d = t;
  186. }
  187. }
  188. depth = d + 1;
  189. }
  190. return depth;
  191. }
  192.  
  193. public int compareTo(Node o)
  194. {
  195. return depth - o.depth;
  196. }
  197.  
  198. Node add(String name)
  199. {
  200. depth = 0;
  201. if (map == null) {
  202. map = new TreeMap<>();
  203. }
  204. Node node = map.get(name);
  205. if (node == null) {
  206. map.put(name, node = new Node(name));
  207. }
  208. return node;
  209. }
  210.  
  211. public boolean checkChildIP() {
  212. if (map != null) {
  213. for (Node node : map.values()) {
  214. if (node.ip != null) {
  215. return true;
  216. }
  217. }
  218. }
  219. return false;
  220. }
  221.  
  222. int print(PrintStream out, String host) {
  223. String temp;
  224. if (host.length() > 0) {
  225. temp = name + "." + host;
  226. } else {
  227. out.println();
  228. temp = name;
  229. }
  230. if (map == null) {
  231. if (ip != null) {
  232. out.println("\t" + ip + "\t" + temp);
  233. }
  234. return 1;
  235. } else {
  236. if (temp.length() > 0) {
  237. if (host.length() == 0) {
  238. out.println("# ***********************************************");
  239. out.println("# ." + temp);
  240. out.println("# ***********************************************");
  241. out.println();
  242. } else {
  243. out.println("# ." + temp);
  244. }
  245. }
  246. if (ip != null) {
  247. out.println("\t" + ip + "\t" + temp);
  248. }
  249. if (checkChildIP()) {
  250. Queue<Node> que = new PriorityQueue<>();
  251. for (Node node : map.values()) {
  252. que.add(node);
  253. }
  254. int n = 0;
  255. while (que.isEmpty() == false) {
  256. n += que.poll().print(out, temp);
  257. }
  258. if (n > 0) {
  259. out.println();
  260. }
  261. } else {
  262. int n = 0;
  263. for (Node node : map.values()) {
  264. n += node.print(out, temp);
  265. }
  266. if (n > 0) {
  267. out.println();
  268. }
  269. }
  270.  
  271. return 0;
  272. }
  273. }
  274. }
  275.  
  276. Node root;
  277.  
  278. Tree()
  279. {
  280. root = new Node("");
  281. }
  282.  
  283. void add(String hostname, String ip)
  284. {
  285. String[] tokens = hostname.split("\\.");
  286. Node node = root;
  287.  
  288. for (int i = tokens.length - 1; i >= 0; i--) {
  289. node = node.add(tokens[i]);
  290. }
  291. node.setIP(ip);
  292.  
  293. }
  294.  
  295. void print(PrintStream out)
  296. {
  297. root.calcDepth();
  298. out.println();
  299. root.print(out, "");
  300. }
  301. }
  302.  
Success #stdin #stdout 0.08s 380224KB
stdin
# hosts


# example.com
	127.0.0.1	www.example.com
	127.0.0.1	abc.example.com
	127.0.0.1	xyz.example.com
	127.0.0.1	img.example.com
# yahoo.co.jp
	127.0.0.1	www.yahoo.co.jp
	127.0.0.1	abc.yahoo.co.jp
	127.0.0.1	xyz.yahoo.co.jp
	127.0.0.1	img.yahoo.co.jp
# google.co.jp
	127.0.0.1	www.google.co.jp
	127.0.0.1	abc.google.co.jp
	127.0.0.1	xyz.google.co.jp
	127.0.0.1	img.google.co.jp

stdout
# hosts




# ***********************************************
# .com
# ***********************************************

# .example.com
	127.0.0.1	abc.example.com
	127.0.0.1	xyz.example.com
	127.0.0.1	www.example.com
	127.0.0.1	img.example.com


# ***********************************************
# .jp
# ***********************************************

# .co.jp
# .google.co.jp
	127.0.0.1	abc.google.co.jp
	127.0.0.1	xyz.google.co.jp
	127.0.0.1	www.google.co.jp
	127.0.0.1	img.google.co.jp

# .yahoo.co.jp
	127.0.0.1	abc.yahoo.co.jp
	127.0.0.1	xyz.yahoo.co.jp
	127.0.0.1	www.yahoo.co.jp
	127.0.0.1	img.yahoo.co.jp