You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rm...@apache.org on 2018/07/02 10:20:15 UTC

johnzon git commit: provide a Joni integration for jsonschema when nashorn will be completely dropped - todo: check if we shade it with minimal=true

Repository: johnzon
Updated Branches:
  refs/heads/master 0c09b74f2 -> 33d2acff3


provide a Joni integration for jsonschema when nashorn will be completely dropped - todo: check if we shade it with minimal=true


Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/33d2acff
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/33d2acff
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/33d2acff

Branch: refs/heads/master
Commit: 33d2acff3600d82c5e3a735b869d7b49975c5e76
Parents: 0c09b74
Author: Romain Manni-Bucau <rm...@gmail.com>
Authored: Mon Jul 2 12:20:10 2018 +0200
Committer: Romain Manni-Bucau <rm...@gmail.com>
Committed: Mon Jul 2 12:20:10 2018 +0200

----------------------------------------------------------------------
 LICENSE                                         |  5 ++
 johnzon-jsonschema/pom.xml                      |  8 +++
 .../johnzon/jsonschema/regex/JoniRegex.java     | 70 ++++++++++++++++++++
 .../johnzon/jsonschema/regex/JsRegexTest.java   | 45 +++++++++++++
 4 files changed, 128 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/johnzon/blob/33d2acff/LICENSE
----------------------------------------------------------------------
diff --git a/LICENSE b/LICENSE
index 261eeb9..ef77930 100644
--- a/LICENSE
+++ b/LICENSE
@@ -199,3 +199,8 @@
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
+
+-----
+
+This product bundles Joni, which is available under a
+"MIT" license.  For details, see https://github.com/jruby/joni.
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/johnzon/blob/33d2acff/johnzon-jsonschema/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-jsonschema/pom.xml b/johnzon-jsonschema/pom.xml
index b0d0086..1f74030 100644
--- a/johnzon-jsonschema/pom.xml
+++ b/johnzon-jsonschema/pom.xml
@@ -29,6 +29,14 @@
   <name>Johnzon :: JSON Schema</name>
 
   <dependencies>
+    <dependency> <!-- only when nashorn will be dropped -->
+      <groupId>org.jruby.joni</groupId>
+      <artifactId>joni</artifactId>
+      <version>2.1.16</version>
+      <scope>provided</scope>
+      <optional>true</optional>
+    </dependency>
+
     <dependency>
       <groupId>org.apache.johnzon</groupId>
       <artifactId>johnzon-core</artifactId>

http://git-wip-us.apache.org/repos/asf/johnzon/blob/33d2acff/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/regex/JoniRegex.java
----------------------------------------------------------------------
diff --git a/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/regex/JoniRegex.java b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/regex/JoniRegex.java
new file mode 100644
index 0000000..d0871c7
--- /dev/null
+++ b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/regex/JoniRegex.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.jsonschema.regex;
+
+import java.nio.charset.StandardCharsets;
+import java.util.function.Predicate;
+
+import org.jcodings.specific.ASCIIEncoding;
+import org.joni.Matcher;
+import org.joni.Option;
+import org.joni.Regex;
+import org.joni.Syntax;
+
+public class JoniRegex implements Predicate<CharSequence> {
+    private final Regex regex;
+    private final String toStr;
+
+    public JoniRegex(final String regex) {
+        if (regex.startsWith("/") && regex.length() > 1) {
+            final int end = regex.lastIndexOf('/');
+            if (end < 0) {
+                this.regex = new Regex(regex);
+            } else {
+                final String optStr = regex.substring(end + 1);
+                int option = Option.SINGLELINE;
+
+                if (optStr.contains("i")) {
+                    option |= Option.IGNORECASE;
+                } else if (optStr.contains("m")) {
+                    option &= ~Option.SINGLELINE;
+                    option |= Option.NEGATE_SINGLELINE;
+                }
+
+                this.regex = new Regex(
+                        regex.getBytes(StandardCharsets.US_ASCII), 1, end, option,
+                        ASCIIEncoding.INSTANCE, Syntax.ECMAScript);
+            }
+        } else {
+            this.regex = new Regex(regex);
+        }
+        this.toStr = "JoniRegex{" + regex + '}';
+    }
+
+    @Override
+    public boolean test(final CharSequence string) {
+        return regex.matcher(string.toString().getBytes(StandardCharsets.UTF_8))
+             .search(0, string.length(), Option.NONE) > Matcher.FAILED;
+    }
+
+    @Override
+    public String toString() {
+        return toStr;
+    }
+}

