⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 convertutil.java

📁 一个专门用来快速开发网站的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.common.ubb;

import java.beans.PropertyDescriptor;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import java.io.*;
import java.io.File;


import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.springframework.aop.support.AopUtils;

/**
 * Utility class to convert one object to another.
 *
 * <p>
 * <a href="ConvertUtil.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
public final class ConvertUtil {

	private static Log log = LogFactory.getLog(ConvertUtil.class);

	// ~ Methods
	// ================================================================

	/**
	 * Method to convert a ResourceBundle to a Map object.
	 *
	 * @param rb
	 *            a given resource bundle
	 * @return Map a populated map
	 */
	public static Map convertBundleToMap(ResourceBundle rb) {
		Map map = new HashMap();

		for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
			String key = (String) keys.nextElement();
			map.put(key, rb.getString(key));
		}

		return map;
	}

	/**
	 * Method to convert a ResourceBundle to a Properties object.
	 *
	 * @param rb
	 *            a given resource bundle
	 * @return Properties a populated properties object
	 */
	public static Properties convertBundleToProperties(ResourceBundle rb) {
		Properties props = new Properties();

		for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
			String key = (String) keys.nextElement();
			props.put(key, rb.getString(key));
		}

		return props;
	}

	/**
	 * Convenience method used by tests to populate an object from a
	 * ResourceBundle
	 *
	 * @param obj
	 *            an initialized object
	 * @param rb
	 *            a resource bundle
	 * @return a populated object
	 */
	public static Object populateObject(Object obj, ResourceBundle rb) {
		try {
			Map map = convertBundleToMap(rb);

			BeanUtils.copyProperties(obj, map);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("Exception occured populating object: " + e.getMessage());
		}

		return obj;
	}

        public static String convertUbbToHtml(String input) {
          input = StringUtils.replace(input, "<br/>", "\n");
          input = StringUtils.replace(input, " ", "&nbsp;");


            input = disableHtml(input);
            input = getAutoLink(input);
            input = filterNewLine(input);
            input = filterSimpleCode(input);
            input = filterFont(input);
            input = filterImg(input);
            input = getFlashFilter(input);
            input = getObjectFilter(input);
            input = filterUrl(input);
            input = filterEmail(input);

          return input;
        }



	public static String convertKg(String input) {
		String[] emailPatter = { "\\[email\\]([^\\[]*)\\[/email\\]",
				"\\[email=([^\\[]*)\\]([^\\[]*)\\[/email\\]" };
		String[] emailReplace = { "<a href=\"mailto:$1\">$1</a>",
				"<a href=\"mailto:$1\">$2</a>" };
		String[] containTag = { "[email]", "[email=" };
		if ((input == null) || (input.length() == 0)) {
			return input;
		}

		return RegUtils.substitute(emailPatter, emailReplace, containTag,
				input, 2);
	}

	/**
	 * 把输入的字符串里的[email]email地址[/email]或者是[email=email地址][/email]转换成 <a
	 * href="email地址"></a>
	 *
	 * @param input
	 *            要过滤的字符串
	 * @return 过滤后的字符
	 */
	public static String filterEmail(String input) {
		String[] emailPatter = { "\\[email\\]([^\\[]*)\\[/email\\]",
				"\\[email=([^\\[]*)\\]([^\\[]*)\\[/email\\]" };
		String[] emailReplace = { "<a href=\"mailto:$1\">$1</a>",
				"<a href=\"mailto:$1\">$2</a>" };
		String[] containTag = { "[email]", "[email=" };
		if ((input == null) || (input.length() == 0)) {
			return input;
		}




		return RegUtils.substitute(emailPatter, emailReplace, containTag,
				input, 2);
	}

	/**
	 * Convenience method to convert a form to a POJO and back again
	 *
	 * @param o
	 *            the object to tranfer properties from
	 * @return converted object
	 */
	public static Object convert(Object o) throws Exception {
		if (o == null) {
			return null;
		}
		Object target = getOpposingObject(o);
		BeanUtils.copyProperties(target, o);
		return target;
	}

	/**
	 * This method inspects a POJO or Form and figures out its pojo/form
	 * equivalent.
	 *
	 * @param o
	 *            the object to inspect
	 * @return the Class of the persistable object
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static Object getOpposingObject(Object o)
			throws ClassNotFoundException, InstantiationException,
			IllegalAccessException {
		String name = o.getClass().getName();

		if (o instanceof BaseObject) {
			if (log.isDebugEnabled()) {
				log.debug("getting form equivalent of pojo...");
			}

			name = StringUtils.replace(name, ".model.", ".webapp.form.");
			if (AopUtils.isCglibProxy(o)) {
				name = name.substring(0, name.indexOf("$$"));
			}
			name += "Form";
		} else {
			if (log.isDebugEnabled()) {
				log.debug("getting pojo equivalent of form...");
			}
			name = StringUtils.replace(name, ".webapp.form.", ".model.");
			name = name.substring(0, name.lastIndexOf("Form"));
		}

		Class obj = Class.forName(name);

		if (log.isDebugEnabled()) {
			log.debug("returning className: " + obj.getName());
		}

		return obj.newInstance();
	}

	/**
	 * Convenience method to convert Lists (in a Form) from POJOs to Forms. Also
	 * checks for and formats dates.
	 *
	 * @param o
	 * @return
	 * @throws Exception
	 */
	public static Object convertLists(Object o) throws Exception {
		if (o == null) {
			return null;
		}

		Object target = null;

		PropertyDescriptor[] origDescriptors = PropertyUtils
				.getPropertyDescriptors(o);

		for (int i = 0; i < origDescriptors.length; i++) {
			String name = origDescriptors[i].getName();

			if (origDescriptors[i].getPropertyType().equals(List.class)) {
				List list = (List) PropertyUtils.getProperty(o, name);
				for (int j = 0; j < list.size(); j++) {
					Object origin = list.get(j);
					target = convert(origin);
					list.set(j, target);
				}
				PropertyUtils.setProperty(o, name, list);
			}
		}
		return o;
	}

	/**
	 * 从输入的字符串里得到自动连接和email,并且转换成[url]地址[/url] ,[email]email地址[/email]
	 *
	 * @param input
	 *            要过滤的字符串
	 * @return 过滤后的字符
	 */
	public static String getAutoLink(String input) {
		String[] linkPatter = {
				"^((https?|ftp|gopher|news|telnet):\\/\\/|www\\.)([^ \\r\\n\\(\\)\\^\\$!`\"'\\|\\[\\]\\{\\}<>]*)",
				"([^\\]a-z0-9-=\"'\\/])((https?|ftp|gopher|news|telnet):\\/\\/|www\\.)([^ \\r\\n\\(\\)\\^\\$!`\"'\\|\\[\\]\\{\\}<>]*)",
				"^([_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{2,4}))",
				"([^\\]a-z0-9\\/\\-_.~?=:.])([_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4}))" };
		String[] linkReplace = { "[url]$1$3[/url]", "$1[url]$2$4[/url]",
				"[email]$1[/email]", "$1[email]$2[/email]" };
		String[] containTag = { "://", "://", "@", "@" };
		if ((input == null) || (input.length() == 0)) {
			return input;
		}

		return RegUtils.substitute(linkPatter, linkReplace, containTag, input,
				containTag.length);
	}

	/**
	 * 把输入的字符串里的[url]url地址[/url]或者是[url=url地址][/url]转换成 <a href="url地址">url地址</a>
	 *
	 * @param input
	 *            要过滤的字符串
	 * @return 过滤后的字符
	 */
	public static String filterUrl(String input) {
		String[] urlPatter = { "\\[url\\]([^\\[]*)://([^\\[]*)\\[/url\\]",
				"\\[url\\]([^\\[]*)\\[/url\\]",
				"\\[url=([^\\[]*)://([^\\[]*)\\]([^\\[]*)\\[/url\\]",
				"\\[url=([^\\[]*)\\]([^\\[]*)\\[/url\\]" };
		String[] urlReplace = {
//				"<a href=\"$2\" target=_blank>$1://$2</a>",
//				"<a href=\"$1\" target=_blank>$1</a>",
//				"<a href=\"$2\" target=_blank>$3</a>",
//				"<a href=\"$1\" target=_blank>$2</a>" };
                            "<a href=\"$1://$2\" target=_blank>$1://$2</a>",
                                "<a href=\"http://$1\" target=_blank>$1</a>",
                                "<a href=\"$1://$2\" target=_blank>$3</a>",
                                "<a href=\"http://$1\" target=_blank>$2</a>" };

		String[] containTag = { "[url]", "[url]", "[url=", "[/url]" };
		if ((input == null) || (input.length() == 0)) {
			return input;
		}

		return RegUtils.substitute(urlPatter, urlReplace, containTag, input, 4);
	}

	/**
	 * 过滤[img]地址[/img] [flash]地址[/flash] [media]地址[/media]成相应的html标签
	 *
	 * @param input
	 *            要过滤的字符串
	 * @return 过滤后的字符
	 */
	public static String filterImg(String input) {
		String[] imgPatter = { "\\[img\\]([^\\[]*)\\[/img\\]",
				"\\[media\\]([^\\[]*)\\[/media\\]" };
		String[] imgReplace = {
				"<img src=\"$1\" border=\"0\" onload=\"if(this.width > (screen.width - 200)* 0.55) {this.width = (screen.width - 200)* 0.55; this.alt='点击查看原图';}\" onmouseover=\"if(this.alt) this.style.cursor='hand';\" onclick=\"if(this.alt) window.open('$1');\">",

				"<embed src=\"$1\" autostart=\"0\"></embed>" };//Click Here to Open New Window
		String[] containTag = { "[img]", "[media]" };
		if ((input == null) || (input.length() == 0)) {
			return input;
		}

		return RegUtils.substitute(imgPatter, imgReplace, containTag, input,
				containTag.length);
	}

	/**
	 * 将[font=字体]字[/font] [size=字大小]字[size] [color=字颜色]字[/color] 转换成<font
	 * face=字体>字</font><font size=字大小>字</font><font color=字颜色>字</font>
	 *
	 * @param input
	 *            要过滤的字符串
	 * @return 过滤后的字符
	 */
	public static String filterFont(String input) {
		String[] fontPatter = { "\\[face=([^\\[]*)\\]",
				"\\[color=([^\\[]*)\\]", "\\[size=([^\\[]*)\\]" };

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -