1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 // explicitly packageless 20 21 import org.apache.qetest.CheckService; 22 import org.apache.qetest.Logger; 23 import org.apache.qetest.xsl.StylesheetDatalet; 24 import org.apache.qetest.xsl.TestableExtension; 25 import org.apache.qetest.xsl.XHTFileCheckService; 26 27 import java.io.File; 28 import java.util.Calendar; 29 import java.util.Date; 30 import java.util.Hashtable; 31 32 import javax.xml.parsers.DocumentBuilder; 33 import javax.xml.parsers.DocumentBuilderFactory; 34 35 import org.w3c.dom.Document; 36 import org.w3c.dom.DocumentFragment; 37 import org.w3c.dom.Element; 38 import org.w3c.dom.Node; 39 40 import org.apache.xalan.extensions.XSLProcessorContext; 41 import org.apache.xalan.templates.ElemExtensionCall; 42 43 /** 44 * Extension for testing xml-xalan/samples/extensions. 45 * Various tests of extension elements. 46 */ 47 public class javaElem01 extends TestableExtension 48 { 49 50 /** Extension method - element called from stylesheet. */ putString(XSLProcessorContext context, ElemExtensionCall extensionElement)51 public static String putString(XSLProcessorContext context, 52 ElemExtensionCall extensionElement) 53 { 54 counter++; 55 String attrVal = extensionElement.getAttribute("attr"); 56 if (null != attrVal) 57 return attrVal; 58 else 59 return "javaElem01.putString"; 60 } 61 62 /** Extension method - element called from stylesheet. */ putBoolean(XSLProcessorContext context, ElemExtensionCall extensionElement)63 public static Boolean putBoolean(XSLProcessorContext context, 64 ElemExtensionCall extensionElement) 65 { 66 counter++; 67 return new Boolean(true); 68 } 69 70 /** Extension method - element called from stylesheet. */ putDouble(XSLProcessorContext context, ElemExtensionCall extensionElement)71 public static Double putDouble(XSLProcessorContext context, 72 ElemExtensionCall extensionElement) 73 { 74 counter++; 75 return new Double(1.1); 76 } 77 78 /** Extension method - element called from stylesheet. */ putNode(XSLProcessorContext context, ElemExtensionCall extensionElement)79 public static Node putNode(XSLProcessorContext context, 80 ElemExtensionCall extensionElement) 81 { 82 counter++; 83 String attrVal = extensionElement.getAttribute("attr"); 84 85 Node n = null; 86 try 87 { 88 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 89 dfactory.setNamespaceAware(true); 90 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 91 Document doc = docBuilder.newDocument(); 92 if (null != attrVal) 93 n = doc.createTextNode(attrVal); 94 else 95 n = doc.createTextNode("This is a text node"); 96 } 97 catch (Exception e) 98 { 99 // No-op: no easy way to report this 100 } 101 return n; 102 } 103 104 //// Implementations of TestableExtension 105 /** Simple counter of number of times called. */ 106 private static int counter = 0; 107 108 /** 109 * Description of what this extension does. 110 * @return String description of extension 111 */ getCounter()112 public static int getCounter() 113 { 114 return counter; 115 } 116 117 /** 118 * Perform and log any pre-transformation info. 119 * @return true if OK; false if any fatal error occoured 120 * @param datalet Datalet of current stylesheet test 121 */ preCheck(Logger logger, StylesheetDatalet datalet)122 public static boolean preCheck(Logger logger, StylesheetDatalet datalet) 123 { 124 logger.logMsg(Logger.INFOMSG, "javaElem01.preCheck; counter=" + counter); 125 return true; 126 } 127 128 129 /** 130 * Perform and log any post-transformation info. 131 * 132 * The extension should validate that it's extension was 133 * properly called; we also validate output file. 134 * 135 * @param logger Logger to dump any info to 136 * @param datalet Datalet of current stylesheet test 137 */ postCheck(Logger logger, StylesheetDatalet datalet)138 public static void postCheck(Logger logger, StylesheetDatalet datalet) 139 { 140 // Verify that we've been called at least once 141 if (counter > 0) 142 logger.checkPass("javaElem01 has been called " + counter + " times"); 143 else 144 logger.checkFail("javaElem01 has not been called"); 145 146 // We also validate the output file the normal way 147 CheckService fileChecker = (CheckService)datalet.options.get("fileCheckerImpl"); 148 // Supply default value 149 if (null == fileChecker) 150 fileChecker = new XHTFileCheckService(); 151 fileChecker.check(logger, 152 new File(datalet.outputName), 153 new File(datalet.goldName), 154 "Extension test of " + datalet.getDescription()); 155 } 156 157 158 /** 159 * Description of what this extension does. 160 * @return String description of extension 161 */ getDescription()162 public static String getDescription() 163 { 164 return "Basic extension element test"; 165 } 166 } 167 168