001 package net.sf.tacos.markup; 002 003 import org.apache.tapestry.IMarkupWriter; 004 import org.apache.tapestry.NestedMarkupWriter; 005 import org.apache.tapestry.markup.Attribute; 006 007 /** 008 * Decorates an existing {@link IMarkupWriter} in order to add 009 * a few shortcut methods. 010 * 011 * @see #create(String, String...) 012 * @see #createEmpty(String, String...) 013 */ 014 public class ExtendedMarkupWriterImpl implements IExtendedMarkupWriter { 015 private IMarkupWriter writer; 016 017 public ExtendedMarkupWriterImpl(IMarkupWriter writer) { 018 this.writer = writer; 019 } 020 021 /** 022 * {@inheritDoc} 023 */ 024 public IExtendedMarkupWriter createEmpty(String tag, String... attributes) { 025 writer.beginEmpty(tag); 026 writeAttributesAndValues(attributes); 027 return this; 028 } 029 030 /** 031 * {@inheritDoc} 032 */ 033 public IExtendedMarkupWriter create(String tag, String... attributes) { 034 writer.begin(tag); 035 writeAttributesAndValues(attributes); 036 return this; 037 } 038 039 private void writeAttributesAndValues(String... attributes) { 040 for (int i=0; i<attributes.length; i+=2) { 041 writer.attribute(attributes[i], attributes[i+1]); 042 } 043 } 044 045 public void attribute(String name, int value) { 046 writer.attribute(name, value); 047 } 048 049 public void attribute(String name, boolean value) { 050 writer.attribute(name, value); 051 } 052 053 public void attribute(String name, String value) { 054 writer.attribute(name, value); 055 } 056 057 public void attributeRaw(String name, String value) { 058 writer.attributeRaw(name, value); 059 } 060 061 public void appendAttribute(String name, int value) { 062 writer.appendAttribute(name, value); 063 } 064 065 public void appendAttribute(String name, boolean value) { 066 writer.appendAttribute(name, value); 067 } 068 069 public void appendAttribute(String name, String value) { 070 writer.appendAttribute(name, value); 071 } 072 073 public void appendAttributeRaw(String name, String value) { 074 writer.appendAttributeRaw(name, value); 075 } 076 077 public boolean hasAttribute(String name) { 078 return writer.hasAttribute(name); 079 } 080 081 public Attribute getAttribute(String name) { 082 return writer.getAttribute(name); 083 } 084 085 public Attribute removeAttribute(String name) { 086 return writer.removeAttribute(name); 087 } 088 089 public void clearAttributes() { 090 writer.clearAttributes(); 091 } 092 093 public void begin(String name) { 094 writer.begin(name); 095 } 096 097 public void beginEmpty(String name) { 098 writer.beginEmpty(name); 099 } 100 101 public boolean checkError() { 102 return writer.checkError(); 103 } 104 105 public void close() { 106 writer.close(); 107 } 108 109 public void closeTag() { 110 writer.closeTag(); 111 } 112 113 public void comment(String value) { 114 writer.comment(value); 115 } 116 117 public void end() { 118 writer.end(); 119 } 120 121 public void end(String name) { 122 writer.end(name); 123 } 124 125 public void flush() { 126 writer.flush(); 127 } 128 129 public NestedMarkupWriter getNestedWriter() { 130 return writer.getNestedWriter(); 131 } 132 133 public void print(char[] data, int offset, int length) { 134 writer.print(data, offset, length); 135 } 136 137 public void print(char[] data, int offset, int length, boolean raw) { 138 writer.print(data, offset, length, raw); 139 } 140 141 public void print(char value) { 142 writer.print(value); 143 } 144 145 public void print(int value) { 146 writer.print(value); 147 } 148 149 public void print(String value) { 150 writer.print(value); 151 } 152 153 public void print(String value, boolean raw) { 154 writer.print(value, raw); 155 } 156 157 public void println() { 158 writer.println(); 159 } 160 161 public void printRaw(char[] buffer, int offset, int length) { 162 writer.printRaw(buffer, offset, length); 163 } 164 165 public void printRaw(String value) { 166 writer.printRaw(value); 167 } 168 169 public String getContentType() { 170 return writer.getContentType(); 171 } 172 } 173