1 16 package org.apache.cocoon.xml; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.io.Writer ; 21 import java.io.IOException ; 22 23 import org.xml.sax.ContentHandler ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.Attributes ; 26 27 37 public class ParamSaxBuffer extends SaxBuffer { 38 39 45 private char[] previous_ch = null; 46 47 50 public ParamSaxBuffer() { 51 } 52 53 56 public ParamSaxBuffer(SaxBuffer saxBuffer) { 57 super(saxBuffer); 58 } 59 60 64 public void characters(char ch[], int start, int length) throws SAXException { 65 66 if (previous_ch != null) { 67 char[] buf = new char[length + previous_ch.length]; 69 System.arraycopy(previous_ch, 0, buf, 0, previous_ch.length); 70 System.arraycopy(ch, start, buf, previous_ch.length, length); 71 ch = buf; 72 start = 0; 73 length += previous_ch.length; 74 previous_ch = null; 75 } 76 77 final int end = start + length; 78 for (int i = start; i < end; i++) { 79 if (ch[i] == '{') { 80 if (i > start) { 82 addBit(new Characters(ch, start, i - start)); 83 } 84 85 StringBuffer name = new StringBuffer (); 87 int j = i + 1; 88 for (; j < end; j++) { 89 if (ch[j] == '}') { 90 break; 91 } 92 name.append(ch[j]); 93 } 94 if (j == end) { 95 previous_ch = new char[end - i]; 99 System.arraycopy(ch, i, previous_ch, 0, end - i); 100 break; 101 } 102 addBit(new Parameter(name.toString())); 103 104 i = j; 106 start = j + 1; 107 continue; 108 } 109 } 110 111 if (start < end) { 113 addBit(new Characters(ch, start, end - start)); 114 } 115 } 116 117 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 118 flushChars(); 119 super.endElement(namespaceURI, localName, qName); 120 } 121 122 public void ignorableWhitespace(char ch[], int start, int length) throws SAXException { 123 flushChars(); 124 super.ignorableWhitespace(ch, start, length); 125 } 126 127 public void processingInstruction(String target, String data) throws SAXException { 128 flushChars(); 129 super.processingInstruction(target, data); 130 } 131 132 public void startDocument() throws SAXException { 133 flushChars(); 134 super.startDocument(); 135 } 136 137 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 138 flushChars(); 139 super.startElement(namespaceURI, localName, qName, atts); 140 } 141 142 public void endDocument() throws SAXException { 143 flushChars(); 144 super.endDocument(); 145 } 146 147 public void comment(char ch[], int start, int length) throws SAXException { 148 flushChars(); 149 super.comment(ch, start, length); 150 } 151 152 public void endDTD() throws SAXException { 153 flushChars(); 154 super.endDTD(); 155 } 156 157 public void startDTD(String name, String publicId, String systemId) throws SAXException { 158 flushChars(); 159 super.startDTD(name, publicId, systemId); 160 } 161 162 private void flushChars() { 163 if (previous_ch != null) { 165 addBit(new Characters(previous_ch, 0, previous_ch.length)); 166 previous_ch = null; 167 } 168 } 169 170 173 public void toSAX(ContentHandler contentHandler, Map parameters) throws SAXException { 174 for (Iterator i = bits(); i.hasNext();) { 175 SaxBit saxbit = (SaxBit)i.next(); 176 if (saxbit instanceof Parameter) { 177 ((Parameter)saxbit).send(contentHandler, parameters); 178 } else { 179 saxbit.send(contentHandler); 180 } 181 } 182 } 183 184 185 final static class Parameter implements SaxBit { 186 private final String name; 187 188 public Parameter(String name) { 189 this.name = name; 190 } 191 192 public void send(ContentHandler contentHandler) { 193 } 194 195 public void send(ContentHandler contentHandler, Map parameters) throws SAXException { 196 SaxBuffer value = (SaxBuffer)parameters.get(name); 197 if (value != null) { 198 value.toSAX(contentHandler); 199 } 200 } 201 202 public void dump(Writer writer) throws IOException { 203 writer.write("[Parameter] name=" + name); 204 } 205 } 206 } 207 | Popular Tags |