⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlandxslprocessor.java

📁 一个java 代码生成器
💻 JAVA
字号:
/**
 * Copyright (c) 2002, Siddhartha P. Chandurkar siddhartha@visioncodified.com
 * All rights reserved.
 * Licensed under the Academic Free License version 1.1
 * See the file LICENSE.TXT for details.
 * LICENSE.txt is located in the directory  <install-directory>\Jenerator
 * of your Jenertaor Installation.
 *
 */
package com.jenerator.transformer;

/*<Imports>*/

import com.jenerator.struct.app.AppConfigStruct;
import com.jenerator.util.Configurator;
import org.apache.log4j.Logger;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/*</Imports>*/

/**
 * XmlAndXslProcessor
 * It is for the transformation of xml into java source files using
 * xsl templetes
 * @author Siddhartha P. Chandurkar
 * @version 0.9.0
 */
public class XmlAndXslProcessor implements Processable {

    private Logger log = Logger.getLogger(com.jenerator.transformer.XmlAndXslProcessor.class.getName());
    private AppConfigStruct configStruct;

    public XmlAndXslProcessor() {
        configStruct = Configurator.getInstance().getConfigStructs().getAppConfigStruct();
    }

    protected void makePackageDir(String pack) {
        File dir = new File(pack);
        if (!dir.exists()) {
            log.info("Creating directory for the packages : " + pack);
            dir.mkdirs();
        }
    }

    /**
     *	The function processes the xmlStream and the xslFile using a implementation
     *   of a XSLT processor and outputs the result as text in a java source file
     *   @param	xmlStream	String		xml as a string
     *   @param	xslFile		String		Xsl file which has to be used as a
     *									template
     *   @param	sourceFile	String		The name of the source file which
     *									has to be created using the output.
     */
    public void process(String xmlStream, String xslFile, String sourceFile, String _packageDir) throws NotProcessableException {
        try {
            String genDir = configStruct.getGenDir();
            //throw Exception NoGenDirectoryException
            String packageDir = _packageDir;
            if (_packageDir == null)
                packageDir = "";
            packageDir = genDir + packageDir;
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer(new StreamSource(new File(Configurator.getInstance().getJenHome() + File.separator + "config" + File.separator + "templates" + File.separator, xslFile)));
            log.debug("Template :- " + xslFile);
            log.info("Generated File :- " + sourceFile);
            makePackageDir(packageDir);
            transformer.transform(new StreamSource(new java.io.StringReader(xmlStream)), new StreamResult(new FileOutputStream(new File(packageDir, sourceFile))));
        } catch (TransformerConfigurationException e) {
            log.fatal(e);
            throw new NotProcessableException(e);
        } catch (TransformerException e) {
            log.fatal(e);
            throw new NotProcessableException(e);
        } catch (FileNotFoundException e) {
            log.fatal(e);
            throw new NotProcessableException(e);
        }
    }

}

// end of XmlAndXslProcessor.java

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -