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");
				xml.append("");
					for(Iterator it=aMap.entrySet().iterator();it.hasNext();){
						Map.Entry entry = (Map.Entry) it.next();
						xml.append("<"+entry.getKey()+">");
					}
				xml.append("");
		xml.append("");
		
		return GetSplitedXMLString(xml.toString(),num);
}
	
	public static String getDatabaseXmlItems(ArrayList alist,int num){
		StringBuffer xml = new StringBuffer();
		xml.append("");
			for(int i=0;i");
					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()+">");
					}					
				xml.append("");
			}
		xml.append("");
		
		return GetSplitedXMLString(xml.toString(),num);
}
	public static String GetSplitedXMLString(String sXMLString, int inum) {
		String ssplit = "";
		String ssplitItem = "";
		String sstakeholderItemsWithSplit = sXMLString;
		// sstakeholderItems = "dfdf";
		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("");
					// 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  to the end of string" +
		// test.concat(""));

		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 "";
		}
	}
}

 

你可能感兴趣的:(Java,java)