You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2010/05/12 01:48:25 UTC

svn commit: r943341 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/dom/ main/resources/org/apache/tapestry5/dom/ test/java/org/apache/tapestry5/dom/ test/java/org/apache/tapestry5/internal/services/

Author: hlship
Date: Tue May 11 23:48:25 2010
New Revision: 943341

URL: http://svn.apache.org/viewvc?rev=943341&view=rev
Log:
TAP5-1145: Writing only text (or raw text) into a Document via MarkupWriter results in [empty Document], not the actual text

Removed:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/DomMessages.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/dom/DomStrings.properties
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Document.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/dom/DOMTest.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/MarkupWriterImplTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Document.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Document.java?rev=943341&r1=943340&r2=943341&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Document.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Document.java Tue May 11 23:48:25 2010
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
 //
 // Licensed 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
+// 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,
@@ -28,7 +28,7 @@ import java.util.Map;
 public final class Document extends Node
 {
     /**
-     * XML Namespace URI.  May be bound to the "xml" but must not be bound to any other prefix.
+     * XML Namespace URI. May be bound to the "xml" but must not be bound to any other prefix.
      */
     public static final String XML_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace";
 
@@ -73,8 +73,9 @@ public final class Document extends Node
 
     /**
      * Finds an element based on a path of element names.
-     *
-     * @param path slash separated series of element names
+     * 
+     * @param path
+     *            slash separated series of element names
      * @return the matching element, or null if not found
      * @see Element#find(String)
      */
@@ -82,13 +83,15 @@ public final class Document extends Node
     {
         Defense.notBlank(path, "path");
 
-        if (rootElement == null) return null;
+        if (rootElement == null)
+            return null;
 
         int slashx = path.indexOf("/");
 
         String rootElementName = slashx < 0 ? path : path.substring(0, slashx);
 
-        if (!rootElement.getName().equals(rootElementName)) return null;
+        if (!rootElement.getName().equals(rootElementName))
+            return null;
 
         return slashx < 0 ? rootElement : rootElement.find(path.substring(slashx + 1));
     }
@@ -118,9 +121,11 @@ public final class Document extends Node
 
     /**
      * Creates a new root element within a namespace.
-     *
-     * @param namespace URI of namespace containing the element
-     * @param name      name of element with namespace
+     * 
+     * @param namespace
+     *            URI of namespace containing the element
+     * @param name
+     *            name of element with namespace
      * @return the root element
      */
     public Element newRootElement(String namespace, String name)
@@ -133,14 +138,12 @@ public final class Document extends Node
     @Override
     public void toMarkup(Document document, PrintWriter writer, Map<String, String> namespaceURIToPrefix)
     {
-        if (rootElement == null) throw new IllegalStateException(DomMessages.noRootElement());
-
-
         if (model.isXML())
         {
             writer.print("<?xml version=\"1.0\"");
 
-            if (encoding != null) writer.printf(" encoding=\"%s\"", encoding);
+            if (encoding != null)
+                writer.printf(" encoding=\"%s\"", encoding);
 
             writer.print("?>\n");
         }
@@ -155,6 +158,9 @@ public final class Document extends Node
                 n.toMarkup(this, writer, namespaceURIToPrefix);
         }
 
+        if (rootElement == null)
+            return;
+
         Map<String, String> initialNamespaceMap = CollectionFactory.newMap();
 
         initialNamespaceMap.put("xml", "http://www.w3.org/XML/1998/namespace");
@@ -163,14 +169,6 @@ public final class Document extends Node
         rootElement.toMarkup(document, writer, initialNamespaceMap);
     }
 
