Menu

12 Haziran 2010 Cumartesi

Report Parser

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReportParser {

    private Properties fileTable;
    private String reportDirectory;

    public ReportParser(File file, String reportDirectory) throws Exception {

        this.reportDirectory = reportDirectory;
        if (this.reportDirectory == null) {
            this.reportDirectory = file.getParent();
        }
        fileTable = new Properties();
        parse(file);

        fileTable.store(new FileOutputStream(this.reportDirectory + file.separator + "main.xml"), "Report table");
    }

    private String convertToFullName(String directory, String fileName) {
        if (!fileName.contains(File.separator)) {
            fileName = directory + File.separator + fileName;
        }
        return fileName;
    }

    public void parse() {
    }

    public void parse(File file) throws Exception {

        if (fileTable.getProperty(file.getName()) != null) {
            return;
        }

        fileTable.setProperty(file.getName(), UUID.randomUUID().toString().replaceAll("-", ""));

        Document doc = getDocument(file);

        NodeList imgList = doc.getElementsByTagName("imageExpression");

        for (int i = 0; i < imgList.getLength(); i++) {
            Node imgNode = imgList.item(i);
            String fileName = imgNode.getTextContent().replaceAll("\"", "");
            fileName = convertToFullName(file.getParent(), fileName);
            String value = fileTable.getProperty(fileName);
            if (value == null) {
                value = UUID.randomUUID().toString().replaceAll("-", "");
                fileTable.setProperty(fileName, value);
                imgNode.getFirstChild().setNodeValue(value);
                writeImage(fileName, convertToFullName(reportDirectory, value));
            }
        }

        NodeList subReportList = doc.getElementsByTagName("subreportExpression");

        for (int i = 0; i < subReportList.getLength(); i++) {
            Node subReportNode = subReportList.item(i);
            String fileName = subReportNode.getFirstChild().getNodeValue().replaceAll("\"", "").replaceAll(".jasper", ".jrxml");
            fileName = convertToFullName(file.getParent(), fileName);
            String value = fileTable.getProperty(fileName);
            if (value == null) {
                parse(new File(fileName));
                subReportNode.getFirstChild().setNodeValue(value);
            }
        }
        writeDocument(doc, convertToFullName(this.reportDirectory, fileTable.getProperty(file.getName())));
    }

    public Properties getFileTable() {
        return fileTable;
    }

    public void setFileTable(Properties fileTable) {
        this.fileTable = fileTable;
    }

    public void writeDocument(Document document, String file) {
        try {
            XMLSerializer serializer = new XMLSerializer();
            serializer.setOutputCharStream(
                    new java.io.FileWriter(file));
            serializer.serialize(document);
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

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

    private void writeImage(String fileName, String value) {
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(fileName);
            byte[] buf = new byte[fin.available()];
            fin.read(buf);
            fin.close();
            FileOutputStream fout = new FileOutputStream(value);
            fout.write(buf);
            fout.close();
        } catch (IOException ex) {
            Logger.getLogger(ReportParser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

1 yorum:

  1. Ben almanyadan sevgi, gercekten cok guzel bir blog, eger twitter veya facebook sayfasi varsa hemen
    ekliycegim.

    YanıtlaSil