DMSUtil

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.lang.StringEscapeUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class DMSUtil {
	static final private String cDateFormat = "dd/MM/yyyy";
	
	public static void debug(Object obj){
		try{
			throw new Exception("");
		}
		catch(Exception e){
				System.out.println( "[DMS][" + 
	                    e.getStackTrace()[1].getClassName() + 
	                    "." +
	                    e.getStackTrace()[1].getMethodName() + 
	                    "] " + obj);
			}
	}

	public static boolean saveFile(InputStream is, String filePath)
			throws Exception {
		boolean retVal = false;
		byte[] buffer = new byte[10240];
		FileOutputStream outStream = null;
		try {
			outStream = new FileOutputStream(filePath);
			int len = 0;
			while (true) {
				len = is.read(buffer);
				if (len == -1)
					break;
				outStream.write(buffer, 0, len);
			}
			outStream.close();
			retVal = true;
		} catch (IOException io) {
			System.out.println("Writing the array of bytes into the file "
					+ filePath + " failed.");
			throw new Exception("Writing the array of bytes into the file "
					+ filePath + " failed in saveFile");
		}
		return retVal;
	}
    public String ReadSingleElementAttribute(
    		String sXmlFile,
    		String sPath,
    		String sAttribute) throws Exception
    	{
    		DocumentBuilderFactory oDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    		DocumentBuilder oDocumentBuilder = oDocumentBuilderFactory.newDocumentBuilder();
    		Document oDoc = oDocumentBuilder.parse(getClass().getResourceAsStream(sXmlFile));
    		
    		return GetElementNode(oDoc, sPath).getAttribute(sAttribute);
    	}
    

    public Element GetElementNode(Document oXmlDoc, String sPath)
	{
		String[] oTagList = sPath.split("/");
		Element oElement = (Element)oXmlDoc.getElementsByTagName(oTagList[0]).item(0);
		for (int i=1 ; i<oTagList.length ; i++)
		{
			oElement = (Element)oElement.getElementsByTagName(oTagList[i]).item(0);
		}	
		return oElement;
	}
	

	
	public static String getDatabaseXmlItem(Map aMap,int num){
		StringBuffer xml = new StringBuffer();
		xml.append("<items>");
				xml.append("<item>");
					for(Iterator it=aMap.entrySet().iterator();it.hasNext();){
						Map.Entry entry = (Map.Entry) it.next();
						xml.append("<"+entry.getKey()+"><![CDATA[");
							xml.append(entry.getValue());
						xml.append("]]></"+entry.getKey()+">");
					}
				xml.append("</item>");
		xml.append("</items>");
		
		return GetSplitedXMLString(xml.toString(),num);
}
	
	public static String getDatabaseXmlItems(ArrayList alist,int num){
		StringBuffer xml = new StringBuffer();
		xml.append("<items>");
			for(int i=0;i<alist.size();i++){
				xml.append("<item>");
					Map amap = (Map)alist.get(i);
					for(Iterator it=amap.entrySet().iterator();it.hasNext();){
						Map.Entry entry = (Map.Entry) it.next();
						xml.append("<"+entry.getKey()+"><![CDATA[");
							xml.append(stringEscapeUtils((String)entry.getValue()));
						xml.append("]]></"+entry.getKey()+">");
					}					
				xml.append("</item>");
			}
		xml.append("</items>");
		
		return GetSplitedXMLString(xml.toString(),num);
}
	public static String GetSplitedXMLString(String sXMLString, int inum) {
		String ssplit = "</item>";
		String ssplitItem = "</item></items><SPLIT><items>";
		String sstakeholderItemsWithSplit = sXMLString;
		// sstakeholderItems = "<item>dfdf<item>";
		int numOfTime = 0;
		for (int j = 0; j < sXMLString.length(); j++) {

			if (sXMLString.charAt(j) == ssplit.charAt(0)) {

				if (sXMLString.substring(j, j + ssplit.length()).equals(ssplit)) {
					numOfTime++;
					// sstakeholderItems.concat("<SPLIT>");
					// sXMLString.replace(ssplit, ssplitItem);
					// Sep 12
					// if (numOfTime/inum == 0 && inum != 0)
					if (numOfTime == inum)

						sstakeholderItemsWithSplit = sXMLString.substring(0, j)
								+ ssplitItem
								+ sXMLString.substring(j + ssplit.length(),
										sXMLString.length());
					j = j + ssplit.length();

				} else
					j++;
			}
		}

		System.out.println("xml in String after split" + sXMLString);
		System.out.println("xml in String after split"
				+ sstakeholderItemsWithSplit);
		// System.out.println("add <split> to the end of string" +
		// test.concat("<SPLIT>"));

		return sXMLString;
	}
	public static String getExtension(String docFile) {
		if (docFile != null) {
			int index = docFile.lastIndexOf(".");
			if (index >= 0)
				return docFile.substring(index);
			else
				return "";
		} else
			return "";
	}
	public static String NVL(String s){
		return (s == null) ? "" : s;    	
	}
	
	static public String FormatDate(Date oInputDate)
	{
		return FormatDate(oInputDate, cDateFormat);
	}
	
	static public String FormatDate(Date oInputDate, String sFormat){
		String output = "";
		if(!isEmpty(oInputDate) && !isEmpty(sFormat)){
			try{
				DateFormat oDateFormat = new SimpleDateFormat(sFormat);
		        String sDatetime = oDateFormat.format(oInputDate);
		        output =  sDatetime;
			} catch(Exception e){
				e.printStackTrace();
				output = "";
			}
		}
		return output;
	}
	
	public static boolean isEmpty(Object object){
		boolean result = false;
		
		if(object == null){
			result = true;
		} else if(object instanceof String){
			if(((String)object).trim().equals("")){
				result = true;
			}
		} else if(object instanceof HashMap){
			if(((HashMap)object).size() == 0){
				result = true;
			}
		} else if(object instanceof ArrayList){
			if(((ArrayList)object).size() == 0){
				result = true;
			}
		}
		return result;
	}
	
	public static String stringEscapeUtils(String sInput){
		if(sInput !=null){
			return StringEscapeUtils.escapeSql(sInput);
		}else{
			return "";
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值