Home » Xerces-J-src.2.9.1 » dom » [javadoc | source]

    1   /*
    2    * The Apache Software License, Version 1.1
    3    *
    4    *
    5    * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
    6    * reserved.
    7    *
    8    * Redistribution and use in source and binary forms, with or without
    9    * modification, are permitted provided that the following conditions
   10    * are met:
   11    *
   12    * 1. Redistributions of source code must retain the above copyright
   13    *    notice, this list of conditions and the following disclaimer.
   14    *
   15    * 2. Redistributions in binary form must reproduce the above copyright
   16    *    notice, this list of conditions and the following disclaimer in
   17    *    the documentation and/or other materials provided with the
   18    *    distribution.
   19    *
   20    * 3. The end-user documentation included with the redistribution,
   21    *    if any, must include the following acknowledgment:
   22    *       "This product includes software developed by the
   23    *        Apache Software Foundation (http://www.apache.org/)."
   24    *    Alternately, this acknowledgment may appear in the software itself,
   25    *    if and wherever such third-party acknowledgments normally appear.
   26    *
   27    * 4. The names "Xerces" and "Apache Software Foundation" must
   28    *    not be used to endorse or promote products derived from this
   29    *    software without prior written permission. For written
   30    *    permission, please contact apache@apache.org.
   31    *
   32    * 5. Products derived from this software may not be called "Apache",
   33    *    nor may "Apache" appear in their name, without prior written
   34    *    permission of the Apache Software Foundation.
   35    *
   36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   47    * SUCH DAMAGE.
   48    * ====================================================================
   49    *
   50    * This software consists of voluntary contributions made by many
   51    * individuals on behalf of the Apache Software Foundation and was
   52    * originally based on software copyright (c) 2002, International
   53    * Business Machines, Inc., http://www.apache.org.  For more
   54    * information on the Apache Software Foundation, please see
   55    * <http://www.apache.org/>.
   56    */
   57   
   58   package dom;
   59   
   60   import org.apache.xerces.dom3.DOMConfiguration;
   61   import org.apache.xerces.dom3.DOMError;
   62   import org.apache.xerces.dom3.DOMErrorHandler;
   63   
   64   import org.xml.sax.SAXException;
   65   import org.xml.sax.SAXParseException;
   66   
   67   import org.apache.xerces.dom.ASDOMImplementationImpl;
   68   import org.apache.xerces.dom3.as.DOMImplementationAS;
   69   import org.apache.xerces.dom3.as.ASModel;
   70   import org.apache.xerces.dom3.as.DOMASBuilder;
   71   
   72   import org.w3c.dom.DOMImplementation;
   73   import java.util.Vector;
   74   
   75   /**
   76    * This sample program illustrates how to use DOM3 DOMASBuilder interface to
   77    * preparse ASModels and associate ASModels with an instance document to be
   78    * validated.
   79    * <p>
   80    * Xerces only support preparsing XML Schema grammars, so all ASModel
   81    * appears in this sample program are assumed to be XML Schema grammars. When
   82    * Xerces provide more complete DOM AS support, the sample should be extended
   83    * for other types of grammars.
   84    * <p>
   85    * <p>
   86    * Since XML Schema document might import other schemas: it is better to set each 
   87    * parsed schema (ASModel)  on the parser before parsing another schema document. 
   88    * The schema on the parser will be used in the case it is referenced from a 
   89    * schema document the parser is parsing.
   90    * If a schema document imports other schemas, the parser returns a container ASModel that
   91    * includes the list of all schemas that are referenced plus the schema that was set
   92    * on the parser.
   93    * NOTE: this behavior might be changed
   94    * @deprecated
   95    * @author Sandy Gao, IBM
   96    * @version $Id: ASBuilder.java,v 1.13 2004/01/23 23:38:06 mrglavas Exp $
   97    */
   98   public class ASBuilder implements DOMErrorHandler {
   99   
  100       //
  101       // Constants
  102       //
  103   
  104       // feature ids
  105   
  106       /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
  107       protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
  108   
  109       /** Validation feature id (http://xml.org/sax/features/validation). */
  110       protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
  111   
  112       /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */
  113       protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema";
  114   
  115       /** Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). */
  116       protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
  117   
  118       // default settings
  119   
  120       /** Default Schema full checking support (false). */
  121       protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false;
  122   
  123       //
  124       // MAIN
  125       //
  126   
  127       /** Main program entry point. */
  128       public static void main(String argv[]) {
  129   
  130           // too fee parameters
  131           if (argv.length < 2) {
  132               printUsage();
  133               System.exit(1);
  134           }
  135           // get DOM implementation
  136           DOMImplementationAS domImpl = (DOMImplementationAS)ASDOMImplementationImpl.getDOMImplementation();
  137           // create a new parser, and set the error handler
  138           DOMASBuilder parser = domImpl.createDOMASBuilder();
  139           DOMConfiguration config = parser.getDomConfig();
  140           config.setParameter("error-handler", new ASBuilder());
  141   
  142           boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING;
  143   
  144           String arg = null;
  145           int i = 0;
  146   
  147           // process -f/F
  148           arg = argv[i];
  149           if (arg.equals("-f")) {
  150               schemaFullChecking = true;
  151               arg = argv[++i];
  152           } else if (arg.equals("-F")) {
  153               schemaFullChecking = false;
  154               arg = argv[++i];
  155           }
  156   
  157           // set the features. since we only deal with schema, some features have
  158           // to be true
  159           config.setParameter(NAMESPACES_FEATURE_ID, Boolean.TRUE);
  160           config.setParameter(VALIDATION_FEATURE_ID, Boolean.TRUE);
  161           
  162           config.setParameter(SCHEMA_VALIDATION_FEATURE_ID, Boolean.TRUE);
  163           config.setParameter(SCHEMA_FULL_CHECKING_FEATURE_ID, 
  164           (schemaFullChecking)?Boolean.TRUE:Boolean.FALSE);
  165   
  166           // process -a: as model files
  167           if (!arg.equals("-a")) {
  168               printUsage();
  169               System.exit(1);
  170           }
  171   
  172           i++;
  173           Vector asfiles = new Vector();
  174           while (i < argv.length && !(arg = argv[i]).startsWith("-")) {
  175               asfiles.addElement(arg);
  176               i++;
  177           }
  178   
  179           // has to be at least one as file, and there has to be other parameters
  180           if (asfiles.size() == 0) {
  181               printUsage();
  182               System.exit(1);
  183           }
  184   
  185           // process -i: instance files, if any
  186           Vector ifiles = null;
  187           if (i < argv.length) {
  188               if (!arg.equals("-i")) {
  189                   printUsage();
  190                   System.exit(1);
  191               }
  192   
  193               i++;
  194               ifiles = new Vector();
  195               while (i < argv.length && !(arg = argv[i]).startsWith("-")) {
  196                   ifiles.addElement(arg);
  197                   i++;
  198               }
  199   
  200               // has to be at least one instance file, and there has to be no more
  201               // parameters
  202               if (ifiles.size() == 0 || i != argv.length) {
  203                   printUsage();
  204                   System.exit(1);
  205               }
  206           }
  207   
  208           //
  209           // PARSING XML SCHEMAS
  210           //
  211   
  212           try {
  213               ASModel asmodel = null;
  214               for (i = 0; i < asfiles.size(); i++) {
  215                   asmodel = parser.parseASURI((String)asfiles.elementAt(i));
  216                   parser.setAbstractSchema(asmodel);
  217               }
  218           } catch (Exception e) {
  219               e.printStackTrace();
  220               System.exit(1);
  221           }
  222   
  223           // then for each instance file, try to validate it
  224           if (ifiles != null) {
  225               try {
  226                   for (i = 0; i < ifiles.size(); i++) {
  227                       parser.parseURI((String)ifiles.elementAt(i));
  228                   }
  229               } catch (Exception e) {
  230                   e.printStackTrace();
  231                   System.exit(1);
  232               }
  233           }
  234   
  235       } // main(String[])
  236   
  237       //
  238       // Private static methods
  239       //
  240   
  241       /** Prints the usage. */
  242       private static void printUsage() {
  243   
  244           System.err.println("usage: java dom.ASBuilder [-f|-F] -a uri ... [-i uri ...]");
  245           System.err.println();
  246   
  247           System.err.println("options:");
  248           System.err.println("  -f  | -F    Turn on/off Schema full checking.");
  249           System.err.println("  -a uri ...  Provide a list of schema documents.");
  250           System.err.println("  -i uri ...  Provide a list of instance documents to validate.");
  251           System.err.println();
  252   
  253           System.err.println("default:");
  254           System.err.print("  Schema full checking:     ");
  255           System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off");
  256           System.err.println();
  257           System.err.println("notes:");
  258           System.err.println("DOM Level 3 APIs might change in the future.");
  259   
  260   
  261       } // printUsage()
  262   
  263       public boolean handleError(DOMError error) {
  264   
  265           System.err.print("[");
  266           switch (error.getSeverity()) {
  267               case DOMError.SEVERITY_WARNING:
  268                   System.err.print("Warning");
  269                   break;
  270               case DOMError.SEVERITY_ERROR:
  271                   System.err.print("Error");
  272                   break;
  273               case DOMError.SEVERITY_FATAL_ERROR:
  274                   System.err.print("Fatal Error");
  275                   break;
  276           }
  277           System.err.print("] ");
  278   
  279           String systemId = error.getLocation().getUri();
  280           if (systemId != null) {
  281               int index = systemId.lastIndexOf('/');
  282               if (index != -1)
  283                   systemId = systemId.substring(index + 1);
  284               System.err.print(systemId);
  285           }
  286           System.err.print(':');
  287           System.err.print(error.getLocation().getLineNumber());
  288           System.err.print(':');
  289           System.err.print(error.getLocation().getColumnNumber());
  290           System.err.print(": ");
  291           System.err.print(error.getMessage());
  292           System.err.println();
  293           System.err.flush();
  294   
  295           return error.getSeverity() != DOMError.SEVERITY_FATAL_ERROR;
  296       }
  297   
  298   } // class DOMCount

Home » Xerces-J-src.2.9.1 » dom » [javadoc | source]