001: // Copyright 2006, 2007 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.internal.services;
016:
017: import java.io.PrintWriter;
018:
019: import org.apache.tapestry.Link;
020: import org.apache.tapestry.MarkupWriter;
021: import org.apache.tapestry.dom.DefaultMarkupModel;
022: import org.apache.tapestry.dom.Document;
023: import org.apache.tapestry.dom.Element;
024: import org.apache.tapestry.dom.MarkupModel;
025: import org.apache.tapestry.dom.Text;
026: import org.apache.tapestry.ioc.internal.util.InternalUtils;
027:
028: public class MarkupWriterImpl implements MarkupWriter {
029: private final Document _document;
030:
031: private Element _current;
032:
033: private Text _currentText;
034:
035: private final ComponentInvocationMap _invocationMap;
036:
037: public MarkupWriterImpl() {
038: this (new DefaultMarkupModel(), new NoOpComponentInvocationMap());
039: }
040:
041: public MarkupWriterImpl(MarkupModel model,
042: ComponentInvocationMap invocationMap) {
043: _document = new Document(model);
044: _invocationMap = invocationMap;
045: }
046:
047: public void toMarkup(PrintWriter writer) {
048: _document.toMarkup(writer);
049: }
050:
051: @Override
052: public String toString() {
053: return _document.toString();
054: }
055:
056: public Document getDocument() {
057: return _document;
058: }
059:
060: public Element getElement() {
061: return _current;
062: }
063:
064: public void write(String text) {
065: // Whitespace before and after the root element is quietly ignored.
066: if (_current == null && InternalUtils.isBlank(text))
067: return;
068:
069: ensureCurrentElement();
070:
071: if (text == null)
072: return;
073:
074: if (_currentText == null) {
075: _currentText = _current.text(text);
076: return;
077: }
078:
079: _currentText.write(text);
080: }
081:
082: public void writef(String format, Object... args) {
083: // A bit of a cheat:
084:
085: write("");
086: _currentText.writef(format, args);
087: }
088:
089: public void attributes(Object... namesAndValues) {
090: ensureCurrentElement();
091:
092: int i = 0;
093:
094: while (i < namesAndValues.length) {
095: // name should never be null.
096:
097: String name = namesAndValues[i++].toString();
098: Object value = namesAndValues[i++];
099:
100: if (value == null)
101: continue;
102:
103: if (value instanceof Link)
104: _invocationMap.store(_current, (Link) value);
105:
106: _current.attribute(name, value.toString());
107: }
108:
109: }
110:
111: private void ensureCurrentElement() {
112: if (_current == null)
113: throw new IllegalStateException(ServicesMessages
114: .markupWriterNoCurrentElement());
115: }
116:
117: public Element element(String name, Object... namesAndValues) {
118: if (_current == null)
119: _current = _document.newRootElement(name);
120: else
121: _current = _current.element(name);
122:
123: attributes(namesAndValues);
124:
125: _currentText = null;
126:
127: return _current;
128: }
129:
130: public void writeRaw(String text) {
131: ensureCurrentElement();
132:
133: _currentText = null;
134:
135: _current.raw(text);
136: }
137:
138: public Element end() {
139: ensureCurrentElement();
140:
141: _current = _current.getParent();
142:
143: _currentText = null;
144:
145: return _current;
146: }
147:
148: public void comment(String text) {
149: ensureCurrentElement();
150:
151: _current.comment(text);
152:
153: _currentText = null;
154: }
155:
156: }
|