1 16 package org.apache.cocoon.serialization; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.util.Stack ; 21 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.avalon.framework.service.Serviceable; 25 26 import org.apache.cocoon.components.elementprocessor.CannotCreateElementProcessorException; 27 import org.apache.cocoon.components.elementprocessor.ElementProcessor; 28 import org.apache.cocoon.components.elementprocessor.ElementProcessorFactory; 29 import org.apache.cocoon.components.elementprocessor.types.Attribute; 30 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.Locator ; 33 import org.xml.sax.SAXException ; 34 35 52 public abstract class ElementProcessorSerializer 53 extends AbstractLogEnabled implements Serializer, Serviceable 54 { 55 private static final boolean _should_set_content_length = false; 56 private OutputStream _output_stream; 57 private Stack _open_elements; 58 private Locator _locator; 59 60 protected ServiceManager manager = null; 61 62 65 66 public ElementProcessorSerializer() { 67 _output_stream = null; 68 _open_elements = new Stack (); 69 _locator = null; 70 } 71 72 public void service(ServiceManager manager) { 73 this.manager = manager; 74 } 75 76 81 82 protected abstract ElementProcessorFactory getElementProcessorFactory(); 83 84 92 93 protected abstract void doPreInitialization(ElementProcessor processor) 94 throws SAXException ; 95 96 99 100 protected OutputStream getOutputStream() { 101 return _output_stream; 102 } 103 104 112 113 protected SAXException SAXExceptionFactory(final String message, 114 final Exception e) { 115 StringBuffer message_buffer = new StringBuffer (); 116 117 message_buffer.append((message == null) ? "" : message); 118 if (_locator != null) { 119 message_buffer.append("; System id: \""); 120 message_buffer.append(_locator.getSystemId()); 121 message_buffer.append("\"; public id: \""); 122 message_buffer.append(_locator.getPublicId()); 123 message_buffer.append("\"; line number: "); 124 message_buffer.append(_locator.getLineNumber()); 125 message_buffer.append("; column number: "); 126 message_buffer.append(_locator.getColumnNumber()); 127 } 128 SAXException rval = null; 129 130 if (e != null) { 131 rval = new SAXException (message_buffer.toString(), e); 132 } else { 133 rval = new SAXException (message_buffer.toString()); 134 } 135 return rval; 136 } 137 138 145 146 protected SAXException SAXExceptionFactory(final String message) { 147 return SAXExceptionFactory(message, null); 148 } 149 150 private ElementProcessor getCurrentElementProcessor() { 151 return _open_elements.empty() ? null 152 : (ElementProcessor) _open_elements.peek(); 153 } 154 155 private char [] cleanupArray(final char [] array, final int start, 156 final int length) { 157 char[] output = new char[length]; 158 159 System.arraycopy(array, start, output, 0, length); 160 return output; 161 } 162 163 164 165 172 173 public void setOutputStream(final OutputStream out) { 174 _output_stream = out; 175 } 176 177 182 183 public boolean shouldSetContentLength() { 184 return _should_set_content_length; 185 } 186 187 188 189 190 198 199 public void comment(final char [] ignored_ch, final int ignored_start, 200 final int ignored_length) { 201 } 202 203 206 207 public void endCDATA() { 208 } 209 210 213 214 public void endDTD() { 215 } 216 217 222 223 public void endEntity(final String ignored_name) { 224 } 225 226 229 230 public void startCDATA() { 231 } 232 233 241 242 public void startDTD(final String ignored_name, 243 final String ignored_publicId, final String ignored_systemId) { 244 } 245 246 252 253 public void startEntity(final String ignored_name) { 254 } 255 256 257 258 259 269 270 public void characters(final char [] ch, final int start, 271 final int length) throws SAXException { 272 try { 273 getCurrentElementProcessor().acceptCharacters(cleanupArray(ch, 274 start, length)); 275 } catch (Exception e) { 276 throw SAXExceptionFactory("could not process characters event", e); 277 } 278 } 279 280 289 290 public void endElement(final String ignored_namespaceURI, 291 final String ignored_localName, final String ignored_qName) 292 throws SAXException { 293 try { 294 getCurrentElementProcessor().endProcessing(); 295 _open_elements.pop(); 296 } catch (Exception e) { 297 throw SAXExceptionFactory("could not process endElement event", 298 e); 299 } 300 } 301 302 307 308 public void endPrefixMapping(final String ignored_prefix) { 309 } 310 311 322 323 public void ignorableWhitespace(final char [] ch, final int start, 324 final int length) throws SAXException { 325 try { 326 getCurrentElementProcessor() 327 .acceptWhitespaceCharacters(cleanupArray(ch, start, length)); 328 } catch (Exception e) { 329 throw SAXExceptionFactory( 330 "could not process ignorableWhitespace event", e); 331 } 332 } 333 334 341 342 public void processingInstruction(final String ignored_target, 343 final String ignored_data) { 344 } 345 346 352 353 public void setDocumentLocator(final Locator locator) { 354 _locator = locator; 355 } 356 357 362 363 public void skippedEntity(final String ignored_name) { 364 } 365 366 369 370 public void startDocument() { 371 } 374 375 386 387 public void startElement(final String namespaceURI, 388 final String localName, final String qName, final Attributes atts) 389 throws SAXException { 390 String name = ""; 391 392 if ((localName != null) && (localName.length() != 0)) { 393 name = localName; 394 } else if ((qName != null) && (qName.length() != 0)) { 395 name = qName; 396 } 397 ElementProcessor processor; 398 399 try { 400 processor = 401 getElementProcessorFactory().createElementProcessor(name); 402 } catch (CannotCreateElementProcessorException e) { 403 throw SAXExceptionFactory("could not process startElement event", 404 e); 405 } 406 doPreInitialization(processor); 407 Attribute[] attributes = (atts == null) ? new Attribute[0] 408 : new Attribute[atts.getLength()]; 409 410 for (int j = 0; j < attributes.length; j++) { 411 attributes[j] = new Attribute(atts.getQName(j), atts.getValue(j)); 412 } 413 try { 414 processor.initialize(attributes, getCurrentElementProcessor()); 415 } catch (IOException e) { 416 throw SAXExceptionFactory("Exception processing startElement", e); 417 } 418 _open_elements.push(processor); 419 } 420 421 428 429 public void startPrefixMapping(final String ignored_prefix, 430 final String ignored_uri) { 431 } 432 433 } | Popular Tags |