KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > ParserImpl


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Vector JavaDoc;
31 import org.apache.log4j.Logger;
32
33 /**
34  * This is the default implementation for the aspect-configuration files.
35  *
36  * <p>For the moment, it supports XML and ACC formats.
37  */

38
39 public class ParserImpl implements Parser
40 {
41     static Logger logger = Logger.getLogger("parser");
42
43     public List JavaDoc parse(String JavaDoc filePath, String JavaDoc targetClass,
44                       Set JavaDoc blockKeywords) throws IOException JavaDoc
45     {
46         InputStream JavaDoc inputStream = null;
47         try {
48             logger.debug("FileInputStream("+filePath+")");
49             inputStream = new FileInputStream JavaDoc(filePath);
50         } catch (FileNotFoundException JavaDoc e) {
51             try {
52                 logger.debug("getResourceAsStream("+filePath+")");
53                 inputStream =
54                     getClass().getClassLoader().getResourceAsStream(filePath);
55                 if (inputStream==null) {
56                     logger.warn("Resource "+filePath+" not found");
57                 }
58             } catch (Exception JavaDoc e2) {
59                 logger.debug("caught exception "+e2);
60                 // uncaught MalformedURLException
61
logger.debug("new URL("+filePath+")");
62                 try {
63                     URL JavaDoc url = new URL JavaDoc(filePath);
64                     inputStream = url.openStream();
65                 } catch (MalformedURLException JavaDoc e3) {
66                     e3.printStackTrace();
67                     return null;
68                 }
69             }
70         }
71         if (inputStream!=null) {
72             return parse(new BufferedInputStream JavaDoc(inputStream),
73                          filePath,targetClass,blockKeywords);
74         } else {
75             return new Vector JavaDoc();
76         }
77     }
78
79     public List JavaDoc parse(InputStream JavaDoc inputStream, String JavaDoc filePath,
80                       String JavaDoc targetClass, Set JavaDoc blockKeywords) {
81         List JavaDoc methods = null;
82         if (filePath.endsWith(".xml")) {
83             try {
84                 InputStreamParser parser =
85                     (InputStreamParser)(Class.forName(
86                         "org.objectweb.jac.core.parsers.xml.JacXmlParser").newInstance());
87                 methods = parser.parse(inputStream, filePath,
88                                        targetClass, blockKeywords);
89             } catch (Exception JavaDoc e) {
90                 logger.error("Exception during xml parsing",e);
91             }
92         } else if (filePath.endsWith(".acc")) {
93             try {
94                 InputStreamParser parser =
95                     (InputStreamParser)(Class.forName(
96                         "org.objectweb.jac.core.parsers.acc.AccParserWrapper").newInstance());
97                 methods = parser.parse(inputStream, filePath,
98                                        targetClass, blockKeywords);
99             } catch (Exception JavaDoc e) {
100                 logger.error("Exception during acc parsing",e);
101             }
102         } else {
103             logger.warn("No parser found for "+filePath);
104             methods = new Vector JavaDoc();
105         }
106         return methods;
107     }
108 }
109
Popular Tags