1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.castor.xmlctf.xmldiff.xml;
19
20 import java.io.File;
21 import java.io.FileReader;
22 import java.net.MalformedURLException;
23
24 import javax.xml.parsers.SAXParser;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.castor.core.util.Messages;
28 import org.castor.xmlctf.xmldiff.xml.nodes.Root;
29 import org.castor.xmlctf.xmldiff.xml.nodes.XMLNode;
30 import org.exolab.castor.util.NestedIOException;
31 import org.exolab.castor.xml.util.XMLParserUtils;
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.SAXParseException;
35 import org.xml.sax.XMLReader;
36
37
38
39
40
41
42
43
44
45
46 public class XMLFileReader {
47
48
49
50
51
52 private static final Log LOG = LogFactory.getLog(XMLFileReader.class);
53
54
55 private final File _file;
56
57 private final String _location;
58
59 private XMLReader _parser;
60
61
62
63
64
65
66 public XMLFileReader(final String filename) {
67 if (filename == null) {
68 throw new IllegalArgumentException("You must give a non-null fliename");
69 }
70 _file = new File(filename);
71 if (!_file.exists()) {
72 throw new IllegalArgumentException("File '" + filename + "' does not exist");
73 }
74
75 _location = getUrlFromFile();
76
77 SAXParser saxParser = XMLParserUtils.getSAXParser(false, true);
78 try {
79 _parser = saxParser.getXMLReader();
80 }
81 catch(org.xml.sax.SAXException sx) {
82 LOG.error(Messages.format("conf.configurationError", sx));
83 }
84
85 if (_parser == null) {
86 _parser = XMLParserUtils.instantiateXMLReader("org.apache.xerces.parsers.SAXParser");
87 }
88 }
89
90
91
92
93
94
95
96 public XMLNode read() throws java.io.IOException {
97 XMLNode node = null;
98
99 try {
100 InputSource source = new InputSource();
101 source.setSystemId(_location);
102 source.setCharacterStream(new FileReader(_file));
103
104 XMLContentHandler builder = new XMLContentHandler();
105
106 _parser.setContentHandler(builder);
107 _parser.parse(source);
108
109 node = builder.getRoot();
110 } catch (SAXException sx) {
111 Exception nested = sx.getException();
112
113 SAXParseException sxp = null;
114 if (sx instanceof SAXParseException) {
115 sxp = (SAXParseException) sx;
116 } else if (nested != null && (nested instanceof SAXParseException)) {
117 sxp = (SAXParseException) nested;
118 } else {
119 throw new NestedIOException(sx);
120 }
121
122 StringBuffer err = new StringBuffer(sxp.toString());
123 err.append("\n - ");
124 err.append(sxp.getSystemId());
125 err.append("; line: ");
126 err.append(sxp.getLineNumber());
127 err.append(", column: ");
128 err.append(sxp.getColumnNumber());
129 throw new NestedIOException(err.toString(), sx);
130 }
131
132 Root root = (Root) node;
133 return root;
134 }
135
136
137
138
139
140
141 private String getUrlFromFile() {
142 try {
143 return _file.toURL().toString();
144 } catch (MalformedURLException e) {
145
146 }
147 return null;
148 }
149
150 }