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