jdk/test/javax/xml/jaxp/transform/jdk8004476/XSLTExFuncTest.java
author joehw
Thu, 24 Oct 2013 14:13:09 -0700
changeset 21364 decde35d5139
permissions -rw-r--r--
8004476: XSLT Extension Functions Don't Work in WebStart Reviewed-by: dfuchs, lancea, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21364
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
     1
/*
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
     4
 *
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
     8
 *
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    13
 * accompanied this code).
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    14
 *
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    18
 *
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    21
 * questions.
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    22
 */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    23
/**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    24
 * @test
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    25
 * @bug 8004476
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    26
 * @summary test XSLT extension functions
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    27
 * @run main/othervm XSLTExFuncTest
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    28
 */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    29
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    30
import java.io.StringWriter;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    31
import java.security.AllPermission;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    32
import java.security.CodeSource;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    33
import java.security.Permission;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    34
import java.security.PermissionCollection;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    35
import java.security.Permissions;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    36
import java.security.Policy;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    37
import java.security.ProtectionDomain;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    38
import javax.xml.transform.*;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    39
import javax.xml.transform.sax.SAXSource;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    40
import javax.xml.transform.stream.StreamResult;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    41
import org.xml.sax.InputSource;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    42
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    43
/**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    44
 * test XSLT extension functions
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    45
 *
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    46
 * @author huizhe.wang@oracle.com
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    47
 */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    48
