[java-support] 19/21: XML to DDF conversion for agent config file handling.

Scott Cantor cantor.2 at osu.edu
Thu Jun 2 14:39:06 UTC 2022


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch dev/JSPT-111
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=3924360a99a87fd7e4960ad875a0c7edf4466dc9

commit 3924360a99a87fd7e4960ad875a0c7edf4466dc9
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu May 26 10:31:15 2022 -0400

    XML to DDF conversion for agent config file handling.
---
 .../utilities/java/support/ddf/DDFSupport.java     | 101 +++++++++++++++++++++
 .../utilities/java/support/ddf/DDFSupportTest.java |  94 +++++++++++++++++++
 .../shibboleth/utilities/java/support/ddf/zork.xml |   5 +
 3 files changed, 200 insertions(+)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/ddf/DDFSupport.java b/src/main/java/net/shibboleth/utilities/java/support/ddf/DDFSupport.java
new file mode 100644
index 0000000..1ec6a80
--- /dev/null
+++ b/src/main/java/net/shibboleth/utilities/java/support/ddf/DDFSupport.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.ddf;
+
+import javax.annotation.Nonnull;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.xml.ElementSupport;
+
+/**
+ * Helper methods for {@link DDF} usage.
+ * 
+ * @since 9.0.0
+ */
+public final class DDFSupport {
+
+    /** Name of child element member created by {@link #fromElement(Element)} method. */
+    @Nonnull @NotEmpty static final String CHILD_ELEMENTS_MEMBER = "_children";
+
+    /** Name of content member created by {@link #fromElement(Element)} method. */
+    @Nonnull @NotEmpty static final String CONTENT_MEMBER = "_content";
+
+    /** Private constructor. */
+    private DDFSupport() {
+        
+    }
+
+    /**
+     * Converts a DOM tree rooted at the input element into a {@link DDF} mirroring the tree.
+     *
+     * <p>The name of the object is the local name of the element. 
+     * 
+     * <p>Attributes are converted into named structure members based on the local names.</p>
+     * 
+     * <p>Child elements are recursively processed into a list named {@link #CHILD_ELEMENTS_MEMBER}.</p>
+     * 
+     * <p>Text content is stored in a structure member named {@link #CONTENT_MEMBER}.</p>
+     * 
+     * <p>Namespaces are ignored.</p>
+     * 
+     * @param element input element
+     * 
+     * @return the converted object
+     */
+    static @Nonnull DDF fromElement(@Nonnull final Element element) {
+        
+        Constraint.isNotNull(element, "Element cannot be null");
+        
+        // Named after element.
+        final DDF obj = new DDF(element.getLocalName());
+        
+        // Each attribute is added as a string member.
+        final NamedNodeMap attrs = element.getAttributes();
+        if (attrs != null) {
+            for (int i = 0; i < attrs.getLength(); ++i) {
+                final Attr attr = (Attr) attrs.item(i);
+                obj.addmember(attr.getLocalName()).string(attr.getValue());
+            }
+        }
+
+        DDF children = null;
+        
+        // Recursively convert each child to a child DDF and add it to a list. 
+        Element child = ElementSupport.getFirstChildElement(element);
+        while (child != null) {
+            if (children == null) {
+                children = obj.addmember(CHILD_ELEMENTS_MEMBER).list();
+            }
+            children.add(fromElement(child));
+            child = ElementSupport.getNextSiblingElement(child);
+        }
+        
+        final String content = ElementSupport.getElementContentAsString(element);
+        if (content != null && !content.isBlank()) {
+            obj.addmember(CONTENT_MEMBER).string(content);
+        }
+        
+        return obj;
+    }
+    
+}
\ No newline at end of file
diff --git a/src/test/java/net/shibboleth/utilities/java/support/ddf/DDFSupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/ddf/DDFSupportTest.java
new file mode 100644
index 0000000..c9f7473
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/ddf/DDFSupportTest.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.ddf;
+
+import java.io.IOException;
+import java.util.List;
+
+import javax.annotation.Nullable;
+
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.w3c.dom.Document;
+
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.xml.BasicParserPool;
+import net.shibboleth.utilities.java.support.xml.XMLParserException;
+
+/**
+ * Unit test for {@link DDFSupport}.
+ */
+public class DDFSupportTest {
+    
+    @Nullable BasicParserPool parserPool;
+    
+    /**
+     * Init parser.
+     * 
+     * @throws ComponentInitializationException 
+     */
+    @BeforeClass
+    public void setUp() throws ComponentInitializationException {
+        parserPool = new BasicParserPool();
+        parserPool.initialize();
+    }
+    
+   /**
+    * Teardown. 
+    */
+    @AfterClass
+   public void tearDown() {
+       parserPool.destroy();
+   }
+    
+    /**
+     * Test conversion.
+     * 
+     * @throws IOException 
+     * @throws XMLParserException 
+     */
+    @Test
+    public void test() throws XMLParserException, IOException {
+        final Document doc = parserPool.parse(getClass().getResourceAsStream("zork.xml"));
+        final DDF ddf = DDFSupport.fromElement(doc.getDocumentElement());
+        
+        Assert.assertNotNull(ddf);
+        Assert.assertEquals(ddf.name(), "root");
+        Assert.assertEquals(ddf.getmember("foo").string(), "bar");
+
+        final List<DDF> children = ddf.getmember(DDFSupport.CHILD_ELEMENTS_MEMBER).asList();
+        Assert.assertEquals(children.size(), 2);
+        
+        DDF child = children.get(0);
+        Assert.assertEquals(child.name(), "zork");
+        Assert.assertEquals(child.getmember("zorkmids").integer(), 10);
+        Assert.assertEquals(child.getmember("underground").string(), "true");
+        Assert.assertTrue(child.getmember(DDFSupport.CONTENT_MEMBER).isnull());
+        Assert.assertTrue(child.getmember(DDFSupport.CHILD_ELEMENTS_MEMBER).isnull());
+
+        child = children.get(1);
+        Assert.assertEquals(child.name(), "frobnitz");
+        Assert.assertEquals(child.getmember(DDFSupport.CONTENT_MEMBER).string(), "grue");
+        Assert.assertTrue(child.getmember(DDFSupport.CHILD_ELEMENTS_MEMBER).isnull());
+
+        Assert.assertTrue(ddf.getmember(DDFSupport.CONTENT_MEMBER).isnull());
+    }
+
+}
\ No newline at end of file
diff --git a/src/test/resources/net/shibboleth/utilities/java/support/ddf/zork.xml b/src/test/resources/net/shibboleth/utilities/java/support/ddf/zork.xml
new file mode 100644
index 0000000..fd4ac93
--- /dev/null
+++ b/src/test/resources/net/shibboleth/utilities/java/support/ddf/zork.xml
@@ -0,0 +1,5 @@
+<root foo="bar">
+    <zork zorkmids="10" underground="true" />
+    
+    <frobnitz>grue</frobnitz>
+</root>
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list