Menu

11 Haziran 2010 Cuma

JRXML file parsing ve Zip file olusturma

import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ReadXMLFile_bak {

    public static String directory;
    public static String fXmlFile;
    public static String ZipedFileName;

    public static void main(String argv[]) throws Exception {
        ReadXMLFile_bak mLFile = new ReadXMLFile_bak();
        mLFile.zippedJRXML();

    }

    public void zippedJRXML() throws Exception {
        NodeList nList = getDocument().getElementsByTagName("jasperReport");
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                List files = new ArrayList();
                files.add(fXmlFile);
                files.addAll(getSubReportList(nNode));
                files.addAll(getImageList(nNode));
                createZip(files);
            }
        }
    }

    private List getSubReportList(Node node) {
        Element eElement = (Element) node;
        List subReps = new ArrayList();
        for (String sb : getTagValue("subreportExpression", eElement)) {
            sb = sb.substring(1, sb.length() - 1);
            if (!sb.contains("\\")) {
                subReps.add(directory + sb);
                System.out.println("subReps : " + directory + sb);
            } else {
                subReps.add(sb);
            }
        }
        return subReps;
    }

    private List getImageList(Node node) {
        Element eElement = (Element) node;
        List images = new ArrayList();
        for (String sb : getTagValue("imageExpression", eElement)) {
            sb = sb.substring(1, sb.length() - 1);
            if (!sb.contains("\\")) {
                images.add(directory + sb);
            } else {
                images.add(sb);
            }
        }
        return images;
    }

    private List getTagValue(String sTag, Element eElement) {
        int size = eElement.getElementsByTagName(sTag).getLength();
        List list = new ArrayList();
        for (int i = 0; i < size; i++) {
            NodeList nlList = eElement.getElementsByTagName(sTag).item(i).getChildNodes();
            Node nValue = (Node) nlList.item(0);
            list.add(nValue.getNodeValue());
        }
        return list;
    }

    private void createZip(List files) throws Exception {
        FileOutputStream outputFile = new FileOutputStream(ZipedFileName);
        ZipOutputStream zipFile = new ZipOutputStream(outputFile);
        byte[] buffer = new byte[1024];
        for (int i = 0; i < files.size(); i++) {
            FileInputStream inFile = new FileInputStream(files.get(i));
            zipFile.putNextEntry(new ZipEntry(files.get(i)));
            int length;
            while ((length = inFile.read(buffer)) > 0) {
                zipFile.write(buffer, 0, length);
            }
            zipFile.closeEntry();
            inFile.close();

        }
        System.out.println(".....Created ZIP file Successfully.....");
        zipFile.close();
    }

    private Document getDocument() throws Exception {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        return doc;
    }
}

Hiç yorum yok:

Yorum Gönder