curcuru 01/03/08 14:42:07
Modified: test/java/src/org/apache/qetest/xsl
LoggingContentHandler.java
LoggingLexicalHandler.java
Log:
Implement get/setDefaultHandler; also ContentHandler now
uses LoggingHandler base class (except for setExpected, which is not complete
yet)
Revision Changes Path
1.2 +100 -72
xml-xalan/test/java/src/org/apache/qetest/xsl/LoggingContentHandler.java
Index: LoggingContentHandler.java
===================================================================
RCS file:
/home/cvs/xml-xalan/test/java/src/org/apache/qetest/xsl/LoggingContentHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- LoggingContentHandler.java 2000/12/21 14:18:48 1.1
+++ LoggingContentHandler.java 2001/03/08 22:42:06 1.2
@@ -75,100 +75,102 @@
/**
* Cheap-o ContentHandler for use by API tests.
* <p>Implements ContentHandler and dumps simplistic info
- * everything to a Reporter; a way to debug SAX stuff.</p>
+ * everything to a Logger; a way to debug SAX stuff.</p>
* @author [EMAIL PROTECTED]
- * @version $Id: LoggingContentHandler.java,v 1.1 2000/12/21 14:18:48
curcuru Exp $
+ * @version $Id: LoggingContentHandler.java,v 1.2 2001/03/08 22:42:06
curcuru Exp $
*/
-public class LoggingContentHandler implements ContentHandler
+public class LoggingContentHandler extends LoggingHandler implements
ContentHandler
{
- /** No-op ctor seems useful. */
- public LoggingContentHandler(){}
+ /** No-op sets logger to default. */
+ public LoggingContentHandler()
+ {
+ setLogger(getDefaultLogger());
+ }
/**
- * Ctor that calls setReporter automatically.
+ * Ctor that calls setLogger automatically.
*
- * @param r Reporter we should log to
- * @todo this should really be a Logger, not Reporter
+ * @param r Logger we should log to
*/
- public LoggingContentHandler(Reporter r)
+ public LoggingContentHandler(Logger l)
{
- setReporter(r);
+ setLogger(l);
}
- /**
- * Our Reporter, who we tell all our secrets to.
- * Could/Should be switched to be a Logger.
+
+ /**
+ * Our default handler that we pass all events through to.
*/
- private Reporter reporter;
+ protected ContentHandler defaultHandler = null;
+
/**
- * Accesor methods for our Reporter.
- * @param r Reporter to set
+ * Set a default handler for us to wrapper.
+ * Set a ContentHandler for us to use.
+ *
+ * @param default Object of the correct type to pass-through to;
+ * throws IllegalArgumentException if null or incorrect type
*/
- public void setReporter(Reporter r)
+ public void setDefaultHandler(Object defaultC)
{
- if (r != null)
- reporter = r;
+ try
+ {
+ defaultHandler = (ContentHandler)defaultC;
+ }
+ catch (Throwable t)
+ {
+ throw new java.lang.IllegalArgumentException("setDefaultHandler
illegal type: " + t.toString());
+ }
}
+
/**
- * Accesor methods for our Reporter.
- * @return Reporter we use
+ * Accessor method for our default handler.
+ *
+ * @return default (Object) our default handler; null if unset
*/
- public Reporter getReporter()
+ public Object getDefaultHandler()
{
- return (reporter);
+ return (Object)defaultHandler;
}
+
+
+ /** Prefixed to all logger msg output. */
+ public final String prefix = "LCH:";
- /** Prefixed to all reporter msg output. */
- private final String prefix = "LCH:";
/** Cheap-o string representation of last event we got. */
- private String lastEvent = null;
+ protected String lastItem = NOTHING_HANDLED;
+
/**
- * Method setLastEvent set our lastEvent field.
+ * Accessor for string representation of last event we got.
* @param s string to set
*/
- protected void setLastEvent(String s)
+ protected void setLastItem(String s)
{
- lastEvent = s;
+ lastItem = s;
}
+
/**
* Accessor for string representation of last event we got.
* @return last event string we had
*/
- public String getLastEvent()
+ public String getLast()
{
- return lastEvent;
+ return lastItem;
}
- /** What loggingLevel to use for reporter.logMsg(). */
- private int level = Reporter.DEFAULT_LOGGINGLEVEL;
- /**
- * Accesor methods; don't think it needs to be synchronized.
- * @param l loggingLevel for us to use
- */
- public void setLoggingLevel(int l)
- {
- level = l;
- }
+ /** setExpected, etc. not yet implemented. */
- /**
- * Accesor methods; don't think it needs to be synchronized.
- * @return loggingLevel we use
- */
- public int getLoggingLevel()
- {
- return level;
- }
/** How many characters to report from characters event. */
private int charLimit = 30;
+
/**
* How many characters to report from characters event.
* @param l charLimit for us to use
@@ -178,6 +180,7 @@
charLimit = l;
}
+
/**
* How many characters to report from characters event.
* @return charLimit we use
@@ -189,43 +192,56 @@
////////////////// Implement ContentHandler //////////////////
+ protected Locator ourLocator = null;
+
public void setDocumentLocator (Locator locator)
{
// Note: this implies this class is !not! threadsafe
- setLastEvent("setDocumentLocator");
- reporter.logMsg(level, getLastEvent());
+ setLastItem("setDocumentLocator");
+ ourLocator = locator; // future use
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.setDocumentLocator(locator);
}
public void startDocument ()
throws SAXException
{
- setLastEvent("startDocument");
- reporter.logMsg(level, getLastEvent());
+ setLastItem("startDocument");
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.startDocument();
}
public void endDocument()
throws SAXException
{
- setLastEvent("endDocument");
- reporter.logMsg(level, getLastEvent());
+ setLastItem("endDocument");
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.endDocument();
}
public void startPrefixMapping (String prefix, String uri)
throws SAXException
{
- setLastEvent("startPrefixMapping: " + prefix + ", " + uri);
- reporter.logMsg(level, getLastEvent());
+ setLastItem("startPrefixMapping: " + prefix + ", " + uri);
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.startPrefixMapping(prefix, uri);
}
public void endPrefixMapping (String prefix)
throws SAXException
{
- setLastEvent("endPrefixMapping: " + prefix);
- reporter.logMsg(level, getLastEvent());
+ setLastItem("endPrefixMapping: " + prefix);
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.endPrefixMapping(prefix);
}
@@ -242,16 +258,20 @@
{
buf.append(", " + atts.getQName(i));
}
- setLastEvent(buf.toString());
- reporter.logMsg(level, getLastEvent());
+ setLastItem(buf.toString());
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.startElement(namespaceURI, localName, qName,
atts);
}
public void endElement (String namespaceURI, String localName, String
qName)
throws SAXException
{
- setLastEvent("endElement: " + namespaceURI + ", " + namespaceURI +
", " + qName);
- reporter.logMsg(level, getLastEvent());
+ setLastItem("endElement: " + namespaceURI + ", " + namespaceURI + ",
" + qName);
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.endElement(namespaceURI, localName, qName);
}
@@ -260,34 +280,42 @@
{
String s = new String(ch, start, (length > charLimit) ? charLimit :
length);
if(length > charLimit)
- setLastEvent("characters: \"" + s + "\"...");
+ setLastItem("characters: \"" + s + "\"...");
else
- setLastEvent("characters: \"" + s + "\"");
- reporter.logMsg(level, getLastEvent());
+ setLastItem("characters: \"" + s + "\"");
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.characters(ch, start, length);
}
public void ignorableWhitespace (char ch[], int start, int length)
throws SAXException
{
- setLastEvent("ignorableWhitespace: len " + length);
- reporter.logMsg(level, getLastEvent());
+ setLastItem("ignorableWhitespace: len " + length);
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.ignorableWhitespace(ch, start, length);
}
public void processingInstruction (String target, String data)
throws SAXException
{
- setLastEvent("processingInstruction: " + target + ", " + data);
- reporter.logMsg(level, getLastEvent());
+ setLastItem("processingInstruction: " + target + ", " + data);
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.processingInstruction(target, data);
}
public void skippedEntity (String name)
throws SAXException
{
- setLastEvent("skippedEntity: " + name);
- reporter.logMsg(level, getLastEvent());
+ setLastItem("skippedEntity: " + name);
+ logger.logMsg(level, prefix + getLast());
+ if (null != defaultHandler)
+ defaultHandler.skippedEntity(name);
}
}
1.2 +266 -48
xml-xalan/test/java/src/org/apache/qetest/xsl/LoggingLexicalHandler.java
Index: LoggingLexicalHandler.java
===================================================================
RCS file:
/home/cvs/xml-xalan/test/java/src/org/apache/qetest/xsl/LoggingLexicalHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- LoggingLexicalHandler.java 2001/01/05 18:35:59 1.1
+++ LoggingLexicalHandler.java 2001/03/08 22:42:06 1.2
@@ -62,7 +62,8 @@
*/
package org.apache.qetest.xsl;
-import org.apache.qetest.*;
+import org.apache.qetest.LoggingHandler;
+import org.apache.qetest.Logger;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
@@ -72,13 +73,16 @@
* <p>Implements LexicalHandler and dumps simplistic info
* everything to a Logger; a way to debug SAX stuff.</p>
* @author [EMAIL PROTECTED]
- * @version $Id: LoggingLexicalHandler.java,v 1.1 2001/01/05 18:35:59
curcuru Exp $
+ * @version $Id: LoggingLexicalHandler.java,v 1.2 2001/03/08 22:42:06
curcuru Exp $
*/
-public class LoggingLexicalHandler implements LexicalHandler
+public class LoggingLexicalHandler extends LoggingHandler implements
LexicalHandler
{
- /** No-op ctor seems useful. */
- public LoggingLexicalHandler(){}
+ /** No-op sets logger to default. */
+ public LoggingLexicalHandler()
+ {
+ setLogger(getDefaultLogger());
+ }
/**
* Ctor that calls setLogger automatically.
@@ -90,73 +94,265 @@
setLogger(l);
}
- /**
- * Our Logger, who we tell all our secrets to.
+
+ /**
+ * Our default handler that we pass all events through to.
*/
- private Logger logger;
+ protected LexicalHandler defaultHandler = null;
+
/**
- * Accesor methods for our Logger.
- * @param r Logger to set
+ * Set a default handler for us to wrapper.
+ * Set a LexicalHandler for us to use.
+ *
+ * @param default Object of the correct type to pass-through to;
+ * throws IllegalArgumentException if null or incorrect type
*/
- public void setLogger(Logger l)
+ public void setDefaultHandler(Object defaultH)
{
- if (l != null)
- logger = l;
+ try
+ {
+ defaultHandler = (LexicalHandler)defaultH;
+ }
+ catch (Throwable t)
+ {
+ throw new java.lang.IllegalArgumentException("setDefaultHandler
illegal type: " + t.toString());
+ }
}
+
/**
- * Accesor methods for our Logger.
- * @return Logger we use
+ * Accessor method for our default handler.
+ *
+ * @return default (Object) our default handler; null if unset
*/
- public Logger getLogger()
+ public Object getDefaultHandler()
{
- return (logger);
+ return (Object)defaultHandler;
}
+
/** Prefixed to all logger msg output. */
- private final String prefix = "LLH:";
+ public static final String prefix = "LLH:";
+
+ /** Constant for items returned in getCounters: startDTD. */
+ public static final int TYPE_STARTDTD = 0;
+
+ /** Constant for items returned in getCounters: endDTD. */
+ public static final int TYPE_ENDDTD = 1;
+
+ /** Constant for items returned in getCounters: startEntity. */
+ public static final int TYPE_STARTENTITY = 2;
+ /** Constant for items returned in getCounters: endEntity. */
+ public static final int TYPE_ENDENTITY = 3;
+
+ /** Constant for items returned in getCounters: startCDATA. */
+ public static final int TYPE_STARTCDATA = 4;
+
+ /** Constant for items returned in getCounters: endCDATA. */
+ public static final int TYPE_ENDCDATA = 5;
+
+ /** Constant for items returned in getCounters: comment. */
+ public static final int TYPE_COMMENT = 6;
+
+
+ /**
+ * Counters for how many events we've handled.
+ * Index into array are the TYPE_* constants.
+ */
+ protected int[] counters =
+ {
+ 0, /* startDTD */
+ 0, /* endDTD */
+ 0, /* startEntity */
+ 0, /* endEntity */
+ 0, /* startCDATA */
+ 0, /* endCDATA */
+ 0 /* comment */
+ };
+
+
+ /**
+ * Get a list of counters of all items we've logged.
+ * Returned in order as startDTD, endDTD, startEntity,
+ * endEntity, startCDATA, endCDATA, comment.
+ * Index into array are the TYPE_* constants.
+ *
+ * @return array of int counters for each item we log
+ */
+ public int[] getCounters()
+ {
+ return counters;
+ }
+
+
+ /**
+ * Really Cheap-o string representation of our state.
+ *
+ * @return String of getCounters() rolled up in minimal space
+ */
+ public String getQuickCounters()
+ {
+ return (prefix + "("
+ + counters[TYPE_STARTDTD] + ", " + counters[TYPE_ENDDTD] +
"; "
+ + counters[TYPE_STARTENTITY] + ", " +
counters[TYPE_ENDENTITY] + "; "
+ + counters[TYPE_STARTCDATA] + ", " + counters[TYPE_ENDCDATA]
+ "; "
+ + counters[TYPE_COMMENT] + ")");
+ }
+
+
+ /** Expected values for events we may handle, default=ITEM_DONT_CARE. */
+ protected String[] expected =
+ {
+ ITEM_DONT_CARE, /* startDTD */
+ ITEM_DONT_CARE, /* endDTD */
+ ITEM_DONT_CARE, /* startEntity */
+ ITEM_DONT_CARE, /* endEntity */
+ ITEM_DONT_CARE, /* startCDATA */
+ ITEM_DONT_CARE, /* endCDATA */
+ ITEM_DONT_CARE /* comment */
+ };
+
+
/** Cheap-o string representation of last event we got. */
- private String lastEvent = null;
+ protected String lastItem = NOTHING_HANDLED;
+
/**
- * Method setLastEvent set our lastEvent field.
+ * Accessor for string representation of last event we got.
* @param s string to set
*/
- protected void setLastEvent(String s)
+ protected void setLastItem(String s)
{
- lastEvent = s;
+ lastItem = s;
}
+
/**
* Accessor for string representation of last event we got.
* @return last event string we had
+ */
+ public String getLast()
+ {
+ return lastItem;
+ }
+
+
+ /**
+ * Ask us to report checkPass/Fail for certain events we handle.
+ * Since we may have to handle many events between when a test
+ * will be able to call us, testers can set this to have us
+ * automatically call checkPass when we see an item that matches,
+ * or to call checkFail when we get an unexpected item.
+ * Generally, we only call check* methods when:
+ * <ul>
+ * <li>containsString is not set, reset, or is ITEM_DONT_CARE,
+ * we do nothing (i.e. never call check* for this item)</li>
+ * <li>containsString is ITEM_CHECKFAIL, we will always call
+ * checkFail with the contents of any item if it occours</li>
+ * <li>containsString is anything else, we will grab a String
+ * representation of every item of that type that comes along,
+ * and if the containsString is found, case-sensitive, within
+ * the handled item's string, call checkPass, otherwise
+ * call checkFail</li>
+ * <ul>
+ * Note that any time we handle a particular event that was
+ * expected, we un-set the expected value for that item. This
+ * means that you can only ask us to validate one occourence
+ * of any particular event; all events after that one will
+ * be treated as ITEM_DONT_CARE. Callers can of course call
+ * setExpected again, of course, but this covers the case where
+ * we handle multiple events in a single block, perhaps out of
+ * the caller's direct control.
+ * Note that we first store the event via setLast(), then we
+ * validate the event as above, and then we potentially
+ * re-throw the exception as by setThrowWhen().
+ *
+ * @param itemType which of the various types of items we might
+ * handle; should be defined as a constant by subclasses
+ * @param containsString a string to look for within whatever
+ * item we handle - usually checked for by seeing if the actual
+ * item we handle contains the containsString
*/
- public String getLastEvent()
+ public void setExpected(int itemType, String containsString)
{
- return lastEvent;
+ // Default to don't care on null
+ if (null == containsString)
+ containsString = ITEM_DONT_CARE;
+
+ try
+ {
+ expected[itemType] = containsString;
+ }
+ catch (ArrayIndexOutOfBoundsException aioobe)
+ {
+ // Just log it for callers reference and continue anyway
+ logger.logMsg(level, prefix + " setExpected called with illegal
type:" + itemType);
+ }
}
- /** What loggingLevel to use for logger.logMsg(). */
- private int level = Logger.DEFAULT_LOGGINGLEVEL;
/**
- * Accesor methods; don't think it needs to be synchronized.
- * @param l loggingLevel for us to use
+ * Reset all items or counters we've handled.
*/
- public void setLoggingLevel(int l)
+ public void reset()
{
- level = l;
+ setLastItem(NOTHING_HANDLED);
+ for (int i = 0; i < counters.length; i++)
+ {
+ counters[i] = 0;
+ }
+ for (int j = 0; j < expected.length; j++)
+ {
+ expected[j] = ITEM_DONT_CARE;
+ }
}
+
/**
- * Accesor methods; don't think it needs to be synchronized.
- * @return loggingLevel we use
+ * Worker method to either log or call check* for this event.
+ * A simple way to validate for any kind of event.
+ * Note that various events may store the various arguments
+ * they get differently, so you should check the code to
+ * ensure you're specifying the correct containsString.
+ *
+ * @param type of event (startdtd|enddtd|etc)
+ * @param desc detail info from this kind of message
*/
- public int getLoggingLevel()
+ protected void logOrCheck(int type, String desc)
{
- return level;
+ String tmp = getQuickCounters() + " " + desc;
+ // Either log the exception or call checkPass/checkFail
+ // as requested by setExpected for this type
+ if (ITEM_DONT_CARE == expected[type])
+ {
+ // We don't care about this, just log it
+ logger.logMsg(level, tmp);
+ }
+ else if (ITEM_CHECKFAIL == expected[type])
+ {
+ // We shouldn't have been called here, so fail
+ logger.checkFail(tmp + " was unexpected");
+ }
+ else if ((null != desc)
+ && (desc.indexOf(expected[type]) > -1))
+ {
+ // We got a warning the user expected, so pass
+ logger.checkPass(tmp + " matched");
+ // Also reset this counter
+ //@todo needswork: this is very state-dependent, and
+ // might not be what the user expects, but at least it
+ // won't give lots of extra false fails or passes
+ expected[type] = ITEM_DONT_CARE;
+ }
+ else
+ {
+ // We got a warning the user didn't expect, so fail
+ logger.checkFail(tmp + " did not match");
+ // Also reset this counter
+ expected[type] = ITEM_DONT_CARE;
+ }
}
@@ -165,48 +361,68 @@
throws SAXException
{
// Note: this implies this class is !not! threadsafe
- setLastEvent("startDTD: " + name + ", " + publicId + ", " +
systemId);
- logger.logMsg(level, getLastEvent());
+ // Increment counter and save info
+ counters[TYPE_STARTDTD]++;
+ setLastItem("startDTD: " + name + ", " + publicId + ", " + systemId);
+ logOrCheck(TYPE_STARTDTD, getLast());
+ if (null != defaultHandler)
+ defaultHandler.startDTD(name, publicId, systemId);
}
public void endDTD ()
throws SAXException
{
- setLastEvent("endDTD");
- logger.logMsg(level, getLastEvent());
+ counters[TYPE_ENDDTD]++;
+ setLastItem("endDTD");
+ logOrCheck(TYPE_ENDDTD, getLast());
+ if (null != defaultHandler)
+ defaultHandler.endDTD();
}
public void startEntity (String name)
throws SAXException
{
- setLastEvent("startEntity: " + name);
- logger.logMsg(level, getLastEvent());
+ counters[TYPE_STARTENTITY]++;
+ setLastItem("startEntity: " + name);
+ logOrCheck(TYPE_STARTENTITY, getLast());
+ if (null != defaultHandler)
+ defaultHandler.startEntity(name);
}
public void endEntity (String name)
throws SAXException
{
- setLastEvent("endEntity: " + name);
- logger.logMsg(level, getLastEvent());
+ counters[TYPE_ENDENTITY]++;
+ setLastItem("endEntity: " + name);
+ logOrCheck(TYPE_ENDENTITY, getLast());
+ if (null != defaultHandler)
+ defaultHandler.endEntity(name);
}
public void startCDATA ()
throws SAXException
{
- setLastEvent("startCDATA");
- logger.logMsg(level, getLastEvent());
+ counters[TYPE_STARTCDATA]++;
+ setLastItem("startCDATA");
+ logOrCheck(TYPE_STARTCDATA, getLast());
+ if (null != defaultHandler)
+ defaultHandler.startCDATA();
}
public void endCDATA ()
throws SAXException
{
- setLastEvent("endCDATA");
- logger.logMsg(level, getLastEvent());
+ counters[TYPE_ENDCDATA]++;
+ setLastItem("endCDATA");
+ logOrCheck(TYPE_ENDCDATA, getLast());
+ if (null != defaultHandler)
+ defaultHandler.endCDATA();
}
public void comment (char ch[], int start, int length)
throws SAXException
{
+ counters[TYPE_COMMENT]++;
StringBuffer buf = new StringBuffer("comment: ");
buf.append(ch);
buf.append(", ");
@@ -214,8 +430,10 @@
buf.append(", ");
buf.append(length);
- setLastEvent(buf.toString());
- logger.logMsg(level, getLastEvent());
+ setLastItem(buf.toString());
+ logOrCheck(TYPE_COMMENT, getLast());
+ if (null != defaultHandler)
+ defaultHandler.comment(ch, start, length);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]