Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that validates XML documents
4
 *             This class is designed to be 'parser independent
5
 *             i.e. it uses only org.xml.sax classes
6
 *             It is tied to SAX 2.0 methods
7
 *  Copyright: 2000 Regents of the University of California and the
8
 *             National Center for Ecological Analysis and Synthesis
9
 *    Authors: Dan Higgins, Matt Jones
10
 * 
11
 *   '$Author: daigle $'
12
 *     '$Date: 2009-08-24 14:34:17 -0700 (Mon, 24 Aug 2009) $'
13
 * '$Revision: 5030 $'
14
 *
15
 * This program is free software; you can redistribute it and/or modify
16
 * it under the terms of the GNU General Public License as published by
17
 * the Free Software Foundation; either version 2 of the License, or
18
 * (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 */
29
package edu.ucsb.nceas.metacat;
30

    
31

    
32
import java.net.URL;
33
import java.net.MalformedURLException;
34
import java.util.*;
35
import java.io.*;
36
import java.lang.reflect.*;
37
import java.sql.*;
38

    
39
import org.w3c.dom.*;
40
import org.xml.sax.*;
41
import org.xml.sax.helpers.XMLReaderFactory;
42

    
43
import com.arbortext.catalog.*;
44

    
45
import edu.ucsb.nceas.metacat.database.DBConnection;
46
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
47
import edu.ucsb.nceas.metacat.properties.PropertyService;
48

    
49
/**
50
 * Name: DBValidate.java
51
 *       Purpose: A Class that validates XML documents
52
 * 			   This class is designed to be parser independent
53
 *    			   i.e. it uses only org.xml.sax classes
54
 * 			   It is tied to SAX 2.0 methods
55
 *     Copyright: 2000 Regents of the University of California and the
56
 *                National Center for Ecological Analysis and Synthesis
57
 *                April 28, 2000
58
 *    Authors: Dan Higgins, Matt Jones
59
 */
60
public class DBValidate {
61
    
62
  static int WARNING = 0;
63
  static int ERROR=1;
64
  static int FATAL_ERROR=2;
65

    
66
  XMLReader parser;
67
  ErrorStorer ef;
68
  String xml_doc; // document to be parsed
69
  public boolean alreadyHandle = false;
70
    
71
  /** Construct a new validation object */
72
  public DBValidate() {
73
    alreadyHandle = false;
74
    try {
75
      // Get an instance of the parser
76
      String parserName = PropertyService.getProperty("xml.saxparser");
77
      parser = XMLReaderFactory.createXMLReader(parserName);
78
      parser.setFeature("http://xml.org/sax/features/validation",true);
79
      //parser.setValidationMode(true);     // Oracle
80
    } catch (Exception e) {
81
      System.err.println("Could not create parser in DBValidate.DBValidate");
82
    }
83
  }
84
    
85
  /** Construct a new validation object using an OASIS catalog file */
86
  public DBValidate(String xmlcatalogfile)  {
87
    this();
88

    
89
    CatalogEntityResolver cer = new CatalogEntityResolver();
90
    try {
91
      Catalog myCatalog = new Catalog();
92
      myCatalog.loadSystemCatalogs();
93
      myCatalog.parseCatalog(xmlcatalogfile);
94
      cer.setCatalog(myCatalog);
95
    } catch (Exception e) {
96
      System.out.println("Problem creating Catalog in DBValidate.DBValidate");
97
    }
98

    
99
    parser.setEntityResolver(cer);
100
  }
101

    
102
  /** Construct a new validation object using a database entity resolver */
103
  public DBValidate(DBConnection conn) {
104
    this();
105

    
106
    DBEntityResolver dbresolver = new DBEntityResolver(conn);
107
    parser.setEntityResolver(dbresolver);
108
  }
109

    
110
  /** 
111
   * validate an xml document against its DTD
112
   *
113
   * @param doc the filename of the document to validate
114
   */
115
  public boolean validate(String doc) {
116
    xml_doc = doc;    
117
    ef = new ErrorStorer();
118
    ef.resetErrors();
119
    parser.setErrorHandler(ef);
120
    try {
121
      parser.parse((createURL(xml_doc)).toString());
122
    } catch (IOException e) {
123
      System.out.println("IOException:Could not parse :" + xml_doc +
124
                         " from DBValidate.validate");
125
      ParseError eip = null;
126
      eip = new ParseError("",0,0,
127
                "IOException:Could not parse :"+xml_doc);
128
      if (ef.errorNodes == null)  ef.errorNodes = new Vector();
129
      ef.errorNodes.addElement(eip);
130
        
131
    } catch (Exception e) {} 
132

    
133
    
134
    
135
    if (ef != null && ef.getErrorNodes()!=null && 
136
      ef.getErrorNodes().size() > 0 ) {
137
      return false; 
138
    } else {
139
      return true;
140
    }
141
  }
142
    
143
  /** 
144
   * validate an xml document against its DTD
145
   *
146
   * @param xmldoc the String containing the xml document to validate
147
   */
148
  public boolean validateString(String xmldoc) {
149
    // string is actual XML here, NOT URL or file name    
150
    ef = new ErrorStorer();
151
    ef.resetErrors();
152
    parser.setErrorHandler(ef);
153
      
154
    InputSource is = new InputSource(new StringReader(xmldoc));
155
    try 
156
    {
157
      
158
      parser.parse(is);
159
     
160
    }
161
    catch (SAXParseException e) 
162
    {
163
      System.out.println("SAXParseException Error in DBValidate.validateString"
164
                         +e.getMessage());
165
      ef.error(e);
166
    }
167
    catch (SAXException saxe)
168
    {
169
      System.out.println("SAXException error in validateString: "
170
                          +saxe.getMessage());
171
      ef.otherError(saxe, null);
172
      
173
    }
174
    catch (IOException ioe)
175
    {
176
      System.out.println("IOExcption error in validateString "
177
                          +ioe.getMessage());
178
      ef.otherError(ioe, null);
179
    }
180

    
181
    if (ef != null && ef.getErrorNodes()!=null && 
182
      ef.getErrorNodes().size() > 0 ) {
183
      return false; 
184
    } else {
185
      return true;
186
    }
187
  }
188
    
189
  /** provide a list of errors from the validation process */
190
  public String returnErrors() {
191
    StringBuffer errorstring = new StringBuffer();
192
    errorstring.append("<?xml version=\"1.0\" ?>\n");
193
    if (ef != null && ef.getErrorNodes()!=null && 
194
        ef.getErrorNodes().size() > 0 ) {
195
      Vector errors = ef.getErrorNodes();
196
      errorstring.append("<validationerrors>\n");
197
      for (Enumeration e = errors.elements() ; e.hasMoreElements() ;) {
198
        errorstring.append(
199
                      ((ParseError)(e.nextElement())).toXML());
200
      }
201
      errorstring.append("</validationerrors>\n");
202
    } else {
203
      errorstring.append("<valid />\n");
204
    }
205
    return errorstring.toString();
206
  }
207
              
208
  /** Create a URL object from either a URL string or a plain file name. */
209
  private URL createURL(String name) throws Exception {
210
    try {
211
      URL u = new URL(name);
212
      return u;
213
    } catch (MalformedURLException ex) {
214
    }
215
    URL u = new URL("file:" + new File(name).getAbsolutePath());
216
    return u;
217
  }    
218

    
219
  /** 
220
   * main method for testing 
221
   * <p>
222
   * Usage: java DBValidate <xmlfile or URL>
223
   */
224
  public static void main(String[] args) {
225

    
226
    if (args.length != 1) {
227
      System.out.println("Usage: java DBValidate <xmlfile or URL>");
228
      System.exit(0);
229
    }
230

    
231
    String doc = args[0];
232

    
233
    DBConnection conn = null;
234
    int serailNumber = -1;
235
    try {
236
      conn = DBConnectionPool.getDBConnection("DBValidate.main");
237
      serailNumber = conn.getCheckOutSerialNumber();
238
  
239
      DBValidate gxv = new DBValidate(conn);
240
      if (gxv.validate(doc)) {
241
        System.out.print(gxv.returnErrors());
242
      } else {
243
        System.out.print(gxv.returnErrors());
244
      }
245
    } catch (SQLException e) {
246
      System.out.println("<error>Couldn't open database connection.</error>");
247
    } 
248
    finally
249
    {
250
      DBConnectionPool.returnDBConnection(conn, serailNumber);
251
    }//finally
252
  }
253

    
254
    
255
  /**
256
   * ErrorStorer has been revised here to simply create a Vector of 
257
   * ParseError objects
258
   *
259
   */
260
  class ErrorStorer implements ErrorHandler {
261

    
262
    //
263
    // Data
264
    //
265
    Vector errorNodes = null;
266
        
267
    /**
268
     * Constructor
269
     */
270
    public ErrorStorer() {
271
    }
272

    
273
    /**
274
     * The client is is allowed to get a reference to the Hashtable,
275
     * and so could corrupt it, or add to it...
276
     */
277
    public Vector getErrorNodes() {
278
        return errorNodes;
279
    }
280

    
281
    /**
282
     * The ParseError object for the node key is returned.
283
     * If the node doesn't have errors, null is returned.
284
     */
285
    public Object getError() {
286
        if (errorNodes == null)
287
            return null;
288
        return errorNodes;
289
    }
290
        
291
    /**
292
     * Reset the error storage.
293
     */
294
    public void resetErrors() {
295
        if (errorNodes != null)
296
        errorNodes.removeAllElements();
297
    }
298
    
299
    /***/
300
    public void warning(SAXParseException ex) {
301
        
302
        handleError(ex, WARNING);
303
        
304
    }
305

    
306
    public void error(SAXParseException ex) {
307
      
308
        handleError(ex, ERROR);
309
       
310
    }
311

    
312
    public void fatalError(SAXParseException ex){
313
      
314
        handleError(ex, FATAL_ERROR);
315
        
316
    }
317
    
318
    public void otherError(Exception ex, String fileName)
319
    {
320
      if (!alreadyHandle)
321
      {
322
        if (errorNodes == null) 
323
        {
324
          errorNodes = new Vector();
325
        }
326
      
327
        ParseError error = new ParseError(fileName, ex.getMessage());
328
        errorNodes.addElement(error);
329
       
330
      }
331
    }
332
        
333
    private void handleError(SAXParseException ex, int type) {
334
     
335
      if (errorNodes == null) {
336
        errorNodes = new Vector();
337
      }
338
      
339
      ParseError eip = null;
340
      
341
      eip = new ParseError(ex.getSystemId(), ex.getLineNumber(),
342
                           ex.getColumnNumber(), ex.getMessage());
343
      
344
      // put it in the Hashtable.
345
      errorNodes.addElement(eip);
346
      alreadyHandle = true;
347
      
348
    }
349
        
350
  }
351
    
352
  /**
353
   * The ParseError class wraps up all the error info from
354
   * the ErrorStorer's error method.
355
   *
356
   * @see ErrorStorer
357
   */
358
  class ParseError extends Object {
359

    
360
    //
361
    // Data
362
    //
363

    
364
    String fileName;
365
    int lineNo;
366
    int charOffset;
367
    String msg;
368

    
369
    /**
370
     * Constructor
371
     */
372
    public ParseError(String fileName, int lineNo, int charOffset, String msg) {
373
      this.fileName=fileName;
374
      this.lineNo=lineNo;
375
      this.charOffset=charOffset;
376
      this.msg=msg;
377
    }
378
    public ParseError(String fileName, String msg) {
379
      this.fileName=fileName;
380
      this.msg=msg;
381
    }
382
    //
383
    // Getters...
384
    //
385
    public String getFileName() { return fileName; }
386
    public int getLineNo() { return lineNo; }
387
    public int getCharOffset() { return charOffset;}
388
    public String getMsg() { return msg; }
389
    public void setMsg(String s) { msg = s; }
390

    
391
    /** Return the error message as an xml fragment */
392
    public String toXML() {
393
      StringBuffer err = new StringBuffer();
394
      err.append("<error>\n");
395
      err.append("<filename>").append(getFileName()).append("</filename>\n");
396
      err.append("<line>").append(getLineNo()).append("</line>\n");
397
      err.append("<offset>").append(getCharOffset()).append("</offset>\n");
398
      err.append("<message>").append(getMsg()).append("</message>\n");
399
      err.append("</error>\n");
400
      return err.toString();
401
    }
402
  }
403
}
(23-23/65)