1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package javax.xml.validation;
  25 
  26 import java.io.File;
  27 import java.io.FileWriter;
  28 
  29 import javax.xml.XMLConstants;
  30 import javax.xml.stream.XMLInputFactory;
  31 import javax.xml.stream.XMLOutputFactory;
  32 import javax.xml.stream.XMLStreamReader;
  33 import javax.xml.transform.Result;
  34 import javax.xml.transform.Source;
  35 import javax.xml.transform.stax.StAXSource;
  36 
  37 import org.testng.Assert;
  38 import org.testng.annotations.Test;
  39 
  40 /*
  41  * @bug 6708840
  42  * @summary Test Validator can process StAXSource.
  43  */
  44 public class CR6708840Test {
  45 
  46     @Test
  47     public final void testStream() {
  48         try {
  49             SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  50             Schema schemaGrammar = schemaFactory.newSchema(new File(getClass().getResource("gMonths.xsd").getFile()));
  51 
  52             Validator schemaValidator = schemaGrammar.newValidator();
  53             Source xmlSource = new javax.xml.transform.stream.StreamSource(new File(CR6708840Test.class.getResource("gMonths.xml").toURI()));
  54             schemaValidator.validate(xmlSource);
  55 
  56         } catch (NullPointerException ne) {
  57             Assert.fail("NullPointerException when result is not specified.");
  58         } catch (Exception e) {
  59             Assert.fail(e.getMessage());
  60             e.printStackTrace();
  61         }
  62     }
  63 
  64     /**
  65      * refer to http://forums.java.net/jive/thread.jspa?threadID=41626&tstart=0
  66      */
  67     @Test
  68     public final void testStAX() {
  69         try {
  70             XMLInputFactory xmlif = XMLInputFactory.newInstance();
  71 
  72             // XMLStreamReader staxReader =
  73             // xmlif.createXMLStreamReader((Source)new
  74             // StreamSource(getClass().getResource("Forum31576.xml").getFile()));
  75             XMLStreamReader staxReader = xmlif.createXMLStreamReader(this.getClass().getResourceAsStream("gMonths.xml"));
  76 
  77             SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  78             Schema schemaGrammar = schemaFactory.newSchema(new File(getClass().getResource("gMonths.xsd").getFile()));
  79 
  80             Validator schemaValidator = schemaGrammar.newValidator();
  81 
  82             Source staxSrc = new StAXSource(staxReader);
  83             schemaValidator.validate(staxSrc);
  84 
  85             while (staxReader.hasNext()) {
  86                 int eventType = staxReader.next();
  87                 System.out.println("Event of type: " + eventType);
  88             }
  89         } catch (NullPointerException ne) {
  90             Assert.fail("NullPointerException when result is not specified.");
  91         } catch (Exception e) {
  92             Assert.fail(e.getMessage());
  93             e.printStackTrace();
  94         }
  95     }
  96 
  97     /**
  98      * workaround before the fix: provide a result
  99      */
 100     @Test
 101     public final void testStAXWResult() {
 102         try {
 103             XMLInputFactory xmlif = XMLInputFactory.newInstance();
 104 
 105             // XMLStreamReader staxReader =
 106             // xmlif.createXMLStreamReader((Source)new
 107             // StreamSource(getClass().getResource("Forum31576.xml").getFile()));
 108             XMLStreamReader staxReader = xmlif.createXMLStreamReader(this.getClass().getResourceAsStream("gMonths.xml"));
 109 
 110             SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 111             Schema schemaGrammar = schemaFactory.newSchema(new File(getClass().getResource("gMonths.xsd").getFile()));
 112 
 113             Validator schemaValidator = schemaGrammar.newValidator();
 114 
 115             Source staxSrc = new StAXSource(staxReader);
 116             File resultFile = new File("gMonths.result.xml");
 117             if (resultFile.exists()) {
 118                 resultFile.delete();
 119             }
 120 
 121             Result xmlResult = new javax.xml.transform.stax.StAXResult(XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(resultFile)));
 122             schemaValidator.validate(staxSrc, xmlResult);
 123 
 124             while (staxReader.hasNext()) {
 125                 int eventType = staxReader.next();
 126                 System.out.println("Event of type: " + eventType);
 127             }
 128         } catch (Exception e) {
 129             Assert.fail(e.getMessage());
 130             e.printStackTrace();
 131         }
 132     }
 133 }