1 4 5 9 10 package org.openlaszlo.compiler; 11 12 import java.io.*; 13 import java.lang.reflect.Method ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.*; 16 17 import org.xml.sax.*; 18 import org.xml.sax.Locator ; 19 import org.xml.sax.ext.DeclHandler ; 20 import org.xml.sax.ext.LexicalHandler ; 21 import org.xml.sax.helpers.LocatorImpl ; 22 import org.xml.sax.helpers.AttributesImpl ; 23 import org.xml.sax.helpers.XMLFilterImpl ; 24 25 import org.jdom.*; 26 import org.jdom.Element; 27 28 29 40 public class SourceLocatorSAXOutputter extends XMLFilterImpl { 41 public static final String SOURCEINFO_ATTRIBUTE_NAME = "_lzc_meta_sourceLocation"; 42 43 private LocatorImpl locator = new LocatorImpl (); 44 45 46 private ContentHandler contentHandler; 47 48 49 private ErrorHandler errorHandler; 50 51 private boolean writeMetaData = false; 52 53 60 public SourceLocatorSAXOutputter() { 61 } 62 63 72 public SourceLocatorSAXOutputter(ContentHandler contentHandler) { 73 setContentHandler(contentHandler); 74 } 75 76 84 public void setContentHandler(ContentHandler contentHandler) { 85 this.contentHandler = contentHandler; 86 } 87 88 96 public ContentHandler getContentHandler() { 97 return this.contentHandler; 98 } 99 100 107 public void setErrorHandler(ErrorHandler errorHandler) { 108 this.errorHandler = errorHandler; 109 } 110 111 119 public ErrorHandler getErrorHandler() { 120 return this.errorHandler; 121 } 122 123 124 128 public void setFeature(String name, boolean value) 129 throws SAXNotRecognizedException, SAXNotSupportedException { 130 } 132 133 136 public boolean getFeature(String name) 137 throws SAXNotRecognizedException, SAXNotSupportedException { 138 return false; 140 } 141 142 144 public void setProperty(String name, Object value) 145 throws SAXNotRecognizedException, SAXNotSupportedException { 146 } 148 149 155 public Object getProperty(String name) 156 throws SAXNotRecognizedException, SAXNotSupportedException { 157 return null; 159 } 160 161 public void setWriteMetaData(boolean writeMetaData) { 162 this.writeMetaData = writeMetaData; 163 } 164 165 173 public void output(Document document) throws JDOMException { 174 if (document == null) { 175 return; 176 } 177 178 documentLocator(document); 180 181 _startDocument(); 183 184 187 Iterator i = document.getContent().iterator(); 190 while (i.hasNext()) { 191 Object obj = i.next(); 192 if (obj instanceof Element) { 193 element(document.getRootElement()); 195 } 196 else if (obj instanceof ProcessingInstruction) { 197 processingInstruction((ProcessingInstruction) obj); 199 } 200 else if (obj instanceof CDATA) { 201 characters(((CDATA) obj).getText()); 203 } 204 } 205 206 _endDocument(); 208 } 209 210 216 private void documentLocator(Document document) { 217 String publicID = null; 218 String systemID = null; 219 DocType docType = document.getDocType(); 220 if (docType != null) { 221 publicID = docType.getPublicID(); 222 systemID = docType.getSystemID(); 223 } 224 225 locator.setPublicId(publicID); 226 locator.setSystemId(systemID); 227 locator.setLineNumber(1); 228 locator.setColumnNumber(1); 229 230 contentHandler.setDocumentLocator((Locator ) locator); 231 } 232 233 239 private void _startDocument() throws JDOMException { 240 try { 241 contentHandler.startDocument(); 242 } catch (SAXException se) { 247 throw new JDOMException("Exception in startDocument", se); 248 } 249 } 250 251 257 private void _endDocument() throws JDOMException { 258 try { 259 contentHandler.endDocument(); 260 } 261 catch (SAXException se) { 262 throw new JDOMException("Exception in endDocument", se); 263 } 264 } 265 266 274 private void processingInstruction(ProcessingInstruction pi) 275 throws JDOMException { 276 if (pi != null) { 277 String target = pi.getTarget(); 278 String data = pi.getData(); 279 try { 280 contentHandler.processingInstruction(target, data); 281 } 282 catch (SAXException se) { 283 throw new JDOMException( 284 "Exception in processingInstruction", se); 285 } 286 } 287 } 288 289 297 private void element(Element element) 298 throws JDOMException { 299 300 Integer lineNumber = Parser.getSourceLocation((ElementWithLocationInfo) element, Parser.LINENO); 302 Integer colNumber = Parser.getSourceLocation((ElementWithLocationInfo) element, Parser.COLNO); 303 304 locator.setSystemId(Parser.getSourcePathname(element)); 305 locator.setPublicId(Parser.getSourceMessagePathname(element)); 306 locator.setLineNumber(lineNumber.intValue()); 307 locator.setColumnNumber(colNumber.intValue()); 308 contentHandler.setDocumentLocator((Locator ) locator); 309 310 313 startElement(element); 315 316 elementContent(element); 318 319 endElement(element); 321 322 } 323 324 334 private void startElement(Element element) 335 throws JDOMException { 336 String namespaceURI = element.getNamespaceURI(); 337 String localName = element.getName(); 338 String rawName = element.getQualifiedName(); 339 340 AttributesImpl atts = new AttributesImpl (); 341 List attributes = element.getAttributes(); 342 Iterator i = attributes.iterator(); 343 while (i.hasNext()) { 344 Attribute a = (Attribute) i.next(); 345 atts.addAttribute(a.getNamespaceURI(), 346 a.getName(), 347 a.getQualifiedName(), 348 getAttributeTypeName(a.getAttributeType()), 349 a.getValue()); 350 } 351 if (this.writeMetaData) { 352 atts.addAttribute("", 355 SOURCEINFO_ATTRIBUTE_NAME, 356 SOURCEINFO_ATTRIBUTE_NAME, 357 "CDATA", 358 ((ElementWithLocationInfo) element).getSourceLocator().toString() 359 ); 360 } 361 362 try { 363 contentHandler.startElement(namespaceURI, localName, rawName, atts); 364 } 365 catch (SAXException se) { 366 throw new JDOMException("Exception in startElement", se); 367 } 368 } 369 370 378 private void endElement(Element element) throws JDOMException { 379 String namespaceURI = element.getNamespaceURI(); 380 String localName = element.getName(); 381 String rawName = element.getQualifiedName(); 382 383 try { 384 contentHandler.endElement(namespaceURI, localName, rawName); 385 } 386 catch (SAXException se) { 387 throw new JDOMException("Exception in endElement", se); 388 } 389 } 390 391 398 private void elementContent(Element element) 399 throws JDOMException { 400 List eltContent = element.getContent(); 401 402 boolean empty = eltContent.size() == 0; 403 boolean stringOnly = 404 !empty && 405 eltContent.size() == 1 && 406 eltContent.get(0) instanceof Text; 407 408 if (stringOnly) { 409 characters(element.getText()); 411 } 412 else { 413 Object content = null; 414 for (int i = 0, size = eltContent.size(); i < size; i++) { 415 content = eltContent.get(i); 416 if (content instanceof Element) { 417 element((Element) content); 418 } 419 else if (content instanceof Text) { 420 characters(((Text) content).getText()); 422 } 423 else if (content instanceof CDATA) { 424 characters(((CDATA) content).getText()); 426 } 427 else if (content instanceof ProcessingInstruction) { 428 processingInstruction((ProcessingInstruction) content); 430 } 431 } 432 } 433 } 434 435 442 private void characters(String elementText) throws JDOMException { 443 char[] c = elementText.toCharArray(); 444 try { 445 contentHandler.characters(c, 0, c.length); 446 } 447 catch (SAXException se) { 448 throw new JDOMException("Exception in characters", se); 449 } 450 } 451 452 453 457 private static final String [] attrTypeToNameMap = new String [] { 458 "CDATA", "CDATA", "ID", "IDREF", "IDREFS", "ENTITY", "ENTITIES", "NMTOKEN", "NMTOKENS", "NOTATION", "NMTOKEN", }; 470 471 484 private String getAttributeTypeName(int type) { 485 if ((type < 0) || (type >= attrTypeToNameMap.length)) { 486 type = Attribute.UNDECLARED_ATTRIBUTE; 487 } 488 return attrTypeToNameMap[type]; 489 } 490 491 } 492 | Popular Tags |