~ubuntu-branches/ubuntu/karmic/cobertura/karmic

« back to all changes in this revision

Viewing changes to test/net/sourceforge/cobertura/reporting/JUnitXMLParserEntityResolver.java

  • Committer: Bazaar Package Importer
  • Author(s): Yulia Novozhilova
  • Date: 2009-06-24 13:56:29 UTC
  • Revision ID: james.westby@ubuntu.com-20090624135629-hgvo8631yye7ofj3
Tags: upstream-1.9
ImportĀ upstreamĀ versionĀ 1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
25
import java.io.FileInputStream;
 
26
import java.io.FileNotFoundException;
 
27
 
 
28
import org.xml.sax.InputSource;
 
29
import org.xml.sax.SAXException;
 
30
import org.xml.sax.helpers.DefaultHandler;
 
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
 
49
{
 
50
 
 
51
        private final File DTD_DIRECTORY;
 
52
 
 
53
        public JUnitXMLParserEntityResolver(File dtdDirectory)
 
54
        {
 
55
                this.DTD_DIRECTORY = dtdDirectory;
 
56
        }
 
57
 
 
58
        public InputSource resolveEntity(String publicId, String systemId)
 
59
                        throws SAXException
 
60
        {
 
61
                System.out.println("systemId=" + systemId);
 
62
                String systemIdBasename = systemId.substring(systemId.lastIndexOf('/'));
 
63
                File localDtd = new File(this.DTD_DIRECTORY, systemIdBasename);
 
64
                try
 
65
                {
 
66
                        return new InputSource(new FileInputStream(localDtd));
 
67
                }
 
68
                catch (FileNotFoundException e)
 
69
                {
 
70
                        System.out.println("Unable to open local DTD file "
 
71
                                        + localDtd.getAbsolutePath() + ", using " + systemId
 
72
                                        + " instead.");
 
73
                }
 
74
 
 
75
                InputSource source = null;
 
76
 
 
77
                try {
 
78
                        super.resolveEntity(publicId, systemId);
 
79
                } catch (Exception 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(exception);
 
84
                }
 
85
 
 
86
                return source;
 
87
        }
 
88
 
 
89
}