jdk/src/java.scripting/share/classes/com/sun/tools/script/shell/init.js
author chegar
Wed, 03 Dec 2014 14:22:58 +0000
changeset 27565 729f9700483a
parent 25859 jdk/src/jdk.dev/share/classes/com/sun/tools/script/shell/init.js@3317bb8137f4
permissions -rw-r--r--
8049367: Modular Run-Time Images Reviewed-by: chegar, dfuchs, ihse, joehw, mullan, psandoz, wetmore Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, bradford.wetmore@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, james.laskey@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * jrunscript JavaScript built-in functions and objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    31
 * Creates an object that delegates all method calls on
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * it to the 'invoke' method on the given delegate object.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *     var x  = { invoke: function(name, args) { //code...}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *     var y = new JSInvoker(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *     y.func(3, 3); // calls x.invoke('func', args); where args is array of arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @param obj object to be wrapped by JSInvoker
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
function JSInvoker(obj) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    46
    return new JSAdapter({
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    47
        __get__ : function(name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    48
            return function() {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    49
                return obj.invoke(name, arguments);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    50
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    51
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    52
    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * This variable represents OS environment. Environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * variables can be accessed as fields of this object. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * example, env.PATH will return PATH value configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
var env = new JSAdapter({
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    61
    __get__ : function (name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    62
        return java.lang.System.getenv(name);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    63
    },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    64
    __has__ : function (name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    65
        return java.lang.System.getenv().containsKey(name);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    66
    },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    67
    __getIds__ : function() {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    68
        return java.lang.System.getenv().keySet().toArray();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    69
    },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    70
    __delete__ : function(name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    71
        println("can't delete env item");
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    72
    },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    73
    __put__ : function (name, value) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    74
        println("can't change env item");
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    75
    },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    76
    toString: function() {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    77
        return java.lang.System.getenv().toString();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    78
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * Creates a convenient script object to deal with java.util.Map instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * The result script object's field names are keys of the Map. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * scriptObj.keyName can be used to access value associated with given key.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *     var x = java.lang.SystemProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *     var y = jmap(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *     println(y['java.class.path']); // prints java.class.path System property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *     delete y['java.class.path']; // remove java.class.path System property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * </pre>
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    94
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @param map java.util.Map instance that will be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 */
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    98
function jmap(map) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
    99
    return new JSAdapter({
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   100
        __get__ : function(name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   101
            if (map.containsKey(name)) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   102
                return map.get(name);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   103
            } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   104
                return undefined;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   105
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   106
        },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   107
        __has__ :  function(name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   108
            return map.containsKey(name);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   109
        },
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   111
        __delete__ : function (name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   112
            return map.remove(name);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   113
        },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   114
        __put__ : function(name, value) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   115
            map.put(name, value);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   116
        },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   117
        __getIds__ : function() {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   118
            return map.keySet().toArray();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   119
        },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   120
        toString: function() {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   121
            return map.toString();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   122
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   123
    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * Creates a convenient script object to deal with java.util.List instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * The result script object behaves like an array. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * scriptObj[index] syntax can be used to access values in the List instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * 'length' field gives size of the List. <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *    var x = new java.util.ArrayList(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *    x.add('Java');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *    x.add('JavaScript');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *    x.add('SQL');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *    x.add('XML');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *    var y = jlist(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *    println(y[2]); // prints third element of list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *    println(y.length); // prints size of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * @param map java.util.List instance that will be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * @constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
function jlist(list) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   149
    function isValid(index) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   150
        return typeof(index) == 'number' &&
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   151
            index > -1 && index < list.size();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   152
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   153
    return new JSAdapter({
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   154
        __get__ :  function(name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   155
            if (isValid(name)) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   156
                return list.get(name);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   157
            } else if (name == 'length') {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   158
                return list.size();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   159
            } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   160
                return undefined;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   161
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   162
        },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   163
        __has__ : function (name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   164
            return isValid(name) || name == 'length';
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   165
        },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   166
        __delete__ : function(name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   167
            if (isValid(name)) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   168
                list.remove(name);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   169
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   170
        },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   171
        __put__ : function(name, value) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   172
            if (isValid(name)) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   173
                list.set(name, value);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   174
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   175
        },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   176
        __getIds__: function() {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   177
            var res = new Array(list.size());
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   178
            for (var i = 0; i < res.length; i++) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   179
                res[i] = i;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   180
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   181
            return res;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   182
        },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   183
        toString: function() {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   184
            return list.toString();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   185
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   186
    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
/**
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   190
 * This is java.lang.System properties wrapped by JSAdapter.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * For eg. to access java.class.path property, you can use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * the syntax sysProps["java.class.path"]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 */
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   194
var sysProps = new JSAdapter({
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   195
    __get__ : function (name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   196
        return java.lang.System.getProperty(name);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   197
    },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   198
    __has__ : function (name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   199
        return java.lang.System.getProperty(name) != null;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   200
    },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   201
    __getIds__ : function() {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   202
        return java.lang.System.getProperties().keySet().toArray();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   203
    },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   204
    __delete__ : function(name) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   205
        java.lang.System.clearProperty(name);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   206
        return true;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   207
    },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   208
    __put__ : function (name, value) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   209
        java.lang.System.setProperty(name, value);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   210
    },
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   211
    toString: function() {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   212
        return "<system properties>";
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   213
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   214
});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
// stdout, stderr & stdin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
var out = java.lang.System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
var err = java.lang.System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
// can't use 'in' because it is a JavaScript keyword :-(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
var inp = java.lang.System["in"];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   222
var BufferedInputStream = java.io.BufferedInputStream;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   223
var BufferedOutputStream = java.io.BufferedOutputStream;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   224
var BufferedReader = java.io.BufferedReader;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   225
var DataInputStream = java.io.DataInputStream;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   226
var File = java.io.File;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   227
var FileInputStream = java.io.FileInputStream;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   228
var FileOutputStream = java.io.FileOutputStream;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   229
var InputStream = java.io.InputStream;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   230
var InputStreamReader = java.io.InputStreamReader;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   231
var OutputStream = java.io.OutputStream;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   232
var Reader = java.io.Reader;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   233
var URL = java.net.URL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * Generic any object to input stream mapper
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   237
 * @param str input file name, URL or InputStream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 * @return InputStream object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * @private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
function inStream(str) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   242
    if (typeof(str) == "string") {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   243
        // '-' means standard input
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   244
        if (str == '-') {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   245
            return java.lang.System["in"];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   246
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   247
        // try file first
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   248
        var file = null;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   249
        try {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   250
            file = pathToFile(str);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   251
        } catch (e) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   252
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   253
        if (file && file.exists()) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   254
            return new FileInputStream(file);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   255
        } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   256
            try {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   257
                // treat the string as URL
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   258
                return new URL(str).openStream();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   259
            } catch (e) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   260
                throw 'file or URL ' + str + ' not found';
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   261
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   262
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   263
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   264
        if (str instanceof InputStream) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   265
            return str;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   266
        } else if (str instanceof URL) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   267
            return str.openStream();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   268
        } else if (str instanceof File) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   269
            return new FileInputStream(str);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   270
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   271
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   272
    // everything failed, just give input stream
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   273
    return java.lang.System["in"];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * Generic any object to output stream mapper
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   278
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * @param out output file name or stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * @return OutputStream object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * @private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
function outStream(out) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   284
    if (typeof(out) == "string") {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   285
        if (out == '>') {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   286
            return java.lang.System.out;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   287
        } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   288
            // treat it as file
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   289
            return new FileOutputStream(pathToFile(out));
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   290
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   291
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   292
        if (out instanceof OutputStream) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   293
            return out;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   294
        } else if (out instanceof File) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   295
            return new FileOutputStream(out);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   296
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   297
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   299
    // everything failed, just return System.out
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   300
    return java.lang.System.out;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * stream close takes care not to close stdin, out & err.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * @private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
function streamClose(stream) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   308
    if (stream) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   309
        if (stream != java.lang.System["in"] &&
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   310
            stream != java.lang.System.out &&
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   311
            stream != java.lang.System.err) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   312
            try {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   313
                stream.close();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   314
            } catch (e) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   315
                println(e);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   316
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   317
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   318
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 * Loads and evaluates JavaScript code from a stream or file or URL<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 *    load('test.js'); // load script file 'test.js'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 *    load('http://java.sun.com/foo.js'); // load from a URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 * @param str input from which script is loaded and evaluated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 */
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   334
if (typeof(load) == 'undefined') {
19064
5a45b684b8b1 8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents: 16482
diff changeset
   335
    this.load = function(str) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   336
        var stream = inStream(str);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   337
        var bstream = new BufferedInputStream(stream);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   338
        var reader = new BufferedReader(new InputStreamReader(bstream));
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   339
        var oldFilename = engine.get(engine.FILENAME);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   340
        engine.put(engine.FILENAME, str);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   341
        try {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   342
            engine.eval(reader);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   343
        } finally {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   344
            engine.put(engine.FILENAME, oldFilename);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   345
            streamClose(stream);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   346
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   347
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
// file system utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
 * Creates a Java byte[] of given length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
 * @param len size of the array to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
 * @private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
function javaByteArray(len) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   358
    return java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
var curDir = new File('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 * Print present working directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
function pwd() {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   367
    println(curDir.getAbsolutePath());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
 * Changes present working directory to given directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
 * @param target directory to change to. optional, defaults to user's HOME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
function cd(target) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   375
    if (target == undefined) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   376
        target = sysProps["user.home"];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   377
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   378
    if (!(target instanceof File)) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   379
        target = pathToFile(target);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   380
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   381
    if (target.exists() && target.isDirectory()) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   382
        curDir = target;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   383
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   384
        println(target + " is not a directory");
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   385
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
 * Converts path to java.io.File taking care of shell present working dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
 * @param pathname file path to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
 * @private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
function pathToFile(pathname) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   395
    var tmp = pathname;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   396
    if (!(tmp instanceof File)) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   397
        tmp = new File(tmp);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   398
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   399
    if (!tmp.isAbsolute()) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   400
        return new File(curDir, pathname);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   401
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   402
        return tmp;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   403
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
 * Copies a file or URL or stream to another file or stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
 * @param from input file or URL or stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
 * @param to output stream or file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
function cp(from, to) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   413
    if (from == to) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   414
        println("file " + from + " cannot be copied onto itself!");
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   415
        return;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   416
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   417
    var inp = inStream(from);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   418
    var out = outStream(to);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   419
    var binp = new BufferedInputStream(inp);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   420
    var bout = new BufferedOutputStream(out);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   421
    var buff = javaByteArray(1024);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   422
    var len;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   423
    while ((len = binp.read(buff)) > 0 )
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   424
        bout.write(buff, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   426
    bout.flush();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   427
    streamClose(inp);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   428
    streamClose(out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 * Shows the content of a file or URL or any InputStream<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 * Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
 * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
 *    cat('test.txt'); // show test.txt file contents
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   437
 *    cat('http://java.net'); // show the contents from the URL http://java.net
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
 * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
 * @param obj input to show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
 * @param pattern optional. show only the lines matching the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
function cat(obj, pattern) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   444
    if (obj instanceof File && obj.isDirectory()) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   445
        ls(obj);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   446
        return;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   447
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   448
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   449
    var inp = null;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   450
    if (!(obj instanceof Reader)) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   451
        inp = inStream(obj);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   452
        obj = new BufferedReader(new InputStreamReader(inp));
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   453
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   454
    var line;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   455
    if (pattern) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   456
        var count = 1;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   457
        while ((line=obj.readLine()) != null) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   458
            if (line.match(pattern)) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   459
                println(count + "\t: " + line);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   460
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   461
            count++;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   462
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   463
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   464
        while ((line=obj.readLine()) != null) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   465
            println(line);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   466
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   467
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
 * Returns directory part of a filename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
 * @param pathname input path name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
 * @return directory part of the given file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
function dirname(pathname) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   477
    var dirName = ".";
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   478
    // Normalize '/' to local file separator before work.
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   479
    var i = pathname.replace('/', File.separatorChar ).lastIndexOf(
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   480
        File.separator );
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   481
    if ( i != -1 )
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   482
        dirName = pathname.substring(0, i);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   483
    return dirName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
 * Creates a new dir of given name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
 * @param dir name of the new directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
function mkdir(dir) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   492
    dir = pathToFile(dir);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   493
    println(dir.mkdir()? "created" : "can not create dir");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
/**
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   497
 * Creates the directory named by given pathname, including
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
 * any necessary but nonexistent parent directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
 * @param dir input path name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
function mkdirs(dir) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   503
    dir = pathToFile(dir);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   504
    println(dir.mkdirs()? "created" : "can not create dirs");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
}
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   506
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
/**
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   508
 * Removes a given file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
 *
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   510
 * @param pathname name of the file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
function rm(pathname) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   513
    var file = pathToFile(pathname);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   514
    if (!file.exists()) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   515
        println("file not found: " + pathname);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   516
        return false;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   517
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   518
    // note that delete is a keyword in JavaScript!
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   519
    println(file["delete"]()? "deleted" : "can not delete");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
 * Removes a given directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
 * @param pathname name of the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
function rmdir(pathname) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   528
    rm(pathname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
 * Synonym for 'rm'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
function del(pathname) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   535
    rm(pathname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
 * Moves a file to another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
 * @param from original name of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
 * @param to new name for the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
function mv(from, to) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   545
    println(pathToFile(from).renameTo(pathToFile(to))?
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   546
        "moved" : "can not move");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
 * Synonym for 'mv'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
function ren(from, to) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   553
    mv(from, to);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   556
var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   557
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
 * Helper function called by ls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
 * @private
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   562
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
function printFile(f) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   564
    var sb = new java.lang.StringBuffer();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   565
    sb.append(f.isDirectory()? "d" : "-");
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   566
    sb.append(f.canRead() ? "r": "-" );
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   567
    sb.append(f.canWrite() ? "w": "-" );
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   568
    sb.append(" ");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   570
    var d = new java.util.Date(f.lastModified());
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   571
    var c = new java.util.GregorianCalendar();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   572
    c.setTime(d);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   573
    var day    = c.get(java.util.Calendar.DAY_OF_MONTH);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   574
    sb.append(months[c.get(java.util.Calendar.MONTH)]
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   575
         + " " + day );
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   576
    if (day < 10) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   577
        sb.append(" ");
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   578
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   580
    // to get fixed length 'length' field
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   581
    var fieldlen = 8;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   582
    var len = new java.lang.StringBuffer();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   583
    for(var j=0; j<fieldlen; j++)
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   584
        len.append(" ");
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   585
    len.insert(0, java.lang.Long.toString(f.length()));
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   586
    len.setLength(fieldlen);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   587
    // move the spaces to the front
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   588
    var si = len.toString().indexOf(" ");
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   589
    if ( si != -1 ) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   590
        var pad = len.toString().substring(si);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   591
        len.setLength(si);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   592
        len.insert(0, pad);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   593
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   594
    sb.append(len.toString());
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   595
    sb.append(" ");
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   596
    sb.append(f.getName());
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   597
    if (f.isDirectory()) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   598
        sb.append('/');
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   599
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   600
    println(sb.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
 * Lists the files in a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
 * @param dir directory from which to list the files. optional, default to pwd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
 * @param filter pattern to filter the files listed. optional, default is '.'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
function ls(dir, filter) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   610
    if (dir) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   611
        dir = pathToFile(dir);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   612
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   613
        dir = curDir;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   614
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   615
    if (dir.isDirectory()) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   616
        var files = dir.listFiles();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   617
        for (var i in files) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   618
            var f = files[i];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   619
            if (filter) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   620
                if(!f.getName().match(filter)) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   621
                    continue;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   622
                }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   623
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   624
            printFile(f);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   625
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   626
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   627
        printFile(dir);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   628
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
 * Synonym for 'ls'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
function dir(d, filter) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   635
    ls(d, filter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
 * Unix-like grep, but accepts JavaScript regex patterns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
 * @param pattern to search in files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
 * @param files one or more files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
function grep(pattern, files /*, one or more files */) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   645
    if (arguments.length < 2) return;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   646
    for (var i = 1; i < arguments.length; i++) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   647
        println(arguments[i] + ":");
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   648
        cat(arguments[i], pattern);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   649
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
 * Find in files. Calls arbitrary callback function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
 * for each matching file.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
 *
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   656
 * Examples:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
 * <code>
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   659
 *    find('.')
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   660
 *    find('.', '.*\.class', rm);  // remove all .class files
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   661
 *    find('.', '.*\.java');       // print fullpath of each .java file
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   662
 *    find('.', '.*\.java', cat);  // print all .java files
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
 * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
 * @param dir directory to search files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
 * @param pattern to search in the files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
 * @param callback function to call for matching files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
function find(dir, pattern, callback) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   671
    dir = pathToFile(dir);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   672
    if (!callback) callback = print;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   673
    var files = dir.listFiles();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   674
    for (var f in files) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   675
        var file = files[f];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   676
        if (file.isDirectory()) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   677
            find(file, pattern, callback);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   678
        } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   679
            if (pattern) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   680
                if (file.getName().match(pattern)) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   681
                    callback(file);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   682
                }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   683
            } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   684
                callback(file);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   685
            }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   686
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   687
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
// process utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
 * Exec's a child process, waits for completion &amp; returns exit code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
 * @param cmd command to execute in child process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
function exec(cmd) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   698
    var process = java.lang.Runtime.getRuntime().exec(cmd);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   699
    var inp = new DataInputStream(process.getInputStream());
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   700
    var line = null;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   701
    while ((line = inp.readLine()) != null) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   702
        println(line);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   703
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   704
    process.waitFor();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   705
    $exit = process.exitValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   708
if (typeof(exit) == 'undefined') {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   709
    /**
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   710
     * Exit the shell program.
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   711
     *
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   712
     * @param exitCode integer code returned to OS shell.
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   713
     * optional, defaults to 0
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   714
     */
19064
5a45b684b8b1 8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents: 16482
diff changeset
   715
    this.exit = function (code) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   716
        if (code) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   717
            java.lang.System.exit(code + 0);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   718
        } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   719
            java.lang.System.exit(0);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   720
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   721
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   724
if (typeof(quit) == 'undefined') {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   725
    /**
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   726
     * synonym for exit
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   727
     */
19064
5a45b684b8b1 8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents: 16482
diff changeset
   728
    this.quit = function (code) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   729
        exit(code);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   730
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
// XML utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   735
/**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
 * Converts input to DOM Document object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
 * @param inp file or reader. optional, without this param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
 * this function returns a new DOM Document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
 * @return returns a DOM Document object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
function XMLDocument(inp) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   743
    var factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   744
    var builder = factory.newDocumentBuilder();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   745
    if (inp) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   746
        if (typeof(inp) == "string") {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   747
            return builder.parse(pathToFile(inp));
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   748
        } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   749
            return builder.parse(inp);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   750
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   751
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   752
        return builder.newDocument();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   753
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
 * Converts arbitrary stream, file, URL to XMLSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
 * @param inp input stream or file or URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
 * @return XMLSource object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
function XMLSource(inp) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   763
    if (inp instanceof javax.xml.transform.Source) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   764
        return inp;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   765
    } else if (inp instanceof Packages.org.w3c.dom.Document) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   766
        return new javax.xml.transform.dom.DOMSource(inp);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   767
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   768
        inp = new BufferedInputStream(inStream(inp));
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   769
        return new javax.xml.transform.stream.StreamSource(inp);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   770
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
 * Converts arbitrary stream, file to XMLResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
 * @param inp output stream or file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
 * @return XMLResult object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
function XMLResult(out) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   780
    if (out instanceof javax.xml.transform.Result) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   781
        return out;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   782
    } else if (out instanceof Packages.org.w3c.dom.Document) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   783
        return new javax.xml.transform.dom.DOMResult(out);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   784
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   785
        out = new BufferedOutputStream(outStream(out));
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   786
        return new javax.xml.transform.stream.StreamResult(out);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   787
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
/**
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   791
 * Perform XSLT transform
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
 * @param inp Input XML to transform (URL, File or InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
 * @param style XSL Stylesheet to be used (URL, File or InputStream). optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
 * @param out Output XML (File or OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
function XSLTransform(inp, style, out) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   798
    switch (arguments.length) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   799
    case 2:
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   800
        inp = arguments[0];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   801
        out = arguments[1];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   802
        break;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   803
    case 3:
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   804
        inp = arguments[0];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   805
        style = arguments[1];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   806
        out = arguments[2];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   807
        break;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   808
    default:
21591
35320b590d9b 8026491: Typos in string literals
malenkov
parents: 19064
diff changeset
   809
        println("XSL transform requires 2 or 3 arguments");
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   810
        return;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   811
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   813
    var factory = javax.xml.transform.TransformerFactory.newInstance();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   814
    var transformer;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   815
    if (style) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   816
        transformer = factory.newTransformer(XMLSource(style));
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   817
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   818
        transformer = factory.newTransformer();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   819
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   820
    var source = XMLSource(inp);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   821
    var result = XMLResult(out);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   822
    transformer.transform(source, result);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   823
    if (source.getInputStream) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   824
        streamClose(source.getInputStream());
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   825
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   826
    if (result.getOutputStream) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   827
        streamClose(result.getOutputStream());
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   828
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
// miscellaneous utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
/**
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   834
 * Prints which command is selected from PATH
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
 * @param cmd name of the command searched from PATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
function which(cmd) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   839
    var st = new java.util.StringTokenizer(env.PATH, File.pathSeparator);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   840
    while (st.hasMoreTokens()) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   841
        var file = new File(st.nextToken(), cmd);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   842
        if (file.exists()) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   843
            println(file.getAbsolutePath());
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   844
            return;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   845
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   846
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
 * Prints IP addresses of given domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
 * @param name domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
function ip(name) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   855
    var addrs = InetAddress.getAllByName(name);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   856
    for (var i in addrs) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   857
        println(addrs[i]);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   858
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
 * Prints current date in current locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
function date() {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   865
    println(new Date().toLocaleString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
 * Echoes the given string arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
function echo(x) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   872
    for (var i = 0; i < arguments.length; i++) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   873
        println(arguments[i]);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   874
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   877
if (typeof(printf) == 'undefined') {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   878
    /**
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   879
     * This is C-like printf 
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   880
     *
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   881
     * @param format string to format the rest of the print items
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   882
     * @param args variadic argument list
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   883
     */
19064
5a45b684b8b1 8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents: 16482
diff changeset
   884
    this.printf = function (format, args/*, more args*/) {  
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   885
        var array = java.lang.reflect.Array.newInstance(java.lang.Object, 
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   886
                    arguments.length - 1);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   887
        for (var i = 0; i < array.length; i++) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   888
            array[i] = arguments[i+1];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   889
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   890
        java.lang.System.out.printf(format, array);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   891
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
 * Reads one or more lines from stdin after printing prompt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
 * @param prompt optional, default is '>'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
 * @param multiline to tell whether to read single line or multiple lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
function read(prompt, multiline) {
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   901
    if (!prompt) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   902
        prompt = '>';
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   903
    }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   904
    var inp = java.lang.System["in"];
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   905
    var reader = new BufferedReader(new InputStreamReader(inp));
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   906
    if (multiline) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   907
        var line = '';
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   908
        while (true) {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   909
            java.lang.System.err.print(prompt);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   910
            java.lang.System.err.flush();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   911
            var tmp = reader.readLine();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   912
            if (tmp == '' || tmp == null) break;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   913
            line += tmp + '\n';
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   914
        }
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   915
        return line;
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   916
    } else {
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   917
        java.lang.System.err.print(prompt);
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   918
        java.lang.System.err.flush();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   919
        return reader.readLine();
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   920
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
}
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   922
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   923
if (typeof(println) == 'undefined') {
19064
5a45b684b8b1 8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents: 16482
diff changeset
   924
    // just synonym to print
5a45b684b8b1 8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents: 16482
diff changeset
   925
    this.println = print;
5a45b684b8b1 8021773: print function as defined by jrunscript's init.js script is incompatible with nashorn's definition
sundar
parents: 16482
diff changeset
   926
}
16482
cbc9c93df1fd 8010136: Make jrunscript's init.js to work on nashorn
sundar
parents: 9035
diff changeset
   927