KickJava   Java API By Example, From Geeks To Geeks.

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


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

22
23 package net.sourceforge.cobertura.reporting;
24
25 import java.io.File JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import org.jdom.Document;
30 import org.jdom.JDOMException;
31 import org.jdom.input.SAXBuilder;
32
33 public class JUnitXMLHelper
34 {
35
36     private final static String JavaDoc BASEDIR = (System.getProperty("basedir") != null) ? System
37             .getProperty("basedir") : ".";
38
39     /**
40      * This reads the given file into an XML Document.
41      * @param file A valid file on the file system.
42      * @param validate Whether to validate the XML or not.
43      * @return An XML document representing the given XML file.
44      * @throws FileNotFoundException If the file does not exist.
45      * @throws IOException If the file could not be open/read.
46      * @throws JDOMException If the file is not well-formed XML, or
47      * if validation is enabled and the document is not
48      * valid.
49      */

50     public static Document readXmlFile(File JavaDoc file, boolean validate) throws FileNotFoundException JavaDoc,
51             IOException JavaDoc, JDOMException
52     {
53         System.out.println("Reading " + file.getAbsolutePath());
54
55         // First create an XML document parser
56
SAXBuilder saxBuilder = new SAXBuilder();
57         saxBuilder.setValidation(validate);
58         saxBuilder.setEntityResolver(new JUnitXMLParserEntityResolver(
59                 new File JavaDoc(BASEDIR, "/etc/dtds")));
60         saxBuilder.setErrorHandler(new JUnitXMLParserErrorHandler());
61         return saxBuilder.build(file);
62     }
63
64 }
65
Popular Tags