KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > reporting > JUnitXMLParserEntityResolver


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

21
22 package net.sourceforge.cobertura.reporting;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.helpers.DefaultHandler JavaDoc;
31
32 /**
33  * <p>
34  * This is a very simple XML EntityResolver. If
35  * you are parsing an XML document using a DocumentBuilder,
36  * and you set the DocumentBuilder's EntityResolver to an
37  * instance of this class, then we never attempt to resolve
38  * XML documents on the Internet. Instead we use a local
39  * copy of the DTD.
40  * </p>
41  *
42  * <p>
43  * This is done so that the XMLReportTest.java JUnit test will
44  * not fail when the test is run on a non-networked machine,
45  * or when webpages must be accessed through a proxy server.
46  * </p>
47  */

48 public class JUnitXMLParserEntityResolver extends DefaultHandler JavaDoc
49 {
50
51     private final File JavaDoc DTD_DIRECTORY;
52
53     public JUnitXMLParserEntityResolver(File JavaDoc dtdDirectory)
54     {
55         this.DTD_DIRECTORY = dtdDirectory;
56     }
57
58     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
59             throws SAXException JavaDoc
60     {
61         System.out.println("systemId=" + systemId);
62         String JavaDoc systemIdBasename = systemId.substring(systemId.lastIndexOf('/'));
63         File JavaDoc localDtd = new File JavaDoc(this.DTD_DIRECTORY, systemIdBasename);
64         try
65         {
66             return new InputSource JavaDoc(new FileInputStream JavaDoc(localDtd));
67         }
68         catch (FileNotFoundException JavaDoc e)
69         {
70             System.out.println("Unable to open local DTD file "
71                     + localDtd.getAbsolutePath() + ", using " + systemId
72                     + " instead.");
73         }
74
75         InputSource JavaDoc source = null;
76
77         try {
78             super.resolveEntity(publicId, systemId);
79         } catch (Exception JavaDoc exception) {
80             // apparently 1.5 throws an IOException here, but we can't catch it specifically if
81
// we're not on 1.5 (docs on both kind of say that they throw it)
82
// actual code on 1.4.2 has it remmed out so that it only throws SAXException
83
throw new SAXException JavaDoc(exception);
84         }
85
86         return source;
87     }
88
89 }
90
Popular Tags