001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.dom;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018:
019: import java.io.PrintWriter;
020: import java.util.Collections;
021: import java.util.List;
022:
023: import org.apache.tapestry.dom.util.PrintOutCollector;
024:
025: /**
026: * A node within the DOM.
027: */
028: public abstract class Node {
029: private Node _container;
030:
031: private List<Node> _children;
032:
033: /**
034: * Creates a new node, setting its container to the provided value. Container may also be null,
035: * but that is only used for Document nodes (the topmost node of a DOM).
036: *
037: * @param container
038: */
039: protected Node(Node container) {
040: _container = container;
041: }
042:
043: public Node getContainer() {
044: return _container;
045: }
046:
047: /** Returns the node as an {@link Element}, if it is an element. Returns null otherwise. */
048: Element asElement() {
049: return null;
050: }
051:
052: void addChild(Node child) {
053: if (_children == null)
054: _children = newList();
055:
056: _children.add(child);
057: }
058:
059: void insertChildAt(int index, Node child) {
060: if (_children == null)
061: _children = newList();
062:
063: _children.add(index, child);
064: }
065:
066: boolean hasChildren() {
067: return _children != null && !_children.isEmpty();
068: }
069:
070: void writeChildMarkup(PrintWriter writer) {
071: if (_children == null)
072: return;
073:
074: for (Node child : _children)
075: child.toMarkup(writer);
076: }
077:
078: /**
079: * @return the concatenation of the String representations {@link #toString()} of its children.
080: */
081: public String getChildText() {
082: PrintOutCollector collector = new PrintOutCollector();
083: writeChildMarkup(collector.getPrintWriter());
084: return collector.getPrintOut();
085: }
086:
087: /**
088: * Invokes {@link #toMarkup(PrintWriter)}, collecting output in a string, which is returned.
089: */
090: @Override
091: public String toString() {
092: PrintOutCollector collector = new PrintOutCollector();
093: toMarkup(collector.getPrintWriter());
094: return collector.getPrintOut();
095: }
096:
097: @SuppressWarnings("unchecked")
098: public List<Node> getChildren() {
099: return _children == null ? Collections.EMPTY_LIST : _children;
100: }
101:
102: /** Writes the markup for this node to the writer. */
103: public abstract void toMarkup(PrintWriter writer);
104: }
|