1 package com.icl.saxon; 2 import com.icl.saxon.om.NamePool; 3 import com.icl.saxon.om.Name; 4 import com.icl.saxon.output.Emitter; 5 6 import org.xml.sax.*; 7 import org.xml.sax.ext.LexicalHandler ; 8 9 import javax.xml.transform.TransformerException ; 10 11 import java.net.URL ; 12 13 20 21 public class ContentEmitter implements ContentHandler, LexicalHandler , DTDHandler 22 { 23 private NamePool pool; 24 private Emitter emitter; 25 private boolean inDTD = false; private Locator locator; 27 28 30 private char[] buffer = new char[4096]; 31 private int used = 0; 32 33 35 private int[] namespaces = new int[50]; 36 private int namespacesUsed = 0; 37 38 41 42 public ContentEmitter() { 43 } 44 45 public void setEmitter(Emitter e) { 46 emitter = e; 47 } 48 49 public void setNamePool(NamePool namePool) { 50 pool = namePool; 51 } 52 53 56 57 public void startDocument () throws SAXException { 58 try { 60 used = 0; 61 namespacesUsed = 0; 62 emitter.setDocumentLocator(locator); 63 emitter.startDocument(); 64 } catch (TransformerException err) { 65 throw new SAXException(err); 66 } 67 } 68 69 72 73 public void endDocument () throws SAXException { 74 try { 75 flush(); 76 emitter.endDocument(); 77 } catch (TransformerException err) { 78 throw new SAXException(err); 79 } 80 } 81 82 85 86 public void setDocumentLocator (Locator locator) { 87 this.locator = locator; 88 } 90 91 94 95 public void startPrefixMapping(String prefix, String uri) { 96 if (namespacesUsed >= namespaces.length) { 97 int[] n2 = new int[namespacesUsed * 2]; 98 System.arraycopy(namespaces, 0, n2, 0, namespacesUsed); 99 namespaces = n2; 100 } 101 namespaces[namespacesUsed++] = pool.allocateNamespaceCode(prefix, uri); 102 } 103 104 107 108 public void endPrefixMapping(String prefix) {} 109 110 113 114 public void startElement (String uri, String localname, String rawname, Attributes atts) 115 throws SAXException 116 { 117 try { 119 flush(); 120 int nameCode = getNameCode(uri, localname, rawname); 121 emitter.startElement(nameCode, atts, namespaces, namespacesUsed); 122 namespacesUsed = 0; 123 } catch (TransformerException err) { 124 throw new SAXException(err); 125 } 126 } 127 128 private int getNameCode(String uri, String localname, String rawname) { 129 String prefix = Name.getPrefix(rawname); 130 return pool.allocate(prefix, uri, localname); 131 } 132 133 136 137 public void endElement (String uri, String localname, String rawname) throws SAXException { 138 try { 139 flush(); 140 String prefix = Name.getPrefix(rawname); 142 int nameCode = pool.allocate(prefix, uri, localname); 143 emitter.endElement(nameCode); 144 } catch (TransformerException err) { 145 throw new SAXException(err); 146 } 147 } 148 149 152 153 public void characters (char ch[], int start, int length) { 154 155 157 while (used + length > buffer.length) { 158 char[] newbuffer = new char[buffer.length*2]; 159 System.arraycopy(buffer, 0, newbuffer, 0, used); 160 buffer = newbuffer; 161 } 162 System.arraycopy(ch, start, buffer, used, length); 163 used += length; 164 } 165 166 169 170 public void ignorableWhitespace (char ch[], int start, int length) { 171 characters(ch, start, length); 172 } 173 174 177 178 public void processingInstruction (String name, String remainder) throws SAXException 179 { 180 try { 181 flush(); 182 if (!inDTD) { 183 if (name==null) { 184 comment(remainder.toCharArray(), 0, remainder.length()); 186 } else { 187 if (!Name.isNCName(name)) { 189 throw new SAXException("Invalid processing instruction name (" + name + ")"); 190 } 191 emitter.processingInstruction(name, remainder); 192 } 193 } 194 } catch (TransformerException err) { 195 throw new SAXException(err); 196 } 197 } 198 199 202 203 public void comment (char ch[], int start, int length) throws SAXException { 204 try { 205 flush(); 206 if (!inDTD) { 207 emitter.comment(ch, start, length); 208 } 209 } catch (TransformerException err) { 210 throw new SAXException(err); 211 } 212 } 213 214 217 218 private void flush() throws TransformerException { 219 if (used > 0) { 220 emitter.characters(buffer, 0, used); 221 used = 0; 222 } 223 } 224 225 public void skippedEntity(String name) {} 226 227 229 233 234 public void startDTD (String name, String publicId, String systemId) { 235 inDTD = true; 236 } 237 238 242 243 public void endDTD () { 244 inDTD = false; 245 } 246 247 public void startEntity (String name) {}; 248 249 public void endEntity (String name) {}; 250 251 public void startCDATA () {}; 252 253 public void endCDATA () {}; 254 255 259 260 public void notationDecl( String name, 261 String publicId, 262 String systemId) 263 {} 264 265 266 public void unparsedEntityDecl( String name, 267 String publicId, 268 String systemId, 269 String notationName) throws SAXException 270 { 271 273 276 String uri = systemId; 277 if (locator!=null) { 278 try { 279 String baseURI = locator.getSystemId(); 280 URL absoluteURI = new URL (new URL (baseURI), systemId); 281 uri = absoluteURI.toString(); 282 } catch (Exception err) {} 283 } 284 try { 285 emitter.setUnparsedEntity(name, uri); 286 } catch (TransformerException err) { 287 throw new SAXException(err); 288 } 289 } 290 291 292 } 294 | Popular Tags |