http://git-wip-us.apache.org/repos/asf/johnzon/blob/33d2acff/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/regex/JsRegexTest.java
----------------------------------------------------------------------
diff --git a/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/regex/JsRegexTest.java b/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/regex/JsRegexTest.java
new file mode 100644
index 0000000..b925ff7
--- /dev/null
+++ b/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/regex/JsRegexTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.jsonschema.regex;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.stream.Stream;
+
+import org.junit.Test;
+
+public class JsRegexTest {
+
+    @Test
+    public void valid() {
+        final String regex = "/^foo/i";
+        Stream.of("foo", "foobar", "FoO_")
+                .forEach(val -> Stream.of(new JavascriptRegex(regex), new JoniRegex(regex))
+                        .forEach(validator -> assertTrue(validator.toString() + " # " + val, validator.test(val))));
+    }
+
+    @Test
+    public void invalid() {
+        final String regex = "/^foo/";
+        Stream.of("bar", "FoO")
+                .forEach(val -> Stream.of(new JavascriptRegex(regex), new JoniRegex(regex))
+                        .forEach(validator -> assertFalse(validator.toString() + " # " + val, validator.test(val))));
+    }
+}


Fwd: johnzon git commit: provide a Joni integration for jsonschema when nashorn will be completely dropped - todo: check if we shade it with minimal=true

Posted by Romain Manni-Bucau <rm...@apache.org>.
Hi guys,

to prepare the drop of nashorn i added joni as an optional license, it is a
jruby library providing a java implementation of emacs regex and therefore
we can use it for jsonschema validation implementation. I amde it optional
for now but code is ready to make it mainstream.

Its license (and of its single dep) is MIT and i wonder if we want to just
shade it (with minimize=true?) and drop the nashorn dependency. it will be
faster anyway since it will avoid the interpretation of the js and just
compile and run the regex.

wdyt?

Romain

---------- Forwarded message ---------
From: <rm...@apache.org>
Date: lun. 2 juil. 2018 à 12:20
Subject: johnzon git commit: provide a Joni integration for jsonschema when
nashorn will be completely dropped - todo: check if we shade it with
minimal=true
To: <co...@johnzon.apache.org>


Repository: johnzon
Updated Branches:
  refs/heads/master 0c09b74f2 -> 33d2acff3


provide a Joni integration for jsonschema when nashorn will be completely
dropped - todo: check if we shade it with minimal=true


Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/33d2acff
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/33d2acff
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/33d2acff

Branch: refs/heads/master
Commit: 33d2acff3600d82c5e3a735b869d7b49975c5e76
Parents: 0c09b74
Author: Romain Manni-Bucau <rm...@gmail.com>
Authored: Mon Jul 2 12:20:10 2018 +0200
Committer: Romain Manni-Bucau <rm...@gmail.com>
Committed: Mon Jul 2 12:20:10 2018 +0200

----------------------------------------------------------------------
 LICENSE                                         |  5 ++
 johnzon-jsonschema/pom.xml                      |  8 +++
 .../johnzon/jsonschema/regex/JoniRegex.java     | 70 ++++++++++++++++++++
 .../johnzon/jsonschema/regex/JsRegexTest.java   | 45 +++++++++++++
 4 files changed, 128 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/johnzon/blob/33d2acff/LICENSE
