/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */
package org.jfox.ejb.meta;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.jfox.system.JFoxMeta;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class EJBEntityResolver implements EntityResolver {
    // public id -> local dtd name
    // local dtd in the JFOX_HOME/dtd/dtd.properties
    private static Properties pubToLocal = new Properties();
    private final static String DTD_PATH = JFoxMeta.getJFoxHome() + "/dtd/";

    static {
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(DTD_PATH + "dtd.properties");
            pubToLocal.load(fin);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            if(fin != null) {
                try {
                    fin.close();
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        if(publicId == null) return null;
        // let it be
        if(!pubToLocal.containsKey(publicId)) return null;

        String dtd = pubToLocal.getProperty(publicId);
        try {
            FileInputStream dtdStream = new FileInputStream(DTD_PATH + dtd);
            InputSource inputSource = new InputSource(dtdStream);
            return inputSource;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {

    }
}

