You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2006/09/28 06:35:16 UTC

svn commit: r450689 - /xerces/java/branches/stax-dev/samples/jaxp/SourceValidator.java

Author: mrglavas
Date: Wed Sep 27 21:35:15 2006
New Revision: 450689

URL: http://svn.apache.org/viewvc?view=rev&rev=450689
Log:
Adding an option to select StAX as the input to the validator.

Modified:
    xerces/java/branches/stax-dev/samples/jaxp/SourceValidator.java

Modified: xerces/java/branches/stax-dev/samples/jaxp/SourceValidator.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/samples/jaxp/SourceValidator.java?view=diff&rev=450689&r1=450688&r2=450689
==============================================================================
--- xerces/java/branches/stax-dev/samples/jaxp/SourceValidator.java (original)
+++ xerces/java/branches/stax-dev/samples/jaxp/SourceValidator.java Wed Sep 27 21:35:15 2006
@@ -1,9 +1,10 @@
 /*
- * Copyright 2005 The Apache Software Foundation.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
  * 
  *      http://www.apache.org/licenses/LICENSE-2.0
  * 
@@ -22,9 +23,12 @@
 import javax.xml.XMLConstants;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.Source;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stax.StAXSource;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
@@ -41,19 +45,19 @@
 import org.xml.sax.helpers.XMLReaderFactory;
 
 /**
- * <p>A sample demonstrating how to use the JAXP 1.3 Validation API
+ * <p>A sample demonstrating how to use the JAXP 1.4 Validation API
  * to create a validator and use the validator to validate input
- * from SAX, DOM or a stream. The output of this program shows the 
- * time spent executing the Validator.validate(Source) method.</p>
+ * from SAX, DOM, StAX or a stream. The output of this program shows 
+ * the time spent executing the Validator.validate(Source) method.</p>
  * 
  * <p>This class is useful as a "poor-man's" performance tester to
- * compare the speed of various JAXP 1.3 validators with different
+ * compare the speed of various JAXP 1.4 validators with different
  * input sources. However, it is important to note that the first 
  * validation time of a validator will include both VM class load time 
  * and validator initialization that would not be present in subsequent
  * validations with the same document. Also note that when the source for
- * validation is SAX or a stream, the validation time will also include
- * the time to parse the document, whereas the DOM validation is
+ * validation is SAX, StAX or a stream, the validation time will also 
+ * include the time to parse the document, whereas the DOM validation is
  * completely in memory.</p>
  * 
  * <p><strong>Note:</strong> The results produced by this program
@@ -162,6 +166,43 @@
         }
     } // validate(Validator,Source,String,int,boolean)
     
+    public void validate(Validator validator, 
+            XMLInputFactory xif, String systemId,
+            int repetitions, boolean memoryUsage) {
+        try {
+            Source source = new StreamSource(systemId);
+            long timeBefore = System.currentTimeMillis();
+            long memoryBefore = Runtime.getRuntime().freeMemory();
+            for (int j = 0; j < repetitions; ++j) {
+                XMLStreamReader reader = xif.createXMLStreamReader(source);
+                validator.validate(new StAXSource(reader));
+                reader.close();
+            }
+            long memoryAfter = Runtime.getRuntime().freeMemory();
+            long timeAfter = System.currentTimeMillis();
+
+            long time = timeAfter - timeBefore;
+            long memory = memoryUsage
+                        ? memoryBefore - memoryAfter : Long.MIN_VALUE;
+            printResults(fOut, systemId, time, memory, repetitions);
+        }
+        catch (SAXParseException e) {
+            // ignore
+        }
+        catch (Exception e) {
+            System.err.println("error: Parse error occurred - "+e.getMessage());
+            Exception se = e;
+            if (e instanceof SAXException) {
+                se = ((SAXException)e).getException();
+            }
+            if (se != null)
+              se.printStackTrace(System.err);
+            else
+              e.printStackTrace(System.err);
+
+        }
+    } // validate(Validator,XMLInputFactory,String,int,boolean)
+    
     /** Prints the results. */
     public void printResults(PrintWriter out, String uri, long time,
                              long memory, int repetition) {
@@ -321,7 +362,7 @@
                 }
                 if (arg.equals("-vs")) {
                     if (i + 1 < argv.length && !(arg = argv[i + 1]).startsWith("-")) {
-                        if (arg.equals("sax") || arg.equals("dom") || arg.equals("stream")) {
+                        if (arg.equals("sax") || arg.equals("dom") || arg.equals("stax") || arg.equals("stream")) {
                             validationSource = arg;
                         }
                         else {
@@ -465,6 +506,7 @@
                 if (validationSource.equals("sax")) {
                     // SAXSource
                     XMLReader reader = XMLReaderFactory.createXMLReader();
+                    reader.setErrorHandler(sourceValidator);
                     for (int j = 0; j < length; ++j) {
                         String systemId = (String) instances.elementAt(j);
                         SAXSource source = new SAXSource(reader, new InputSource(systemId));
@@ -485,6 +527,14 @@
                         sourceValidator.validate(validator, source, systemId, repetition, memoryUsage);
                     }
                 }
+                else if (validationSource.equals("stax")) {
+                    // StAXSource
+                    XMLInputFactory xif = XMLInputFactory.newInstance();
+                    for (int j = 0; j < length; ++j) {
+                        String systemId = (String) instances.elementAt(j);
+                        sourceValidator.validate(validator, xif, systemId, repetition, memoryUsage);
+                    }
+                }
                 else {
                     // StreamSource
                     for (int j = 0; j < length; ++j) {
@@ -525,7 +575,7 @@
         System.err.println("  -x number   Select number of repetitions.");
         System.err.println("  -a uri ...  Provide a list of schema documents");
         System.err.println("  -i uri ...  Provide a list of instance documents to validate");
-        System.err.println("  -vs source  Select validation source (sax|dom|stream)");
+        System.err.println("  -vs source  Select validation source (sax|dom|stax|stream)");
         System.err.println("  -f  | -F    Turn on/off Schema full checking.");
         System.err.println("              NOTE: Not supported by all schema factories and validators.");
         System.err.println("  -hs | -HS   Turn on/off honouring of all schema locations.");



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org