8078174: Add few FX and parser API samples for nashorn
Reviewed-by: hannesw, lagergren
This commit is contained in:
parent
bc04834c26
commit
dbbb8bf78e
65
nashorn/samples/clickcounter.fxml
Normal file
65
nashorn/samples/clickcounter.fxml
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<!-- simple self-contained .fxml file -->
|
||||||
|
|
||||||
|
<!--
|
||||||
|
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* - Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* - Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* - Neither the name of Oracle nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||||
|
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<?import javafx.scene.*?>
|
||||||
|
<?import javafx.scene.control.*?>
|
||||||
|
<?import javafx.scene.layout.*?>
|
||||||
|
<?language javascript?>
|
||||||
|
|
||||||
|
<VBox xmlns:fx="http://javafx.com/fxml">
|
||||||
|
<!-- script to handle events -->
|
||||||
|
|
||||||
|
<fx:script>
|
||||||
|
|
||||||
|
// button click handler
|
||||||
|
var clickCount = 0;
|
||||||
|
function onButtonClick(event) {
|
||||||
|
// get another control via scene object
|
||||||
|
var scene = event.source.scene;
|
||||||
|
var textField = scene.lookup("#nameText");
|
||||||
|
|
||||||
|
print(textField.text + ", you clicked " + ++clickCount + " times!");
|
||||||
|
}
|
||||||
|
|
||||||
|
</fx:script>
|
||||||
|
|
||||||
|
<!-- GUI description -->
|
||||||
|
<children>
|
||||||
|
<HBox>
|
||||||
|
<Label text="Your name please:"/>
|
||||||
|
<TextField fx:id="nameText" text="Nashorn"/>
|
||||||
|
</HBox>
|
||||||
|
<Button text="Click!" onAction="onButtonClick(event)"/>
|
||||||
|
</children>
|
||||||
|
</VBox>
|
||||||
|
|
144
nashorn/samples/colorfulcircles.js
Normal file
144
nashorn/samples/colorfulcircles.js
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* - Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* - Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* - Neither the name of Oracle nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||||
|
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Nashorn port of ColorfulCircles.java JavaFX animation example at
|
||||||
|
// https://docs.oracle.com/javafx/2/get_started/ColorfulCircles.java.html
|
||||||
|
// ColorfulCircles.java is under the following license terms:
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2011, 2012 Oracle and/or its affiliates.
|
||||||
|
* All rights reserved. Use is subject to license terms.
|
||||||
|
*
|
||||||
|
* This file is available and licensed under the following license:
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* - Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* - Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the distribution.
|
||||||
|
* - Neither the name of Oracle nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Usage: jjs -fx colorfulcircles.fx
|
||||||
|
|
||||||
|
// Porting note: No imports - just load these fx scripts!
|
||||||
|
load("fx:controls.js");
|
||||||
|
load("fx:graphics.js");
|
||||||
|
|
||||||
|
// Porting note: whatever is inside
|
||||||
|
// public void start(Stage primaryStage)
|
||||||
|
// goes into "start" function
|
||||||
|
|
||||||
|
function start(primaryStage) {
|
||||||
|
// Porting note: Replace types with 'var'. "Group root" becomes "var root".
|
||||||
|
// and so on..
|
||||||
|
|
||||||
|
var root = new Group();
|
||||||
|
var scene = new Scene(root, 800, 600, Color.BLACK);
|
||||||
|
primaryStage.setScene(scene);
|
||||||
|
var circles = new Group();
|
||||||
|
// Porting note: for (int i = 0....) becomes for (var i = 0...)
|
||||||
|
|
||||||
|
for (var i = 0; i < 30; i++) {
|
||||||
|
var circle = new Circle(150, Color.web("white", 0.05));
|
||||||
|
circle.setStrokeType(StrokeType.OUTSIDE);
|
||||||
|
circle.setStroke(Color.web("white", 0.16));
|
||||||
|
circle.setStrokeWidth(4);
|
||||||
|
circles.getChildren().add(circle);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Porting note: There is no "f" suffix for float literals in JS.
|
||||||
|
// LinearGradient(0f, 1f, 1f, 0f,..) becomes just
|
||||||
|
// LinearGradient(0, 1, 1, 0,..)
|
||||||
|
|
||||||
|
// Porting note: LinearGradient's constructor is a varargs method
|
||||||
|
// No need to create Stop[] just pass more Stop objects at the end!
|
||||||
|
var colors = new Rectangle(scene.getWidth(), scene.getHeight(),
|
||||||
|
new LinearGradient(0, 1, 1, 0, true, CycleMethod.NO_CYCLE,
|
||||||
|
new Stop(0, Color.web("#f8bd55")),
|
||||||
|
new Stop(0.14, Color.web("#c0fe56")),
|
||||||
|
new Stop(0.28, Color.web("#5dfbc1")),
|
||||||
|
new Stop(0.43, Color.web("#64c2f8")),
|
||||||
|
new Stop(0.57, Color.web("#be4af7")),
|
||||||
|
new Stop(0.71, Color.web("#ed5fc2")),
|
||||||
|
new Stop(0.85, Color.web("#ef504c")),
|
||||||
|
new Stop(1, Color.web("#f2660f"))));
|
||||||
|
colors.widthProperty().bind(scene.widthProperty());
|
||||||
|
colors.heightProperty().bind(scene.heightProperty());
|
||||||
|
var blendModeGroup =
|
||||||
|
new Group(new Group(new Rectangle(scene.getWidth(), scene.getHeight(),
|
||||||
|
Color.BLACK), circles), colors);
|
||||||
|
colors.setBlendMode(BlendMode.OVERLAY);
|
||||||
|
root.getChildren().add(blendModeGroup);
|
||||||
|
circles.setEffect(new BoxBlur(10, 10, 3));
|
||||||
|
|
||||||
|
// Porting note: Java code uses static import of
|
||||||
|
// java.lang.Math.random. Just use JS Math.random here
|
||||||
|
var random = Math.random;
|
||||||
|
|
||||||
|
var timeline = new Timeline();
|
||||||
|
// Porting note: Java enhanced for loop
|
||||||
|
// for (Node circle : circles.getChildren())
|
||||||
|
// becomes
|
||||||
|
// for each (var circle: circles.getChildren())
|
||||||
|
|
||||||
|
for each (var circle in circles.getChildren()) {
|
||||||
|
timeline.getKeyFrames().addAll(
|
||||||
|
new KeyFrame(Duration.ZERO, // set start position at 0
|
||||||
|
new KeyValue(circle.translateXProperty(), random() * 800),
|
||||||
|
new KeyValue(circle.translateYProperty(), random() * 600)),
|
||||||
|
new KeyFrame(new Duration(40000), // set end position at 40s
|
||||||
|
new KeyValue(circle.translateXProperty(), random() * 800),
|
||||||
|
new KeyValue(circle.translateYProperty(), random() * 600)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// play 40s of animation
|
||||||
|
timeline.play();
|
||||||
|
primaryStage.show();
|
||||||
|
}
|
52
nashorn/samples/colorpick.js
Normal file
52
nashorn/samples/colorpick.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#simple color picker example
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* - Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* - Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* - Neither the name of Oracle nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||||
|
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (! $OPTIONS._fx) {
|
||||||
|
print("Usage: jjs -fx colorpick.js");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
var ColorPicker = Java.type("javafx.scene.control.ColorPicker");
|
||||||
|
var Scene = Java.type("javafx.scene.Scene");
|
||||||
|
|
||||||
|
function start(stage) {
|
||||||
|
var picker = new ColorPicker();
|
||||||
|
// just print color details in stdout for fun!
|
||||||
|
picker.onAction = function(evt) {
|
||||||
|
var ld = Object.bindProperties({}, picker.value);
|
||||||
|
print(JSON.stringify(ld));
|
||||||
|
}
|
||||||
|
var scene = new Scene(picker, 200, 30);
|
||||||
|
stage.scene = scene;
|
||||||
|
stage.show();
|
||||||
|
}
|
51
nashorn/samples/datepick.js
Normal file
51
nashorn/samples/datepick.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#simple sample to use #javafx8 date picker with #nashorn
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* - Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* - Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* - Neither the name of Oracle nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||||
|
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (! $OPTIONS._fx) {
|
||||||
|
print("Usage: jjs -fx datepick.js");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
load("fx:controls.js");
|
||||||
|
|
||||||
|
function start(stage) {
|
||||||
|
var picker = new DatePicker();
|
||||||
|
picker.onAction = function(evt) {
|
||||||
|
var ld = Object.bindProperties({}, picker.value);
|
||||||
|
print(JSON.stringify(ld));
|
||||||
|
}
|
||||||
|
|
||||||
|
var scene = new Scene(picker, 100, 30);
|
||||||
|
stage.scene = scene;
|
||||||
|
stage.show();
|
||||||
|
}
|
58
nashorn/samples/fjson.js
Normal file
58
nashorn/samples/fjson.js
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* - Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* - Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* - Neither the name of Oracle nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||||
|
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Usage: jjs -scripting flexijson.js fjson.js
|
||||||
|
|
||||||
|
var obj = FlexiJSON.parse(<<EOF
|
||||||
|
// this is a comment
|
||||||
|
{
|
||||||
|
foo: 23,
|
||||||
|
bar: [ 34, 454, 54,],
|
||||||
|
// inline comment here
|
||||||
|
|
||||||
|
/** multi line
|
||||||
|
comments are fine too! */
|
||||||
|
|
||||||
|
# shell style line comment is fine!
|
||||||
|
|
||||||
|
regex: /gdfg/i, // regexp literal
|
||||||
|
|
||||||
|
str: <<END
|
||||||
|
Multiple line strings via nashorn
|
||||||
|
-scripting mode extension as well
|
||||||
|
END
|
||||||
|
}
|
||||||
|
EOF)
|
||||||
|
|
||||||
|
print(obj.foo);
|
||||||
|
print(obj.bar);
|
||||||
|
print(obj.regex);
|
||||||
|
print(obj.str);
|
167
nashorn/samples/flexijson.js
Normal file
167
nashorn/samples/flexijson.js
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* - Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* - Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* - Neither the name of Oracle nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||||
|
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Hjson - "the Human JSON - A configuration file format that
|
||||||
|
* caters to humans and helps reduce the errors they make"
|
||||||
|
* See also: http://hjson.org/
|
||||||
|
*
|
||||||
|
* I wanted to see if we can use Nashorn Parser API (jdk9) to support
|
||||||
|
* similar flexible JSON extension with #nashorn. In this FlexiJSON.parse
|
||||||
|
* implementation, Nashorn Parser API is used to validate that the
|
||||||
|
* extendable flexi JSON is "data only" (i.e., no executable code) and
|
||||||
|
* then 'eval'ed to make an object out of it.
|
||||||
|
*
|
||||||
|
* FlexiJSON allows the following:
|
||||||
|
*
|
||||||
|
* * single and mutliple line comments anywhere
|
||||||
|
* * non-quoted property names and values
|
||||||
|
* * regexp literal values
|
||||||
|
* * omitting trailing comma
|
||||||
|
*
|
||||||
|
* When nashorn -scripting mode is enabled, FlexiJSON supports these
|
||||||
|
* as well:
|
||||||
|
*
|
||||||
|
* * shell style # comments
|
||||||
|
* * multiple line (Unix heredoc style) string values
|
||||||
|
*/
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
function FlexiJSON() {}
|
||||||
|
|
||||||
|
// helper to locate Nashorn Parser API classes
|
||||||
|
FlexiJSON.treeType = function(name) {
|
||||||
|
return Java.type("jdk.nashorn.api.tree." + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nashorn Parser API classes used
|
||||||
|
FlexiJSON.ArrayLiteral = FlexiJSON.treeType("ArrayLiteralTree");
|
||||||
|
FlexiJSON.ExpressionStatement = FlexiJSON.treeType("ExpressionStatementTree");
|
||||||
|
FlexiJSON.ObjectLiteral = FlexiJSON.treeType("ObjectLiteralTree");
|
||||||
|
FlexiJSON.RegExpLiteral = FlexiJSON.treeType("RegExpLiteralTree");
|
||||||
|
FlexiJSON.Literal = FlexiJSON.treeType("LiteralTree");
|
||||||
|
FlexiJSON.Parser = FlexiJSON.treeType("Parser");
|
||||||
|
FlexiJSON.SimpleTreeVisitor = FlexiJSON.treeType("SimpleTreeVisitorES5_1");
|
||||||
|
|
||||||
|
// FlexiJSON.parse API
|
||||||
|
|
||||||
|
FlexiJSON.parse = function(str) {
|
||||||
|
var parser = (typeof $OPTIONS == "undefined")?
|
||||||
|
FlexiJSON.Parser.create() :
|
||||||
|
FlexiJSON.Parser.create("-scripting");
|
||||||
|
|
||||||
|
// force the string to be an expression by putting it inside (, )
|
||||||
|
str = "(" + str + ")";
|
||||||
|
var ast = parser.parse("<flexijsondoc>", str, null);
|
||||||
|
// Should not happen. parse would have thrown syntax error
|
||||||
|
if (!ast) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// allowed 'literal' values in flexi JSON
|
||||||
|
function isLiteral(node) {
|
||||||
|
return node instanceof FlexiJSON.ArrayLiteral ||
|
||||||
|
node instanceof FlexiJSON.Literal ||
|
||||||
|
node instanceof FlexiJSON.ObjectLiteral ||
|
||||||
|
node instanceof FlexiJSON.RegExpLiteral;
|
||||||
|
}
|
||||||
|
|
||||||
|
var visitor;
|
||||||
|
ast.accept(visitor = new (Java.extend(FlexiJSON.SimpleTreeVisitor)) {
|
||||||
|
lineMap: null,
|
||||||
|
|
||||||
|
throwError: function(msg, node) {
|
||||||
|
if (this.lineMap) {
|
||||||
|
var pos = node.startPosition;
|
||||||
|
var line = this.lineMap.getLineNumber(pos);
|
||||||
|
var column = this.lineMap.getColumnNumber(pos);
|
||||||
|
// we introduced extra '(' at start. So, adjust column number
|
||||||
|
msg = msg + " @ " + line + ":" + (column - 1);
|
||||||
|
}
|
||||||
|
throw new TypeError(msg);
|
||||||
|
},
|
||||||
|
|
||||||
|
visitLiteral: function(node, extra) {
|
||||||
|
print(node.value);
|
||||||
|
},
|
||||||
|
|
||||||
|
visitExpressionStatement: function(node, extra) {
|
||||||
|
var expr = node.expression;
|
||||||
|
if (isLiteral(expr)) {
|
||||||
|
expr.accept(visitor, extra);
|
||||||
|
} else {
|
||||||
|
this.throwError("only literals can occur", expr);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
visitArrayLiteral: function(node, extra) {
|
||||||
|
for each (var elem in node.elements) {
|
||||||
|
if (isLiteral(elem)) {
|
||||||
|
elem.accept(visitor, extra);
|
||||||
|
} else {
|
||||||
|
this.throwError("only literal array element value allowed", elem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
visitObjectLiteral: function(node, extra) {
|
||||||
|
for each (var prop in node.properties) {
|
||||||
|
if (prop.getter != null || prop.setter != null) {
|
||||||
|
this.throwError("getter/setter property not allowed", node);
|
||||||
|
}
|
||||||
|
|
||||||
|
var value = prop.value;
|
||||||
|
if (isLiteral(value)) {
|
||||||
|
value.accept(visitor, extra);
|
||||||
|
} else {
|
||||||
|
this.throwError("only literal property value allowed", value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
visitCompilationUnit: function(node, extra) {
|
||||||
|
this.lineMap = node.lineMap;
|
||||||
|
var elements = node.sourceElements;
|
||||||
|
if (elements.length > 1) {
|
||||||
|
this.throwError("more than one top level expression", node.sourceElements[1]);
|
||||||
|
}
|
||||||
|
var stat = node.sourceElements[0];
|
||||||
|
if (! (stat instanceof FlexiJSON.ExpressionStatement)) {
|
||||||
|
this.throwError("only one top level expresion allowed", stat);
|
||||||
|
}
|
||||||
|
stat.accept(visitor, extra);
|
||||||
|
},
|
||||||
|
}, null);
|
||||||
|
|
||||||
|
// safe to eval given string as flexi JSON!
|
||||||
|
return eval(str);
|
||||||
|
}
|
86
nashorn/samples/fxml_example.js
Normal file
86
nashorn/samples/fxml_example.js
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
#Usage: jjs -fx fxml_example.js
|
||||||
|
#nashorn simple example using FXML with #javafx
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* - Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* - Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* - Neither the name of Oracle nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||||
|
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// See also https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html
|
||||||
|
|
||||||
|
if (! $OPTIONS._fx) {
|
||||||
|
print("Usage: jjs -fx fxml_example.js");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// inline FXML document here
|
||||||
|
var fxml = <<EOF
|
||||||
|
|
||||||
|
<?import javafx.scene.*?>
|
||||||
|
<?import javafx.scene.control.*?>
|
||||||
|
<?import javafx.scene.layout.*?>
|
||||||
|
|
||||||
|
<VBox xmlns:fx="http://javafx.com/fxml">
|
||||||
|
<children>
|
||||||
|
<!-- ids will be used script later -->
|
||||||
|
<HBox>
|
||||||
|
<Label text="Your name please:"/>
|
||||||
|
<TextField fx:id="nameText" text="Nashorn"/>
|
||||||
|
</HBox>
|
||||||
|
<Button fx:id="clickButton" text="Click!"/>
|
||||||
|
</children>
|
||||||
|
</VBox>
|
||||||
|
|
||||||
|
EOF
|
||||||
|
|
||||||
|
// Java and FX classes used
|
||||||
|
var ByteArrayInputStream = Java.type("java.io.ByteArrayInputStream");
|
||||||
|
var FXMLLoader = Java.type("javafx.fxml.FXMLLoader");
|
||||||
|
var Scene = Java.type("javafx.scene.Scene");
|
||||||
|
|
||||||
|
function start(stage) {
|
||||||
|
var loader = new FXMLLoader();
|
||||||
|
// load FXML from a string
|
||||||
|
var root = loader.load(new ByteArrayInputStream(fxml.getBytes("UTF-8")));
|
||||||
|
|
||||||
|
// get the button and the text field controls
|
||||||
|
var button = root.lookup("#clickButton");
|
||||||
|
var textField = root.lookup("#nameText");
|
||||||
|
|
||||||
|
// event handler for button
|
||||||
|
var clickCount = 0;
|
||||||
|
button.onAction = function() {
|
||||||
|
print(textField.text + ", you clicked me: " + ++clickCount + " time(s)");
|
||||||
|
}
|
||||||
|
|
||||||
|
var scene = new Scene(root, 300, 275);
|
||||||
|
stage.title = "FXML Example";
|
||||||
|
stage.scene = scene;
|
||||||
|
stage.show();
|
||||||
|
}
|
64
nashorn/samples/fxmlrunner.js
Normal file
64
nashorn/samples/fxmlrunner.js
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#Usage: jjs -fx fxmlrunner.js -- <.fxml file>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* - Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* - Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* - Neither the name of Oracle nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||||
|
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// See also https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html
|
||||||
|
|
||||||
|
// Simple script to "run" a .FXML file specified in
|
||||||
|
// command line. FXML file is expected to have inline
|
||||||
|
// fx:script to handle GUI events. i.e., self-contained
|
||||||
|
// FXML file is assumed.
|
||||||
|
|
||||||
|
var file = arguments[0];
|
||||||
|
var File = Java.type("java.io.File");
|
||||||
|
if (!$OPTIONS._fx || !file || !new File(file).isFile()) {
|
||||||
|
print("Usage: jjs -fx fxmlrunner.js -- <.fxml file> [width] [height]");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// optional stage width and height from command line
|
||||||
|
var width = arguments[1]? parseInt(arguments[1]) : 400;
|
||||||
|
var height = arguments[2]? parseInt(arguments[2]) : 300;
|
||||||
|
|
||||||
|
// JavaFX classes used
|
||||||
|
var FXMLLoader = Java.type("javafx.fxml.FXMLLoader");
|
||||||
|
var Scene = Java.type("javafx.scene.Scene");
|
||||||
|
|
||||||
|
function start(stage) {
|
||||||
|
// load FXML
|
||||||
|
var root = FXMLLoader.load(new File(file).toURL());
|
||||||
|
// show it in a scene
|
||||||
|
var scene = new Scene(root, width, height);
|
||||||
|
stage.title = file;
|
||||||
|
stage.scene = scene;
|
||||||
|
stage.show();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user