You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/08/31 07:38:36 UTC

[groovy] branch master updated: GROOVY-10210: Provide XML factory helper factory classes to assist with porting

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 0cdaf2b  GROOVY-10210: Provide XML factory helper factory classes to assist with porting
0cdaf2b is described below

commit 0cdaf2bf24a24fb1a0553e049c5fd8924bbaf572
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Aug 31 17:20:31 2021 +1000

    GROOVY-10210: Provide XML factory helper factory classes to assist with porting
---
 .../main/groovy/groovy/xml/XmlParserFactory.groovy | 34 ++++++++++++++++++++++
 .../groovy/groovy/xml/XmlSlurperFactory.groovy     | 34 ++++++++++++++++++++++
 .../groovy/groovy/xml/XmlParserFactoryTest.groovy  | 31 ++++++++++++++++++++
 .../groovy/groovy/xml/XmlSlurperFactoryTest.groovy | 31 ++++++++++++++++++++
 4 files changed, 130 insertions(+)

diff --git a/subprojects/groovy-xml/src/main/groovy/groovy/xml/XmlParserFactory.groovy b/subprojects/groovy-xml/src/main/groovy/groovy/xml/XmlParserFactory.groovy
new file mode 100644
index 0000000..d19739d
--- /dev/null
+++ b/subprojects/groovy-xml/src/main/groovy/groovy/xml/XmlParserFactory.groovy
@@ -0,0 +1,34 @@
+/*
+ *  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 groovy.xml
+
+/**
+ * Factory method targetting dynamic contexts which allows a new {@code XmlParser} to be created.
+ * It is intended to assist with migration from Groovy 2.5.x to Groovy 3+.
+ * Code using this factory will not need to change moving from 2.x to 3+.
+ * In Groovy 2.5.x, a {@code groovy.util.XmlParser} will be returned.
+ * For Groovy 3+, a {@code groovy.xml.XmlParser} will be returned.
+ * For contexts requiring additional static type checking, it is recommended simply to use the
+ * normal constructor and change the package to {@code groovy.xml} prior to reaching Groovy 4.
+ */
+class XmlParserFactory {
+    static newParser(... args) {
+        new XmlParser(*args)
+    }
+}
diff --git a/subprojects/groovy-xml/src/main/groovy/groovy/xml/XmlSlurperFactory.groovy b/subprojects/groovy-xml/src/main/groovy/groovy/xml/XmlSlurperFactory.groovy
new file mode 100644
index 0000000..d5898b9
--- /dev/null
+++ b/subprojects/groovy-xml/src/main/groovy/groovy/xml/XmlSlurperFactory.groovy
@@ -0,0 +1,34 @@
+/*
+ *  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 groovy.xml
+
+/**
+ * Factory method targetting dynamic contexts which allows a new {@code XmlSlurper} to be created.
+ * It is intended to assist with migration from Groovy 2.5.x to Groovy 3+.
+ * Code using this factory will not need to change moving from 2.x to 3+.
+ * In Groovy 2.5.x, a {@code groovy.util.XmlSlurper} will be returned.
+ * For Groovy 3+, a {@code groovy.xml.XmlSlurper} will be returned.
+ * For contexts requiring additional static type checking, it is recommended simply to use the
+ * normal constructor and change the package to {@code groovy.xml} prior to reaching Groovy 4.
+ */
+class XmlSlurperFactory {
+    static newSlurper(... args) {
+        new XmlSlurper(*args)
+    }
+}
diff --git a/subprojects/groovy-xml/src/test/groovy/groovy/xml/XmlParserFactoryTest.groovy b/subprojects/groovy-xml/src/test/groovy/groovy/xml/XmlParserFactoryTest.groovy
new file mode 100644
index 0000000..6b70d6d
--- /dev/null
+++ b/subprojects/groovy-xml/src/test/groovy/groovy/xml/XmlParserFactoryTest.groovy
@@ -0,0 +1,31 @@
+/*
+ *  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 groovy.xml
+
+import org.junit.Test
+
+class XmlParserFactoryTest {
+    @Test
+    void testCreateParser() {
+        def xml = '<root><old/></root>'
+        def parser = XmlParserFactory.newParser()
+        def root = parser.parseText(xml)
+        assert root.children()[0].name() == 'old'
+    }
+}
diff --git a/subprojects/groovy-xml/src/test/groovy/groovy/xml/XmlSlurperFactoryTest.groovy b/subprojects/groovy-xml/src/test/groovy/groovy/xml/XmlSlurperFactoryTest.groovy
new file mode 100644
index 0000000..113a28f
--- /dev/null
+++ b/subprojects/groovy-xml/src/test/groovy/groovy/xml/XmlSlurperFactoryTest.groovy
@@ -0,0 +1,31 @@
+/*
+ *  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 groovy.xml
+
+import org.junit.Test
+
+class XmlSlurperFactoryTest {
+    @Test
+    void testCreateParser() {
+        def xml = '<root><old/></root>'
+        def slurper = XmlSlurperFactory.newSlurper(true, true)
+        def root = slurper.parseText(xml)
+        assert root.children()[0].name() == 'old'
+    }
+}