1 package com.icl.saxon.output; 2 import com.icl.saxon.*; 3 import com.icl.saxon.om.NamePool; 4 import com.icl.saxon.om.Namespace; 5 import org.xml.sax.*; 6 import org.xml.sax.helpers.AttributesImpl ; 7 import org.xml.sax.ext.LexicalHandler ; 8 import java.io.*; 9 import java.util.*; 10 import javax.xml.transform.Result ; 11 import javax.xml.transform.TransformerException ; 12 13 23 24 public class ContentHandlerProxy extends Emitter implements Locator 25 { 26 protected ContentHandler handler; 27 protected LexicalHandler lexicalHandler; 28 protected Locator locator = this; 30 private int depth = 0; 31 protected boolean requireWellFormed = true; 32 33 36 37 public void setUnderlyingContentHandler(ContentHandler handler) { 38 this.handler = handler; 39 if (handler instanceof LexicalHandler ) { 40 this.lexicalHandler = (LexicalHandler )handler; 41 } 42 } 43 44 48 49 public void setLexicalHandler(LexicalHandler handler) { 50 this.lexicalHandler = handler; 51 } 52 53 57 58 public void setRequireWellFormed(boolean wellFormed) { 59 requireWellFormed = wellFormed; 60 } 61 62 65 66 public void setDocumentLocator(Locator locator) { 67 this.locator = locator; 68 } 69 70 73 74 public void startDocument() throws TransformerException { 75 if (handler==null) { 77 throw new TransformerException ("ContentHandlerProxy.startDocument(): no underlying handler provided"); 78 } 79 try { 80 handler.setDocumentLocator(locator); 81 handler.startDocument(); 82 } catch (SAXException err) { 83 throw new TransformerException (err); 84 } 85 depth = 0; 86 } 87 88 91 92 public void endDocument() throws TransformerException { 93 try { 94 handler.endDocument(); 95 } catch (SAXException err) { 96 throw new TransformerException (err); 97 } 98 } 99 100 101 104 105 public void startElement(int nameCode, Attributes atts, 106 int[] namespaces, int nscount) throws TransformerException { 107 depth++; 108 try { 109 if (depth<=0 && requireWellFormed) { 110 notifyNotWellFormed(); 111 } 112 if (depth>0 || !requireWellFormed) { 113 for (int n=0; n<nscount; n++) { 114 String prefix = namePool.getPrefixFromNamespaceCode(namespaces[n]); 115 String uri = namePool.getURIFromNamespaceCode(namespaces[n]); 116 handler.startPrefixMapping(prefix, uri); 117 } 118 119 handler.startElement( 120 namePool.getURI(nameCode), 121 namePool.getLocalName(nameCode), 122 namePool.getDisplayName(nameCode), 123 atts); 124 } 125 } catch (SAXException err) { 126 throw new TransformerException (err); 127 } 128 } 129 130 133 134 public void endElement(int nameCode) throws TransformerException { 135 if (depth>0) { 136 try { 137 handler.endElement( 138 namePool.getURI(nameCode), 139 namePool.getLocalName(nameCode), 140 namePool.getDisplayName(nameCode)); 141 } catch (SAXException err) { 142 throw new TransformerException (err); 143 } 144 } 145 depth--; 146 if (requireWellFormed && depth<=0) { 149 depth = Integer.MIN_VALUE; } 151 152 } 153 154 157 158 public void characters(char[] chars, int start, int len) throws TransformerException { 159 try { 160 if (depth<=0 && requireWellFormed) { 161 boolean isWhite = new String (chars, start, len).trim().length()==0; 162 if (isWhite) { 163 } else { 165 notifyNotWellFormed(); 166 if (!requireWellFormed) { 167 handler.characters(chars, start, len); 168 } 169 } 170 } else { 171 handler.characters(chars, start, len); 172 } 173 } catch (SAXException err) { 174 throw new TransformerException (err); 175 } 176 } 177 178 185 186 protected void notifyNotWellFormed() throws SAXException { 187 try { 188 handler.processingInstruction( 189 "saxon:warning", "Output suppressed because it is not well-formed"); 190 } catch (SAXException err) { 191 if (err.getMessage().equals("continue")) { 192 requireWellFormed = false; 193 } else { 194 throw err; 195 } 196 } 197 } 198 199 200 203 204 public void processingInstruction(String target, String data) 205 throws TransformerException { 206 try { 207 handler.processingInstruction(target, data); 208 } catch (SAXException err) { 209 throw new TransformerException (err); 210 } 211 } 212 213 217 218 public void comment (char ch[], int start, int length) 219 throws TransformerException { 220 try { 221 if (lexicalHandler != null) { 222 lexicalHandler.comment(ch, start, length); 223 } 224 } catch (SAXException err) { 225 throw new TransformerException (err); 226 } 227 } 228 229 230 236 237 public void setEscaping(boolean escaping) { 238 try { 239 handler.processingInstruction( 240 (escaping ? Result.PI_ENABLE_OUTPUT_ESCAPING : PI_DISABLE_OUTPUT_ESCAPING), 241 ""); 242 } catch (SAXException err) {} 243 } 244 245 249 public String getPublicId() { 250 return null; 251 } 252 253 public int getLineNumber() { 254 return -1; 255 } 256 257 public int getColumnNumber() { 258 return -1; 259 } 260 } 261 262 | Popular Tags |