[java-metadata-aggregator] 02/03: MDA-109 add stage to trim whitespace from elements

Ian Young ian at iay.org.uk
Tue Dec 1 09:14:59 EST 2015


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

iay pushed a commit to branch master
in repository java-metadata-aggregator.

commit ec1b14b654ba9436531261d60b67292f10d1cda1
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Tue Dec 1 15:14:29 2015 +0100

    MDA-109 add stage to trim whitespace from elements
    
    Import WhitespaceTrimmingVisitor from ukf-mda project.
---
 .../metadata/dom/WhitespaceTrimmingVisitor.java    | 53 ++++++++++++++++++
 .../dom/WhitespaceTrimmingVisitorTest.java         | 65 ++++++++++++++++++++++
 2 files changed, 118 insertions(+)

diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/WhitespaceTrimmingVisitor.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/WhitespaceTrimmingVisitor.java
new file mode 100644
index 0000000..387f462
--- /dev/null
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/WhitespaceTrimmingVisitor.java
@@ -0,0 +1,53 @@
+/*
+ * 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.metadata.dom;
+
+import javax.annotation.Nonnull;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import net.shibboleth.metadata.Item;
+
+/**
+ * {@link Node} visitor which trims leading and trailing whitespace from the
+ * visited node's text content.
+ */
+class WhitespaceTrimmingVisitor implements NodeVisitor, ElementVisitor, AttrVisitor {
+
+    @Override
+    public void visitNode(@Nonnull final Node visited, @Nonnull final Item<Element> item) {
+        assert visited != null;
+        assert item != null;
+        final String originalText = visited.getTextContent();
+        final String newText = originalText.trim();
+        visited.setTextContent(newText);
+    }
+
+    @Override
+    public void visitElement(@Nonnull final Element visited, @Nonnull final Item<Element> item) {
+        visitNode(visited, item);
+    }
+    
+    @Override
+    public void visitAttr(@Nonnull final Attr visited, @Nonnull final Item<Element> item) {
+        visitNode(visited, item);
+    }
+    
+}
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/WhitespaceTrimmingVisitorTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/WhitespaceTrimmingVisitorTest.java
new file mode 100644
index 0000000..0f5fd9d
--- /dev/null
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/WhitespaceTrimmingVisitorTest.java
@@ -0,0 +1,65 @@
+
+package net.shibboleth.metadata.dom;
+
+import javax.xml.parsers.DocumentBuilder;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import net.shibboleth.metadata.dom.saml.SAMLMetadataSupport;
+import net.shibboleth.utilities.java.support.xml.ElementSupport;
+import net.shibboleth.utilities.java.support.xml.ParserPool;
+
+public class WhitespaceTrimmingVisitorTest extends BaseDOMTest {
+    
+    /** Constructor sets class under test. */
+    public WhitespaceTrimmingVisitorTest() {
+        super(WhitespaceTrimmingVisitor.class);
+    }
+    
+    private DOMElementItem makeItem() throws Exception {
+        final ParserPool parserPool = getParserPool();
+        final DocumentBuilder builder = parserPool.getBuilder();
+        final Document document = builder.newDocument();
+        final Element element = ElementSupport.constructElement(document,
+                SAMLMetadataSupport.ENTITY_DESCRIPTOR_NAME);
+        ElementSupport.setDocumentElement(document, element);
+        return new DOMElementItem(element);
+    }
+    
+    @Test
+    public void visitAttr() throws Exception {
+        final DOMElementItem item = makeItem();
+        final Document doc = item.unwrap().getOwnerDocument();
+        final Attr attr = doc.createAttribute("foo");
+        attr.setTextContent("   trimmed   \n\n   \t   ");
+        final AttrVisitor av = new WhitespaceTrimmingVisitor();
+        av.visitAttr(attr, item);
+        Assert.assertEquals(attr.getTextContent(), "trimmed");
+    }
+
+    @Test
+    public void visitElement() throws Exception {
+        final DOMElementItem item = makeItem();
+        final Document doc = item.unwrap().getOwnerDocument();
+        final Element e = doc.createElement("foo");
+        e.setTextContent("   trimmed   \n\n   \t   ");
+        final ElementVisitor ev = new WhitespaceTrimmingVisitor();
+        ev.visitElement(e, item);
+        Assert.assertEquals(e.getTextContent(), "trimmed");
+    }
+
+    @Test
+    public void visitNode() throws Exception {
+        final DOMElementItem item = makeItem();
+        final Document doc = item.unwrap().getOwnerDocument();
+        final Attr attr = doc.createAttribute("foo");
+        attr.setTextContent("   trimmed   \n\n   \t   ");
+        final NodeVisitor nv = new WhitespaceTrimmingVisitor();
+        nv.visitNode(attr, item);
+        Assert.assertEquals(attr.getTextContent(), "trimmed");
+    }
+}

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


More information about the commits mailing list