Example: PullValidator

import javax.xml.stream.*;
import java.net.*;
import java.io.*;

 
public class PullValidator {

  private static boolean valid;

  public static void main(String[] args) {
        
    if (args.length == 0) {
      System.err.println("Usage: java PullValidator url" );
      return;   
    }
        
    try {

      InputStream in;
      try {
        URL u = new URL(args[0]);
        in = u.openStream();
      }
      catch (MalformedURLException ex) {
          // Maybe it's a file name
          in = new FileInputStream(args[0]);
      }
      
      XMLInputFactory factory = XMLInputFactory.newInstance();
      if (!factory.isPropertySupported("javax.xml.stream.isValidating")) {
        System.err.println("This StAX implementation does not support validation.");
        return;   
      }
      factory.setProperty("javax.xml.stream.isValidating", Boolean.TRUE);
      XMLStreamReader parser = factory.createXMLStreamReader(in);
      
      valid = true;
      factory.setProperty("javax.xml.stream.reporter", new XMLReporter() {
        public void report(String message, String errorType, Object relatedInformation, Location location) {
          System.err.println(message);
          valid = false;
        }
      });
      
        
      while (true) {
           int event = parser.next();
           if (event == XMLStreamConstants.END_DOCUMENT) {
                parser.close();
                break;
           }
      }
      parser.close();
            
      // If we get here there are no exceptions
      if (valid) System.out.println(args[0] + " is valid.");      
      else System.out.println(args[0] + " is not valid.");      
    }
    catch (XMLStreamException ex) {
       System.out.println(args[0] + " is not well-formed."); 
       System.out.println(ex);  
    }
    catch (IOException ex) {
      System.out.println(args[0] + " could not be checked due to an " 
       + ex.getClass().getName());   
      ex.printStackTrace();      
    }
        
  }

}

Previous | Next | Top | Cafe con Leche

Copyright 2000-2004 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified January 8, 2004