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