You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2006/08/14 15:37:23 UTC
svn commit: r431345 - in /tapestry/tapestry5/tapestry-core/trunk/src:
main/java/org/apache/tapestry/ main/java/org/apache/tapestry/dom/
main/java/org/apache/tapestry/internal/services/
main/java/org/apache/tapestry/services/ main/resources/org/apache/t...
Author: hlship
Date: Mon Aug 14 06:37:21 2006
New Revision: 431345
URL: http://svn.apache.org/viewvc?rev=431345&view=rev
Log:
Start of a new MarkupWriter implementation based on constructing a light-weight DOM tree.
Added:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Comment.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Document.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Element.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Node.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Text.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/MarkupWriterImpl.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/MarkupWriterFactory.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/dom/
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/dom/DOMTest.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/MarkupWriterImplTest.java
tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/
tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/document_with_root_element_and_attributes.txt
tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/nested_elements.txt
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/MarkupWriter.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/MarkupWriter.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/MarkupWriter.java?rev=431345&r1=431344&r2=431345&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/MarkupWriter.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/MarkupWriter.java Mon Aug 14 06:37:21 2006
@@ -14,13 +14,77 @@
package org.apache.tapestry;
+import java.io.PrintWriter;
+
+import org.apache.tapestry.dom.Element;
+
/**
* An interface used by objects, such as Tapestry components, that need to render themselves as some
- * form of XML markup.
+ * form of XML markup. A markup writer maintains the idea of a current element. Attributes are added
+ * to the current element, and new text and elements are placed inside the current element. In this
+ * way, the markup writer maintains a facade that XML markup is generated as a stream, even though
+ * the implementation builds a kind of DOM tree. The DOM tree can be also be manipulated. This
+ * solves a number of problems from Tapestry 4 (and earlier) where random access to the DOM was
+ * desired and had to be simulated through complex buffering.
*
* @author Howard M. Lewis Ship
*/
public interface MarkupWriter
{
+ /**
+ * Begins a new element as a child of the current element. The new element becomes the current
+ * element. The new Element is returned and can be directly manipulated (possibly at a later
+ * date).
+ *
+ * @param name
+ * the name of the element to create
+ * @return the new DOM Element node
+ */
+ Element element(String name);
+
+ /**
+ * Ends the current element. The new current element will be the parent element. Returns the new
+ * current element (which may be null when ending the root element for the document).
+ */
+
+ Element end();
+
+ /**
+ * Writes the text as a child of the current element.
+ * <p>
+ * TODO: Filtering of XML entities.
+ */
+
+ void write(String text);
+
+ /** Writes a line separator. */
+ void writeln();
+
+ /** Writes a formatted string. */
+ void writef(String format, Object... args);
+
+ /**
+ * Adds an XML comment. The text should be just the comment content, the comment delimiters will
+ * be provided.
+ */
+ void comment(String text);
+
+ /**
+ * Adds an attribute to the current element. If an attribute with the given name has already
+ * been added to the element, then the new value is <strong>ignored</strong>
+ * <p>
+ * TODO: Overrides for primitive types?
+ *
+ * @param name
+ * the name of the attribute to add
+ * @param value
+ * the value to be stored as the attribute
+ */
+ void attribute(String name, String value);
+
+ /** Adds a series of attributes and values. */
+ void attributes(String... namesAndValues);
+ /** Converts the collected markup into an XML stream, sent to the writer. */
+ void toXML(PrintWriter writer);
}
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Comment.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Comment.java?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Comment.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Comment.java Mon Aug 14 06:37:21 2006
@@ -0,0 +1,41 @@
+// Copyright 2006 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
+//
+// 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 org.apache.tapestry.dom;
+
+import java.io.PrintWriter;
+
+/**
+ * A node that represents a comment within the DOM.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public class Comment extends Node
+{
+ private String _comment;
+
+ Comment(Node container, String comment)
+ {
+ super(container);
+
+ _comment = comment;
+ }
+
+ @Override
+ public void toXML(PrintWriter writer)
+ {
+ writer.printf("<!-- %s -->", _comment);
+ }
+
+}
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Document.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Document.java?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Document.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Document.java Mon Aug 14 06:37:21 2006
@@ -0,0 +1,59 @@
+// Copyright 2006 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
+//
+// 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 org.apache.tapestry.dom;
+
+import java.io.PrintWriter;
+
+/**
+ * The root node of a DOM.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public class Document extends Node
+{
+ private Element _rootElement;
+
+ public Document()
+ {
+ super(null);
+ }
+
+ /** Creates the root element for this document, replacing any previous root element. */
+ public Element newRootElement(String name)
+ {
+ _rootElement = new Element(this, name);
+
+ return _rootElement;
+ }
+
+ @Override
+ public void toXML(PrintWriter writer)
+ {
+ if (_rootElement == null)
+ throw new IllegalStateException("No root element has been defined.");
+
+ // TODO: XML declaration, plus lead-in comments, directives, DOCTYPE.
+ _rootElement.toXML(writer);
+ }
+
+ @Override
+ public String toString()
+ {
+ if (_rootElement == null)
+ return "[empty Document]";
+
+ return super.toString();
+ }
+}
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Element.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Element.java?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Element.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Element.java Mon Aug 14 06:37:21 2006
@@ -0,0 +1,147 @@
+// Copyright 2006 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
+//
+// 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 org.apache.tapestry.dom;
+
+import java.io.PrintWriter;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.tapestry.internal.annotations.SuppressNullCheck;
+
+import static org.apache.tapestry.util.CollectionFactory.newList;
+import static org.apache.tapestry.util.CollectionFactory.newMap;
+import static org.apache.tapestry.util.Defense.notBlank;
+
+/**
+ * An element that will render with a begin tag and attributes, a body, and an end tag. Also acts as
+ * a factory for enclosed Element, Text and Comment nodes. TODO: Support for CDATA nodes. Do we need
+ * Entity nodes?
+ *
+ * @author Howard M. Lewis Ship
+ */
+public class Element extends Node
+{
+ private String _name;
+
+ private Map<String, String> _attributes;
+
+ private Element _parent;
+
+ Element(Document container, String name)
+ {
+ super(container);
+
+ _name = name;
+ }
+
+ Element(Element parent, String name)
+ {
+ super(parent);
+
+ _parent = parent;
+ _name = name;
+ }
+
+ /**
+ * Returns the containing element for this element. This will be null for the root element of a
+ * document.
+ */
+ public Element getParent()
+ {
+ return _parent;
+ }
+
+ /**
+ * Adds an attribute to the element, but only if the attribute name does not already exist.
+ *
+ * @param name
+ * the name of the attribute to add
+ * @param value
+ * the value for the attribute. A value of null is allowed, and no attribute will be
+ * added to the element.
+ */
+ @SuppressNullCheck
+ public void addAttribute(String name, String value)
+ {
+ notBlank(name, "name");
+
+ if (value == null)
+ return;
+
+ if (_attributes == null)
+ _attributes = newMap();
+
+ if (!_attributes.containsKey(name))
+ _attributes.put(name, value);
+ }
+
+ /** Creates and returns a new Element node as a child of this node. */
+ public Element element(String name)
+ {
+ notBlank(name, "name");
+
+ return newChild(new Element(this, name));
+ }
+
+ public Comment comment(String text)
+ {
+ return newChild(new Comment(this, text));
+ }
+
+ public Text text(String text)
+ {
+ return newChild(new Text(this, text));
+ }
+
+ private <T extends Node> T newChild(T child)
+ {
+ addChild(child);
+
+ return child;
+ }
+
+ @Override
+ public void toXML(PrintWriter writer)
+ {
+ writer.printf("<%s", _name);
+
+ if (_attributes != null)
+ {
+ List<String> keys = newList(_attributes.keySet());
+ Collections.sort(keys);
+
+ for (String key : keys)
+ {
+ String value = _attributes.get(key);
+
+ // TODO: URL encoding of attributes!
+
+ writer.printf(" %s=\"%s\"", key, value);
+ }
+ }
+
+ if (hasChildren())
+ {
+ writer.print(">");
+ writeChildXML(writer);
+ writer.printf("</%s>", _name);
+ }
+ else
+ {
+ writer.print("/>");
+ }
+ }
+}
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Node.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Node.java?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Node.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Node.java Mon Aug 14 06:37:21 2006
@@ -0,0 +1,93 @@
+// Copyright 2006 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
+//
+// 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 org.apache.tapestry.dom;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.List;
+
+import org.apache.tapestry.internal.annotations.SuppressNullCheck;
+
+import static org.apache.tapestry.util.CollectionFactory.newList;
+
+/**
+ * A node within the DOM.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public abstract class Node
+{
+ private Node _container;
+
+ private List<Node> _children;
+
+ /**
+ * Creates a new node, setting its container to the provided value. Container may also be null,
+ * but that is only used for Document nodes (the topmost node of a DOM).
+ *
+ * @param container
+ */
+ @SuppressNullCheck
+ protected Node(Node container)
+ {
+ _container = container;
+ }
+
+ public Node getContainer()
+ {
+ return _container;
+ }
+
+ protected void addChild(Node child)
+ {
+ if (_children == null)
+ _children = newList();
+
+ _children.add(child);
+ }
+
+ protected boolean hasChildren()
+ {
+ return _children != null && !_children.isEmpty();
+ }
+
+ protected void writeChildXML(PrintWriter writer)
+ {
+ if (_children == null)
+ return;
+
+ for (Node child : _children)
+ child.toXML(writer);
+ }
+
+ /**
+ * Invokes {@link #toString()}, collecting output in a string, which is returned.
+ */
+ @Override
+ public String toString()
+ {
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter pw = new PrintWriter(stringWriter);
+
+ toXML(pw);
+
+ pw.close();
+
+ return stringWriter.toString();
+ }
+
+ /** Writes the XML for this node to the writer. */
+ public abstract void toXML(PrintWriter writer);
+}
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Text.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Text.java?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Text.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/dom/Text.java Mon Aug 14 06:37:21 2006
@@ -0,0 +1,58 @@
+// Copyright 2006 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
+//
+// 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 org.apache.tapestry.dom;
+
+import java.io.PrintWriter;
+import java.util.Formatter;
+
+/**
+ * A type of node that contains text.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public class Text extends Node
+{
+ private final StringBuilder _buffer;
+
+ private Formatter _formatter;
+
+ Text(Node container, String text)
+ {
+ super(container);
+
+ _buffer = new StringBuilder(text);
+ }
+
+ /** Writes additional text into the node, appending it to any existing text. */
+ public void write(String text)
+ {
+ _buffer.append(text);
+ }
+
+ public void writef(String format, Object... args)
+ {
+ if (_formatter == null)
+ _formatter = new Formatter(_buffer);
+
+ _formatter.format(format, args);
+ }
+
+ @Override
+ public void toXML(PrintWriter writer)
+ {
+ writer.print(_buffer.toString());
+ }
+
+}
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/MarkupWriterImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/MarkupWriterImpl.java?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/MarkupWriterImpl.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/MarkupWriterImpl.java Mon Aug 14 06:37:21 2006
@@ -0,0 +1,133 @@
+// Copyright 2006 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
+//
+// 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 org.apache.tapestry.internal.services;
+
+import java.io.PrintWriter;
+
+import org.apache.tapestry.MarkupWriter;
+import org.apache.tapestry.dom.Document;
+import org.apache.tapestry.dom.Element;
+import org.apache.tapestry.dom.Text;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+public class MarkupWriterImpl implements MarkupWriter
+{
+ private final Document _document = new Document();
+
+ private Element _current;
+
+ private Text _currentText;
+
+ public void toXML(PrintWriter writer)
+ {
+ _document.toXML(writer);
+ }
+
+ @Override
+ public String toString()
+ {
+ return _document.toString();
+ }
+
+ public void write(String text)
+ {
+ ensureCurrentElement();
+
+ if (_currentText == null)
+ {
+ _currentText = _current.text(text);
+ return;
+ }
+
+ _currentText.write(text);
+ }
+
+ public void writef(String format, Object... args)
+ {
+ // A bit of a cheat:
+
+ write("");
+ _currentText.writef(format, args);
+ }
+
+ public void writeln()
+ {
+ write("\n");
+ }
+
+ public void attribute(String name, String value)
+ {
+ ensureCurrentElement();
+
+ _current.addAttribute(name, value);
+ }
+
+ public void attributes(String... namesAndValues)
+ {
+ ensureCurrentElement();
+
+ int i = 0;
+
+ while (i < namesAndValues.length)
+ {
+ String name = namesAndValues[i++];
+ String value = namesAndValues[i++];
+
+ _current.addAttribute(name, value);
+ }
+
+ }
+
+ private void ensureCurrentElement()
+ {
+ if (_current == null)
+ throw new IllegalStateException(ServicesMessages.markupWriterNoCurrentElement());
+ }
+
+ public Element element(String name)
+ {
+ if (_current == null)
+ _current = _document.newRootElement(name);
+ else
+ _current = _current.element(name);
+
+ _currentText = null;
+
+ return _current;
+ }
+
+ public Element end()
+ {
+ ensureCurrentElement();
+
+ _current = _current.getParent();
+
+ _currentText = null;
+
+ return _current;
+ }
+
+ public void comment(String text)
+ {
+ ensureCurrentElement();
+
+ _current.comment(text);
+
+ _currentText = null;
+ }
+
+}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java?rev=431345&r1=431344&r2=431345&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java Mon Aug 14 06:37:21 2006
@@ -49,4 +49,9 @@
return MESSAGES.format("infrastructure-property-wrong-type", name, object, desiredType
.getName());
}
+
+ static final String markupWriterNoCurrentElement()
+ {
+ return MESSAGES.get("markup-writer-no-current-element");
+ }
}
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/MarkupWriterFactory.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/MarkupWriterFactory.java?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/MarkupWriterFactory.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/MarkupWriterFactory.java Mon Aug 14 06:37:21 2006
@@ -0,0 +1,28 @@
+// Copyright 2006 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
+//
+// 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 org.apache.tapestry.services;
+
+import org.apache.tapestry.MarkupWriter;
+
+/**
+ * Source for {@link org.apache.tapestry.MarkupWriter} instances.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public interface MarkupWriterFactory
+{
+ /** This will likely expand in the future to control things like output format and encoding, etc. */
+ MarkupWriter newMarkupWriter();
+}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java?rev=431345&r1=431344&r2=431345&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java Mon Aug 14 06:37:21 2006
@@ -17,12 +17,15 @@
import java.util.Collection;
import org.apache.commons.logging.Log;
+import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.internal.InternalModule;
import org.apache.tapestry.internal.services.InfrastructureImpl;
import org.apache.tapestry.internal.services.InfrastructureManagerImpl;
+import org.apache.tapestry.internal.services.MarkupWriterImpl;
import org.apache.tapestry.ioc.Configuration;
import org.apache.tapestry.ioc.MappedConfiguration;
import org.apache.tapestry.ioc.ObjectProvider;
+import org.apache.tapestry.ioc.ServiceResources;
import org.apache.tapestry.ioc.annotations.Contribute;
import org.apache.tapestry.ioc.annotations.Id;
import org.apache.tapestry.ioc.annotations.InjectService;
@@ -61,11 +64,40 @@
}
public void contributeInfrastructure(Configuration<InfrastructureContribution> configuration,
- @InjectService("ComponentNameExpander")
- ComponentNameExpander componentNameExpander)
+ ServiceResources resources)
{
- configuration.add(new InfrastructureContribution("componentNameExpander",
- componentNameExpander));
+ add(configuration, ComponentNameExpander.class, resources);
+ add(configuration, MarkupWriterFactory.class, resources);
+ }
+
+ private <T> void add(Configuration<InfrastructureContribution> configuration,
+ Class<T> serviceInterface, ServiceResources resources)
+ {
+ String className = serviceInterface.getName();
+ int dotx = className.lastIndexOf('.');
+ String serviceId = className.substring(dotx + 1);
+
+ // Convert first character to lower case to form the property name.
+
+ String propertyName = serviceId.substring(0, 1).toLowerCase() + serviceId.substring(1);
+
+ T service = resources.getService(serviceId, serviceInterface);
+
+ InfrastructureContribution contribution = new InfrastructureContribution(propertyName,
+ service);
+
+ configuration.add(contribution);
+ }
+
+ public MarkupWriterFactory buildMarkupWriterFactory()
+ {
+ return new MarkupWriterFactory()
+ {
+ public MarkupWriter newMarkupWriter()
+ {
+ return new MarkupWriterImpl();
+ }
+ };
}
/**
@@ -81,7 +113,6 @@
{
return componentName;
}
-
};
}
}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties?rev=431345&r1=431344&r2=431345&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties Mon Aug 14 06:37:21 2006
@@ -16,3 +16,6 @@
infrastructure-mode-not-set=The infrastructure mode has not been set. Unable to determine the correct set of infrastructure properties.
infrastructure-property-not-found=No infrastructure property '%s' has been configured. Configured properties are: %s.
infrastructure-property-wrong-type=Property '%s' (%s) is not assignable to type %s.
+markup-writer-no-current-element=This markup writer does not have a current element. \
+ The current element is established with the first call to element() and is \
+ maintained across subsequent calls.
Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/dom/DOMTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/dom/DOMTest.java?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/dom/DOMTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/dom/DOMTest.java Mon Aug 14 06:37:21 2006
@@ -0,0 +1,198 @@
+// Copyright 2006 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
+//
+// 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 org.apache.tapestry.dom;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.Reader;
+
+import org.testng.Assert;
+import org.testng.annotations.ExpectedExceptions;
+import org.testng.annotations.Test;
+
+/**
+ * Tests for a number of DOM node classes, including {@link org.apache.tapestry.dom.Element} and
+ * {@link org.apache.tapestry.dom.Document}.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public class DOMTest extends Assert
+{
+ /**
+ * Reads the content of a file into a string. Each line is trimmed of line separators and
+ * leading/trailing whitespace.
+ */
+ private String readFile(String file) throws Exception
+ {
+ InputStream is = getClass().getResourceAsStream(file);
+ is = new BufferedInputStream(is);
+ Reader reader = new BufferedReader(new InputStreamReader(is));
+ LineNumberReader in = new LineNumberReader(reader);
+
+ StringBuffer buffer = new StringBuffer();
+
+ while (true)
+ {
+ String line = in.readLine();
+
+ if (line == null)
+ break;
+
+ buffer.append(line.trim());
+ }
+
+ in.close();
+
+ return buffer.toString();
+ }
+
+ @Test
+ public void document_with_empty_root_element()
+ {
+ Document d = new Document();
+
+ d.newRootElement("empty");
+
+ assertEquals(d.toString(), "<empty/>");
+ }
+
+ /** Also demonstrates that attributes are provided in alphabetical order. */
+ @Test
+ public void document_with_root_element_and_attributes() throws Exception
+ {
+ Document d = new Document();
+
+ Element e = d.newRootElement("has-attributes");
+
+ e.addAttribute("fred", "flintstone");
+ e.addAttribute("barney", "rubble");
+
+ assertEquals(d.toString(), readFile("document_with_root_element_and_attributes.txt"));
+ }
+
+ @Test
+ public void nested_elements() throws Exception
+ {
+ Document d = new Document();
+
+ Element e = d.newRootElement("population");
+
+ Element p = e.element("person");
+ p.addAttribute("first-name", "Fred");
+ p.addAttribute("last-name", "Flintstone");
+
+ assertSame(p.getParent(), e);
+
+ p = e.element("person");
+ p.addAttribute("first-name", "Barney");
+ p.addAttribute("last-name", "Rubble");
+
+ assertSame(p.getParent(), e);
+
+ 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()
+ {
+ Document d = new Document();
+
+ Element e = d.newRootElement("fred");
+
+ e.addAttribute("", "value");
+ }
+
+ @Test
+ public void element_name_may_not_be_blank()
+ {
+ Document d = new Document();
+
+ d.newRootElement("");
+ }
+
+ @Test
+ public void attribute_value_null_is_no_op()
+ {
+ Document d = new Document();
+
+ Element e = d.newRootElement("root");
+
+ e.addAttribute("foo", "bar");
+
+ final String expected = "<root foo=\"bar\"/>";
+
+ assertEquals(d.toString(), expected);
+
+ e.addAttribute("foo", null);
+
+ assertEquals(d.toString(), expected);
+
+ e.addAttribute("gnip", null);
+
+ assertEquals(d.toString(), expected);
+ }
+
+ @Test
+ public void comments() throws Exception
+ {
+ Document d = new Document();
+
+ // Can't add comments to the document, not yet.
+
+ Element e = d.newRootElement("html");
+
+ e.comment("Created by Tapestry 5.0");
+
+ assertEquals(d.toString(), "<html><!-- Created by Tapestry 5.0 --></html>");
+ }
+
+ @Test
+ public void text()
+ {
+ Document d = new Document();
+
+ Element e = d.newRootElement("body");
+
+ e.text("Tapestry does DOM.");
+
+ assertEquals(d.toString(), "<body>Tapestry does DOM.</body>");
+ }
+
+ @Test
+ public void writef_with_text()
+ {
+ Document d = new Document();
+
+ Element e = d.newRootElement("root");
+
+ Text t = e.text("Start: ");
+
+ t.writef("** %s: %d **", "foo", 5);
+
+ assertEquals(d.toString(), "<root>Start: ** foo: 5 **</root>");
+ }
+}
Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/MarkupWriterImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/MarkupWriterImplTest.java?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/MarkupWriterImplTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/MarkupWriterImplTest.java Mon Aug 14 06:37:21 2006
@@ -0,0 +1,157 @@
+package org.apache.tapestry.internal.services;
+
+import org.apache.tapestry.MarkupWriter;
+import org.apache.tapestry.dom.Element;
+import org.testng.Assert;
+import org.testng.annotations.ExpectedExceptions;
+import org.testng.annotations.Test;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+public class MarkupWriterImplTest extends Assert
+{
+ @Test
+ @ExpectedExceptions(IllegalStateException.class)
+ public void write_with_no_current_element()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.write("fail!");
+ }
+
+ @Test
+ @ExpectedExceptions(IllegalStateException.class)
+ public void comment_with_no_current_element()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.comment("fail!");
+ }
+
+ @Test
+ @ExpectedExceptions(IllegalStateException.class)
+ public void end_with_no_current_element()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.end();
+ }
+
+ @Test
+ @ExpectedExceptions(IllegalStateException.class)
+ public void attribute_with_no_current_element()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.attribute("fail", "now");
+ }
+
+ @Test
+ @ExpectedExceptions(IllegalStateException.class)
+ public void attributes_with_no_current_element()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.attributes("fail", "now");
+ }
+
+ @Test
+ public void current_element_at_end_of_root_element_is_null()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.element("root");
+
+ assertNull(w.end());
+ }
+
+ @Test
+ public void element_nesting()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ Element root = w.element("root");
+
+ w.attribute("foo", "bar");
+
+ w.write("before child");
+
+ assertNotSame(w.element("nested"), root);
+
+ w.write("inner text");
+
+ assertSame(w.end(), root);
+
+ w.write("after child");
+
+ root.addAttribute("gnip", "gnop");
+
+ assertEquals(
+ w.toString(),
+ "<root foo=\"bar\" gnip=\"gnop\">before child<nested>inner text</nested>after child</root>");
+ }
+
+ @Test
+ public void writeln()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.element("root");
+ w.write("before");
+ w.writeln();
+ w.write("after");
+ w.end();
+
+ assertEquals(w.toString(), "<root>before\nafter</root>");
+ }
+
+ @Test
+ public void attributes()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.element("root");
+
+ w.attributes("foo", "bar", "gnip", "gnop");
+
+ assertEquals(w.toString(), "<root foo=\"bar\" gnip=\"gnop\"/>");
+ }
+
+ @Test
+ public void comment()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.element("root");
+ w.comment("A comment");
+ w.end();
+
+ assertEquals(w.toString(), "<root><!-- A comment --></root>");
+ }
+
+ @Test
+ public void new_text_node_after_comment_node()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.element("root");
+ w.write("before");
+ w.comment("A comment");
+ w.write("after");
+ w.end();
+
+ assertEquals(w.toString(), "<root>before<!-- A comment -->after</root>");
+ }
+
+ @Test
+ public void writef()
+ {
+ MarkupWriter w = new MarkupWriterImpl();
+
+ w.element("root");
+ w.writef("Test name: %s", "writef");
+
+ assertEquals(w.toString(), "<root>Test name: writef</root>");
+ }
+}
Added: tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/document_with_root_element_and_attributes.txt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/document_with_root_element_and_attributes.txt?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/document_with_root_element_and_attributes.txt (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/document_with_root_element_and_attributes.txt Mon Aug 14 06:37:21 2006
@@ -0,0 +1 @@
+<has-attributes barney="rubble" fred="flintstone"/>
\ No newline at end of file
Added: tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/nested_elements.txt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/nested_elements.txt?rev=431345&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/nested_elements.txt (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/dom/nested_elements.txt Mon Aug 14 06:37:21 2006
@@ -0,0 +1,4 @@
+<population>
+ <person first-name="Fred" last-name="Flintstone"/>
+ <person first-name="Barney" last-name="Rubble"/>
+</population>
\ No newline at end of file