1 20 21 27 28 package com.sun.org.apache.xalan.internal.xsltc.trax; 29 30 import java.util.Vector ; 31 32 import javax.xml.stream.Location; 33 import javax.xml.stream.XMLReporter; 34 import javax.xml.stream.XMLStreamException; 35 36 import org.xml.sax.Attributes ; 37 import org.xml.sax.Locator ; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.SAXParseException ; 40 import org.xml.sax.ext.LexicalHandler ; 41 import org.xml.sax.helpers.DefaultHandler ; 42 43 44 public abstract class SAX2StAXBaseWriter extends DefaultHandler 45 implements 46 LexicalHandler { 47 48 49 protected boolean isCDATA; 50 51 protected StringBuffer CDATABuffer; 52 53 protected Vector namespaces; 54 55 protected Locator docLocator; 56 57 protected XMLReporter reporter; 58 59 public SAX2StAXBaseWriter() { 60 } 61 62 public SAX2StAXBaseWriter(XMLReporter reporter) { 63 this.reporter = reporter; 64 } 65 66 public void setXMLReporter(XMLReporter reporter) { 67 this.reporter = reporter; 68 } 69 70 public void setDocumentLocator(Locator locator) { 71 this.docLocator = locator; 72 } 73 74 75 public Location getCurrentLocation() { 76 if (docLocator != null) { 77 return new SAXLocation(docLocator); 78 } else { 79 return null; 80 } 81 82 } 83 84 public void error(SAXParseException e) throws SAXException { 85 reportException("ERROR", e); 86 } 87 88 public void fatalError(SAXParseException e) throws SAXException { 89 reportException("FATAL", e); 90 } 91 92 public void warning(SAXParseException e) throws SAXException { 93 reportException("WARNING", e); 94 } 95 96 public void startDocument() throws SAXException { 97 namespaces = new Vector (2); 98 } 99 100 public void endDocument() throws SAXException { 101 namespaces = null; 102 } 103 104 public void startElement(String uri, String localName, String qName, 105 Attributes attributes) throws SAXException { 106 namespaces = null; 107 } 108 109 public void endElement(String uri, String localName, String qName) 110 throws SAXException { 111 namespaces = null; 112 } 113 114 public void startPrefixMapping(String prefix, String uri) 115 throws SAXException { 116 117 if (prefix == null) { 118 prefix = ""; 119 } else if (prefix.equals("xml")) { 120 return; 121 } 122 123 if (namespaces == null) { 124 namespaces = new Vector (2); 125 } 126 namespaces.addElement(prefix); 127 namespaces.addElement(uri); 128 } 129 130 131 public void endPrefixMapping(String prefix) throws SAXException { 132 } 133 134 public void startCDATA() throws SAXException { 135 isCDATA = true; 136 if (CDATABuffer == null) { 137 CDATABuffer = new StringBuffer (); 138 } else { 139 CDATABuffer.setLength(0); 140 } 141 } 142 143 public void characters(char[] ch, int start, int length) 144 throws SAXException { 145 if (isCDATA) { 146 CDATABuffer.append(ch, start, length); 147 } 148 } 149 150 public void endCDATA() throws SAXException { 151 isCDATA = false; 152 CDATABuffer.setLength(0); 153 } 154 155 public void comment(char[] ch, int start, int length) throws SAXException { 156 } 157 158 public void endDTD() throws SAXException { 159 } 160 161 public void endEntity(String name) throws SAXException { 162 } 163 164 public void startDTD(String name, String publicId, String systemId) 165 throws SAXException { 166 } 167 168 public void startEntity(String name) throws SAXException { 169 } 170 171 175 protected void reportException(String type, SAXException e) 176 throws SAXException { 177 178 if (reporter != null) { 179 try { 180 reporter.report(e.getMessage(), type, e, getCurrentLocation()); 181 } catch (XMLStreamException e1) { 182 throw new SAXException (e1); 183 } 184 } 185 } 186 187 196 public static final void parseQName(String qName, String [] results) { 197 198 String prefix, local; 199 int idx = qName.indexOf(':'); 200 if (idx >= 0) { 201 prefix = qName.substring(0, idx); 202 local = qName.substring(idx + 1); 203 } else { 204 prefix = ""; 205 local = qName; 206 } 207 results[0] = prefix; 208 results[1] = local; 209 } 210 211 218 private static final class SAXLocation implements Location { 219 220 private int lineNumber; 221 private int columnNumber; 222 private String publicId; 223 private String systemId; 224 private SAXLocation(Locator locator) { 225 lineNumber = locator.getLineNumber(); 226 columnNumber = locator.getColumnNumber(); 227 publicId = locator.getPublicId(); 228 systemId = locator.getSystemId(); 229 } 230 231 public int getLineNumber() { 232 return lineNumber; 233 } 234 235 public int getColumnNumber() { 236 return columnNumber; 237 } 238 239 public int getCharacterOffset() { 240 return -1; 241 } 242 243 public String getPublicId() { 244 return publicId; 245 } 246 247 public String getSystemId() { 248 return systemId; 249 } 250 } 251 } 252 | Popular Tags |