----------------------------------------------------------------------
diff --git a/LICENSE b/LICENSE
index 261eeb9..ef77930 100644
--- a/LICENSE
+++ b/LICENSE
@@ -199,3 +199,8 @@
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
+
+-----
+
+This product bundles Joni, which is available under a
+"MIT" license.  For details, see https://github.com/jruby/joni.
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/johnzon/blob/33d2acff/johnzon-jsonschema/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-jsonschema/pom.xml b/johnzon-jsonschema/pom.xml
index b0d0086..1f74030 100644
--- a/johnzon-jsonschema/pom.xml
+++ b/johnzon-jsonschema/pom.xml
@@ -29,6 +29,14 @@
   <name>Johnzon :: JSON Schema</name>

   <dependencies>
+    <dependency> <!-- only when nashorn will be dropped -->
+      <groupId>org.jruby.joni</groupId>
+      <artifactId>joni</artifactId>
+      <version>2.1.16</version>
+      <scope>provided</scope>
+      <optional>true</optional>
+    </dependency>
+
     <dependency>
       <groupId>org.apache.johnzon</groupId>
       <artifactId>johnzon-core</artifactId>

http://git-wip-us.apache.org/repos/asf/johnzon/blob/33d2acff/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/regex/JoniRegex.java
----------------------------------------------------------------------
diff --git
a/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/regex/JoniRegex.java
b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/regex/JoniRegex.java
new file mode 100644
index 0000000..d0871c7
--- /dev/null
+++
b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/regex/JoniRegex.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.jsonschema.regex;
+
+import java.nio.charset.StandardCharsets;
+import java.util.function.Predicate;
+
+import org.jcodings.specific.ASCIIEncoding;
+import org.joni.Matcher;
+import org.joni.Option;
+import org.joni.Regex;
+import org.joni.Syntax;
+
+public class JoniRegex implements Predicate<CharSequence> {
+    private final Regex regex;
+    private final String toStr;
+
+    public JoniRegex(final String regex) {
+        if (regex.startsWith("/") && regex.length() > 1) {
+            final int end = regex.lastIndexOf('/');
+            if (end < 0) {
+                this.regex = new Regex(regex);
+            } else {
+                final String optStr = regex.substring(end + 1);
+                int option = Option.SINGLELINE;
+
+                if (optStr.contains("i")) {
+                    option |= Option.IGNORECASE;
+                } else if (optStr.contains("m")) {
+                    option &= ~Option.SINGLELINE;
+                    option |= Option.NEGATE_SINGLELINE;
+                }
+
+                this.regex = new Regex(
+                        regex.getBytes(StandardCharsets.US_ASCII), 1, end,
option,
+                        ASCIIEncoding.INSTANCE, Syntax.ECMAScript);
+            }
+        } else {
+            this.regex = new Regex(regex);
+        }
+        this.toStr = "JoniRegex{" + regex + '}';
+    }
+
+    @Override
+    public boolean test(final CharSequence string) {
+        return
regex.matcher(string.toString().getBytes(StandardCharsets.UTF_8))
+             .search(0, string.length(), Option.NONE) > Matcher.FAILED;
+    }
+
+    @Override
+    public String toString() {
+        return toStr;
+    }
+}

http://git-wip-us.apache.org/repos/asf/johnzon/blob/33d2acff/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/regex/JsRegexTest.java
----------------------------------------------------------------------
diff --git
a/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/regex/JsRegexTest.java
b/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/regex/JsRegexTest.java
new file mode 100644
index 0000000..b925ff7
--- /dev/null
+++
b/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/regex/JsRegexTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.jsonschema.regex;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.stream.Stream;
+
+import org.junit.Test;
+
+public class JsRegexTest {
+
+    @Test
+    public void valid() {
+        final String regex = "/^foo/i";
+        Stream.of("foo", "foobar", "FoO_")
+                .forEach(val -> Stream.of(new JavascriptRegex(regex), new
JoniRegex(regex))
+                        .forEach(validator ->
assertTrue(validator.toString() + " # " + val, validator.test(val))));
+    }
+
+    @Test
+    public void invalid() {
+        final String regex = "/^foo/";
+        Stream.of("bar", "FoO")
+                .forEach(val -> Stream.of(new JavascriptRegex(regex), new
JoniRegex(regex))
+                        .forEach(validator ->
assertFalse(validator.toString() + " # " + val, validator.test(val))));
+    }
+}