001: // $Id: ContentHandler.java 193 2005-10-17 22:16:47Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTOSRS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit.data.processing.parser;
038:
039: import junitx.ddtunit.DDTException;
040: import junitx.ddtunit.data.IDataSet;
041: import junitx.ddtunit.data.processing.Engine;
042:
043: import org.apache.log4j.Logger;
044: import org.xml.sax.Attributes;
045: import org.xml.sax.Locator;
046: import org.xml.sax.SAXException;
047: import org.xml.sax.SAXParseException;
048: import org.xml.sax.ext.LexicalHandler;
049: import org.xml.sax.helpers.DefaultHandler;
050:
051: /**
052: * SAX content handler of ddtunit xml extension
053: *
054: * @author jg
055: */
056: class ContentHandler extends DefaultHandler implements LexicalHandler {
057: private static final String LF = System
058: .getProperty("line.separator");
059:
060: Logger log = Logger.getLogger(ContentHandler.class);
061:
062: private Locator locator;
063:
064: private String clusterId;
065:
066: private String resourceName;
067:
068: private int levelCount;
069:
070: private Engine eventConsumer;
071:
072: /**
073: * Instanciate sax content handler
074: *
075: * @param aParser
076: */
077: public ContentHandler(String resourceName, IDataSet clusterDataSet) {
078: super ();
079: this .resourceName = resourceName;
080: this .eventConsumer = new Engine(clusterDataSet);
081: this .levelCount = 0;
082: }
083:
084: /**
085: * Set Locator
086: *
087: * @param aLocator
088: */
089: public void setDocumentLocator(Locator aLocator) {
090: this .locator = aLocator;
091: }
092:
093: /**
094: * SAX event startDocument ...
095: */
096: public void startDocument() {
097: log.debug("Start Document...");
098: }
099:
100: /**
101: * SAX event endDocument ...
102: */
103: public void endDocument() {
104: log.debug("End Document...");
105: }
106:
107: /**
108: * SAX startElement()
109: *
110: * @param namespaceURI namespace not null if provided in xml element
111: * @param localName of xml element
112: * @param qName qualified name of xml element
113: * @param attributes of xml element
114: */
115: public void startElement(String namespaceURI, String localName,
116: String qName, Attributes attributes) {
117: try {
118: this .levelCount++;
119: this .eventConsumer.processStartElement(qName, attributes,
120: this .levelCount);
121: } catch (Exception ex) {
122: StringBuffer sb = new StringBuffer(ex.getClass().getName());
123: sb.append(ex.getMessage());
124: sb.append(LF).append("Resource \'").append(
125: this .resourceName).append("\' line/column ")
126: .append(locator.getLineNumber()).append("/")
127: .append(locator.getColumnNumber());
128: DDTException ddtEx = new DDTException(sb.toString(), ex);
129: ddtEx.setStackTrace(ex.getStackTrace());
130: log.warn(sb.toString(), ddtEx);
131: throw ddtEx;
132: }
133: }
134:
135: /**
136: * SAX endElement()
137: *
138: * @param namespace of xml element
139: * @param localName of xml element
140: * @param qName qualified name of xml element
141: *
142: * @throws SAXException if any parsing exception occures
143: */
144: public void endElement(String namespace, String localName,
145: String qName) throws SAXException {
146: try {
147: this .eventConsumer
148: .processEndElement(qName, this .levelCount);
149: } catch (Exception ex) {
150: StringBuffer sb = new StringBuffer(ex.getClass().getName());
151: sb.append(ex.getMessage());
152: sb.append(LF).append("Resource \'").append(
153: this .resourceName).append("\' line/column ")
154: .append(locator.getLineNumber()).append("/")
155: .append(locator.getColumnNumber());
156: DDTException ddtEx = new DDTException(sb.toString(), ex);
157: ddtEx.setStackTrace(ex.getStackTrace());
158: log.warn(sb.toString(), ddtEx);
159: throw ddtEx;
160: } finally {
161: this .levelCount--;
162: }
163: }
164:
165: /**
166: * Process simple content of xml tag values. <br/ > Ignore content outside
167: * of <code><obj></code> tags. <br/ > No splitted text content is
168: * processed correctly.
169: *
170: * @param buffer of character that contains value of xml element
171: * @param offset of xml value start
172: * @param length of xml value content
173: *
174: * @throws SAXException if any processing error occures
175: */
176: public void characters(char[] buffer, int offset, int length)
177: throws SAXException {
178: this .eventConsumer.processCharacters(buffer, offset, length);
179: }
180:
181: public void ignorableWhitespace(char[] buffer, int offset,
182: int length) {
183: System.err.println(new String(buffer, offset, length));
184: }
185:
186: /**
187: * Implementation of ErrorHandler interface
188: *
189: * @param e
190: * @throws SAXException DOCUMENT ME!
191: */
192: public void error(SAXParseException e) throws SAXException {
193: log.error("Parse error detected", e);
194: throw e;
195: }
196:
197: public void setClusterId(String clusterId) {
198: this .clusterId = clusterId;
199: this .eventConsumer.setClusterId(clusterId);
200: }
201:
202: public String getClusterId() {
203: return clusterId;
204: }
205:
206: /*
207: * (non-Javadoc)
208: *
209: * @see org.xml.sax.ext.LexicalHandler#endCDATA()
210: */
211: public void endCDATA() throws SAXException {
212: this .eventConsumer.endCDATA();
213: }
214:
215: /*
216: * (non-Javadoc)
217: *
218: * @see org.xml.sax.ext.LexicalHandler#endDTD()
219: */
220: public void endDTD() throws SAXException {
221: }
222:
223: /*
224: * (non-Javadoc)
225: *
226: * @see org.xml.sax.ext.LexicalHandler#startCDATA()
227: */
228: public void startCDATA() throws SAXException {
229: this .eventConsumer.startCDATA();
230: }
231:
232: /*
233: * (non-Javadoc)
234: *
235: * @see org.xml.sax.ext.LexicalHandler#comment(char[], int, int)
236: */
237: public void comment(char[] ch, int start, int length)
238: throws SAXException {
239: }
240:
241: /*
242: * (non-Javadoc)
243: *
244: * @see org.xml.sax.ext.LexicalHandler#endEntity(java.lang.String)
245: */
246: public void endEntity(String name) throws SAXException {
247: }
248:
249: /*
250: * (non-Javadoc)
251: *
252: * @see org.xml.sax.ext.LexicalHandler#startEntity(java.lang.String)
253: */
254: public void startEntity(String name) throws SAXException {
255: }
256:
257: /*
258: * (non-Javadoc)
259: *
260: * @see org.xml.sax.ext.LexicalHandler#startDTD(java.lang.String,
261: * java.lang.String, java.lang.String)
262: */
263: public void startDTD(String name, String publicId, String systemId)
264: throws SAXException {
265: }
266: }
|