001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.core;
020    
021    import java.io.BufferedInputStream;
022    import java.io.FileInputStream;
023    import java.io.FileNotFoundException;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.net.MalformedURLException;
027    import java.net.URL;
028    import java.util.List;
029    import java.util.Set;
030    import java.util.Vector;
031    import org.apache.log4j.Logger;
032    
033    /**
034     * This is the default implementation for the aspect-configuration files.
035     *
036     * <p>For the moment, it supports XML and ACC formats.
037     */
038    
039    public class ParserImpl implements Parser 
040    {
041        static Logger logger = Logger.getLogger("parser");
042    
043        public List parse(String filePath, String targetClass, 
044                          Set blockKeywords) throws IOException
045        {
046            InputStream inputStream = null;
047            try {
048                logger.debug("FileInputStream("+filePath+")");
049                inputStream = new FileInputStream(filePath);
050            } catch (FileNotFoundException e) {
051                try {
052                    logger.debug("getResourceAsStream("+filePath+")");
053                    inputStream = 
054                        getClass().getClassLoader().getResourceAsStream(filePath);
055                    if (inputStream==null) {
056                        logger.warn("Resource "+filePath+" not found");
057                    }
058                } catch (Exception e2) {
059                    logger.debug("caught exception "+e2);
060                    // uncaught MalformedURLException
061                    logger.debug("new URL("+filePath+")");
062                    try {
063                        URL url = new URL(filePath);
064                        inputStream = url.openStream();
065                    } catch (MalformedURLException e3) {
066                        e3.printStackTrace();
067                        return null;
068                    }
069                }
070            }
071            if (inputStream!=null) {
072                return parse(new BufferedInputStream(inputStream),
073                             filePath,targetClass,blockKeywords);
074            } else {
075                return new Vector();
076            }
077        }
078    
079        public List parse(InputStream inputStream, String filePath, 
080                          String targetClass, Set blockKeywords) {
081            List methods = null;
082            if (filePath.endsWith(".xml")) {
083                try {
084                    InputStreamParser parser = 
085                        (InputStreamParser)(Class.forName(
086                            "org.objectweb.jac.core.parsers.xml.JacXmlParser").newInstance());
087                    methods = parser.parse(inputStream, filePath, 
088                                           targetClass, blockKeywords);
089                } catch (Exception e) {
090                    logger.error("Exception during xml parsing",e);
091                }
092            } else if (filePath.endsWith(".acc")) {
093                try {
094                    InputStreamParser parser = 
095                        (InputStreamParser)(Class.forName(
096                            "org.objectweb.jac.core.parsers.acc.AccParserWrapper").newInstance());
097                    methods = parser.parse(inputStream, filePath, 
098                                           targetClass, blockKeywords);
099                } catch (Exception e) {
100                    logger.error("Exception during acc parsing",e);
101                }
102            } else {
103                logger.warn("No parser found for "+filePath);
104                methods = new Vector();
105            }
106            return methods;
107        }
108    }