jdk/test/javax/xml/jaxp/transform/jdk8004476/XPathExFuncTest.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 XPath extension functions
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    27
 * @run main/othervm XPathExFuncTest
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
import java.io.FileInputStream;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    30
import java.io.InputStream;
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 java.util.Iterator;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    39
import java.util.List;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    40
import javax.xml.XMLConstants;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    41
import javax.xml.namespace.NamespaceContext;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    42
import javax.xml.namespace.QName;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    43
import javax.xml.parsers.DocumentBuilder;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    44
import javax.xml.parsers.DocumentBuilderFactory;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    45
import javax.xml.xpath.XPath;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    46
import javax.xml.xpath.XPathExpressionException;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    47
import javax.xml.xpath.XPathFactory;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    48
import javax.xml.xpath.XPathFactoryConfigurationException;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    49
import javax.xml.xpath.XPathFunction;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    50
import javax.xml.xpath.XPathFunctionException;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    51
import javax.xml.xpath.XPathFunctionResolver;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    52
import org.w3c.dom.Document;
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
/**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    55
 * test XPath extension functions
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    56
 *
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    57
 * @author huizhe.wang@oracle.com
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
public class XPathExFuncTest extends TestBase {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    60
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    61
    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
    62
    final static String CLASSNAME = "DocumentBuilderFactoryImpl";
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    63
    final String XPATH_EXPRESSION = "ext:helloWorld()";
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    64
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    65
    /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    66
     * Creates a new instance of StreamReader
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
    public XPathExFuncTest(String name) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    69
        super(name);
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
    boolean hasSM;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    72
    String xslFile, xslFileId;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    73
    String xmlFile, xmlFileId;
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
    protected void setUp() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    76
        super.setUp();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    77
        xmlFile = filepath + "/SecureProcessingTest.xml";
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    78
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    79
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    80
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
     * @param args the command line arguments
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    83
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    84
    public static void main(String[] args) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    85
        XPathExFuncTest test = new XPathExFuncTest("OneTest");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    86
        test.setUp();
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
        test.testExtFunc();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    89
        test.testExtFuncNotAllowed();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    90
        test.testEnableExtFunc();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    91
        test.tearDown();
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
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    94
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
     * by default, extension function is enabled
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    97
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    98
    public void testExtFunc() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
    99
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   100
        try {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   101
            evaluate(false);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   102
            System.out.println("testExtFunc: OK");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   103
        } catch (XPathFactoryConfigurationException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   104
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   105
        } catch (XPathExpressionException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   106
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   107
        }
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
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   110
    /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   111
     * Security is enabled, extension function not allowed
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   112
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   113
    public void testExtFuncNotAllowed() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   114
        Policy p = new SimplePolicy(new AllPermission());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   115
        Policy.setPolicy(p);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   116
        System.setSecurityManager(new SecurityManager());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   117
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   118
        try {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   119
            evaluate(false);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   120
        } catch (XPathFactoryConfigurationException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   121
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   122
        } catch (XPathExpressionException ex) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   123
            //expected since extension function is disallowed
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   124
            System.out.println("testExtFuncNotAllowed: OK");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   125
        } finally {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   126
            System.setSecurityManager(null);
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
    }
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
    /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   131
     * Security is enabled, use new feature: enableExtensionFunctions
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   132
     */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   133
    public void testEnableExtFunc() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   134
        Policy p = new SimplePolicy(new AllPermission());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   135
        Policy.setPolicy(p);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   136
        System.setSecurityManager(new SecurityManager());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   137
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   138
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   139
        try {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   140
            evaluate(true);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   141
            System.out.println("testEnableExt: OK");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   142
        } catch (XPathFactoryConfigurationException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   143
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   144
        } catch (XPathExpressionException e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   145
            fail(e.getMessage());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   146
        } finally {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   147
            System.setSecurityManager(null);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   148
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   149
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   150
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   151
    Document getDocument() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   152
        // the xml source
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   153
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   154
        DocumentBuilder documentBuilder = null;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   155
        Document document = null;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   156
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   157
        try {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   158
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   159
            InputStream xmlStream = new FileInputStream(xmlFile);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   160
            document = documentBuilder.parse(xmlStream);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   161
        } catch (Exception e) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   162
            fail(e.toString());
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
        return document;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   165
    }
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
    void evaluate(boolean enableExt) throws XPathFactoryConfigurationException, XPathExpressionException {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   168
        Document document = getDocument();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   169
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   170
        XPathFactory xPathFactory = XPathFactory.newInstance();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   171
        /**
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   172
         * 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
   173
         * 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
   174
         * Attempt to use the new property to enable extension function
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   175
         */
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   176
        if (enableExt) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   177
            boolean isExtensionSupported = enableExtensionFunction(xPathFactory);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   178
        }
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
        xPathFactory.setXPathFunctionResolver(new MyXPathFunctionResolver());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   181
        if (System.getSecurityManager() == null) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   182
            xPathFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   183
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   184
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   185
        XPath xPath = xPathFactory.newXPath();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   186
        xPath.setNamespaceContext(new MyNamespaceContext());
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   187
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   188
        String xPathResult = xPath.evaluate(XPATH_EXPRESSION, document);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   189
        System.out.println(
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   190
                "XPath result (enableExtensionFunction == " + enableExt + ") = \""
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   191
                + xPathResult
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   192
                + "\"");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   193
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   194
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   195
    public class MyXPathFunctionResolver
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   196
            implements XPathFunctionResolver {
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
        public XPathFunction resolveFunction(QName functionName, int arity) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   199
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   200
            // not a real ewsolver, always return a default XPathFunction
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   201
            return new MyXPathFunction();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   202
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   203
    }
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
    public class MyXPathFunction
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   206
            implements XPathFunction {
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
        public Object evaluate(List list) throws XPathFunctionException {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   209
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   210
            return "Hello World";
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   211
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   212
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   213
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   214
    public class MyNamespaceContext implements NamespaceContext {
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
        public String getNamespaceURI(String prefix) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   217
            if (prefix == null) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   218
                throw new IllegalArgumentException("The prefix cannot be null.");
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   219
            }
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
            if (prefix.equals("ext")) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   222
                return "http://ext.com";
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   223
            } else {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   224
                return null;
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
        public String getPrefix(String namespace) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   229
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   230
            if (namespace == null) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   231
                throw new IllegalArgumentException("The namespace uri cannot be null.");
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
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   234
            if (namespace.equals("http://ext.com")) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   235
                return "ext";
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   236
            } else {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   237
                return null;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   238
            }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   239
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   240
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   241
        public Iterator getPrefixes(String namespace) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   242
            return null;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   243
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   244
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   245
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   246
    boolean enableExtensionFunction(XPathFactory factory) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   247
        boolean isSupported = true;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   248
        try {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   249
            factory.setFeature(ENABLE_EXTENSION_FUNCTIONS, true);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   250
        } catch (XPathFactoryConfigurationException ex) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   251
            isSupported = false;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   252
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   253
        return isSupported;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   254
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   255
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   256
    class SimplePolicy extends Policy {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   257
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   258
        private final Permissions perms;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   259
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   260
        public SimplePolicy(Permission... permissions) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   261
            perms = new Permissions();
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   262
            for (Permission permission : permissions) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   263
                perms.add(permission);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   264
            }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   265
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   266
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   267
        @Override
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   268
        public PermissionCollection getPermissions(CodeSource cs) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   269
            return perms;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   270
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   271
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   272
        @Override
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   273
        public PermissionCollection getPermissions(ProtectionDomain pd) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   274
            return perms;
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   275
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   276
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   277
        @Override
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   278
        public boolean implies(ProtectionDomain pd, Permission p) {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   279
            return perms.implies(p);
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   280
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   281
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   282
        //for older jdk
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   283
        @Override
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   284
        public void refresh() {
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   285
        }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   286
    }
decde35d5139 8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
diff changeset
   287
}