-    @Override
-    public String toString()
-    {
-        if (rootElement == null) return "[empty Document]";
-
-        return super.toString();
-    }
-
     public Element getRootElement()
     {
         return rootElement;
@@ -178,8 +176,9 @@ public final class Document extends Node
 
     /**
      * Tries to find an element in this document whose id is specified.
-     *
-     * @param id the value of the id attribute of the element being looked for
+     * 
+     * @param id
+     *            the value of the id attribute of the element being looked for
      * @return the element if found. null if not found.
      */
     public Element getElementById(String id)
@@ -195,18 +194,16 @@ public final class Document extends Node
     @Override
     protected Map<String, String> getNamespaceURIToPrefix()
     {
-        if (rootElement == null)
-        {
-            return Collections.emptyMap();
-        }
+        if (rootElement == null) { return Collections.emptyMap(); }
 
         return rootElement.getNamespaceURIToPrefix();
     }
 
     /**
      * Visits the root element of the document.
-     *
-     * @param visitor callback
+     * 
+     * @param visitor
+     *            callback
      * @since 5.1.0.0
      */
     void visit(Visitor visitor)
@@ -226,7 +223,7 @@ public final class Document extends Node
 
     /**
      * Adds the comment and returns this document for further construction.
-     *
+     * 
      * @since 5.1.0.0
      */
     public Document comment(String text)
@@ -238,7 +235,7 @@ public final class Document extends Node
 
     /**
      * Adds the raw text and returns this document for further construction.
-     *
+     * 
      * @since 5.1.0.0
      */
     public Document raw(String text)
@@ -249,10 +246,11 @@ public final class Document extends Node
     }
 
     /**
-     * Adds and returns a new text node (the text node is returned so that {@link Text#write(String)} or [@link {@link
-     * Text#writef(String, Object[])} may be invoked.
-     *
-     * @param text initial text for the node
+     * Adds and returns a new text node (the text node is returned so that {@link Text#write(String)} or [@link
+     * {@link Text#writef(String, Object[])} may be invoked.
+     * 
+     * @param text
+     *            initial text for the node
      * @return the new Text node
      */
     public Text text(String text)
@@ -262,8 +260,9 @@ public final class Document extends Node
 
     /**
      * Adds and returns a new CDATA node.
-     *
-     * @param content the content to be rendered by the node
+     * 
+     * @param content
+     *            the content to be rendered by the node
      * @return the newly created node
      */
     public CData cdata(String content)

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/dom/DOMTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/dom/DOMTest.java?rev=943341&r1=943340&r2=943341&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/dom/DOMTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/dom/DOMTest.java Tue May 11 23:48:25 2010
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -159,14 +159,6 @@ public class DOMTest extends InternalBas
         assertEquals(d.toString(), readFile("nested_elements.txt"));
     }
 
-    @Test
-    public void to_string_on_empty_document()
-    {
-        Document d = new Document();
-
-        assertEquals(d.toString(), "[empty Document]");
-    }
-
     @Test(expectedExceptions = IllegalArgumentException.class)
     public void attribute_names_may_not_be_blank()
     {

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/MarkupWriterImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/MarkupWriterImplTest.java?rev=943341&r1=943340&r2=943341&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/MarkupWriterImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/MarkupWriterImplTest.java Tue May 11 23:48:25 2010
@@ -4,7 +4,7 @@
 // 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
+// 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,
@@ -43,7 +43,7 @@ public class MarkupWriterImplTest extend
         catch (RuntimeException ex)
         {
             assertEquals(ex.getMessage(),
-                         "A document must have exactly one root element. Element <root1> is already the root element.");
+                    "A document must have exactly one root element. Element <root1> is already the root element.");
         }
     }
 
@@ -57,8 +57,7 @@ public class MarkupWriterImplTest extend
         w.element("root");
         w.end();
 
-        assertEquals(w.toString(), "<?xml version=\"1.0\"?>\n" +
-                "  <root/>");
+        assertEquals(w.toString(), "<?xml version=\"1.0\"?>\n" + "  <root/>");
     }
 
     @Test
@@ -74,7 +73,7 @@ public class MarkupWriterImplTest extend
         assertEquals(w.toString(), "<?xml version=\"1.0\"?>\n  <root/>");
     }
 
-    @Test()
+    @Test
     public void preamble_content() throws Exception
     {
         MarkupWriter w = new MarkupWriterImpl(new XMLMarkupModel());
@@ -92,6 +91,17 @@ public class MarkupWriterImplTest extend
         assertEquals(w.getDocument().toString(), readFile("preamble_content.txt"));
     }
 
