com.pactera.edg.am.metamanager.extractor.util.ExtractorContext.java Source code

Java tutorial

Introduction

Here is the source code for com.pactera.edg.am.metamanager.extractor.util.ExtractorContext.java

Source

/*
 * Copyright 2009 by pactera.edg.am Corporation. Address:HePingLi East Street No.11
 * 5-5, BeiJing,
 * 
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information of pactera.edg.am
 * Corporation ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with pactera.edg.am.
 */

package com.pactera.edg.am.metamanager.extractor.util;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;

import net.sf.saxon.FeatureKeys;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.xml.sax.InputSource;

/**
 * ?
 * 
 * @author fbchen
 * @version 1.0 Date: 2009-8-14 ?08:39:30
 * @category
 * 
 * 2009-09-10
 * getXsltTemplate?getTransformerExceptionTransformerConfigurationException
 * 
 */
public class ExtractorContext {

    static Log log = LogFactory.getLog(ExtractorContext.class);

    private static ExtractorContext instance = new ExtractorContext();

    /**
     * XSLT?
     */
    // private final Map<String, Templates> templates = new HashMap<String,
    // Templates>();
    /**
     * ??
     */
    private ExtractorContext() {
        super();
    }

    /**
     * getInstance
     * 
     * @return this
     */
    public static ExtractorContext getInstance() {
        return instance;
    }

    /**
     * ????XSLT
     * 
     * @return the xsltTemplate throws Exception ?XLST
     */
    public Templates getXsltTemplate(String xslt) throws IOException, TransformerConfigurationException {
        // if (!this.templates.containsKey(xslt)) {
        InputStream in = new DefaultResourceLoader().getResource(xslt).getInputStream();
        Source xsltSource = new SAXSource(new InputSource(in));
        TransformerFactory factory = TransformerFactory.newInstance();
        factory.setAttribute(FeatureKeys.DTD_VALIDATION, Boolean.FALSE); //TODO DTD?
        factory.setAttribute(FeatureKeys.SCHEMA_VALIDATION, 4);
        Templates xsltTemplate = factory.newTemplates(xsltSource);
        // this.templates.put(xslt, xsltTemplate);
        if (in != null)
            in.close();
        // }
        return xsltTemplate;
    }

    /**
     * ?????XSLT
     * 
     * @return new Transformer
     * @throws Exception
     *             ?XLST
     */
    public Transformer getTransformer(String xslt) throws IOException, TransformerConfigurationException {
        return getXsltTemplate(xslt).newTransformer();
    }

}