[spring-extensions] 02/02: JSE-49 Allow NamespaceHandler base class to sub-delegate to additional handlers

Rod Widdowson rdw at steadingsoftware.com
Sun Jun 19 14:19:53 UTC 2022


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

rdw pushed a commit to branch main
in repository spring-extensions.

View the commit online:
http://git.shibboleth.net/view/?p=spring-extensions.git;a=commit;h=7c5f07ae7ba3c30f43ba66b0a72988c10c360502

commit 7c5f07ae7ba3c30f43ba66b0a72988c10c360502
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sun Jun 19 15:18:18 2022 +0100

    JSE-49 Allow NamespaceHandler base class to sub-delegate to additional handlers
    
    https://shibboleth.atlassian.net/browse/JSE-49
    
    Test for delegated parsing.
---
 .../ext/spring/util/LowerParsersAndBean.java       | 79 ++++++++++++++++++++++
 .../ext/spring/util/NestedParsersTest.java         | 52 ++++++++++++++
 .../ext/spring/util/UpperParserAndBean.java        | 60 ++++++++++++++++
 .../spring/handlers/urn-mace-shibboleth-2.0-nested |  1 +
 src/test/resources/META-INF/spring.handlers        |  1 +
 src/test/resources/META-INF/spring.schemas         |  2 +
 .../net/shibboleth/ext/spring/util/nested.xml      | 10 +++
 src/test/resources/schema/nested.xsd               | 31 +++++++++
 8 files changed, 236 insertions(+)

diff --git a/src/test/java/net/shibboleth/ext/spring/util/LowerParsersAndBean.java b/src/test/java/net/shibboleth/ext/spring/util/LowerParsersAndBean.java
new file mode 100644
index 0000000..3a351f6
--- /dev/null
+++ b/src/test/java/net/shibboleth/ext/spring/util/LowerParsersAndBean.java
@@ -0,0 +1,79 @@
+/*
+ * 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.ext.spring.util;
+
+import javax.annotation.Nonnull;
+import javax.xml.namespace.QName;
+
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.xml.BeanDefinitionParser;
+import org.springframework.beans.factory.xml.ParserContext;
+import org.w3c.dom.Element;
+
+import net.shibboleth.utilities.java.support.xml.AttributeSupport;
+import net.shibboleth.utilities.java.support.xml.ElementSupport;
+
+public class LowerParsersAndBean extends BaseSpringNamespaceHandler {
+    
+    protected static final String NAMESPACE = "urn:mace:shibboleth:2.0:nested";
+    
+    private String message;
+    
+    public void setMessage(String theMessage) {
+        message = theMessage;
+    }
+    
+    public String getMessage() {
+        return message;
+    }
+
+    /** {@inheritDoc} */
+    public void init() {
+        registerBeanDefinitionParser(new QName(NAMESPACE, "LowerElement"), new LowerElementParser());
+        registerBeanDefinitionParser(new QName(NAMESPACE, "OuterElement"), new OuterElementParser());
+        final String adjustedName=NAMESPACE.replaceAll("\\:", "-");  
+        initializeOtherHandlers(adjustedName);
+    }
+
+    static class LowerElementParser extends AbstractCustomBeanDefinitionParser {
+        
+        /** {@inheritDoc} */
+        protected Class<LowerParsersAndBean> getBeanClass(Element element) {
+            return LowerParsersAndBean.class;
+        }
+        
+        /** {@inheritDoc} */
+        @Override protected void doParse(@Nonnull final Element config, @Nonnull final ParserContext parserContext,
+                @Nonnull final BeanDefinitionBuilder builder) {
+            builder.addPropertyValue("message", 
+                    AttributeSupport.getAttributeValue(config, null, "theMessage"));
+        }
+    }
+    
+    static class OuterElementParser implements BeanDefinitionParser {
+        
+        /** {@inheritDoc} */
+        public BeanDefinition parse(final Element config, final ParserContext parserContext) {
+            SpringSupport.parseCustomElements(ElementSupport.getChildElements(config), parserContext);
+            return null;
+        }
+
+    }
+    
+}
diff --git a/src/test/java/net/shibboleth/ext/spring/util/NestedParsersTest.java b/src/test/java/net/shibboleth/ext/spring/util/NestedParsersTest.java
new file mode 100644
index 0000000..3a21eca
--- /dev/null
+++ b/src/test/java/net/shibboleth/ext/spring/util/NestedParsersTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.ext.spring.util;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.io.ClassPathResource;
+import org.testng.annotations.Test;
+
+public class NestedParsersTest {
+
+    @Test void Test() {
+        final GenericApplicationContext context = new GenericApplicationContext();
+        context.setDisplayName("ApplicationContext for Nested ");
+
+        final SchemaTypeAwareXMLBeanDefinitionReader beanDefinitionReader =
+                new SchemaTypeAwareXMLBeanDefinitionReader(context);
+
+        beanDefinitionReader.loadBeanDefinitions(new ClassPathResource("/net/shibboleth/ext/spring/util/nested.xml"));
+        context.refresh();
+
+        final Collection<UpperParserAndBean> uppers = context.getBeansOfType(UpperParserAndBean.class).values();
+        final Collection<LowerParsersAndBean> lowers = context.getBeansOfType(LowerParsersAndBean.class).values();
+        
+        assertEquals(uppers.size(), 2);
+        
+        assertEquals(lowers.size(), 1);
+        assertEquals(lowers.iterator().next().getMessage(), "first");
+
+        assertTrue(uppers.stream().allMatch(x -> "2ndFirst".equals(x.getMessage()) || "2ndSecond".equals(x.getMessage()))); 
+    }
+    
+}
diff --git a/src/test/java/net/shibboleth/ext/spring/util/UpperParserAndBean.java b/src/test/java/net/shibboleth/ext/spring/util/UpperParserAndBean.java
new file mode 100644
index 0000000..bd10bbd
--- /dev/null
+++ b/src/test/java/net/shibboleth/ext/spring/util/UpperParserAndBean.java
@@ -0,0 +1,60 @@
+/*
+ * 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.ext.spring.util;
+
+import javax.annotation.Nonnull;
+import javax.xml.namespace.QName;
+
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.xml.ParserContext;
+import org.w3c.dom.Element;
+
+import net.shibboleth.utilities.java.support.xml.AttributeSupport;
+
+public class UpperParserAndBean extends SecondaryNamespaceHandler {
+    
+    private String message;
+
+    public void setMessage(String theMessage) {
+        message = theMessage;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    /** {@inheritDoc} */
+    public void doInit() {
+        registerBeanDefinitionParser(new QName(LowerParsersAndBean.NAMESPACE, "UpperElement"), new UpperElementParser());
+    }
+
+    static class UpperElementParser extends AbstractCustomBeanDefinitionParser {
+
+        /** {@inheritDoc} */
+        protected Class<UpperParserAndBean> getBeanClass(Element element) {
+            return UpperParserAndBean.class;
+        }
+        
+        /** {@inheritDoc} */
+        @Override protected void doParse(@Nonnull final Element config, @Nonnull final ParserContext parserContext,
+                @Nonnull final BeanDefinitionBuilder builder) {
+            builder.addPropertyValue("message", 
+                    AttributeSupport.getAttributeValue(config, null, "theSecondMessage"));
+        }
+    }
+}
diff --git a/src/test/resources/META-INF/net/shibboleth/spring/handlers/urn-mace-shibboleth-2.0-nested b/src/test/resources/META-INF/net/shibboleth/spring/handlers/urn-mace-shibboleth-2.0-nested
new file mode 100644
index 0000000..1641f97
--- /dev/null
+++ b/src/test/resources/META-INF/net/shibboleth/spring/handlers/urn-mace-shibboleth-2.0-nested
@@ -0,0 +1 @@
+net.shibboleth.ext.spring.util.UpperParserAndBean
\ No newline at end of file
diff --git a/src/test/resources/META-INF/spring.handlers b/src/test/resources/META-INF/spring.handlers
index b647db9..6b6cd71 100644
--- a/src/test/resources/META-INF/spring.handlers
+++ b/src/test/resources/META-INF/spring.handlers
@@ -1 +1,2 @@
 urn\:mace\:shibboleth\:2.0\:naturestudy = net.shibboleth.ext.spring.naturestudy.NamespaceHandler