+    /** TAP5-1145 */
+    @Test
+    public void document_without_root_element()
+    {
+        MarkupWriter w = new MarkupWriterImpl(new XMLMarkupModel());
+
+        w.write("preamble text");
+
+        assertEquals(w.getDocument().toString(), "<?xml version=\"1.0\"?>\n" + "preamble text");
+    }
+
     @Test(expectedExceptions = IllegalStateException.class)
     public void attribute_ns_with_no_current_element()
     {
@@ -156,7 +166,7 @@ public class MarkupWriterImplTest extend
         root.attribute("gnip", "gnop");
 
         assertEquals(w.toString(),
-                     "<root gnip=\"gnop\" foo=\"bar\">before child<nested>inner text</nested>after child</root>");
+                "<root gnip=\"gnop\" foo=\"bar\">before child<nested>inner text</nested>after child</root>");
     }
 
     @Test
@@ -183,7 +193,7 @@ public class MarkupWriterImplTest extend
 
         assertEquals(w.toString(), "<root gnip=\"gnop\" foo=\"bar\"></root>");
     }
-    
+
     @Test
     public void attributes_odd_number()
     {
@@ -198,8 +208,7 @@ public class MarkupWriterImplTest extend
         }
         catch (RuntimeException ex)
         {
-            assertMessageContains(ex, 
-                    "Writing attributes of the element 'img' failed.", 
+            assertMessageContains(ex, "Writing attributes of the element 'img' failed.",
                     "A attribute name or value is omitted [src, foo.png, width, 20, 30].",
                     "Please provide an even number of values, alternating names and values");
         }
@@ -266,7 +275,6 @@ public class MarkupWriterImplTest extend
         assertEquals(w.toString(), "<root>Test name: writef</root>");
     }
 
-
     @Test
     public void write_raw()
     {
@@ -301,8 +309,9 @@ public class MarkupWriterImplTest extend
         w.end(); // child
         w.end(); // root
 
-        assertEquals(w.toString(),
-                     "<?xml version=\"1.0\"?>\n<fred:root fred:foo=\"bar\" xmlns:barney=\"barneyns\" xmlns:fred=\"fredns\"><barney:child/></fred:root>");
+        assertEquals(
+                w.toString(),
+                "<?xml version=\"1.0\"?>\n<fred:root fred:foo=\"bar\" xmlns:barney=\"barneyns\" xmlns:fred=\"fredns\"><barney:child/></fred:root>");
     }
 
     @Test
@@ -316,7 +325,7 @@ public class MarkupWriterImplTest extend
         w.write("More Normal Text");
 
         assertEquals(w.toString(),
-                     "<?xml version=\"1.0\"?>\n<root>Normal Text <![CDATA[< & >]]>More Normal Text</root>");
+                "<?xml version=\"1.0\"?>\n<root>Normal Text <![CDATA[< & >]]>More Normal Text</root>");
     }
 
     @Test
@@ -359,10 +368,13 @@ public class MarkupWriterImplTest extend
         w.end();
         w.end();
 
-        // Because we are invoking Element.text(), the text added by the listener is appended to the body of the element,
+        // Because we are invoking Element.text(), the text added by the listener is appended to the body of the
+        // element,
         // which is correct but may not be what you'd expect.
 
-        assertEquals(w.toString(), "<?xml version=\"1.0\"?>\n" +
-                "<root><no-listener>before listener<listener>[Start: listener]before n-w-l<nested-with-listener>[Start: nested-with-listener]n-w-l text[End: nested-with-listener]</nested-with-listener>after n-w-l[End: listener]</listener>after listener</no-listener></root>");
+        assertEquals(
+                w.toString(),
+                "<?xml version=\"1.0\"?>\n"
+                        + "<root><no-listener>before listener<listener>[Start: listener]before n-w-l<nested-with-listener>[Start: nested-with-listener]n-w-l text[End: nested-with-listener]</nested-with-listener>after n-w-l[End: listener]</listener>after listener</no-listener></root>");
     }
 }