1 16 package org.apache.cocoon.util.location; 17 18 import org.w3c.dom.Attr ; 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.NodeList ; 22 import org.xml.sax.Attributes ; 23 import org.xml.sax.ContentHandler ; 24 import org.xml.sax.Locator ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.helpers.AttributesImpl ; 27 28 44 public class LocationAttributes { 45 46 public static final String PREFIX = "loc"; 47 48 public static final String URI = "http://apache.org/cocoon/location"; 49 50 51 public static final String SRC_ATTR = "src"; 52 53 public static final String LINE_ATTR = "line"; 54 55 public static final String COL_ATTR = "column"; 56 57 58 public static final String Q_SRC_ATTR = "loc:src"; 59 60 public static final String Q_LINE_ATTR = "loc:line"; 61 62 public static final String Q_COL_ATTR = "loc:column"; 63 64 private LocationAttributes() { 66 } 68 69 76 public static Attributes addLocationAttributes(Locator locator, Attributes attrs) { 77 if (locator == null || attrs.getIndex(URI, SRC_ATTR) != -1) { 78 return attrs; 80 } 81 82 AttributesImpl newAttrs = attrs instanceof AttributesImpl ? 84 (AttributesImpl )attrs : new AttributesImpl (attrs); 85 86 newAttrs.addAttribute(URI, SRC_ATTR, Q_SRC_ATTR, "CDATA", locator.getSystemId()); 87 newAttrs.addAttribute(URI, LINE_ATTR, Q_LINE_ATTR, "CDATA", Integer.toString(locator.getLineNumber())); 88 newAttrs.addAttribute(URI, COL_ATTR, Q_COL_ATTR, "CDATA", Integer.toString(locator.getColumnNumber())); 89 90 return newAttrs; 91 } 92 93 100 public static Location getLocation(Attributes attrs, String description) { 101 String src = attrs.getValue(URI, SRC_ATTR); 102 if (src == null) { 103 return Location.UNKNOWN; 104 } 105 106 return new LocationImpl(description, src, getLine(attrs), getColumn(attrs)); 107 } 108 109 117 public static String getLocationString(Attributes attrs) { 118 String src = attrs.getValue(URI, SRC_ATTR); 119 if (src == null) { 120 return LocationUtils.UNKNOWN_STRING; 121 } 122 123 return src + ":" + attrs.getValue(URI, LINE_ATTR) + ":" + attrs.getValue(URI, COL_ATTR); 124 } 125 126 133 public static String getURI(Attributes attrs) { 134 String src = attrs.getValue(URI, SRC_ATTR); 135 return src != null ? src : LocationUtils.UNKNOWN_STRING; 136 } 137 138 145 public static int getLine(Attributes attrs) { 146 String line = attrs.getValue(URI, LINE_ATTR); 147 return line != null ? Integer.parseInt(line) : -1; 148 } 149 150 157 public static int getColumn(Attributes attrs) { 158 String col = attrs.getValue(URI, COL_ATTR); 159 return col != null ? Integer.parseInt(col) : -1; 160 } 161 162 169 public static Location getLocation(Element elem, String description) { 170 Attr srcAttr = elem.getAttributeNodeNS(URI, SRC_ATTR); 171 if (srcAttr == null) { 172 return Location.UNKNOWN; 173 } 174 175 return new LocationImpl(description == null ? elem.getNodeName() : description, 176 srcAttr.getValue(), getLine(elem), getColumn(elem)); 177 } 178 179 182 public static Location getLocation(Element elem) { 183 return getLocation(elem, null); 184 } 185 186 187 195 public static String getLocationString(Element elem) { 196 Attr srcAttr = elem.getAttributeNodeNS(URI, SRC_ATTR); 197 if (srcAttr == null) { 198 return LocationUtils.UNKNOWN_STRING; 199 } 200 201 return srcAttr.getValue() + ":" + elem.getAttributeNS(URI, LINE_ATTR) + ":" + elem.getAttributeNS(URI, COL_ATTR); 202 } 203 204 211 public static String getURI(Element elem) { 212 Attr attr = elem.getAttributeNodeNS(URI, SRC_ATTR); 213 return attr != null ? attr.getValue() : LocationUtils.UNKNOWN_STRING; 214 } 215 216 223 public static int getLine(Element elem) { 224 Attr attr = elem.getAttributeNodeNS(URI, LINE_ATTR); 225 return attr != null ? Integer.parseInt(attr.getValue()) : -1; 226 } 227 228 235 public static int getColumn(Element elem) { 236 Attr attr = elem.getAttributeNodeNS(URI, COL_ATTR); 237 return attr != null ? Integer.parseInt(attr.getValue()) : -1; 238 } 239 240 246 public static void remove(Element elem, boolean recurse) { 247 elem.removeAttributeNS(URI, SRC_ATTR); 248 elem.removeAttributeNS(URI, LINE_ATTR); 249 elem.removeAttributeNS(URI, COL_ATTR); 250 if (recurse) { 251 NodeList children = elem.getChildNodes(); 252 for (int i = 0; i < children.getLength(); i++) { 253 Node child = children.item(i); 254 if (child.getNodeType() == Node.ELEMENT_NODE) { 255 remove((Element )child, recurse); 256 } 257 } 258 } 259 } 260 261 281 public static class Pipe implements ContentHandler { 282 283 private Locator locator; 284 285 private ContentHandler nextHandler; 286 287 290 public Pipe() { 291 } 292 293 297 public Pipe(ContentHandler next) { 298 nextHandler = next; 299 } 300 301 public void setDocumentLocator(Locator locator) { 302 this.locator = locator; 303 nextHandler.setDocumentLocator(locator); 304 } 305 306 public void startDocument() throws SAXException { 307 nextHandler.startDocument(); 308 nextHandler.startPrefixMapping(LocationAttributes.PREFIX, LocationAttributes.URI); 309 } 310 311 public void endDocument() throws SAXException { 312 endPrefixMapping(LocationAttributes.PREFIX); 313 nextHandler.endDocument(); 314 } 315 316 public void startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException { 317 nextHandler.startElement(uri, loc, raw, LocationAttributes.addLocationAttributes(locator, attrs)); 319 } 320 321 public void endElement(String arg0, String arg1, String arg2) throws SAXException { 322 nextHandler.endElement(arg0, arg1, arg2); 323 } 324 325 public void startPrefixMapping(String arg0, String arg1) throws SAXException { 326 nextHandler.startPrefixMapping(arg0, arg1); 327 } 328 329 public void endPrefixMapping(String arg0) throws SAXException { 330 nextHandler.endPrefixMapping(arg0); 331 } 332 333 public void characters(char[] arg0, int arg1, int arg2) throws SAXException { 334 nextHandler.characters(arg0, arg1, arg2); 335 } 336 337 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException { 338 nextHandler.ignorableWhitespace(arg0, arg1, arg2); 339 } 340 341 public void processingInstruction(String arg0, String arg1) throws SAXException { 342 nextHandler.processingInstruction(arg0, arg1); 343 } 344 345 public void skippedEntity(String arg0) throws SAXException { 346 nextHandler.skippedEntity(arg0); 347 } 348 } 349 } 350 | Popular Tags |