com.chenq.aspose.generator.BaseGenerator.java Source code

Java tutorial

Introduction

Here is the source code for com.chenq.aspose.generator.BaseGenerator.java

Source

package com.chenq.aspose.generator;

import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.aspose.words.Document;
import com.aspose.words.License;

public abstract class BaseGenerator {

    protected final static Logger logger = Logger.getLogger(BaseGenerator.class);
    // ?
    //   private final String TEMPLATE_PATH = "C:/Users/chenq/Desktop/aspose??/";
    private final String TEMPLATE_PATH = "wordTemplate/";
    // ?
    private final String OUT_PATH = "D:/wordExport/";
    // ?
    private final String PREFIX = "$";
    private final String SUFFIX = "$";

    /**
     * ???
     * license,?  
     */
    static {
        try {
            InputStream is = BaseGenerator.class.getClassLoader().getResourceAsStream("\\license.xml");
            new License().setLicense(is);
        } catch (Exception e) {
            logger.warn("?ASPOSE?", e);
        }
    }

    /**
     * ???
     * @return
     */
    protected abstract String getTemplateName();

    /**
     * ??
     */
    protected String getExportName() {
        return System.currentTimeMillis() + "--" + getTemplateName();
    }

    /**
     * ?
     */
    protected String getTemplatePath() {
        String path = TEMPLATE_PATH + getTemplateName();
        path = GenHelper.BOOT_PATH + path;
        return path;
    }

    /**
     * 
     */
    protected String getExportPath() {
        return OUT_PATH + getExportName();
    }

    /**
     * ?,,??Document.save
     * @throws Exception
     */
    public void execute() throws Exception {
        createDocument().save(getExportPath());
    }

    public Document returnDoc() {
        Document doc = null;
        try {
            doc = createDocument();
        } catch (Exception e) {
            logger.warn("ASPOSE.WORD Document ?", e);
        }
        return doc;
    }

    protected abstract Document createDocument() throws Exception;

    /** 
     * ??? 
     * @param datas ?
     */
    protected Document replaceDocTem(Map<String, Object> datas) {
        return replaceDocTem(datas, null);
    }

    protected Document replaceDocTem(Map<String, Object> datas, Document doc) {
        try {
            if (doc == null) {
                doc = new Document(getTemplatePath());
            }
            // ????
            Iterator<String> keys = datas.keySet().iterator();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = String.valueOf(datas.get(key));
                value = StringUtils.defaultIfEmpty(value, "").replaceAll("\n\n", "" + (char) 11);//??
                // ????
                doc.getRange().replace(wrap(key), value, true, false);
            }
        } catch (Exception e) {
            logger.warn("??", e);
        }

        return doc;
    }

    protected String wrap(String property) {
        return PREFIX + property + SUFFIX;
    }

    protected boolean needParse(Object obj) {
        if (obj == null || StringUtils.isEmpty(obj + "")) {
            return false;
        }
        return true;
    }

    /**
     * ?double
     * @param obj
     * @return
     */
    protected boolean needParseDouble(Object obj) {
        if (needParse(obj) && (Double) obj != 0d) {
            return true;
        }
        return false;
    }
}