1 16 package org.apache.cocoon.portal.serialization; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.util.HashMap ; 21 import java.util.LinkedList ; 22 import java.util.Map ; 23 24 import org.apache.cocoon.serialization.HTMLSerializer; 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.SAXException ; 27 28 29 41 public class IncludingHTMLSerializer 42 extends HTMLSerializer { 43 44 public static final ThreadLocal portlets = new ThreadLocal (); 45 46 public static final String NAMESPACE = "http://apache.org/cocoon/portal/include"; 47 48 protected LinkedList orderedPortletList = new LinkedList (); 49 50 protected static final char token = '~'; 51 52 protected static final char[] tokens = new char[] {token, token}; 53 54 57 public void recycle() { 58 super.recycle(); 59 Map map = (Map )portlets.get(); 60 if ( map != null ) { 61 map.clear(); 62 } 63 this.orderedPortletList.clear(); 64 } 65 66 69 public static void addPortlet(String name, String content) { 70 Map map = (Map ) portlets.get(); 71 if ( map == null ) { 72 map = new HashMap (); 73 portlets.set(map); 74 } 75 map.put(name, content); 76 } 77 78 81 public void endElement(String uri, String loc, String raw) 82 throws SAXException { 83 if (!NAMESPACE.equals(uri)) { 84 super.endElement(uri, loc, raw); 85 } 86 } 87 88 91 public void startElement(String uri, String loc, String raw, Attributes a) 92 throws SAXException { 93 if (!NAMESPACE.equals(uri)) { 94 super.startElement(uri, loc, raw, a); 95 } else { 96 final String portletId = a.getValue("portlet"); 97 98 String value = null; 99 Map map = (Map )portlets.get(); 100 if ( map != null ) { 101 value = (String )map.get(portletId); 102 } 103 if ( value != null ) { 104 this.orderedPortletList.addFirst(value); 105 this.characters(tokens, 0, tokens.length); 106 } 107 } 108 } 109 110 113 public void setOutputStream(OutputStream stream) throws IOException { 114 super.setOutputStream(new ReplacingOutputStream(stream, this.orderedPortletList)); 115 } 116 117 } 118 class ReplacingOutputStream extends OutputStream { 119 120 121 protected final OutputStream stream; 122 123 protected boolean inKey; 124 125 protected final LinkedList orderedValues; 126 127 130 public ReplacingOutputStream(OutputStream stream, LinkedList values) { 131 this.stream = stream; 132 this.orderedValues = values; 133 this.inKey = false; 134 } 135 136 139 public void close() throws IOException { 140 this.stream.close(); 141 } 142 143 146 public void flush() throws IOException { 147 this.stream.flush(); 148 } 149 150 153 public void write(byte[] b, int off, int len) throws IOException { 154 if ( len == 0 ) { 155 return; 156 } 157 if ( this.inKey ) { 158 if ( b[off] == IncludingHTMLSerializer.token ) { 159 this.writeNextValue(); 160 off++; 161 len--; 162 } else { 163 this.write(IncludingHTMLSerializer.token); 164 } 165 this.inKey = false; 166 } 167 boolean end = false; 169 do { 170 int s = off; 171 int e = off+len; 172 while (s < e && b[s] != IncludingHTMLSerializer.token) { 173 s++; 174 } 175 if ( s == e ) { 176 this.stream.write(b, off, len); 177 end = true; 178 } else if ( s == e-1 ) { 179 this.stream.write(b, off, len-1); 180 this.inKey = true; 181 end = true; 182 } else { 183 if ( b[s+1] == IncludingHTMLSerializer.token) { 184 final int l = s-off; 185 this.stream.write(b, off, l); 186 off += (l+2); 187 len -= (l+2); 188 this.writeNextValue(); 189 190 } else { 191 final int l = s-off+2; 192 this.stream.write(b, off, l); 193 off += l; 194 len -= l; 195 } 196 end = (len == 0); 197 } 198 } while (!end); 199 } 200 201 204 public void write(byte[] b) throws IOException { 205 this.write(b, 0, b.length); 206 } 207 208 211 public void write(int b) throws IOException { 212 if ( b == IncludingHTMLSerializer.token ) { 213 if ( this.inKey ) { 214 this.writeNextValue(); 215 } 216 this.inKey = !this.inKey; 217 } else { 218 if ( this.inKey ) { 219 this.inKey = false; 220 this.stream.write(IncludingHTMLSerializer.token); 221 } 222 this.stream.write(b); 223 } 224 } 225 226 229 protected void writeNextValue() throws IOException { 230 final String value = (String )this.orderedValues.removeLast(); 231 if ( value != null ) { 232 this.stream.write(value.getBytes(), 0, value.length()); 233 } 234 } 235 } 236 237 | Popular Tags |