Building Controls Virtual Test Bed
DTDValidator.java
Go to the documentation of this file.
1 import java.io.File;
2 import java.io.IOException;
3 import javax.xml.parsers.DocumentBuilderFactory;
4 import javax.xml.parsers.DocumentBuilder;
5 import javax.xml.parsers.ParserConfigurationException;
6 import org.w3c.dom.Document;
7 import org.xml.sax.SAXException;
8 import org.xml.sax.SAXParseException;
9 import org.xml.sax.ErrorHandler;
10 
17 public class DTDValidator {
19  static String fil;
20 
26  public static void main(String[] args) {
27  fil = args[0];
28  // Document builder factory
29  DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
30  fac.setValidating(true);
31  try {
32  // Document builder and error handler
33  DocumentBuilder b = fac.newDocumentBuilder();
34  b.setErrorHandler(new MyErrorHandler());
35  // Parse document
36  // Document d = b.parse(args[0]);
37  Document d = b.parse(new java.io.FileInputStream(fil),
38  args[1] + File.separator);
39  }
40  catch(SAXException e) {
41  System.err.println(e.getMessage());
42  }
43  catch(IOException e) {
44  System.err.println(e.getMessage());
45  }
46  catch (ParserConfigurationException e) {
47  System.err.println(e.getMessage());
48  }
49  }
52  private static class MyErrorHandler implements ErrorHandler {
53 
59  public void warning(SAXParseException e) throws SAXException {
60  printInfo("Warning: ", e);
61  System.exit(1);
62  }
63 
69  public void error(SAXParseException e) throws SAXException {
70  printInfo("Error: ", e);
71  System.exit(1);
72  }
73 
79  public void fatalError(SAXParseException e) throws SAXException {
80  error(e);
81  }
82 
83 
89  private void printInfo(String s, SAXParseException e) {
90  System.err.println(s +
91  fil + ":"
92  + e.getLineNumber() + ": "
93  + e.getMessage());
94  }
95  }
96 }
97 /********************************************************************
98 Copyright Notice
99 ----------------
100 
101 Building Controls Virtual Test Bed (BCVTB) Copyright (c) 2008-2009, The
102 Regents of the University of California, through Lawrence Berkeley
103 National Laboratory (subject to receipt of any required approvals from
104 the U.S. Dept. of Energy). All rights reserved.
105 
106 If you have questions about your rights to use or distribute this
107 software, please contact Berkeley Lab's Technology Transfer Department
108 at TTD@lbl.gov
109 
110 NOTICE. This software was developed under partial funding from the U.S.
111 Department of Energy. As such, the U.S. Government has been granted for
112 itself and others acting on its behalf a paid-up, nonexclusive,
113 irrevocable, worldwide license in the Software to reproduce, prepare
114 derivative works, and perform publicly and display publicly. Beginning
115 five (5) years after the date permission to assert copyright is obtained
116 from the U.S. Department of Energy, and subject to any subsequent five
117 (5) year renewals, the U.S. Government is granted for itself and others
118 acting on its behalf a paid-up, nonexclusive, irrevocable, worldwide
119 license in the Software to reproduce, prepare derivative works,
120 distribute copies to the public, perform publicly and display publicly,
121 and to permit others to do so.
122 
123 
124 Modified BSD License agreement
125 ------------------------------
126 
127 Building Controls Virtual Test Bed (BCVTB) Copyright (c) 2008-2009, The
128 Regents of the University of California, through Lawrence Berkeley
129 National Laboratory (subject to receipt of any required approvals from
130 the U.S. Dept. of Energy). All rights reserved.
131 
132 Redistribution and use in source and binary forms, with or without
133 modification, are permitted provided that the following conditions are met:
134 
135  1. Redistributions of source code must retain the above copyright
136  notice, this list of conditions and the following disclaimer.
137  2. Redistributions in binary form must reproduce the above copyright
138  notice, this list of conditions and the following disclaimer in
139  the documentation and/or other materials provided with the
140  distribution.
141  3. Neither the name of the University of California, Lawrence
142  Berkeley National Laboratory, U.S. Dept. of Energy nor the names
143  of its contributors may be used to endorse or promote products
144  derived from this software without specific prior written permission.
145 
146 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
147 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
148 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
149 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
150 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
151 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
152 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
153 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
154 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
155 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
156 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
157 
158 You are under no obligation whatsoever to provide any bug fixes,
159 patches, or upgrades to the features, functionality or performance of
160 the source code ("Enhancements") to anyone; however, if you choose to
161 make your Enhancements available either publicly, or directly to
162 Lawrence Berkeley National Laboratory, without imposing a separate
163 written license agreement for such Enhancements, then you hereby grant
164 the following license: a non-exclusive, royalty-free perpetual license
165 to install, use, modify, prepare derivative works, incorporate into
166 other computer software, distribute, and sublicense such enhancements or
167 derivative works thereof, in binary and source code form.
168 
169 ********************************************************************
170 */
void fatalError(SAXParseException e)
Writes error messages to System.err
void warning(SAXParseException e)
Writes warning messages to System.err
static void main(String[] args)
Main method.
void printInfo(String s, SAXParseException e)
Prints the error message to System.err
Inner class for error handling.
void error(SAXParseException e)
Writes error messages to System.err
Class to validate xml files.