+urn\:mace\:shibboleth\:2.0\:nested = net.shibboleth.ext.spring.util.LowerParsersAndBean
\ No newline at end of file
diff --git a/src/test/resources/META-INF/spring.schemas b/src/test/resources/META-INF/spring.schemas
index 4a15e24..76ed0cc 100644
--- a/src/test/resources/META-INF/spring.schemas
+++ b/src/test/resources/META-INF/spring.schemas
@@ -1 +1,3 @@
 urn\:mace\:shibboleth\:2.0\:naturestudy = schema/nature-study.xsd
+urn\:mace\:shibboleth\:2.0\:nested = schema/nested.xsd
+http\://shibboleth.net/schema/nested.xsd = schema/nested.xsd
\ No newline at end of file
diff --git a/src/test/resources/net/shibboleth/ext/spring/util/nested.xml b/src/test/resources/net/shibboleth/ext/spring/util/nested.xml
new file mode 100644
index 0000000..8e92186
--- /dev/null
+++ b/src/test/resources/net/shibboleth/ext/spring/util/nested.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<OuterElement xmlns="urn:mace:shibboleth:2.0:nested" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
+   xsi:schemaLocation="urn:mace:shibboleth:2.0:nested http://shibboleth.net/schema/nested.xsd"
+>
+
+    <UpperElement id="1" theSecondMessage="2ndFirst"/>
+    <LowerElement id="2" theMessage="first"/>
+    <UpperElement id="3" theSecondMessage="2ndSecond"/>
+
+</OuterElement>
diff --git a/src/test/resources/schema/nested.xsd b/src/test/resources/schema/nested.xsd
new file mode 100644
index 0000000..afd2731
--- /dev/null
+++ b/src/test/resources/schema/nested.xsd
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema" 
+        xmlns:ns="urn:mace:shibboleth:2.0:nested"
+        targetNamespace="urn:mace:shibboleth:2.0:nested"
+        elementFormDefault="qualified">
+
+    <complexType name="LowerType">
+        <attribute name="theMessage" type="string" use="required"/>
+        <attribute name="id" type="string" use="required"/>
+    </complexType>
+
+    <element name="LowerElement" type="ns:LowerType"/>
+
+    <complexType name="UpperType">
+        <attribute name="theSecondMessage" type="string" use="required"/>
+        <attribute name="id" type="string" use="required"/>
+    </complexType>
+
+    <element name="UpperElement" type="ns:UpperType"/>
+    
+
+    <complexType name="OuterType">
+        <choice minOccurs="1" maxOccurs="unbounded">
+            <element ref="ns:UpperElement"/>
+            <element ref="ns:LowerElement"/>
+        </choice>
+        
+    </complexType>
+    
+    <element name="OuterElement" type="ns:OuterType" />
+</schema>

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


More information about the commits mailing list