Attachment 'LocationTracker.java'

Download

   1 /* Creado en 23-ago-2005 */
   2 package fcc.ima.cocoon;
   3 
   4 import java.util.Stack;
   5 
   6 import org.apache.cocoon.xml.AbstractXMLConsumer;
   7 import org.xml.sax.Attributes;
   8 import org.xml.sax.Locator;
   9 import org.xml.sax.SAXException;
  10 
  11 /**Util para localizar en q parte de 1 flujo XML hemos tenido 1 problema.
  12  * <p>Especialmente util cuando se combina con {@link org.apache.cocoon.components.sax.XMLTeePipe}.*/
  13 public class LocationTracker extends AbstractXMLConsumer implements Locator {
  14 	static class ElemEx extends Elem {
  15 		/**Denotes the number of child elements received.
  16 		 * <p>Denota numero de elementos hijo q se han recibido*/
  17 		int nivel;
  18 		
  19 		public ElemEx(String uri, String nombreLocal, String nombreCua, Attributes atr) {
  20 			super(uri, nombreLocal, nombreCua, atr);
  21 		}
  22 
  23 		/**@see fcc.ima.cocoon.Elem#toString() */
  24 		public String toString() {
  25 			return super.toString();
  26 		}
  27 	}
  28 	/**Stack of {@link ElemEx}.*/
  29 	private Stack stack = new Stack();
  30 	/**Has the end of document been reached?*/
  31 	private boolean reachedEnd;
  32 	
  33 	/**@see org.xml.sax.helpers.DefaultHandler#startDocument() */
  34 	public void startDocument() throws SAXException {
  35 		reachedEnd = false;
  36 		stack.clear();
  37 	}
  38 	/**@see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) */
  39 	public void startElement(String uri, String localName, String qName, Attributes atrs) throws SAXException {
  40 		if (stack.size() > 0) {
  41 			((ElemEx)stack.peek()).nivel++;
  42 		}
  43 		stack.add(new ElemEx(uri, localName, qName, atrs));
  44 	}
  45 	/**@see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String) */
  46 	public void endElement(String uri, String localName, String qName) throws SAXException {
  47 		stack.pop();
  48 	}
  49 	/**@see org.xml.sax.helpers.DefaultHandler#endDocument() */
  50 	public void endDocument() throws SAXException {
  51 		reachedEnd = true;
  52 	}
  53 	public String describeCurrentPos() {
  54 		StringBuffer sb = new StringBuffer();
  55 		if (reachedEnd) {
  56 			sb.append("Llegado al final documento\n");
  57 		}
  58 		int tam = stack.size();
  59 		int nivel = 0;
  60 		for (int i = 0; i < tam; i++) {
  61 			if (i > 0) {
  62 				sb.append("/");
  63 			}
  64 			ElemEx el = (ElemEx) stack.get(i);
  65 			sb.append(el);
  66 			if (nivel > 0) {
  67 				sb.append("["+nivel+"]");
  68 			}
  69 			nivel = el.nivel;
  70 		}
  71 		return sb.toString();
  72 	}
  73 	
  74 	//####### Implementation of Locator interface
  75 	
  76 	public int getColumnNumber() {
  77 		return -1;
  78 	}
  79 	public int getLineNumber() {
  80 		return -1;
  81 	}
  82 	public String getPublicId() {
  83 		return describeCurrentPos();
  84 	}
  85 	public String getSystemId() {
  86 		return null;
  87 	}
  88 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.