Java tutorial
/* * Copyright 2012 Kazumune Katagiri. (http://d.hatena.ne.jp/nemuzuka) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package jp.co.nemuzuka.utils; import org.apache.commons.lang.StringUtils; /** * ???????? * @author k-katagiri */ public class HtmlStringUtils { /** * textarea. * textArea????? * ? * http:????https:????<a/> * \r\n/\r/\n?<br />????? * @param target * @return */ public static final String escapeTextAreaString(String target) { if (StringUtils.isEmpty(target)) { return ""; } //? String resultStr = escape(target); resultStr = getATagString(resultStr); return replaseNl(resultStr); } /** * . * ?\r\n/\r/\n?<br />????? * @param target * @return ? */ private static String replaseNl(String target) { String result = target.replaceAll("\r\n", "\n"); result = result.replaceAll("\r", "\n"); return result.replaceAll("\n", "<br />"); } /** * http?. * ???http/https??<a/>???? * @param str * @return a? */ private static String getATagString(String str) { return str.replaceAll("(http://|https://){1}[\\w\\.\\-/:%]+", "<a href='$0' target='_blank'>$0</a>"); } /** * ?. * ??? * @param value * @return */ private static String escape(String value) { if (value == null || value.length() == 0) { return value; } StringBuffer result = null; String filtered = null; for (int i = 0; i < value.length(); i++) { filtered = null; switch (value.charAt(i)) { case '<': filtered = "<"; break; case '>': filtered = ">"; break; case '&': filtered = "&"; break; case '"': filtered = """; break; case '\'': filtered = "'"; break; } if (result == null) { if (filtered != null) { result = new StringBuffer(value.length() + 50); if (i > 0) { result.append(value.substring(0, i)); } result.append(filtered); } } else { if (filtered == null) { result.append(value.charAt(i)); } else { result.append(filtered); } } } return result == null ? value : result.toString(); } /** * ??. * ??????? * ???????????? * zipCode<br> * prefecturalCapital cities townName buildingName * @param zipCode ? * @param prefecturalCapital ? * @param cities ? * @param townName ?? * @param buildingName ?? * @return ? */ public static String createAddress(String zipCode, String prefecturalCapital, String cities, String townName, String buildingName) { StringBuilder sb = new StringBuilder(); if (StringUtils.isNotEmpty(zipCode)) { sb = sb.append(zipCode).append("\n"); } if (StringUtils.isNotEmpty(prefecturalCapital)) { sb = sb.append(prefecturalCapital).append(" "); } if (StringUtils.isNotEmpty(cities)) { sb = sb.append(cities).append(" "); } if (StringUtils.isNotEmpty(townName)) { sb = sb.append(townName).append(" "); } if (StringUtils.isNotEmpty(buildingName)) { sb = sb.append(buildingName).append(" "); } return escapeTextAreaString(sb.toString()); } }