Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14215
  • 博文数量: 12
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 15:05
文章分类

全部博文(12)

文章存档

2011年(1)

2009年(11)

我的朋友
最近访客

分类: Java

2009-08-20 11:59:04

package com.wm.affnet.util;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.wm.affnet.dao.impl.BaseDao;
import com.wm.affnet.xml.Node;
import com.wm.affnet.xml.XmlDocument;
public class AffUtils {
 public static Hashtable hashDelims;
 public static Vector delimNames;
 public static Hashtable hashOmniPages;
 public static String strOmniBaseTag = "
\n"
   + "   \n"
   + "   \n"
   + "   \n"
   + "   \n" + "   \n" + "   \n"
   + "   \n" + "
";
 static {
  hashDelims = new Hashtable();
  hashDelims.put("CSV", ",");
  hashDelims.put("TSV", " ");
  hashDelims.put("PIPE", "|");
  hashDelims.put("XML", "XML");
  delimNames = new Vector();
  delimNames.add("CSV");
  delimNames.add("TSV");
  delimNames.add("PIPE");
  delimNames.add("XML");
  hashOmniPages = new Hashtable();
  hashOmniPages.put("aff_home.jsp", "Affiliate Home");
  hashOmniPages.put("aff_benefits.jsp", "Affiliate Benefits");
  hashOmniPages.put("aff_newsletter.jsp", "Affiliate Newsletters");
  hashOmniPages.put("aff_faqs.jsp", "Affiliate FAQ");
  hashOmniPages.put("aff_contactus.jsp", "Affiliate Contact Us");
  hashOmniPages.put("aff_member_home.jsp", "Affiliate Member Home");
  hashOmniPages.put("aff_banners.jsp", "Affiliate Get Banners");
  hashOmniPages.put("aff_getdatafeeds.jsp", "Affiliate Get Data Feeds");
  hashOmniPages.put("aff_custom_url.jsp", "Affiliate Create Link");
  hashOmniPages.put("aff_manage_account.jsp", "Affiliate Manage Account");
  hashOmniPages.put("aff_login.jsp", "Affiliate Sign In/Register");
  hashOmniPages.put("aff_create_account.jsp", "Affiliate Create Account");
  hashOmniPages.put("aff_custom_url_gl.jsp", "Affiliate Get Link");
  hashOmniPages.put("aff_become_member.jsp", "Affiliate Join Now");
  hashOmniPages.put("aff_rss.jsp", "Affiliate RSS Page");
 }
 public static String getDelim(String strFileFormat) {
  return (String) hashDelims.get(strFileFormat);
 }
 public static String nvl(String x, String def) {
  return (x == null ? def : x);
 }
 public static String nvl(String x) {
  return (x == null ? "" : x).trim();
 }
 public static boolean isnull(String x) {
  return ((x == null || x == "") ? true : false);
 }
 public static boolean isNull(String x) {
  return ((nvl(x).length() == 0) ? true : false);
 }
 public static boolean isrequestnull(String x) {
  return (x == null || x == "" || x == "null" ? true : false);
 }
 public static String iif(String param1, String param2, String param3, String param4) {
  return (param1.equals(param2) ? param3 : param4);
 }
 public static String iif(boolean param1, boolean param2, String param3, String param4) {
  return (param1 == param2 ? param3 : param4);
 }
 public static String iif(int param1, int param2, String param3, String param4) {
  return (param1 == param2 ? param3 : param4);
 }
 public static boolean isValidEmail(String strEmail) {
  Pattern p = Pattern.compile(Constants.EMAIL_REGEX);
  // Match the given string with the pattern
  Matcher m = p.matcher(strEmail);
  // check whether match is found
  return m.matches();
 }
 public static String getSelected(String strFeedValue, String strFeedType) {
  return ("\"" + strFeedValue + "\" " + (strFeedValue.equals(strFeedType) ? "SELECTED" : ""));
 }
 public static String getNewsDate() {
  return ((new java.util.Date()).toString());
 }
 public static boolean isAlphaNumeric(String strWord) {
  Pattern p = Pattern.compile("^[\\w]+$");
  // Match the given string with the pattern
  Matcher m = p.matcher(strWord);
  // check whether match is found
  return m.matches();
 }
 public static boolean isUSPhone(String strPhone) {
  Pattern p = Pattern.compile("^[0-9]{3}-[0-9]{3}-[0-9]{4}$");
  Matcher m = p.matcher(strPhone);
  return m.matches();
 }
 public static boolean isIntlPhone(String strPhone) {
  Pattern p = Pattern.compile("^[0-9]{11,12}$");
  Matcher m = p.matcher(strPhone);
  return m.matches();
 }
 public static boolean isName(String strCharVal) {
  return isRegEx("^[a-zA-Z ", strCharVal);
 }
 public static boolean isAllChar(String strCharVal) {
  Pattern p = Pattern.compile("^[a-zA-Z ]*$");
  Matcher m = p.matcher(strCharVal);
  return m.matches();
 }
 public static boolean isRegEx(String strRegEx, String strRegVal) {
  Pattern p = Pattern.compile(strRegEx);
  Matcher m = p.matcher(strRegVal);
  return m.matches();
 }
 public static String getFileStamp() {
  Calendar calendar = new GregorianCalendar();
  Date date = calendar.getTime();
  DateFormat format1 = new SimpleDateFormat("yyyyMMddhhmmss");
  return format1.format(date);
 }
 public static void sendEmailMessage(String senderId, String subjectText, String emailText) {
  SendEmail.messageSend(senderId, Constants.CONTACT_EMAIL_ALIAS, subjectText, emailText);
 }
 public static String getPageName(String strPageJsp) {
  return AffUtils.nvl((String) hashOmniPages.get(strPageJsp));
 }
 public static String getOmnitureTag(String strPageName) {
  return strOmniBaseTag.replaceFirst("", getPageName(strPageName));
 }
 /**
  * @param:
  * String p_str, String p_delim
  * @content
  * Transform String.
  * If p_str is null,just need return " "; If p_str contains p_delim,just
  * delete p_delim from p_str and add the new p_str into ""; any other
  * case,just return p_str directly.
  * @return
  * It will return String value.
  */
 public static String transformString(String p_str, String p_delim) {
  if (p_str == null) {
   return " ";
  } else if (p_str.indexOf(p_delim) > 0) {
   return "\"" + p_str.replace("\"", " ") + "\"";
  } else {
   return p_str;
  }
 }
    /**
     * Get a XML format file.
     * The affiliate client can choose File Format XML from the jsp page
     * It will return String value.
     */
    public static String generateXml(ResultSet rs, String strFeedType) {
        if (rs == null) {
            return "";
        }
        XmlDocument document = new XmlDocument();
        Node node = new Node("wmdatafeed");
        node.setAttribute("feed", strFeedType);
        try {
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            for (int i = 1; rs.next(); i++) {
                Node product = new Node("product");
                product.setAttribute("num", String.valueOf(i));
                for (int j = 1; j < columnCount + 1; j++) {
                    Node child = new Node(rsmd.getColumnName(j).toLowerCase());
                    Object object = rs.getObject(j);
                    if (object != null) {
                        String value = object.toString();
                        child.setValue(value.replace("&", "&").replace("<", "<").replace(">",">"));
                    }
                    product.addChild(child);
                }
                node.addChild(product);
            }
            document.addChild(node);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            BaseDao.closeResultSet(rs);
        }
        return document.toString();
    }
}
阅读(394) | 评论(0) | 转发(0) |
0

上一篇:select options

下一篇:log

给主人留下些什么吧!~~