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.FileInputStream; 27 import java.io.FileNotFoundException; 28 import java.io.StringWriter; 29 30 import javax.xml.transform.Result; 31 import javax.xml.transform.Source; 32 import javax.xml.transform.SourceLocator; 33 import javax.xml.transform.Templates; 34 import javax.xml.transform.Transformer; 35 import javax.xml.transform.TransformerConfigurationException; 36 import javax.xml.transform.TransformerException; 37 import javax.xml.transform.TransformerFactory; 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 45 /* 46 * @test 47 * @bug 6957215 48 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 49 * @run testng/othervm -DrunSecMngr=true transform.CR6957215Test 50 * @run testng/othervm transform.CR6957215Test 51 * @summary Test XSLT generates the element content using xsl:attribute instructions. 52 */ 53 @Listeners({jaxp.library.FilePolicy.class}) 54 public class CR6957215Test { 55 56 @Test 57 public final void testTransform() { 58 xsl(getClass().getResource("CR6957215.xml").getFile(), getClass().getResource("CR6957215.xsl").getFile()); 59 } 60 61 public static void xsl(String inFilename, String xslFilename) { 62 try { 63 // Create transformer factory 64 TransformerFactory factory = TransformerFactory.newInstance(); 65 66 // Use the factory to create a template containing the xsl file 67 Templates template = factory.newTemplates(new StreamSource(new FileInputStream(xslFilename))); 68 69 // Use the template to create a transformer 70 Transformer xformer = template.newTransformer(); 71 72 // Prepare the input and output files 73 Source source = new StreamSource(new FileInputStream(inFilename)); 74 75 StringWriter strResult = new StringWriter(); 76 Result result = new StreamResult(strResult); 77 78 // Apply the xsl file to the source file and write the result to the 79 // output file 80 xformer.transform(source, result); 81 String resultString = strResult.toString(); 82 System.out.println(resultString); 83 if (resultString.indexOf("aaa-ref/") > 0) 84 Assert.fail("missing attributes"); 85 } catch (FileNotFoundException e) { 86 e.printStackTrace(); 87 } catch (TransformerConfigurationException e) { 88 // An error occurred in the XSL file 89 e.printStackTrace(); 90 } catch (TransformerException e) { 91 e.printStackTrace(); 92 // An error occurred while applying the XSL file 93 // Get location of error in input file 94 SourceLocator locator = e.getLocator(); 95 int col = locator.getColumnNumber(); 96 int line = locator.getLineNumber(); 97 String publicId = locator.getPublicId(); 98 String systemId = locator.getSystemId(); 99 } 100 } 101 }