public class XSLTExFuncTest extends TestBase {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    49
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    50
    final static String ENABLE_EXTENSION_FUNCTIONS = "http://www.oracle.com/xml/jaxp/properties/enableExtensionFunctions";
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    51
    final static String CLASSNAME = "DocumentBuilderFactoryImpl";
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    52
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    53
    /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    54
     * Creates a new instance of StreamReader
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    55
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    56
    public XSLTExFuncTest(String name) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    57
        super(name);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    58
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    59
    boolean hasSM;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    60
    String xslFile, xslFileId;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    61
    String xmlFile, xmlFileId;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    62
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    63
    protected void setUp() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    64
        super.setUp();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    65
        xmlFile = filepath + "/tokenize.xml";
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    66
        xslFile = filepath + "/tokenize.xsl";
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    67
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    68
        /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    69
         * if (isWindows) { xslFile = "/" + xslFile; }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    70
         *
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    71
         */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    72
        xslFileId = "file://" + xslFile;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    73
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    74
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    75
    /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    76
     * @param args the command line arguments
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    77
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    78
    public static void main(String[] args) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    79
        XSLTExFuncTest test = new XSLTExFuncTest("OneTest");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    80
        test.setUp();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    81
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    82
        test.testExtFunc();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    83
        test.testExtFuncNotAllowed();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    84
        test.testEnableExtFunc();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    85
        test.testTemplatesEnableExtFunc();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    86
        test.tearDown();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    87
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    88
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    89
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    90
    /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    91
     * by default, extension function is enabled
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    92
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    93
    public void testExtFunc() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    94
        TransformerFactory factory = TransformerFactory.newInstance();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    95
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    96
        try {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    97
            transform(factory);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    98
            System.out.println("testExtFunc: OK");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    99
        } catch (TransformerConfigurationException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   100
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   101
        } catch (TransformerException ex) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   102
            fail(ex.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   103
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   104
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   105
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   106
    /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   107
     * Security is enabled, extension function not allowed
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   108
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   109
    public void testExtFuncNotAllowed() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   110
        Policy p = new SimplePolicy(new AllPermission());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   111
        Policy.setPolicy(p);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   112
        System.setSecurityManager(new SecurityManager());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   113
        TransformerFactory factory = TransformerFactory.newInstance();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   114
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   115
        try {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   116
            transform(factory);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   117
        } catch (TransformerConfigurationException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   118
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   119
        } catch (TransformerException ex) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   120
            //expected since extension function is disallowed
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   121
            System.out.println("testExtFuncNotAllowed: OK");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   122
        } finally {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   123
            System.setSecurityManager(null);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   124
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   125
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   126
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   127
    /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   128
     * Security is enabled, use new feature: enableExtensionFunctions
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   129
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   130
    public void testEnableExtFunc() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   131
        Policy p = new SimplePolicy(new AllPermission());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   132
        Policy.setPolicy(p);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   133
        System.setSecurityManager(new SecurityManager());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   134
        TransformerFactory factory = TransformerFactory.newInstance();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   135
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   136
        /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   137
         * Use of the extension function 'http://exslt.org/strings:tokenize' is
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   138
         * not allowed when the secure processing feature is set to true.
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   139
         * Attempt to use the new property to enable extension function
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   140
         */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   141
        boolean isExtensionSupported = enableExtensionFunction(factory);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   142
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   143
        try {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   144
            transform(factory);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   145
            System.out.println("testEnableExt: OK");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   146
        } catch (TransformerConfigurationException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   147
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   148
        } catch (TransformerException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   149
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   150
        } finally {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   151
            System.setSecurityManager(null);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   152
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   153
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   154
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   155
    /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   156
     * use Templates template = factory.newTemplates(new StreamSource( new
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   157
     * FileInputStream(xslFilename))); // Use the template to create a
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   158
     * transformer Transformer xformer = template.newTransformer();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   159
     *
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   160
     * @param factory
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   161
     * @return
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   162
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   163
    /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   164
     * Security is enabled, use new feature: enableExtensionFunctions Use the
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   165
     * template to create a transformer
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   166
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   167
    public void testTemplatesEnableExtFunc() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   168
        Policy p = new SimplePolicy(new AllPermission());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   169
        Policy.setPolicy(p);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   170
        System.setSecurityManager(new SecurityManager());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   171
        TransformerFactory factory = TransformerFactory.newInstance();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   172
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   173
        /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   174
         * Use of the extension function 'http://exslt.org/strings:tokenize' is
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   175
         * not allowed when the secure processing feature is set to true.
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   176
         * Attempt to use the new property to enable extension function
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   177
         */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   178
        boolean isExtensionSupported = enableExtensionFunction(factory);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   179
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   180
        try {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   181
            SAXSource xslSource = new SAXSource(new InputSource(xslFile));
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   182
            xslSource.setSystemId(xslFileId);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   183
            Templates template = factory.newTemplates(xslSource);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   184
            Transformer transformer = template.newTransformer();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   185
            StringWriter stringResult = new StringWriter();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   186
            Result result = new StreamResult(stringResult);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   187
            transformer.transform(new SAXSource(new InputSource(xmlFile)), result);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   188
            System.out.println("testTemplatesEnableExtFunc: OK");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   189
        } catch (TransformerConfigurationException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   190
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   191
        } catch (TransformerException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   192
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   193
        } finally {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   194
            System.setSecurityManager(null);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   195
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   196
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   197
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   198
    boolean enableExtensionFunction(TransformerFactory factory) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   199
        boolean isSupported = true;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   200
        try {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   201
            factory.setFeature(ENABLE_EXTENSION_FUNCTIONS, true);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   202
        } catch (TransformerConfigurationException ex) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   203
            isSupported = false;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   204
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   205
        return isSupported;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   206
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   207
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   208
    void transform(TransformerFactory factory) throws TransformerConfigurationException, TransformerException {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   209
        SAXSource xslSource = new SAXSource(new InputSource(xslFile));
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   210
        xslSource.setSystemId(xslFileId);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   211
        Transformer transformer = factory.newTransformer(xslSource);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   212
        StringWriter stringResult = new StringWriter();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   213
        Result result = new StreamResult(stringResult);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   214
        transformer.transform(new SAXSource(new InputSource(xmlFile)), result);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   215
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   216
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   217
    class SimplePolicy extends Policy {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   218
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   219
        private final Permissions perms;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   220
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   221
        public SimplePolicy(Permission... permissions) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   222
            perms = new Permissions();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   223
            for (Permission permission : permissions) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   224
                perms.add(permission);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   225
            }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   226
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   227
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   228
        @Override
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   229
        public PermissionCollection getPermissions(CodeSource cs) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   230
            return perms;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   231
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   232
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   233
        @Override
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   234
        public PermissionCollection getPermissions(ProtectionDomain pd) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   235
            return perms;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   236
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   237
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   238
        @Override
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   239
        public boolean implies(ProtectionDomain pd, Permission p) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   240
            return perms.implies(p);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   241
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   242
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   243
        //for older jdk
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   244
        @Override
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   245
        public void refresh() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   246
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   247
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   248
}