001: /*
002: * File: BaseHandler.java
003: * Project: jMOS, com.aranova.java.jmos.handler
004: * Revision: 0.9 - Inicial
005: * Date: 30-sep-2005 10:39:16
006: *
007: * Copyright (C) Aragón Innovación Tecnológica S.L.L.
008: * All rights reserved.
009: *
010: * This software is distributed under the terms of the Aranova License version 1.0.
011: * See the terms of the Aranova License in the documentation provided with this software.
012: */
013:
014: package com.aranova.java.jmos.handler;
015:
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018: import org.xml.sax.Attributes;
019: import org.xml.sax.SAXException;
020: import org.xml.sax.helpers.DefaultHandler;
021:
022: import com.aranova.java.jmos.io.XMLInputStream;
023:
024: /**
025: * Class for handler SAX2 events.
026: *
027: * @author <a href="http://www.aranova.net/contactar/">Daniel Sánchez</a>
028: * @version 0.9.1
029: * @since 0.9
030: */
031: public class BaseHandler extends DefaultHandler {
032: private static final Log _log = LogFactory
033: .getLog(BaseHandler.class);
034: private final XMLInputStream _XMLis;
035: private String _firstElement;
036: private int _firstNivel;
037: private int _nivel;
038: private long _start;
039: private long _time;
040:
041: /**
042: * @param XMLis
043: */
044: public BaseHandler(final XMLInputStream XMLis) {
045: super ();
046: _XMLis = XMLis;
047: }
048:
049: /* (non-Javadoc)
050: * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
051: */
052: @Override
053: public void characters(final char[] ch, final int start,
054: final int length) throws SAXException {
055: }
056:
057: /* (non-Javadoc)
058: * @see org.xml.sax.helpers.DefaultHandler#endDocument()
059: */
060: @Override
061: public void endDocument() throws SAXException {
062: _time = System.currentTimeMillis() - _start;
063: _log.trace("Time: " + _time);
064: }
065:
066: /* (non-Javadoc)
067: * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
068: */
069: @Override
070: public void endElement(final String uri, final String localName,
071: final String qName) throws SAXException {
072: if (qName.equals(_firstElement)) {
073: if (_firstNivel == 0) {
074: _XMLis.endStream();
075: _log.info("Fin de parseo: " + _firstElement);
076: _firstElement = null;
077: } else {
078: _firstNivel--;
079: }
080: }
081: _nivel--;
082: }
083:
084: /* (non-Javadoc)
085: * @see org.xml.sax.helpers.DefaultHandler#startDocument()
086: */
087: @Override
088: public void startDocument() throws SAXException {
089: _nivel = 0;
090: _firstNivel = 0;
091: _start = System.currentTimeMillis();
092: }
093:
094: /* (non-Javadoc)
095: * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
096: */
097: @Override
098: public void startElement(final String uri, final String localName,
099: final String qName, final Attributes attributes)
100: throws SAXException {
101: if (_firstElement == null) {
102: _firstElement = qName;
103: _log.info("Parseando: " + _firstElement);
104: } else {
105: if (qName.equals(_firstElement)) {
106: _firstNivel++;
107: }
108: }
109: _nivel++;
110: }
111:
112: /**
113: * @return Returns the nivel.
114: */
115: protected final int getNivel() {
116: return _nivel;
117: }
118:
119: /**
120: * @return Returns the start.
121: */
122: public final long getStart() {
123: return _start;
124: }
125:
126: /**
127: * @return Returns the time.
128: */
129: public final long getTime() {
130: return _time;
131: }
132: }
|