1 package com.icl.saxon.output; 2 import com.icl.saxon.*; 3 import com.icl.saxon.om.NamePool; 5 import org.xml.sax.*; 6 import org.xml.sax.helpers.AttributeListImpl ; 7 import java.io.*; 8 import java.util.*; 9 import javax.xml.transform.TransformerException ; 10 11 18 19 public class DocumentHandlerProxy extends Emitter 20 { 21 protected DocumentHandler handler; 22 protected AttributeListImpl outputAtts = new AttributeListImpl (); 24 private int depth = 0; 25 26 29 30 public void setUnderlyingDocumentHandler(DocumentHandler handler) { 31 this.handler = handler; 32 } 33 34 37 38 public void setDocumentLocator(Locator locator) { 39 if (handler!=null) 40 handler.setDocumentLocator(locator); 41 } 42 43 46 47 public void startDocument() throws TransformerException { 48 if (handler==null) { 49 throw new TransformerException ("DocumentHandlerProxy.startDocument(): no underlying handler provided"); 50 } 51 try { 52 handler.startDocument(); 53 } catch (SAXException err) { 54 throw new TransformerException (err); 55 } 56 57 depth = 0; 58 } 59 60 63 64 public void endDocument() throws TransformerException { 65 try { 66 handler.endDocument(); 67 } catch (SAXException err) { 68 throw new TransformerException (err); 69 } 70 } 71 72 75 76 public void startElement(int nameCode, Attributes attributes, 77 int[] namespaces, int nscount) throws TransformerException { 78 depth++; 79 outputAtts.clear(); 80 for (int a=0; a<attributes.getLength(); a++) { 81 outputAtts.addAttribute( 82 attributes.getQName(a), 83 attributes.getType(a), 84 attributes.getValue(a) ); 85 } 86 if (depth>0) { 88 for (int n=0; n<nscount; n++) { 89 String prefix = namePool.getPrefixFromNamespaceCode(namespaces[n]); 90 String uri = namePool.getURIFromNamespaceCode(namespaces[n]); 91 if (prefix.equals("")) { 92 outputAtts.addAttribute("xmlns", "NMTOKEN", uri); 93 } else { 94 outputAtts.addAttribute("xmlns:" + prefix, "NMTOKEN", uri); 95 } 96 } 97 try { 98 handler.startElement(namePool.getDisplayName(nameCode), outputAtts); 99 } catch (SAXException err) { 100 throw new TransformerException (err); 101 } 102 } 103 } 104 105 108 109 public void endElement(int nameCode) throws TransformerException { 110 if (depth>0) { 111 try { 112 handler.endElement(namePool.getDisplayName(nameCode)); 113 } catch (SAXException err) { 114 throw new TransformerException (err); 115 } 116 } 117 depth--; 118 if (depth<=0) { 120 depth = Integer.MIN_VALUE; } 122 } 123 124 127 128 public void characters(char[] chars, int start, int len) throws TransformerException { 129 if (depth>0) { 130 try { 131 handler.characters(chars, start, len); 132 } catch (SAXException err) { 133 throw new TransformerException (err); 134 } 135 } 136 } 137 138 141 142 148 151 152 public void processingInstruction(String target, String data) throws TransformerException { 153 try { 154 handler.processingInstruction(target, data); 155 } catch (SAXException err) { 156 throw new TransformerException (err); 157 } 158 } 159 160 163 164 public void comment (char ch[], int start, int length) {} 165 166 167 172 173 175 } 176 177 | Popular Tags |