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