Debian Codegraph @

src/main/java/net/sf/antcontrib/antclipse/ClassPathParser.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.antclipse;
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import javax.xml.parsers.ParserConfigurationException;
22 import javax.xml.parsers.SAXParser;
23 import javax.xml.parsers.SAXParserFactory;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Location;
27 import org.xml.sax.HandlerBase;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.SAXException;
30 import org.xml.sax.SAXParseException;
31
32 /**
33 * Classic tool firing a SAX parser. Must feed the source file and a handler.
34 * Nothing really special about it, only probably some special file handling in nasty cases
35 * (Windows files containing strange chars, internationalized filenames,
36 * but you shouldn't be doing this, anyway :)).
37 * @author Adrian Spinei [email protected]
38 * @version $Revision: 1.2 $
39 * @since Ant 1.5
40 */
41 public class ClassPathParser
42 {
43 void parse(File file, HandlerBase handler) throws BuildException
44 {
45 String fName = file.getName();
46 FileInputStream fileInputStream = null;
47 InputSource inputSource = null;
48 try
49 {
50 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
51 //go to UFS if we're on win
52 String uri = "file:" + fName.replace('\\', '/');
53 fileInputStream = new FileInputStream(file);
54 inputSource = new InputSource(fileInputStream);
55 inputSource.setSystemId(uri);
56 saxParser.parse(inputSource, handler);
57 }
58 catch (ParserConfigurationException pceException)
59 {
60 throw new BuildException("Parser configuration failed", pceException);
61 }
62 catch (SAXParseException exc)
63 {
64 Location location = new Location(fName.toString(), exc.getLineNumber(), exc.getColumnNumber());
65 Throwable throwable = exc.getException();
66 if ((Object) throwable instanceof BuildException)
67 {
68 BuildException be = (BuildException) (Object) throwable;
69 if (be.getLocation() == Location.UNKNOWN_LOCATION)
70 be.setLocation(location);
71 throw be;
72 }
73 throw new BuildException(exc.getMessage(), throwable, location);
74 }
75 catch (SAXException exc)
76 {
77 Throwable throwable = exc.getException();
78 if ((Object) throwable instanceof BuildException)
79 throw (BuildException) (Object) throwable;
80 throw new BuildException(exc.getMessage(), throwable);
81 }
82 catch (FileNotFoundException exc)
83 {
84 throw new BuildException(exc);
85 }
86 catch (IOException exc)
87 {
88 throw new BuildException("Error reading file", exc);
89 }
90 finally
91 {
92 if (fileInputStream != null)
93 {
94 try
95 {
96 fileInputStream.close();
97 }
98 catch (IOException ioexception)
99 {
100 //do nothing, should not appear
101 }
102 }
103 }
104 }
105 }