Archive for June 13th, 2005

You are currently browsing the archives of Enabling Technology .

Java Collection

 

Java collectin:

SortedSet is a sorted set: it will sort all items added into the set automatically according to its natural order or specified by comparator (you should build your own comparator)

Collections has many static methods , one of which is sort(list, comparator)

when finding an object, you should write your won equals

Posted by micas on Jun 13th 2005 | Filed in JavaBasic | Comments (0)

Cast array from object[] to specified type

 

this is sample code:

ArrayList a = new ArrayList();
  a.add(new ReportDTO("a","b","c","d"));
  a.add(new ReportDTO("ae","b","c","d"));
  a.add(new ReportDTO("a2d","b","c","d"));
  a.add(new ReportDTO("aed","b","c","d"));
  a.add(new ReportDTO("a3d","b","c","d"));
  ReportDTO[] o = (ReportDTO[]) a.toArray(new ReportDTO[0]);
  for (int i = 0; i < o.length; i++) {
   System.out.println(o[i]);
  }

Posted by micas on Jun 13th 2005 | Filed in JavaBasic | Comments (0)

HashMap Operation:traverse

 

  HashMap al = dictManager.getK3CompanyMap();
   Iterator it = (al.entrySet()).iterator()  ;
   while (it.hasNext()) {
    Map.Entry  e = (Map.Entry) it.next() ;
  System.out.println(e.getKey()+" : "+e.getValue());
   }
   System.out.println("successfully finished!");

or

Set subTotalSet = subTotal.keySet();
      subTotal.entrySet().iterator()
   Iterator it = subTotalSet.iterator();
   double sumCrossTotal = 0.0;
   double sumHours = 0.0;
   while (it.hasNext()) {

Posted by micas on Jun 13th 2005 | Filed in Enabling-tech | Comments (0)

All kinds of iteration

 

HashMap custResult = new HashMap();  

Iterator iter = custResult.values().iterator();
while (iter.hasNext()) {
  custItem = (CSRCustSearchResult) iter.next();

Posted by micas on Jun 13th 2005 | Filed in Enabling-tech | Comments (0)

Singletons

Singletons

http://java.sun.com/developer/technicalArticles/Programming/singletons/

Posted by micas on Jun 13th 2005 | Filed in Enabling-tech | Comments (0)

Complex key search for HashMap

 

关键是:重写equals和hashCode。两者缺一不可。
hashCode可以简单的返回一个整数。

import java.util.HashMap;

/*
* Created on Jun 5, 2005
*
* TODO To change the template for this generated file go to Window -
* Preferences - Java - Code Style - Code Templates
*/

public class HashMapFinder {

    public static void main(String[] args) {
        HashMap h = new HashMap();
        h.put(new MyOb("1", "2"), "a");
        h.put(new MyOb("2","3"),"b");
        // test if the key exists.
        System.out.println(h.containsKey(new MyOb("2", "3")));
        System.out.println(h.get(new MyOb("2","3")));
        System.out.println(h.get(new MyOb("1","2")));

    }
}

class MyOb {
    private int number;
    MyOb(String vv1, String vv2) {
        this.v1 = vv1;
        this.v2 = vv2;
        number =1;

    }

    /**
     * @return Returns the v1.
     */
    public String getV1() {
        return v1;
    }

    /**
     * @param v1
     *            The v1 to set.
     */
    public void setV1(String v1) {
        this.v1 = v1;
    }

    /**
     * @return Returns the v2.
     */
    public String getV2() {
        return v2;
    }

    /**
     * @param v2
     *            The v2 to set.
     */
    public void setV2(String v2) {
        this.v2 = v2;
    }

    private String v1;

    private String v2;

    public int hashCode() {
        return number;
    }

    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        else if (obj instanceof MyOb) {
            MyOb o = (MyOb) obj;
            return this.v1.equals(o.getV1()) && this.v2.equals(o.getV2());

        } else
            return false;
    }

}

Posted by micas on Jun 13th 2005 | Filed in JavaBasic | Comments (0)

Get all environment names and value in java

 

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class EnvDump {
    public static void main(String args[]) throws Exception {
        Set s = System.getProperties().entrySet();
        for (Iterator i = s.iterator(); i.hasNext();) {
            Map.Entry entry = (Map.Entry) i.next();
            System.out.println(entry.getKey() + " ======" +
                    entry.getValue());
        }

    }
}

Posted by micas on Jun 13th 2005 | Filed in JavaBasic | Comments (0)

java: xslt+xml==>html

6月23日

 

/*
* Created on Jun 22, 2005 by yangyuan
*
* To change the template for this generated file go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
package test.xml;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import com.pepboys.shopx.report.xml.elements.XMLReport;

/**
* @author Yang Yuan
* All rights are reserved.
*
*/
public class ConvertXML {

private static boolean DEBUG = true;
public static void main(String[] args) {
  ConvertXML cm = new ConvertXML();
  try {
   System.out.println("—————————————");
   System.out.println(cm.convertXmlToHTML(null, null));
  } catch (Exception e) {
   e.printStackTrace();
  }

}

private String convertXmlToHTML(String xmlFile, String xslFile)
  throws Exception {

  String path =
   "D:\\Project\\IBM\\Pepboy\\workspace"
    + "\\ReportTeamDoc\\design\\XSLSample";

  xslFile = path + "\\" + "DisplayAllReport_HTML.xsl";
  xmlFile = path + "\\" + "DisplayAllReport.xml";

  String retStr = "";

  if (DEBUG) {
   System.out.println("#####################");
   System.out.println("xsl file:" + xslFile);
   System.out.println("xml data: \n " + xmlFile);
   System.out.println("######################");
  }

  try {
   BufferedReader fr = new BufferedReader(new FileReader(xmlFile));
   StringBuffer report = new StringBuffer();
   String tmpString = "";
   while ((tmpString = fr.readLine()) != null) {
    report.append(tmpString );
   }

   TransformerFactory factory = TransformerFactory.newInstance();
   Transformer transformer =
    factory.newTransformer(new StreamSource(xslFile));

   Source src = new StreamSource(new StringReader(report.toString()));

   StringWriter out = new StringWriter(2000);
   StreamResult res = new StreamResult(out);

   transformer.transform(src, res);

   // delete HTML header and tailer
   StringBuffer buff = out.getBuffer();
   int pos = buff.indexOf("<body>");
   int lastpos = buff.lastIndexOf("</body>");

   if (pos > -1 && lastpos > -1) {
    retStr = buff.substring(pos + 6, lastpos);
   } else {
    retStr = buff.toString();
   }
  } catch (TransformerException ex) {
   ex.printStackTrace();
  }

  return retStr;
}

}

Posted by micas on Jun 13th 2005 | Filed in J2EE | Comments (0)