1 56 57 package org.jdom.contrib.input; 58 59 import org.jdom.Element; 60 import org.jdom.Namespace; 61 import org.jdom.DefaultJDOMFactory; 62 import org.jdom.JDOMFactory; 63 import org.jdom.input.SAXBuilder; 64 import org.jdom.input.SAXHandler; 65 import org.xml.sax.Attributes ; 66 import org.xml.sax.Locator ; 67 import org.xml.sax.SAXException ; 68 69 85 public class LineNumberSAXBuilder extends SAXBuilder 86 { 87 protected SAXHandler createContentHandler() 88 { 89 return new MySAXHandler(new MyFactory()); 90 } 91 92 private class MyFactory extends DefaultJDOMFactory 93 { 94 95 public Element element(String name) 96 { 97 return new LineNumberElement(name); 98 } 99 100 public Element element(String name, String prefix, String uri) 101 { 102 return new LineNumberElement(name, prefix, uri); 103 } 104 105 public Element element(String name, Namespace namespace) 106 { 107 return new LineNumberElement(name, namespace); 108 } 109 110 public Element element(String name, String uri) 111 { 112 return new LineNumberElement(name, uri); 113 } 114 115 } 116 117 private class MySAXHandler extends SAXHandler 118 { 119 120 public MySAXHandler(JDOMFactory f) 121 { 122 super(f); 123 } 124 125 126 public void startElement( 127 String arg0, 128 String arg1, 129 String arg2, 130 Attributes arg3) 131 throws SAXException 132 { 133 super.startElement(arg0, arg1, arg2, arg3); 134 Locator l = getDocumentLocator(); 135 if (l != null) 136 { 137 ((LineNumberElement) getCurrentElement()).setStartLine( 138 l.getLineNumber()); 139 } 140 } 141 142 143 public void endElement(String arg0, String arg1, String arg2) 144 throws SAXException 145 { 146 Locator l = getDocumentLocator(); 147 if (l != null) 148 { 149 ((LineNumberElement) getCurrentElement()).setEndLine( 150 l.getLineNumber()); 151 } 152 153 super.endElement(arg0, arg1, arg2); 154 } 155 156 } 157 158 } 159 | Popular Tags |