Java tutorial
/* * @(#)AddrUtil.java $version 2012. 3. 14. * * Copyright 2007 NHN Corp. All rights Reserved. * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.reizes.shiva.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; import com.reizes.shiva.utils.StringUtil; /** * MergeUtil? ?? * @author reizes */ public class AddrUtil { private static final Pattern PATTERN_NORMALIZE_ADDR = Pattern.compile( "^([- ]+\\d*[???])(?:\\s*\\d+\\s*~\\s*\\d+\\s*)?(?:\\s*\\d+[])?\\s*(.*)$"); //private static final Pattern PATTERN_NORMALIZE_ADDR = Pattern.compile("^(.+[???])(?: ?(?:\\d+~\\d+)? ?)(?: \\d+[])? ?((?:\\s*)?[\\d-]*)(?:)?(?:[ [^\\d]].+)?$"); private static final Pattern PATTERN_ADDR2_1 = Pattern .compile("^\\s*()?\\s*0*(\\d*)(?:\\s*(?:-|)\\s*0*(\\d*))?\\s*(?:|)? *"); private static final Pattern PATTERN_ADDR2_2 = Pattern .compile("^\\s*()?\\s*0*(\\d+)\\s*(?:\\s*0*(\\d+)\\s*)\\s*"); private static final Pattern PATTERN_NORMALIZE_ADDR1 = Pattern.compile("(?:\\d+([?]))"); private static final Pattern PATTERN_INVALID_ADDR3 = Pattern.compile("(?:[\\(\\[{].+[\\)\\]}])"); // 2012.3.14 ? ? private static final Pattern PATTERN_NORMALIZE_ROAD_ADDR = Pattern.compile( "^(.+[])\\s*(\\S+[???])?\\s*(\\S+?[](?:\\s*\\d+\\s*?)?)\\s+(\\d+)(?:(?:-|)(\\d+))?()?[,\\s]?(\\s*.+)?$"); private static String organizeAddr2(String san, String addr21, String addr22) { StringBuilder sb = new StringBuilder(); if (san != null) { sb.append(san); } if (addr21 != null) { sb.append(addr21); } if (addr22 != null) { // 2012.9.3 178 3 ? ? ? if (sb.length() > 0) { sb.append('-'); } sb.append(addr22); } return StringUtil.normalize(sb.toString()); } /** * ?(addr2)? * * - ? . ) 238-1 000 0? 0 * - ''? ? * ? * - ? ? ? ? ?-? ? ? * - ) 5-1 * * @param subaddr ? ? * @return ? ?? */ public static String getStreetAddr(String addr2) { if (addr2 != null) { Matcher matcher = PATTERN_ADDR2_2.matcher(addr2); boolean find = matcher.find(); if (!find) { matcher = PATTERN_ADDR2_1.matcher(addr2); find = matcher.find(); } if (find) { String san = matcher.group(1); String retaddr21 = StringUtil.normalize(matcher.group(2)); String retaddr22 = StringUtil.normalize(matcher.group(3)); return organizeAddr2(san, retaddr21, retaddr22); } } return null; } /** * ?1? -> ?? ? * @param addr1 * @return */ public static String getNormalizedAddr1(String addr1) { if (addr1 != null) { return PATTERN_NORMALIZE_ADDR1.matcher(addr1).replaceAll("$1"); } return addr1; } public static String normalizeAddr(String addr) { if (addr != null) { Matcher matcher = PATTERN_NORMALIZE_ADDR.matcher(addr); if (matcher.find()) { String addr1 = StringUtil.normalize(matcher.group(1)); String tmpAddr2 = StringUtil.normalize(matcher.group(2)); String addr2 = getStreetAddr(tmpAddr2); return (addr1 != null ? addr1 : "") + (addr2 != null ? " " + addr2 : ""); } } return null; } /** * 1,2 * 2012.9.4 - 3 * @param addr * @return */ public static String[] splitAddr(String addr) { String[] addrs = new String[3]; if (addr != null) { Matcher matcher = PATTERN_NORMALIZE_ADDR.matcher(addr); if (matcher.find()) { addrs[0] = StringUtil.normalize(matcher.group(1)); String tmpAddr2 = StringUtil.normalize(matcher.group(2)); addrs[1] = getStreetAddr(tmpAddr2); addrs[2] = getAddr3FromAddr2(tmpAddr2); } } return addrs; } /** * ? ? * @param addr1 * @return */ public static String normalizeAddr1(String addr1) { addr1 = StringUtil.normalize(addr1); if (addr1 != null) { return StringUtils.join(StringUtils.split(addr1, ' '), ' '); } return addr1; } /** * ? ? ? * @param addr * @return */ public static boolean isRoadAddr(String addr) { if (addr != null) { Matcher matcher = PATTERN_NORMALIZE_ROAD_ADDR.matcher(addr); boolean ret = matcher.find(); return ret && StringUtil.normalize(matcher.group(6)) == null; // / .. ?? ? . } return false; } /** * ? ? 0, 1,2 (common? // ?) * @param addr * @return */ public static String[] splitRoadAddr(String addr) { String[] addrs = new String[5]; if (addr != null) { Matcher matcher = PATTERN_NORMALIZE_ROAD_ADDR.matcher(addr); if (matcher.find()) { addrs[0] = StringUtil.normalize(matcher.group(1)); // // addrs[1] = StringUtil.normalize(matcher.group(2)); // ??//? - ? ??//?? ? . addrs[2] = StringUtil.normalize(StringUtils.join(StringUtils.split(matcher.group(3), ' '))); // ? - ?? ? . addrs[3] = StringUtil .normalize(matcher.group(4) + (matcher.group(5) != null ? "-" + matcher.group(5) : "")); // String invalid = StringUtil.normalize(matcher.group(6)); // / .. ?? ? . if (invalid != null) { addrs[3] = null; } // 2012.11.8 addr3 String addr3 = StringUtil.normalize(matcher.group(7)); if (addr3 != null) { Matcher matcher2 = PATTERN_INVALID_ADDR3.matcher(addr3); // addr3? if (matcher2.find()) { addr3 = StringUtil.normalize(matcher2.replaceAll("")); } addrs[4] = StringUtil.normalize(addr3); // addr3 } } } return addrs; } /** * addr2? ? ? . * @param addr2 * @return */ public static String getAddr3FromAddr2(String addr2) { if (addr2 != null) { Matcher matcher2 = PATTERN_ADDR2_2.matcher(addr2); if (matcher2.find()) { return StringUtil.normalize(StringUtils.strip(matcher2.replaceAll(""), " ,/.\t-_")); } else { Matcher matcher = PATTERN_ADDR2_1.matcher(addr2); if (matcher.find()) { return StringUtil.normalize(StringUtils.strip(matcher.replaceAll(""), " ,/.\t-_")); } return StringUtil.normalize(addr2); } } return null; } /** * addr2 addr2, addr3 split. * @param addr2 * @return */ public static String[] splitAddr2(String addr2) { return new String[] { getStreetAddr(addr2), getAddr3FromAddr2(addr2) }; } /** * 3 ? ? ? . * ? ? * ? trim * @param addr1 1 * @param addr2 2 * @param addr3 3 * @return */ public static String mergeAddrs(String addr1, String addr2, String addr3) { StringBuffer address = new StringBuffer(); address.append(addr1 == null ? "" : StringUtils.trim(addr1)); if (addr2 != null) { String newAddr2 = StringUtils.trim(addr2); if (newAddr2.length() > 0) { address.append(" "); address.append(newAddr2); } } if (addr3 != null) { String newAddr3 = StringUtils.trim(addr3); if (newAddr3.length() > 0) { address.append(" "); address.append(newAddr3); } } return StringUtils.trim(address.toString()); } }