You are viewing a plain text version of this content. The canonical link for it is here.
Posted to tsik-dev@ws.apache.org by ha...@apache.org on 2005/08/08 22:03:01 UTC

svn commit: r230871 [25/61] - in /incubator/tsik/trunk: ./ data/ data/schema/ resources/ resources/junit/ src/ src/org/ src/org/apache/ src/org/apache/tsik/ src/org/apache/tsik/c14n/ src/org/apache/tsik/c14n/elements/ src/org/apache/tsik/common/ src/or...

Added: incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/Pkcs12Import.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/Pkcs12Import.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/Pkcs12Import.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/Pkcs12Import.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,334 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xmlsig.tools;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.security.GeneralSecurityException;
+import java.security.Key;
+import java.security.KeyStore;
+import java.security.cert.Certificate;
+import java.util.Enumeration;
+
+/**
+ * Pkcs12Import contains a simple static function and command line interpreter
+ * for importing a PKCS12 certificate file into a Java KeyStore.
+ *
+ * @see #pkcs12Import
+ * @see #usage
+ */
+public class Pkcs12Import
+{
+    /**
+     * The default keystore is defined as the ".keystore" file in the user's
+     * home directory.  The home directory is defined by the "user.home"
+     * system property.
+     */
+    public static final File DEFAULT_KEYSTORE =
+        new File(System.getProperty("user.home"), ".keystore");
+
+    private static final String JSSE_MSG = 
+        "NOTE: JSSE or another PKCS12 keystore provider must be installed\n" +
+        "for this program to work properly!";
+
+    /**
+     * Execute command as described by {@link #usage}.
+     *
+     * @param argv is the command argument array.
+     */
+    public static void main(String argv[])
+    {
+        File pkcs12File = null;
+        char[] pkcs12Password = null;
+        String keyStoreAlias = null;
+        File keyStoreFile = null;
+        char[] storePassword = null;
+        char[] keyPassword = null;
+        String keyStoreType = null;
+
+        if (argv.length % 2 != 0) {
+            usage();
+        }
+
+        for (int i = 0; i < argv.length; i += 2) {
+
+            String name = argv[i];
+            String val = argv[i + 1];
+
+            if (name.equals("-file")) {
+                pkcs12File = new File(val);
+            } else if (name.equals("-pass")) {
+                pkcs12Password = val.toCharArray();
+            } else if (name.equals("-alias")) {
+                keyStoreAlias = val;
+            } else if (name.equals("-keystore")) {
+                keyStoreFile = new File(val);
+            } else if (name.equals("-storepass")) {
+                storePassword = val.toCharArray();
+            } else if (name.equals("-keypass")) {
+                keyPassword = val.toCharArray();
+            } else if (name.equals("-keystoretype")) {
+                keyStoreType = val;
+            }
+        }
+
+        if (pkcs12File == null) {
+            usage();
+        }
+
+        if (pkcs12Password == null ||
+                storePassword == null ||
+                keyPassword == null) {
+            BufferedReader in = new BufferedReader(
+                new InputStreamReader(System.in));
+            String line;
+            try {
+                if (pkcs12Password == null) {
+                    System.out.print("pkcs12-password: ");
+                    line = in.readLine();
+                    if (line == null || line.length() == 0) {
+                        usage();
+                    }
+                    pkcs12Password = line.toCharArray();
+                }
+                if (storePassword == null) {
+                    System.out.print("store-password: ");
+                    line = in.readLine();
+                    if (line == null || line.length() == 0) {
+                        usage();
+                    }
+                    storePassword = line.toCharArray();
+                }
+                if (keyPassword == null) {
+                    System.out.println("(press return to use store-password)");
+                    System.out.print("key-password: ");
+                    line = in.readLine();
+                    if (line == null) {
+                        usage();
+                    }
+                    keyPassword = (line.length() == 0) ? storePassword
+                                                       : line.toCharArray();
+                }
+            } catch (IOException e) {
+                usage();
+            }
+        }
+
+        if (keyStoreType == null) {
+            keyStoreType = KeyStore.getDefaultType();
+        }
+        if (keyStoreFile == null) {
+            keyStoreFile = DEFAULT_KEYSTORE;
+        }
+        if (keyPassword  == null) {
+            keyPassword = pkcs12Password;
+        }
+
+        try {
+            pkcs12Import(pkcs12File, pkcs12Password,
+                         keyStoreAlias, keyStoreFile,
+                         storePassword, keyPassword, keyStoreType);
+
+            System.out.println("Imported PKCS12 file " + pkcs12File +
+                               " into keystore " + keyStoreFile);
+        } catch (Exception e) {
+            e.printStackTrace();
+            System.err.println();
+            System.err.println(JSSE_MSG);
+        }
+    }
+
+    /**
+     * Print the usage command and exit.
+     * <pre>
+     *  java Pkcs12Import
+     *      -file pkcs12-file           # pkcs12 file to be imported
+     *    [ -pass pkcs12-password ]     # user is prompted if omitted
+     *    [ -alias alias ]              # defaults to pkcs12 alias
+     *    [ -keystore keystore-file ]   # defaults to {user-home}/.keystore
+     *    [ -storepass store-password ] # user is prompted if omitted
+     *    [- keystoretype type ]        # defaults to the default keystore type
+     *    [ -keypass key-password ]     # user is prompted if omitted and may
+     *                                  # hit return to use store-password
+     * </pre>
+     */
+    public static void usage()
+    {
+        System.err.println(
+"usage:\n" +
+" java Pkcs12Import\n" +
+"     -file pkcs12-file           # pkcs12 file to be imported\n" +
+"   [ -pass pkcs12-password ]     # user is prompted if omitted\n" +
+"   [ -alias alias ]              # defaults to pkcs12 alias\n" +
+"   [ -keystore keystore-file ]   # defaults to {user-home}/.keystore\n" +
+"   [ -storepass store-password ] # defaults to pkcs12-password\n" +
+"   [ -keystoretype type ]        # defaults to the default keystore type\n" +
+"   [ -keypass key-password ]     # user is prompted if omitted and may\n" +
+"                                 # hit return to use store-password\n" +
+"\n" +
+"Using -alias is recommended since the default pkcs12 alias, at least when\n" +
+"exported from IE, is a UUID and may not be appropriate as a keystore alias\n"
+);
+        System.err.println(JSSE_MSG);
+        System.exit(2);
+    }
+
+    /**
+     * Execute command as described by {@link #usage}. Assumes the keystore
+     * is of the default keystore type, 
+     * as defined by <tt>KeyStore.getDefaultType()</tt>
+     *
+     * @param pkcs12File is the exported PKCS12 certificate file.
+     *
+     * @param pkcs12Password is the password used to encrypt the PKCS12 file
+     * when it was exported.
+     *
+     * @param keyStoreAlias is the alias used to store the imported cert, or
+     * null to use the alias stored in the pkcs12File.  It is recommended that
+     * the pkcs12 alias be overridden since, at least when exported from IE,
+     * it is a UUID and is not appropriate for a keystore alias.
+     *
+     * @param keyStoreFile is the KeyStore file where the imported cert is
+     * to be saved, or null to use the default Java KeyStore.
+     *
+     * @param storePassword is the password used to open and save the java
+     * KeyStore.
+     *
+     * @param keyPassword is the password used to protect the imported key.
+     *
+     * @throws IllegalArgumentException if the PKCS12 file contains zero or
+     * more than one cert, or if the entry it contains cannot be recognized.
+     *
+     * @throws IOException if a problem reading or writing the KeyStore files
+     * occurs.
+     *
+     * @throws GeneralSecurityException if a security related problem occurs
+     * while accessing the KeyStores.  This includes problems obtaining a
+     * PKCS12 keystore provider or other security algorithms that are needed.
+     */
+    public static void pkcs12Import(File pkcs12File, char[] pkcs12Password,
+                                    String keyStoreAlias,
+                                    File keyStoreFile,
+                                    char[] storePassword,
+                                    char[] keyPassword)
+        throws IllegalArgumentException, IOException, GeneralSecurityException
+    {
+        String keyStoreType = KeyStore.getDefaultType();
+        pkcs12Import(pkcs12File, pkcs12Password, keyStoreAlias, 
+                     keyStoreFile, storePassword, keyPassword, keyStoreType);
+        
+    }
+
+    /**
+     * Execute command as described by {@link #usage}. 
+     * See {@link #pkcs12Import(File, char[], String, File, char[], char[])}
+     * for more information.
+     *
+     * This mathod adds parameter <tt>keyStoreType</tt>.
+     *
+     * @param pkcs12File TODO.
+     * @param pkcs12Password TODO.
+     * @param keyStoreAlias TODO.
+     * @param keyStoreFile TODO.
+     * @param storePassword TODO.
+     * @param keyPassword TODO.
+     * @param keyStoreType the keystore type of <tt>keyStoreFile</tt>. 
+     *
+     * @throws IllegalArgumentException TODO.
+     * @throws IOException TODO.
+     * @throws GeneralSecurityException TODO.
+     */
+    public static void pkcs12Import(File pkcs12File, char[] pkcs12Password,
+                                    String keyStoreAlias,
+                                    File keyStoreFile,
+                                    char[] storePassword,
+                                    char[] keyPassword,
+                                    String keyStoreType)
+        throws IllegalArgumentException, IOException, GeneralSecurityException
+    {
+        // Load both KeyStores
+
+        if (keyStoreFile == null) {
+            keyStoreFile = DEFAULT_KEYSTORE;
+        }
+        
+        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
+        KeyStore pkcs12KeyStore = KeyStore.getInstance("PKCS12");
+
+        if (keyStoreFile.exists()) {
+            FileInputStream is = new FileInputStream(keyStoreFile);
+            keyStore.load(is, storePassword);
+            is.close();
+        } else {
+            // load() w/ null input stream is required to initialize new store
+            keyStore.load(null, storePassword);
+        }
+
+        FileInputStream is = new FileInputStream(pkcs12File);
+        pkcs12KeyStore.load(is, pkcs12Password);
+        is.close();
+
+        // Get first (and only) PKCS12 alias
+
+        Enumeration pkcs12Aliases = pkcs12KeyStore.aliases();
+        if (!pkcs12Aliases.hasMoreElements()) {
+            throw new IllegalArgumentException(
+                "pkcs12 file is empty: " + pkcs12File);
+        }
+
+        String pkcs12Alias = (String) pkcs12Aliases.nextElement();
+        if (pkcs12Aliases.hasMoreElements()) {
+            throw new IllegalArgumentException(
+                "pkcs12 file has more than one entry: " + pkcs12File);
+        }
+
+        if (keyStoreAlias == null) {
+            keyStoreAlias = pkcs12Alias;
+        }
+
+        // Copy PKCS12 entry to Java KeyStore
+
+        if (pkcs12KeyStore.isCertificateEntry(pkcs12Alias)) {
+
+            Certificate cert = pkcs12KeyStore.getCertificate(pkcs12Alias);
+            keyStore.setCertificateEntry(keyStoreAlias, cert);
+
+        } else if (pkcs12KeyStore.isKeyEntry(pkcs12Alias)) {
+
+            Key key = pkcs12KeyStore.getKey(pkcs12Alias, pkcs12Password);
+            Certificate[] certChain =
+                pkcs12KeyStore.getCertificateChain(pkcs12Alias);
+            keyStore.setKeyEntry(keyStoreAlias, key, keyPassword, certChain);
+
+        } else {
+            throw new IllegalArgumentException(
+                "pkcs12 entry is not a key or a certificate: " + pkcs12Alias);
+        }
+
+        // Write Java KeyStore
+
+        FileOutputStream os = new FileOutputStream(keyStoreFile);
+        keyStore.store(os, storePassword);
+        os.close();
+    }
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/Pkcs12Import.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/Pkcs12Import_README.txt
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/Pkcs12Import_README.txt?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/Pkcs12Import_README.txt (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/Pkcs12Import_README.txt Mon Aug  8 12:58:58 2005
@@ -0,0 +1,88 @@
+The Pkcs12Import program
+========================
+Pkcs12Import is a Java program that imports a private key and cert chain from
+a PKCS12 file and stores it in a Java keystore under a specified alias (label).
+
+Pkcs12Import requires that JSSE or another PKCS12 keystore provider be
+installed.  JSSE is included in Java 1.4.  To install JSSE for Java 1.3 see:
+
+    http://java.sun.com/products/jsse
+
+Export
+------
+Pkcs12Import is intended to be used to import PKCS12 files that have been
+exported by other programs, such as browsers.  When exporting from Netscape
+Navigator no special handling is needed.  When exporting from IE take note of
+the following:
+- be sure to uncheck the 'Enable strong protection' checkbox; if this is
+  checked, the file will not be readable by the PKCS12 keystore provider
+- if you wish to import the certificate chain and not just a single
+  certificate, check the 'Include all certificates in the certification path'
+  checkbox.
+- a password should always be assigned when exporting; if the password is left
+  blank, the file will not be readable by the PKCS12 keystore provider.  Note
+  that this password becomes the default password for the imported key.
+
+Usage
+-----
+The source code and class file for Pkcs12Import is included in this package.
+To call Pkcs12Import form a Java program, see the Pkcs12Import.java source
+file and javadoc comments.
+
+To run Pkcs12Import as a command line program use the following syntax:
+
+java org.apache.tsik.xmlsig.tools.Pkcs12Import
+     -file pkcs12-file           # pkcs12 file to be imported
+   [ -pass pkcs12-password ]     # user is prompted if omitted
+   [ -alias alias ]              # defaults to pkcs12 alias
+   [ -keystore keystore-file ]   # defaults to {user-home}/.keystore
+   [ -storepass store-password ] # user is prompted if omitted
+   [ -keypass key-password ]     # user is prompted if omitted and may
+                                 # hit return to use store-password
+
+The pkcs12-password is the password used to export the pkcs12-file.  Note
+that this value is not used automatically as the key-password in the keystore;
+instead, the key-password defaults to the store-password, which is consistent
+with the behavior of -keypass in keytool.
+
+The -alias, -keystore, -storepass, and -keypass options work just like the
+equivalent options of the Java keytool program.
+
+Using -alias is recommended since the default pkcs12 alias, at least when
+exported from IE, is a UUID and may not be appropriate as a keystore alias.
+
+Note that there is apparently a bug in the JSSE provider that causes an
+'unknown attr...' message to be printed to standard output while reading the
+PKCS12 file.  This message appears when using Pkcs12Import or keytool to read
+a PKCS12 file.  It seems to be harmless and can be ignored.
+
+Example
+-------
+The Java keytool program and Pkcs12Import can be used together.  For example,
+the following three commands:
+- use keytool to list the contents of the exported.pfx file, exported from IE,
+- use Pkcs12Import to import exported.pfx file into the mystore keystore file,
+- use keytool to print the imported cert by alias
+
+> keytool -list -v -storetype pkcs12 -keystore exported.pfx \
+> -storepass exportpass
+
+unknown attr1.3.6.1.4.1.311.17.1
+Keystore type: pkcs12
+Keystore provider: SunJSSE
+Your keystore contains 1 entry:
+Alias name: {39238567-c586-11d5-b7b7-0000865eef78}
+...
+
+> java -cp . Pkcs12Import -file exported.pfx -pass exportpass -alias mykey \
+> -keystore mystore -storepass mypass -keypass mypass
+
+unknown attr1.3.6.1.4.1.311.17.1
+Imported PKCS12 file exported.pfx into keystore mystore
+
+> keytool -list -v -alias mykey -keystore mystore -storepass mypass
+
+Alias name: mykey
+Creation date: Tue Oct 30 20:38:16 PST 2001
+Entry type: keyEntry
+...

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/Pkcs12Import_README.txt
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/PrivateKeyTool.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/PrivateKeyTool.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/PrivateKeyTool.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/PrivateKeyTool.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,329 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xmlsig.tools; 
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.ObjectInputStream;
+import java.io.PrintStream;
+import java.security.Key;
+import java.security.KeyFactory;
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.util.Collection;
+import java.util.Enumeration;
+import org.apache.tsik.common.Logger;
+import org.apache.tsik.common.LoggerFactory;
+import org.apache.tsik.resource.ResourceFactory;
+import org.apache.tsik.resource.XMLResource;
+import org.apache.tsik.xmlsig.tools.KeyConverter; 
+import org.apache.tsik.xpath.XPath;
+
+/**
+ * PrivateKeyTool lets you import and export keys and certificates
+ * from and to JKS keystores. It works in the following way:
+ *
+ * <em>From and to JKS keystores</em>
+ * <ul><li>Export private keys in PKCS #8 format</li>
+ * <li>Import private keys in PKCS #8 format</li>
+ * <li>Import private keys and certs in PKCS #12 format</li>
+ * </ul>
+ * 
+ * <p><code>java PrivateKeyTool -help</code> will show the 
+ * syntax.
+ */
+public class PrivateKeyTool 
+{
+    private static Logger log = LoggerFactory.getLogger(PrivateKeyTool.class);
+
+    private static String keyStorePassword;
+    private static KeyStore ks = null;
+
+    /**
+     * Reads the keystore, creates one if non-existant.
+     */
+    private static void readKeystore(String keyStoreName) throws Exception
+    {
+        File keyStoreFile = new File(keyStoreName);
+        if (keyStoreFile.exists()) {
+            FileInputStream fis = new FileInputStream(keyStoreFile); 
+            log.debug("reading jks keystore in " + keyStoreName);
+            ks = KeyStore.getInstance("JKS");
+            ks.load(fis, keyStorePassword.toCharArray());
+        } else {
+            log.debug("creating new keystore ("+keyStoreName+" not found)");
+            ks = KeyStore.getInstance("JKS");
+            ks.load(null, keyStorePassword.toCharArray());
+        }
+    }
+
+    private static void writeKeystore(String keyStoreName) throws Exception
+    {
+        File keyStoreFile = new File(keyStoreName);
+        FileOutputStream fos = new FileOutputStream(keyStoreFile); 
+        ks.store(fos, keyStorePassword.toCharArray());
+    }
+
+    /**
+     * TODO javadoc.
+     *
+     * @param argv the command line.
+     */
+    public static void main(String argv[]) 
+    {
+        try {
+            if (findFlag("-?", argv) || findFlag("-h", argv) 
+                || findFlag("-help", argv)){
+                printHelp();
+                System.exit(1);
+            }
+
+            String keyStoreName = findValue("-keystore", argv);
+            keyStorePassword = findValue(true, "-storepass", argv);
+
+            if (keyStoreName == null){
+                keyStoreName = System.getProperty("user.home") + "/.keystore";
+            }
+
+            if (argv.length >=1 && argv[0].equals("-importp12")){
+                readKeystore(keyStoreName);
+                importPkcs12(argv);
+            } else if (argv.length >=1 && argv[0].equals("-importp8")){
+                readKeystore(keyStoreName);
+                importPkcs8(argv);
+            } else if (argv.length >=1 && argv[0].equals("-exportp8")){
+                readKeystore(keyStoreName);
+                exportPkcs8(argv);
+            } else {
+                printHelp();
+                System.exit(1);
+            }
+            writeKeystore(keyStoreName);        
+        } catch (Exception e){
+            if (log.isDebugEnabled()) { 
+                e.printStackTrace();
+            } 
+            log.error(e.toString());
+            System.exit(1);
+        }
+    }
+
+    private static void printHelp(){
+        PrintStream p = System.err;
+        p.println("For all commands, keystore defaults to .keystore in ");
+        p.println("user's home directory.\n");
+        p.println("java "+ PrivateKeyTool.class.getName() +" usage:\n");
+        p.println("-importp12   [-keystore <string>] -storepass<string>");
+        p.println("              -file <string>      -p12password <string>");
+        p.println("              -password <string> [-alias <string>]\n");
+
+        p.println("  where alias defaults to alias found in PKCS#12 file.\n");
+
+        p.println("-importp8   [-keystore <string>] -storepass<string>");
+        p.println("             -keyfile <string>   -certfile <string>");
+        p.println("             -password <string>  -alias <string>\n");
+
+        p.println("-exportp8   [-keystore <string>] -storepass<string>");
+        p.println("             -keyfile <string>   -password <string>");
+        p.println("             -alias <string>\n");
+    }
+
+    private static void importPkcs8(String argv[]) throws Exception 
+    {
+        String keyFileName = findValue(true, "-keyfile", argv);
+        String certFileName = findValue(true, "-certfile", argv);
+        String password = findValue(true, "-password", argv);
+        String alias = findValue(true, "-alias", argv);
+    
+        PrivateKey key = (PrivateKey) readKey(keyFileName);
+        Certificate[] certs = readCert(certFileName);
+
+        ks.setKeyEntry(alias, key, password.toCharArray(), certs);
+    }
+
+    private static void exportPkcs8(String argv[]) throws Exception 
+    {
+        String keyFileName = findValue(true, "-keyfile", argv);
+        String alias = findValue(true, "-alias", argv);
+        String password = findValue(true, "-password", argv);
+
+        Key key = ks.getKey(alias, password.toCharArray());
+
+        if (!"PKCS8".equals(key.getFormat())){
+            throw new IllegalArgumentException("Key is not exportable in "
+                                               +"PKCS #8 format (found " 
+                                               +key.getFormat() + " format)");
+        }
+        FileOutputStream fos = new FileOutputStream(keyFileName); 
+        fos.write(key.getEncoded());
+        fos.close();
+    }
+        
+
+    /**
+     * Imports a pkcs#12 file into the keystore. A pkcs#12 file 
+     * contains a private key and an optional certificate chain.
+     *
+     */
+    private static void importPkcs12(String argv[]) throws Exception 
+    {
+        String p12FileName = findValue(true, "-file", argv);
+        String p12Password = findValue(true, "-p12password", argv);
+        String password = findValue(true, "-password", argv);
+        String alias = findValue("-alias", argv);
+        
+        KeyStore p12KeyStore = KeyStore.getInstance("PKCS12");
+        FileInputStream is = new FileInputStream(new File(p12FileName));
+        p12KeyStore.load(is, p12Password.toCharArray());
+        is.close();
+
+        Enumeration p12Aliases = p12KeyStore.aliases();
+        if (!p12Aliases.hasMoreElements()) {
+            throw new IllegalArgumentException(
+                "pkcs12 file is empty: " + p12FileName);
+        }
+        String p12Alias = (String) p12Aliases.nextElement();
+        if (p12Aliases.hasMoreElements()) {
+            throw new IllegalArgumentException(
+                "pkcs12 file has more than one entry: " + p12FileName);
+        }
+
+        if (alias == null) {
+            alias = p12Alias;
+        }
+    
+        if (p12KeyStore.isCertificateEntry(p12Alias)) {
+            Certificate cert = p12KeyStore.getCertificate(p12Alias);
+            ks.setCertificateEntry(alias, cert);
+        } else if (p12KeyStore.isKeyEntry(p12Alias)) {
+            Key key = p12KeyStore.getKey(p12Alias, p12Password.toCharArray());
+            Certificate[] chain = p12KeyStore.getCertificateChain(p12Alias);
+            ks.setKeyEntry(alias, key, password.toCharArray(), chain);
+        }
+    }
+
+
+
+    private static Certificate[] readCert(String filename) throws Exception
+    {
+        FileInputStream fis = new FileInputStream(filename);
+        CertificateFactory cf
+            = CertificateFactory.getInstance("X.509");
+        Collection c = cf.generateCertificates(fis);
+        fis.close();
+        return (Certificate[]) c.toArray(new Certificate[0]);
+    }
+
+    
+    /**
+     * Reads a private key from a file. Tries the following formats:
+     * <ul>
+     * <li>Serialized format</li>
+     * <li>XML format</li>
+     * <li>PKCS#8 format</li>
+     * </ul>
+     */
+    private static Key readKey(String filename) throws Exception
+    {
+        try {
+            FileInputStream fis = new FileInputStream(filename);
+            ObjectInputStream p = new ObjectInputStream(fis);
+            RSAPrivateKey key = (RSAPrivateKey) p.readObject();
+            fis.close();
+            return (Key) key;
+        } catch (java.io.StreamCorruptedException e){
+            try {
+                log.debug(filename + " is not a serialized key.");
+                log.debug("Converting XML to java key");
+                XMLResource xmlres = ResourceFactory.getXMLResource();
+                FileInputStream fis = new FileInputStream(filename);
+                return (RSAPrivateKey) KeyConverter.keyInfoToPrivateKey( 
+                                xmlres.parseXML(fis, false), 
+                                new XPath("/"));
+            } catch (java.io.IOException ex){
+                log.debug(filename + " is not an XML key.");
+                log.debug("Converting PKCS#8 to java key");
+
+                FileInputStream fis = new FileInputStream(filename);
+                byte[] b = new byte[fis.available()];
+                fis.read(b);
+                fis.close();
+
+                PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(b);
+                KeyFactory keyFactory
+                    = KeyFactory.getInstance("rsa");
+                return keyFactory.generatePrivate(spec);
+            }
+        }
+    }
+
+    private static String findValue(String toFind, String argv[])
+    {
+        return findValue(false, toFind, argv);
+    }
+
+    private static String findValue(boolean required, String toFind, 
+                                    String argv[])
+    {
+        for (int i = 0; i < argv.length; ++i){
+            if (argv[i].equals(toFind)){
+                String s = nextArgv(argv, i);
+                log.debug("Argument: " + toFind + "=" + s);
+                return s;
+            }
+        }
+        if (required) {
+            throw new IllegalArgumentException("'" + toFind + 
+                                               "' argument missing in input");
+        } else {
+            return null;
+        }
+    }
+
+    private static boolean findFlag(String toFind, String argv[])
+    {
+        for (int i = 0; i < argv.length; ++i){
+            if (argv[i].equals(toFind)){
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+    /**
+     * Returns the next entry in argv, unless it starts with
+     * a '-' (to catch errors such as 'java xyz -a -b' where
+     * '-a' needs a value 'java xyz -a value -b'
+     */
+    private static String nextArgv(String[] argv, int i)
+    {
+        if ((argv[i+1] != null) && (!argv[i+1].startsWith("-"))){
+            return argv[i+1];
+        } else {
+            return null;
+        }
+    }
+}
+

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/PrivateKeyTool.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/package.html
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/package.html?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/package.html (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/package.html Mon Aug  8 12:58:58 2005
@@ -0,0 +1,6 @@
+<html>
+<!-- $Id: package.html,v 1.1 2002/08/30 14:53:22 mark Exp $ -->
+<body>
+This API provides useful tools for XML signature processing.
+</body>
+</html>

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xmlsig/tools/package.html
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/BindingContext.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/BindingContext.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/BindingContext.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/BindingContext.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,53 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+/**
+ * A <code>BindingContext</code> contains context information to which an
+ * expression may be bound.  It provides prefix to namespace-URI resolution,
+ * variables, and a function library.  It must be immutable, since the
+ * reference to the BindingContext is made available to the evaluation system
+ * and to any functions called at evaluation time.
+ */
+public interface BindingContext
+{
+    /**
+     * Maps a namespace prefix to a namespace URI.
+     * @param prefix a prefix to map
+     * @return the namespace URI the prefix maps to, or null if none
+     */
+    String getNamespaceURI(String prefix);
+
+    /**
+     * Retrieves the value of a specified variable.
+     * @param namespaceURI the namespace in which the variable is located
+     * @param localName the name of the variable within its namespace
+     * @return the variable's value, or null
+     */
+    Object getVariable(String namespaceURI, String localName);
+
+    /**
+     * Retrieves a reference to a function invocation interface.
+     * @param namespaceURI the namespace in which the function is located
+     * @param localName the name of the function within its namespace
+     * @return the function invocation interface
+     * @see Function
+     */
+    Function getFunction(String namespaceURI, String localName);
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/BindingContext.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/BoundExpression.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/BoundExpression.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/BoundExpression.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/BoundExpression.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,44 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+import org.w3c.dom.Node;
+
+/**
+ * An expression that has been bound and is ready to evaluate.
+ */
+public interface BoundExpression
+{
+    /**
+     * Evaluates the expression with the specified context node,
+     * position, and size.  Returns an instance of one of the following:
+     * <ul>
+     * <li><code>String</code>, for the XPath <i>string</i> type
+     * <li><code>Number</code>, for the XPath <i>number</i> type
+     * <li><code>Boolean</code>, for the XPath <i>boolean</i> type
+     * <li><code>NodeList</code>, for the XPath <i>node-set</i> type
+     * </ul>
+     *
+     * @param node TODO.
+     * @param position TODO.
+     * @param size TODO.
+     * @return TODO.
+     */
+    Object evaluate(Node node, int position, int size);
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/BoundExpression.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/BoundExpressionImpl.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/BoundExpressionImpl.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/BoundExpressionImpl.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/BoundExpressionImpl.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,46 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+import org.apache.tsik.xpath.evaluator.*;
+import org.w3c.dom.Node;
+
+class BoundExpressionImpl implements BoundExpression
+{
+    ExprEvaluator ev;
+    BindingContext bc;
+
+    BoundExpressionImpl(ExprEvaluator ev, BindingContext bc)
+    {
+        this.ev = ev;
+        this.bc = bc;
+    }
+
+    public Object evaluate(Node node, int position, int size)
+    {
+        InvocationContext ic = new InvocationContext();
+        ic.bindingContext = bc;
+        ContextImpl c = new ContextImpl();
+        c.node = node;
+        c.position = position;
+        c.size = size;
+        c.invocation = ic;
+        return ev.evaluate(c);
+    }
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/BoundExpressionImpl.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/Context.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/Context.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/Context.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/Context.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,58 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+import org.w3c.dom.Node;
+
+/**
+ * A <code>Context</code> represents an XPath evaluation context.  It
+ * provides methods to get the context node, position, and size, and
+ * indirectly via <code>BindingContext</code>, the namespace,
+ * variable, and function definitions.
+ */
+public interface Context
+{
+    /** 
+     * Retrieves the context node. 
+     *
+     * @return TODO.
+     */
+    Node getNode();
+
+    /** 
+     * Retrieves the context position. 
+     *
+     * @return TODO.
+     */
+    int getPosition();
+
+    /** 
+     * Retrieves the context size. 
+     *
+     * @return TODO.
+     */
+    int getSize();
+
+    /** 
+     * Retrieves a reference to the current <code>BindingContext</code>. 
+     *
+     * @return TODO.
+     */
+    BindingContext getBindingContext();
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/Context.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/DefaultBindingContextImpl.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/DefaultBindingContextImpl.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/DefaultBindingContextImpl.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/DefaultBindingContextImpl.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,50 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+class DefaultBindingContextImpl implements BindingContext
+{
+    public String getNamespaceURI(String prefix)
+    {
+        if(prefix == null) {
+            return null;
+        }
+        if(prefix.equals("xmlns")){
+            return "http://www.w3.org/2000/xmlns/";
+        }
+        if(prefix.equals("xml")){
+            return "http://www.w3.org/XML/1998/namespace";
+        }
+        return null;
+    }
+
+    public Object getVariable(String namespaceURI, String localName)
+    {
+        return null;
+    }
+
+    public Function getFunction(String namespaceURI, String localName)
+    {
+        if(namespaceURI == null){
+            return StandardLibrary.getFunction(localName);
+        } else {
+            return null;
+        }
+    }
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/DefaultBindingContextImpl.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/Expression.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/Expression.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/Expression.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/Expression.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,38 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+/**
+ * An <code>Expression</code> represents an XPath expression that has been
+ * parsed but not bound.  It must be bound to a <code>BindingContext</code>
+ * in order to be evaluated.
+ */
+public interface Expression
+{
+    /**
+     * Returns a <code>BoundExpression</code> representing this
+     * expression bound to the specified context.  More than one
+     * <code>BoundExpression</code> may be created from a single
+     * <code>Expression</code>.
+     *
+     * @param bindingContext TODO.
+     * @return TODO.
+     */
+    BoundExpression bind(BindingContext bindingContext);
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/Expression.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/ExpressionImpl.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/ExpressionImpl.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/ExpressionImpl.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/ExpressionImpl.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,36 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+import org.apache.tsik.xpath.evaluator.ExprEvaluator;
+
+class ExpressionImpl implements Expression
+{
+    ExprEvaluator ev;
+
+    ExpressionImpl(ExprEvaluator ev)
+    {
+        this.ev = ev;
+    }
+
+    public BoundExpression bind(BindingContext bc)
+    {
+        return new BoundExpressionImpl(ev, bc);
+    }
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/ExpressionImpl.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/Function.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/Function.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/Function.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/Function.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,40 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+import java.util.List;
+
+/**
+ * A function invocation interface.  The function has access to the
+ * <code>Context</code> it is evaluated in and the <code>List</code>
+ * of its arguments.
+ */
+
+public interface Function
+{
+    /**
+     * Evaluates the function in the specified context with the
+     * specified arguments.
+     * @param context The context to evaluate in.
+     * @param args The function's argument list.
+     * @return The function's return value.  Should be an instance of 
+     *   String, (some subclass of) Number, or Boolean.
+     */
+    Object evaluate(Context context, List args);
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/Function.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/IDDiscriminator.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/IDDiscriminator.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/IDDiscriminator.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/IDDiscriminator.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,45 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+import org.w3c.dom.Element;
+
+/**
+ * Used to identify XML attributes of type ID when searching using the XPath
+ * 'id' function.
+ */
+public interface IDDiscriminator
+{
+    /**
+     * Returns whether the attribute with the specified namespace URI and local
+     * name is of type ID, in the context of the given Element.
+     *
+     * @param element is the context element (the element containing the 
+     * attribute) and may not be null.
+     *
+     * @param namespaceURI is the namespace URI of the attribute and may be
+     * null.
+     *
+     * @param localName is the local name of the attribute and may not be null.
+     *
+     * @return TODO.
+     */
+    boolean isIDAttribute(Element element, String namespaceURI,
+                          String localName);
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/IDDiscriminator.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/InScopeNamespacesBindingContext.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/InScopeNamespacesBindingContext.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/InScopeNamespacesBindingContext.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/InScopeNamespacesBindingContext.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,85 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+/**
+ * TODO javadoc.
+ */
+public class InScopeNamespacesBindingContext implements BindingContext
+{
+    BindingContext def;
+    Node context;
+
+    private static Node parent(Node n)
+    {
+        if(n.getNodeType() == Node.ATTRIBUTE_NODE){
+            return ((Attr)n).getOwnerElement();
+        } else {
+            return n.getParentNode();
+        }
+    }
+
+    public String getNamespaceURI(String prefix)
+    {
+        if (prefix == null){
+            return null;
+        }
+
+        Node n = context;
+        while(n != null) {
+            if(n.getNodeType() == Node.ELEMENT_NODE) {
+                Element elt = (Element) n;
+                Attr attr = elt.getAttributeNode("xmlns:" + prefix);
+                if (attr != null){
+                    return attr.getNodeValue();
+                }
+            }
+            n = parent(n);
+        }
+
+        return def.getNamespaceURI(prefix);
+    }
+    
+    public Object getVariable(String namespaceURI, String localName)
+    {
+        return def.getVariable(namespaceURI, localName);
+    }
+    
+    public Function getFunction(String namespaceURI, String localName)
+    {
+        return def.getFunction(namespaceURI, localName);
+    }
+    
+    /**
+     * TODO javadoc.
+     *
+     * @param def TODO.
+     * @param context TODO.
+     */
+    public InScopeNamespacesBindingContext(BindingContext def, Node context)
+    {
+        this.def = def;
+        this.context = context;
+    }
+}
+

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/InScopeNamespacesBindingContext.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/SimpleBindingContext.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/SimpleBindingContext.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/SimpleBindingContext.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/SimpleBindingContext.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,91 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+import java.util.Map;
+
+/**
+ * Implements namespace bindings using a Map of namespaces.
+ * TODO: add ability to pass a Map, or some other data structure, for holding
+ * variables and functions.
+ */
+public class SimpleBindingContext implements BindingContext
+{
+    private static BindingContext staticDefContext =
+        new DefaultBindingContextImpl();
+
+    private Map namespaces;
+    private BindingContext def;
+
+    /**
+     * Construct a simple binding with a given namespace Map and use
+     * DefaultBindingContextImpl as the default binding.
+     *
+     * @param namespaces TODO.
+     */
+    public SimpleBindingContext(Map namespaces)
+    {
+        this(namespaces, null);
+    }
+
+    /**
+     * Construct a simple binding with a given namespace Map and default
+     * binding.
+     *
+     * @param namespaces TODO.
+     * @param def TODO.
+     */
+    public SimpleBindingContext(Map namespaces, BindingContext def)
+    {
+        this.namespaces = namespaces;
+        this.def = (def != null) ? def : staticDefContext;
+    }
+
+    /**
+     * Return the URI value from the namespace map if the prefix is present
+     * there, otherwise call the default binding.
+     */
+    public String getNamespaceURI(String prefix)
+    {
+        if (prefix == null) {
+            return null;
+        }
+        String uri = (String) namespaces.get(prefix);
+        if (uri != null) {
+            return uri;
+        }
+        return def.getNamespaceURI(prefix);
+    }
+    
+    /**
+     * This currently only returns variables in the default binding.
+     */
+    public Object getVariable(String namespaceURI, String localName)
+    {
+        return def.getVariable(namespaceURI, localName);
+    }
+    
+    /**
+     * This currently only returns functions in the default binding.
+     */
+    public Function getFunction(String namespaceURI, String localName)
+    {
+        return def.getFunction(namespaceURI, localName);
+    }
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/SimpleBindingContext.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/SimpleIDDiscriminator.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/SimpleIDDiscriminator.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/SimpleIDDiscriminator.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/SimpleIDDiscriminator.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,34 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+import org.w3c.dom.Element;
+
+/**
+ * A simple IDDiscriminator that considers all attributes with a local name of
+ * 'id' (of any case) to be of type ID.
+ */
+public class SimpleIDDiscriminator implements IDDiscriminator
+{
+    public boolean isIDAttribute(Element element, String namespaceURI,
+                                 String localName)
+    {
+        return ("id".equalsIgnoreCase(localName));
+    }
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/SimpleIDDiscriminator.java
------------------------------------------------------------------------------
    svn:executable = 

Added: incubator/tsik/trunk/src/org/apache/tsik/xpath/StandardLibrary.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/xpath/StandardLibrary.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/xpath/StandardLibrary.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/xpath/StandardLibrary.java Mon Aug  8 12:58:58 2005
@@ -0,0 +1,605 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc.  All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised 
+// that they may need authorizations under certain patents from 
+// Microsoft and IBM, or others. Please see notice.txt file. 
+// VeriSign disclaims any obligation to notify the user of any 
+// such third party rights.
+//
+
+package org.apache.tsik.xpath;
+
+import java.lang.reflect.*;
+import java.util.*;
+import org.apache.tsik.xpath.util.*;
+import org.w3c.dom.*;
+
+/**
+ * This class implements the standard XPath functions as defined in
+ * the W3C XPath spec.  id() is not implemented as it requires
+ * schema/DTD information not accessible via DOM level 2.
+ *
+ * The function implementation method names are derived by starting
+ * with the XPath function name, replacing '-' with '_', and
+ * prepending "f_" to the name.
+ */
+
+final class StandardLibrary
+{
+    StandardLibrary() { throw new Error(); }
+
+    /** A map of defined functions, filled in by a static initializer block. */
+    static Map functions = null;
+
+    /**
+     * This class reflects out a static method to implement the
+     * Function interface in terms of. 
+     */
+    static class FunctionImpl implements Function
+    {
+        static final Class[] generalArgTypes 
+            = new Class[] { Context.class, List.class };
+        String funcName, methodName;
+        Method varMethod;
+        
+        FunctionImpl(String funcName, String methodName)
+        {
+            this.funcName = funcName;
+            this.methodName = methodName;
+            this.varMethod = null;
+        }
+        
+        public Object evaluate(Context context, List args)
+        {
+            Method method = null;
+
+            try {
+
+                // Let's look for a general version of the function.
+
+                try {
+                    if(varMethod == null){
+                        varMethod = method = StandardLibrary.class
+                            .getMethod(methodName, generalArgTypes);
+                    }else{
+                        method = varMethod;
+                    }
+                } catch(Exception ignore) { }
+                
+                if(method != null) {
+                    try {
+                        return method.invoke(null, 
+                                             new Object[] {context, args});
+                    } catch(InvocationTargetException ex) {
+                        throw (RuntimeException) ex.getTargetException();
+                    }
+                }
+
+                // If we got here, no dice on the general, try for a specific.
+
+                Class argtypes[] = new Class[args.size() + 1];
+                Object argv[] = new Object[args.size() + 1];
+                argtypes[0] = Context.class;
+                argv[0] = context;
+                int i;
+
+                for(i = 0; i < args.size(); i++) {
+                    Object arg = args.get(i);
+                    if(arg instanceof String) {
+                        argtypes[i + 1] = String.class;
+                        argv[i + 1] = arg;
+                    } else if(arg instanceof Number) {
+                        argtypes[i + 1] = Double.TYPE;
+                        if(arg instanceof Double){
+                            argv[i + 1] = arg;
+                        }else{
+                            argv[i + 1] 
+                                = new Double(((Number)arg).doubleValue());
+                        }
+                    } else if(arg instanceof Boolean) {
+                        argtypes[i + 1] = Boolean.TYPE;
+                        argv[i + 1] = arg;
+                    } else if(arg instanceof NodeSet) {
+                        argtypes[i + 1] = NodeSet.class;
+                        argv[i + 1] = arg;
+                    }
+                }
+            
+                try {
+                    method = StandardLibrary.class.getMethod(methodName, 
+                                                             argtypes);
+                } catch(Exception ignore) {}
+
+                if(method != null) {
+                    try {
+                        return method.invoke(null, argv);
+                    } catch(InvocationTargetException ex) {
+                        throw (RuntimeException) ex.getTargetException();
+                    }
+                }
+
+                // If we got HERE, the function exists but the
+                // signature didn't match anything defined.
+                // Last ditch: match on number of arguments,
+                // and coerce like mad.
+
+                Method[] methods = StandardLibrary.class.getMethods();
+                Class[] paramTypes = null;
+
+                for(i = 0; i < methods.length; i++) {
+                    if(methods[i].getName().equals(methodName)) {
+                        paramTypes = methods[i].getParameterTypes();
+                        if(paramTypes.length == argv.length) {
+                            method = methods[i];
+                            break;
+                        }
+                    }
+                }
+
+                if (method != null) {
+                    for (i = 1; i < paramTypes.length; i++) {
+                        if (paramTypes[i] == String.class){
+                            argv[i] = Coerce.toString(argv[i]);
+                        } else if(paramTypes[i] == Double.TYPE){
+                            argv[i] = new Double(Coerce
+                                         .toNumber(argv[i]).doubleValue());
+                        } else if(paramTypes[i] == Boolean.TYPE) {
+                            argv[i] = Coerce.toBoolean(argv[i]);
+                        }
+                    }
+                    try {
+                        return method.invoke(null, argv);
+                    } catch(InvocationTargetException ex) {
+                        throw (RuntimeException) ex.getTargetException();
+                    }
+                }                                              
+
+                throw new IllegalArgumentException(
+                      "Incorrect argument list for function " + funcName);
+            } catch(IllegalAccessException ex) {
+                throw new RuntimeException("huh?: " + ex.toString());
+            }
+        }
+    }
+    
+    static
+    {
+        Method[] methods = StandardLibrary.class.getMethods();
+        functions = new HashMap(methods.length);
+        for(int i = 0; i < methods.length; i++) {
+            String name = methods[i].getName();
+            if(name.startsWith("f_")) {
+                String funcName = name.substring(2).replace('_', '-');
+                if(functions.get(funcName) == null) {
+                    functions.put(funcName, new FunctionImpl(funcName, name));
+                }
+            }
+        }
+    }
+
+    /** Gets a node's parent, working around Attr idiocy. */
+    static Node parent(Node n)
+    {
+        if(n.getNodeType() == Node.ATTRIBUTE_NODE){
+            return ((Attr)n).getOwnerElement();
+        }else{
+            return n.getParentNode();
+        }
+    }
+
+    /** Gets the local part of a QName. */
+    static String getLocalName(String name)
+    {
+        int colonPos = name.lastIndexOf(':');
+        if(colonPos == -1) {
+            return name;
+        }
+        return name.substring(colonPos + 1, name.length());
+    }
+
+    /** Gets the local name of a node. */
+    static String getLocalName(Node n)
+    {
+        String name;
+
+        switch(n.getNodeType()) {
+        case Node.ELEMENT_NODE:
+        case Node.ATTRIBUTE_NODE:
+            name = n.getLocalName();
+            if(name != null) {
+                return name;
+            }
+            return getLocalName(n.getNodeName());
+        default:
+            return null;
+        }
+    }
+
+    /** Gets the prefix part of a QName. */
+    static String getPrefix(String name)
+    {
+        int colonPos = name.lastIndexOf(':');
+        if(colonPos == -1) {
+            return null;
+        }
+        return name.substring(0, colonPos);
+    }
+
+    /** Gets the prefix of a node's name. */
+    static String getPrefix(Node n)
+    {
+        String name;
+
+        switch(n.getNodeType()) {
+        case Node.ELEMENT_NODE:
+        case Node.ATTRIBUTE_NODE:
+            return getPrefix(n.getNodeName());
+
+        default:
+            return null;
+        }
+    }
+
+    /** Gets the namespace URI corresponding to a given prefix
+     *  based on a given node. */
+    static String resolvePrefix(String prefix, Node n)
+    {
+        Element e;
+        Attr a;
+
+        while(n != null) {
+            if(n.getNodeType() == Node.ELEMENT_NODE) {
+                e = (Element) n;
+                if(prefix == null){
+                    a = e.getAttributeNode("xmlns");
+                } else {
+                    a = e.getAttributeNode("xmlns:" + prefix);
+                }
+                if (a != null) {
+                    return a.getValue();
+                }
+            }
+
+            n = parent(n);
+        }
+        return null;
+    }
+
+    static String getNamespaceURI(Node n)
+    {
+        switch(n.getNodeType()) {
+        case Node.ELEMENT_NODE:
+        case Node.ATTRIBUTE_NODE:
+            break;
+        default:
+            return null;
+        }
+
+        if(getPrefix(n) == null && n.getNodeType() == Node.ATTRIBUTE_NODE) {
+            return null;
+        }
+            
+        return resolvePrefix(getPrefix(n), n);
+    }
+
+    public static Function getFunction(String name)
+    {
+        return (Function) functions.get(name);
+    }
+
+    public static int f_last(Context context)
+    {
+        return context.getSize();
+    }
+
+    public static int f_position(Context context)
+    {
+        return context.getPosition();
+    }
+
+    public static int f_count(Context context, NodeSet ns)
+    {
+        return ns.size();
+    }
+
+    public static String f_local_name(Context context, List args)
+    {
+        switch(args.size()) {
+        case 0:
+            return getLocalName(context.getNode());
+        case 1:
+            try {
+                Node n = (Node) ((NodeSet)args.get(0)).first();
+                return getLocalName(n);
+            } catch(NoSuchElementException ex) {
+                return "";
+            }
+        default:
+            throw new IllegalArgumentException("too many arguments");
+        }
+    }
+
+    public static String f_namespace_uri(Context context, List args)
+    {
+        switch(args.size()) {
+        case 0:
+            return getNamespaceURI(context.getNode());
+        case 1:
+            try {
+                Node n = (Node) ((NodeSet)args.get(0)).first();
+                return getNamespaceURI(n);
+            } catch(NoSuchElementException ex) {
+                return "";
+            }
+        default:
+            throw new IllegalArgumentException("too many arguments");
+        }
+    }
+
+    public static String f_name(Context context)
+    {
+        return context.getNode().getNodeName();
+    }
+
+    public static String f_name(Context context, NodeSet ns)
+    {
+        try {
+            Node n = (Node) ns.first();
+            return n.getNodeName();
+        } catch(NoSuchElementException ex) {
+            return "";
+        }
+    }
+
+    public static Object f_string(Context context, List args)
+    {
+        Object arg;
+        if(args.size() == 0) {
+            NodeSet ns = new NodeSet();
+            ns.add(context.getNode());
+            arg = ns;
+        } else if(args.size() == 1) {
+            arg = args.get(0);
+        } else {
+            throw new IllegalArgumentException(
+                                  "Too many arguments to function string");
+        }
+        return Coerce.toString(arg);
+    }
+
+    public static Object f_concat(Context context, List args)
+    {
+        if(args.size() < 2) {
+            throw new IllegalArgumentException(
+                           "Not enough arguments to function concat");
+        }
+        StringBuffer sb = new StringBuffer();
+        Iterator it = args.iterator();
+        while(it.hasNext()) {
+            sb.append((String) it.next());
+        }
+        return sb.toString();
+    }
+
+    public static boolean f_starts_with(Context context, String s1, String s2)
+    {
+        return s1.startsWith(s2);
+    }
+
+    public static boolean f_contains(Context context, String s1, String s2)
+    {
+        return s1.indexOf(s2) != -1;
+    }
+
+    public static String f_substring_before(Context context, String s1, 
+                                            String s2)
+    {
+        int i = s1.indexOf(s2);
+        if(i == -1){
+            return "";
+        } else {
+            return s1.substring(0, i);
+        }
+    }
+
+    public static String f_substring_after(Context context, String s1, 
+                                           String s2)
+    {
+        int i = s1.indexOf(s2);
+        if(i == -1) {
+            return "";
+        } else {
+            return s1.substring(i + s2.length());
+        }
+    }
+
+    public static String f_substring(Context context, List args)
+    {
+        if (args.size() < 2 || args.size() > 3) {
+            throw new IllegalArgumentException(
+              "Bad number of arguments to function substring");
+        }
+
+        String s1 = (String) args.get(0);
+        double n2 = Math.round(((Number) args.get(1)).doubleValue()) - 1;
+        int beginIndex = (int) n2;
+        if (beginIndex < 0) {
+            beginIndex = 0;
+        }
+        if(args.size() == 2) {
+            return s1.substring(beginIndex);
+        } else {
+            double n3 = Math.round(((Number)args.get(2)).doubleValue());
+            int endIndex = (int) (n2 + n3);
+            if (endIndex < beginIndex) {
+                endIndex = beginIndex;
+            }
+            if (endIndex > s1.length()) {
+                endIndex = s1.length();
+            }
+            return s1.substring(beginIndex, endIndex);
+        }
+    }
+
+    public static int f_string_length(Context context)
+    {
+        return f_string_length(context, 
+                               Coerce.stringValueOfNode(context.getNode()));
+    }
+
+    public static int f_string_length(Context context, String s)
+    {
+        return s.length();
+    }
+
+    public static String f_normalize_space(Context context)
+    {
+        return f_normalize_space(context, 
+                                 Coerce.stringValueOfNode(context.getNode()));
+    }
+
+    public static String f_normalize_space(Context context, String s)
+    {
+        StringBuffer sb = new StringBuffer(s.length());
+        int i;
+        boolean gotSpace = true;
+        char ch;
+        for(i = 0; i < s.length(); i++) {
+            ch = s.charAt(i);
+            switch(ch) {
+            case '\t':
+            case '\r':
+            case '\n':
+            case ' ':
+                if(gotSpace){
+                    break;
+                }
+                sb.append(' ');
+                gotSpace = true;
+                break;
+            default:
+                gotSpace = false;
+                sb.append(ch);
+                break;
+            }
+        }
+        if(gotSpace && sb.length() > 0){
+            sb.setLength(sb.length() - 1);
+        }
+        return sb.toString();
+    }
+
+    public static String f_translate(Context context, String s1, 
+                                     String s2, String s3)
+    {
+        StringBuffer sb = new StringBuffer(s1.length());
+        int i, x;
+        char ch;
+        for(i = 0; i < s1.length(); i++) {
+            ch = s1.charAt(i);
+            x = s2.indexOf(ch);
+            if(x < 0){
+                sb.append(ch);
+            }else if(x < s3.length()){
+                sb.append(s3.charAt(x));
+            }
+        }
+        return sb.toString();
+    }
+
+    public static Object f_boolean(Context context, List args)
+    {
+        return Coerce.toBoolean(args.get(0));
+    }
+
+    public static boolean f_true(Context context)
+    {
+        return true;
+    }
+
+    public static boolean f_false(Context context)
+    {
+        return false;
+    }
+
+    public static boolean f_not(Context context, boolean arg)
+    {
+        return !arg;
+    }
+
+    public static boolean f_lang(Context context, String s1)
+    {
+        String lang = "";
+        Node n = context.getNode();
+        while(n != null) {
+            if(n.getNodeType() == Node.ELEMENT_NODE) {
+                Element e = (Element) n;
+                lang = e.getAttributeNS("http://www.w3.org/XML/1998/namespace", 
+                                        "lang");
+                if(!("".equals(lang))) {
+                    break;
+                }
+            }
+            n = parent(n);
+        }
+        if(lang.equalsIgnoreCase(s1)){
+            return true;
+        }
+        if(lang.length() > s1.length() &&
+           lang.substring(0, s1.length() + 1).equalsIgnoreCase(s1 + "-")){
+            return true;
+        }
+        return false;
+    }
+
+    public static Object f_number(Context context, List args)
+    {
+        Object arg;
+        if(args.size() == 0) {
+            NodeSet ns = new NodeSet();
+            ns.add(context.getNode());
+            arg = ns;
+        } else if(args.size() == 1) {
+            arg = args.get(0);
+        } else {
+            throw new IllegalArgumentException(
+                            "Too many arguments to function number");
+        }
+        return Coerce.toNumber(arg);
+    }
+
+    public static double f_sum(Context context, NodeSet ns)
+    {
+        Iterator it = ns.iterator();
+        double s = 0.0;
+        while(it.hasNext()) {
+            Node n = (Node) it.next();
+            s += Coerce.toNumber(Coerce.stringValueOfNode(n)).doubleValue();
+        }
+        return s;
+    }
+
+    public static double f_floor(Context context, double arg)
+    {
+        return Math.floor(arg);
+    }
+
+    public static double f_ceiling(Context context, double arg)
+    {
+        return Math.ceil(arg);
+    }
+
+    public static double f_round(Context context, double arg)
+    {
+        return Math.round(arg);
+    }
+}

Propchange: incubator/tsik/trunk/src/org/apache/tsik/xpath/StandardLibrary.java
------------------------------------------------------------------------------
    svn:executable = 



---------------------------------------------------------------------
To unsubscribe, e-mail: tsik-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: tsik-dev-help@ws.apache.org