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 
  24 package transform;
  25 
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.io.StringWriter;
  29 
  30 import javax.xml.parsers.DocumentBuilder;
  31 import javax.xml.parsers.DocumentBuilderFactory;
  32 import javax.xml.parsers.ParserConfigurationException;
  33 import javax.xml.transform.Transformer;
  34 import javax.xml.transform.TransformerConfigurationException;
  35 import javax.xml.transform.TransformerException;
  36 import javax.xml.transform.TransformerFactory;
  37 import javax.xml.transform.dom.DOMSource;
  38 import javax.xml.transform.stream.StreamResult;
  39 import javax.xml.transform.stream.StreamSource;
  40 
  41 import org.testng.Assert;
  42 import org.testng.annotations.Listeners;
  43 import org.testng.annotations.Test;
  44 import org.w3c.dom.Document;
  45 import org.xml.sax.SAXException;
  46 import org.xml.sax.SAXParseException;
  47 
  48 /*
  49  * @test
  50  * @bug 6206491
  51  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  52  * @run testng/othervm -DrunSecMngr=true transform.Bug6206491
  53  * @run testng/othervm transform.Bug6206491
  54  * @summary Test key searches over more than one document.
  55  */
  56 @Listeners({jaxp.library.FilePolicy.class})
  57 public class Bug6206491 {
  58 
  59     private String getResource(String s) {
  60         return getClass().getResource(s).toString();
  61 
  62     }
  63 
  64     @Test
  65     public void test() {
  66         try {
  67             Document document = getNewXmlDoc(new File(getClass().getResource("Bug6206491.xml").getFile()));
  68 
  69             xmlxsl2html(TransformerFactory.newInstance(), new File(getClass().getResource("Bug6206491.xsl").getFile()), document);
  70         } catch (Exception ex) {
  71             System.err.println(ex.getMessage());
  72             ex.printStackTrace(System.err);
  73             Assert.fail(ex.toString());
  74         }
  75     }
  76 
  77     void xmlxsl2html(TransformerFactory tFactory, File xslFile, Document document) throws Exception {
  78         try {
  79             // tFactory.setAttribute("generate-translet", Boolean.TRUE);
  80         } catch (Exception e) {
  81             // Ignore
  82         }
  83 
  84         try {
  85             StreamSource stylesource = new StreamSource(xslFile);
  86             Transformer transformer = tFactory.newTransformer(stylesource);
  87 
  88             transformer.clearParameters();
  89 
  90             DOMSource source = new DOMSource(document);
  91 
  92             StringWriter sw = new StringWriter();
  93             StreamResult result = new StreamResult(sw);
  94             transformer.transform(source, result);
  95             String s = sw.toString();
  96             Assert.assertFalse(s.contains("<must-be-one>0</must-be-one>"));
  97         } catch (TransformerConfigurationException ex) {
  98             throw ex;
  99 
 100         } catch (TransformerException ex) {
 101             throw ex;
 102         }
 103     }
 104 
 105     Document getNewXmlDoc(File xmlFile) throws Exception {
 106         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 107         factory.setNamespaceAware(true);
 108         factory.setValidating(true);
 109 
 110         try {
 111             DocumentBuilder builder = factory.newDocumentBuilder();
 112             builder.setErrorHandler(new org.xml.sax.helpers.DefaultHandler() {
 113                 public void fatalError(SAXParseException e) throws SAXParseException {
 114                     throw e;
 115                 }
 116 
 117                 public void error(SAXParseException e) throws SAXParseException {
 118                     throw e;
 119                 }
 120 
 121                 public void warning(SAXParseException e) throws SAXParseException {
 122                     throw e;
 123                 }
 124             });
 125             return builder.parse(xmlFile);
 126 
 127         } catch (SAXException ex) {
 128             throw ex;
 129         } catch (ParserConfigurationException ex) {
 130             throw ex;
 131         } catch (IOException ex) {
 132             throw ex;
 133         }
 134     }
 135 
 136 }
 137