Java tutorial
/* * This file is part of Dorado 7.x (http://dorado7.bsdn.org). * * Copyright (c) 2002-2012 BSTEK Corp. All rights reserved. * * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) * and BSDN commercial (http://www.bsdn.org/licenses) licenses. * * If you are unsure which license is appropriate for your use, please contact the sales department * at http://www.bstek.com/contact. */ package com.bstek.dorado.view.output; import java.io.IOException; import java.io.Writer; import java.util.Collection; import java.util.Date; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.StringEscapeUtils; import com.bstek.dorado.common.Ignorable; /** * ? * * @author Benny Bao (mailto:benny.bao@bstek.com) * @since Sep 19, 2008 */ public abstract class OutputUtils { /** * * <ul> * <li>java.lang.String"#default"null""</li> * <li>java.lang.Number"#default"0</li> * <li>java.lang.Boolean"#default"false</li> * <li>java.util.Collection"#default"??0</li> * <li>"#default"null</li> * </ul> */ public static final String ESCAPE_VALUE = "#default"; /** * */ public static final String IGNORE_VALUE = "#ignore"; /** * ? * * @param value * * @return ? */ public static boolean isEscapeValue(Object value) { return isEscapeValue(value, ESCAPE_VALUE); } /** * ? * * @param value * * @param escapeValue * * @return ? */ public static boolean isEscapeValue(Object value, Object escapeValue) { if (value != escapeValue) { if (ESCAPE_VALUE.equals(escapeValue)) { return (value == null || (value instanceof String && value.equals("")) || (value instanceof Number && ((Number) value).doubleValue() == 0) || (value instanceof Boolean && !((Boolean) value).booleanValue()) || (value instanceof Collection<?> && ((Collection<?>) value).isEmpty()) || (value instanceof Ignorable && ((Ignorable) value).isIgnored())); } else if (IGNORE_VALUE.equals(escapeValue)) { return true; } return String.valueOf(value).equals(String.valueOf(escapeValue)); } return true; } /** * HTMLJavaScript * * @throws IOException */ public static void outputScriptBeginTag(Writer writer) throws IOException { writer.write("<script language=\"javascript\" type=\"text/javascript\">\n"); } /** * HTMLJavaScript? * * @throws IOException */ public static void outputScriptEndTag(Writer writer) throws IOException { writer.write("</script>\n"); } /** * ??HTML * * @throws IOException */ public static void outputString(Writer writer, String s) throws IOException { writer.write(StringEscapeUtils.escapeHtml(s)); } /** * Java?JavaScript * * @param writer * Writer * @param owner * JavaScript? * @param object * Java * @param property * ??? * @param escapeValue * Java? * @throws Exception * @see #DEFAULT_VALUE */ public static void outputProperty(Writer writer, String owner, Object object, String property, Object escapeValue) throws Exception { Object value = PropertyUtils.getProperty(object, property); if (value == escapeValue || (escapeValue != null && escapeValue.equals(value))) { return; } writer.write(owner); writer.write('.'); writer.write(property); writer.write('='); if (value == null) { writer.write("null"); } else if (value instanceof String) { writer.write("\""); writer.write((String) value); writer.write("\""); } else if (value instanceof Number || value instanceof Boolean) { writer.write(value.toString()); } else if (value instanceof Date) { writer.write("new Date("); writer.write(String.valueOf(((Date) value).getTime())); writer.write(")"); } else { writer.write("\""); writer.write(value.toString()); writer.write("\""); } writer.write(";\n"); } /** * Java?JavaScript * * @param writer * Writer * @param owner * JavaScript? * @param object * Java * @param property * ??? */ public static void outputProperty(Writer writer, String owner, Object object, String property) throws Exception { outputProperty(writer, owner, object, property, null); } }