1 /*
   2  * Copyright (c) 2014, 2015, 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 validation;
  25 
  26 import java.io.FileInputStream;
  27 
  28 import javax.xml.XMLConstants;
  29 import javax.xml.transform.Source;
  30 import javax.xml.transform.Transformer;
  31 import javax.xml.transform.TransformerFactory;
  32 import javax.xml.transform.dom.DOMResult;
  33 import javax.xml.transform.dom.DOMSource;
  34 import javax.xml.transform.stream.StreamResult;
  35 import javax.xml.transform.stream.StreamSource;
  36 import javax.xml.validation.Schema;
  37 import javax.xml.validation.SchemaFactory;
  38 import javax.xml.validation.Validator;
  39 
  40 import org.testng.Assert;
  41 import org.testng.annotations.Test;
  42 import org.w3c.dom.Node;
  43 
  44 /*
  45  * @bug 6684227
  46  * @summary Test property current-element-node works.
  47  */
  48 public class JaxpIssue49 {
  49 
  50     private Schema schema;
  51     private Validator validator;
  52 
  53     @Test
  54     public void testValidatorTest() throws Exception {
  55         try {
  56             SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  57             String file = getClass().getResource("types.xsd").getFile();
  58             Source[] sources = new Source[] { new StreamSource(new FileInputStream(file), file) };
  59             Schema schema = sf.newSchema(sources);
  60             validator = schema.newValidator();
  61             validate();
  62         } catch (Exception e) {
  63             Node node = (Node) validator.getProperty("http://apache.org/xml/properties/dom/current-element-node");
  64             if (node != null) {
  65                 System.out.println("Node: " + node.getLocalName());
  66             } else
  67                 Assert.fail("No node returned");
  68         }
  69     }
  70 
  71     public void validate() throws Exception {
  72         validator.reset();
  73         Source source = new StreamSource(getClass().getResourceAsStream("JaxpIssue49.xml"));
  74         // If you comment the following line, it works
  75         source = toDOMSource(source);
  76         validator.validate(source);
  77     }
  78 
  79     DOMSource toDOMSource(Source source) throws Exception {
  80         if (source instanceof DOMSource) {
  81             return (DOMSource) source;
  82         }
  83         Transformer trans = TransformerFactory.newInstance().newTransformer();
  84         DOMResult result = new DOMResult();
  85         trans.transform(source, result);
  86         trans.transform(new DOMSource(result.getNode()), new StreamResult(System.out));
  87         return new DOMSource(result.getNode());
  88     }
  89 
  90 }