001/*
002 *  MsvXmlValidator.java
003 *  $Header$
004 *  Copyright (c) 2002 Tom B. Gutwin P.Eng.
005 *
006 *  This program is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU General Public License
008 *  as published by the Free Software Foundation; either version 2
009 *  of the License, or any later version.
010 *
011 *  This program is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 *  GNU General Public License for more details.
015 *
016 *  You should have received a copy of the GNU General Public License
017 *  along with this program; if not, write to the Free Software
018 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
019 */
020package ca.bc.webarts.tools;
021
022//import com.sun.msv.grammar.Grammar;
023import com.sun.msv.reader.util.GrammarLoader;
024import com.sun.msv.reader.util.IgnoreController;
025import com.sun.msv.verifier.DocumentDeclaration;
026import com.sun.msv.verifier.ValidityViolation;
027//import com.sun.msv.verifier.VerificationErrorHandler;
028import com.sun.msv.verifier.Verifier;
029import java.io.File;
030
031import java.net.URL;
032
033import javax.xml.parsers.SAXParserFactory;
034
035import org.dom4j.Document;
036//import org.dom4j.DocumentException;
037import org.dom4j.io.SAXReader;
038import org.dom4j.io.SAXWriter;
039
040import org.xml.sax.ContentHandler;
041import org.xml.sax.ErrorHandler;
042import org.xml.sax.Locator;
043import org.xml.sax.SAXParseException;
044
045
046/**
047 *  Reads in an XML file and indicates if it is valid based on its specified XML
048 *  Schema. This class uses dom4j. <PRE>
049 *  Syntax: java ca.bc.webarts.tools.MsvXmlValidator &lt;xml URL> &lt;xml schema URL>
050 *  </pre>
051 *
052 * @author     tgutwin
053 * @created    Oct 10, 2002
054 */
055public class MsvXmlValidator
056{
057
058  /**
059   *  The validation Main entry
060   *
061   * @param  argv  Description of the Parameter
062   */
063  public static void main(String argv[])
064  {
065    try
066    {
067      String filename = argv[0];
068      String schema = argv[1];
069
070      URL fileURL = new File(filename).toURL();
071      URL schemaURL = new File(schema).toURL();
072
073      SAXReader reader = new SAXReader();
074      Document doc = reader.read(fileURL);
075      validate(doc, schemaURL.toExternalForm());
076    }
077    catch (Exception e)
078    {
079      e.printStackTrace(System.err);
080    }
081  }
082
083
084  /**
085   *  Description of the Method
086   *
087   * @param  doc            The Xml Doc to Validate
088   * @param  schema         The Schema URL string that will be used to validate
089   *                        against
090   * @exception  Exception  Description of the Exception
091   */
092  public static void validate(Document doc, String schema)
093    throws Exception
094  {
095
096    // Turn on Namespace handling in theJAXP SAXParserFactory
097    SAXParserFactory saxFactory = SAXParserFactory.newInstance();
098    saxFactory.setNamespaceAware(true);
099
100    // create MSVs DocumentDeclaration by overriding
101    // a IgnoreController in an anonyous inner class
102    DocumentDeclaration docDeclaration =
103        GrammarLoader.loadVGM(schema,
104      new IgnoreController()
105      {
106
107        public void error(Locator[] locations,
108            String message,
109            Exception exception)
110        {
111          System.out.println("ERROR: " + message);
112        }
113
114
115        public void error(Locator[] locations, String message)
116        {
117          System.out.println("WARNING: " + message);
118        }
119      }, saxFactory);
120
121    // create a new Verifier that reports vlidation errors
122    // using an anonymous inner class
123    Verifier verifier =
124        new Verifier(docDeclaration,
125      new ErrorHandler()
126      {
127
128        public void onError(ValidityViolation e)
129        {
130          System.out.println("Document invalid! Error: " + e);
131        }
132
133        public void error(SAXParseException e)
134        {
135          System.out.println("Document invalid! Error: " + e);
136        }
137
138        public void fatalError(SAXParseException e)
139        {
140          System.out.println("Document invalid! Error: " + e);
141        }
142
143        public void onWarning(ValidityViolation e)
144        {
145          System.out.println("Document invalid! Warning: " + e);
146        }
147
148        public void warning(SAXParseException e)
149        {
150          System.out.println("Document invalid! Warning: " + e);
151        }
152      });
153
154    SAXWriter writer = new SAXWriter((ContentHandler) verifier);
155    writer.setErrorHandler(
156      new ErrorHandler()
157      {
158
159        public void error(SAXParseException e)
160        {
161          System.out.println("ERROR:" + e);
162        }
163
164
165        public void fatalError(SAXParseException e)
166        {
167          System.out.println("Fatal:" + e);
168        }
169
170
171        public void warning(SAXParseException e)
172        {
173          System.out.println("Warning:" + e);
174        }
175      });
176
177    // validate now!
178    writer.write(doc);
179    if (verifier.isValid())
180    {
181      System.err.println("The document was valid");
182    }
183    else
184    {
185      System.err.println("The document was not valid");
186    }
187  }
188}
189