curcuru 01/03/08 17:10:09
Modified: test/java/src/org/apache/qetest/trax LoggingURIResolver.java
TransformerAPITest.java
Log:
Update LoggingURIResolver to use LoggingHandler base class;
minor API changes and implements setExpected() in two ways
Revision Changes Path
1.3 +199 -110
xml-xalan/test/java/src/org/apache/qetest/trax/LoggingURIResolver.java
Index: LoggingURIResolver.java
===================================================================
RCS file:
/home/cvs/xml-xalan/test/java/src/org/apache/qetest/trax/LoggingURIResolver.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- LoggingURIResolver.java 2000/11/09 22:12:38 1.2
+++ LoggingURIResolver.java 2001/03/09 01:10:06 1.3
@@ -66,9 +66,11 @@
import javax.xml.transform.Source;
import javax.xml.transform.URIResolver;
import javax.xml.transform.TransformerException;
+import javax.xml.transform.stream.StreamSource;
-import org.w3c.dom.Node;
+import org.apache.xml.utils.SystemIDResolver;
+import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
@@ -78,186 +80,268 @@
* Implementation of URIResolver that logs all calls.
* Currently just provides default service; returns null.
* @author [EMAIL PROTECTED]
- * @version $Id: LoggingURIResolver.java,v 1.2 2000/11/09 22:12:38 curcuru
Exp $
+ * @version $Id: LoggingURIResolver.java,v 1.3 2001/03/09 01:10:06 curcuru
Exp $
*/
-public class LoggingURIResolver implements URIResolver
+public class LoggingURIResolver extends LoggingHandler implements URIResolver
{
- /** No-op ctor since it's often useful to have one. */
- public LoggingURIResolver(){}
+ /** No-op sets logger to default. */
+ public LoggingURIResolver()
+ {
+ setLogger(getDefaultLogger());
+ }
/**
- * Ctor that calls setReporter automatically.
+ * Ctor that calls setLogger automatically.
*
- * NEEDSDOC @param r
+ * @param l Logger we should log to
*/
- public LoggingURIResolver(Reporter r)
+ public LoggingURIResolver(Logger l)
{
- setReporter(r);
+ setLogger(l);
}
- /** Our Reporter, who we tell all our secrets to. */
- private Reporter reporter;
/**
- * Accesor methods for our Reporter.
+ * Our default handler that we pass all events through to.
+ */
+ protected URIResolver defaultHandler = null;
+
+
+ /**
+ * Set a default handler for us to wrapper.
+ * Set a URIResolver for us to use.
+ * // Note that we don't currently have a default URIResolver,
+ * // so the LoggingURIResolver class will just attempt
+ * // to use the SystemIDResolver class instead
*
- * NEEDSDOC @param r
+ * @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 defaultU)
{
- if (r != null)
- reporter = r;
+ try
+ {
+ defaultHandler = (URIResolver)defaultU;
+ }
+ catch (Throwable t)
+ {
+ throw new java.lang.IllegalArgumentException("setDefaultHandler
illegal type: " + t.toString());
+ }
}
+
/**
- * Accesor methods for our Reporter.
+ * Accessor method for our default handler.
*
- * NEEDSDOC ($objectName$) @return
+ * @return default (Object) our default handler; null if unset
*/
- public Reporter getReporter()
+ public Object getDefaultHandler()
{
- return (reporter);
+ return (Object)defaultHandler;
}
- /** Prefixed to all reporter msg output. */
- private String prefix = "UR:";
+
+ /** Prefixed to all logger msg output. */
+ public static final String prefix = "LUR:";
+
+
+ /**
+ * Counter for how many URIs we've resolved.
+ */
+ protected int[] counters = { 0 };
- /** Counters for how many URIs we've 'resolved'. */
- private int URICtr = 0;
/**
- * Accesor methods for URI counter.
+ * Get a list of counters of all items we've logged.
+ * Only a single array item is returned.
*
- * NEEDSDOC ($objectName$) @return
+ * @return array of int counter for each item we log
*/
- public int getURICtr()
+ public int[] getCounters()
{
- return URICtr;
+ return counters;
}
+
/**
- * Cheap-o string representation of our state.
+ * Really Cheap-o string representation of our state.
*
- * NEEDSDOC ($objectName$) @return
+ * @return String of getCounters() rolled up in minimal space
*/
- public String getCounterString()
+ public String getQuickCounters()
{
- return (prefix + "URIs: " + getURICtr());
+ return (prefix + "(" + counters[0] + ")");
}
+
- /** Cheap-o string representation of last entity we resolved. */
- private String lastURI = null;
+ /** Cheap-o string representation of last URI we resolved. */
+ protected String lastItem = NOTHING_HANDLED;
+
/**
- * NEEDSDOC Method setLastURI
- *
- *
- * NEEDSDOC @param s
+ * Accessor for string representation of last event we got.
+ * @param s string to set
*/
- protected void setLastURI(String s)
+ protected void setLastItem(String s)
{
- lastURI = s;
+ lastItem = s;
}
+
/**
- * Accessor for string representation of last entity we resolved.
- *
- * NEEDSDOC ($objectName$) @return
+ * Accessor for string representation of last event we got.
+ * @return last event string we had
*/
- public String getLastURI()
+ public String getLast()
{
- return lastURI;
+ return lastItem;
}
+
- /** What loggingLevel to use for reporter.logMsg(). */
- private int level = Reporter.DEFAULT_LOGGINGLEVEL;
+ /** Expected value(s) for URIs we may resolve, default=ITEM_DONT_CARE. */
+ protected String[] expected = { ITEM_DONT_CARE };
+
+ /** Counter used when expected is an ordered array. */
+ protected int expectedCtr = 0;
+
+
/**
- * Accesor methods; don't think it needs to be synchronized.
+ * Ask us to report checkPass/Fail for certain URIs we resolve.
*
- * NEEDSDOC @param l
+ * @param itemType ignored, we only do one type
+ * @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 void setLoggingLevel(int l)
+ public void setExpected(int itemType, String containsString)
{
- level = l;
+ // Default to don't care on null
+ if (null == containsString)
+ containsString = ITEM_DONT_CARE;
+
+ expected = new String[1];
+ expected[0] = containsString;
}
/**
- * Accesor methods; don't think it needs to be synchronized.
+ * Ask us to report checkPass/Fail for an ordered list of URIs
+ * we may resolve.
*
- * NEEDSDOC ($objectName$) @return
+ * Users can specify an array of expected URIs we should be
+ * resolving in order. Both the specific items and the exact
+ * order must occour for us to call checkPass for each URI;
+ * we call checkFail for any URI that doesn't match or is out
+ * of order. After we run off the end of the array, we
+ * go back to the defaul of ITEM_DONT_CARE.
+ * Reset by reset(), of course.
+ *
+ * @param containsStrings[] and array of items to look for in
+ * order: this allows you to test a stylesheet that has
+ * three xsl:imports, for example
*/
- public int getLoggingLevel()
+ public void setExpected(String[] containsStrings)
{
- return level;
+ // Default to don't care on null
+ if ((null == containsStrings) || (0 == containsStrings.length))
+ {
+ expected = new String[1];
+ expected[0] = ITEM_DONT_CARE;
+ }
+ else
+ {
+ expected = new String[containsStrings.length];
+ System.arraycopy(containsStrings, 0, expected, 0,
containsStrings.length);
+ }
+ expectedCtr = 0;
}
/**
- * Cheap-o utility to get a string value.
- * @todo improve string return value
- *
- * NEEDSDOC @param i
+ * Cheap-o worker method to get a string value.
+ * //@todo improve string return value
*
- * NEEDSDOC ($objectName$) @return
+ * @param i InputSource to get a string from
+ * @return some String representation thereof
*/
private String getString(InputSource i)
{
return i.toString();
}
+
/**
- * Implement this method: just returns null for now.
- * Also saves the last entity for later retrieval, and counts
- * how many entities we've 'resolved' overall.
- * @todo have a settable property to actually return as the InputSource
- * @param inputSource The value returned from the EntityResolver.
- * @return (null currently) a DOM node that represents the resolution of
the URI
- * @exception TransformerException never thrown currently
+ * Reset all items or counters we've handled.
*/
- public Node getDOMNode(InputSource inputSource) throws
TransformerException
+ public void reset()
{
-
- URICtr++;
-
- setLastURI(getString(inputSource));
-
- if (reporter != null)
- {
- reporter.logMsg(level,
- prefix + getLastURI() + " " +
getCounterString());
- }
-
- return null;
+ setLastItem(NOTHING_HANDLED);
+ counters[0] = 0;
+ expected = new String[1];
+ expected[0] = ITEM_DONT_CARE;
+ expectedCtr = 0;
}
+
/**
- * Implement this method: just returns null for now.
- * Also saves the last entity for later retrieval, and counts
- * how many entities we've 'resolved' overall.
- * @todo have a settable property to actually return as the InputSource
- * @param inputSource The value returned from the EntityResolver.
- * @return (null currently) a SAX2 parser to use with the InputSource.
- * @exception TransformerException never thrown currently
+ * Worker method to either log or call check* for this event.
+ * A simple way to validate for any kind of event.
+ *
+ * @param desc detail info from this kind of message
*/
- public XMLReader getXMLReader(InputSource inputSource)
- throws TransformerException
+ protected void logOrCheck(String desc)
{
-
- URICtr++;
-
- setLastURI(getString(inputSource));
-
- if (reporter != null)
+ String tmp = getQuickCounters() + " " + desc;
+ if (expectedCtr > expected.length)
{
- reporter.logMsg(level,
- prefix + getLastURI() + " " +
getCounterString());
+ // Sanity check: prevent AIOOBE
+ expectedCtr = expected.length;
+ logger.logMsg(Logger.WARNINGMSG, getQuickCounters()
+ + " error: array overbounds " + expectedCtr);
}
-
- return null;
+ // Either log the exception or call checkPass/checkFail
+ // as requested by setExpected for this type
+ if (ITEM_DONT_CARE == expected[expectedCtr])
+ {
+ // We don't care about this, just log it
+ logger.logMsg(level, tmp);
+ }
+ else if (ITEM_CHECKFAIL == expected[expectedCtr])
+ {
+ // We shouldn't have been called here, so fail
+ logger.checkFail(tmp + " was unexpected");
+ }
+ else if ((null != desc)
+ && (desc.indexOf(expected[expectedCtr]) > -1))
+ {
+ // We got a warning the user expected, so pass
+ logger.checkPass(tmp + " matched");
+ // Also reset this counter
+ expected[expectedCtr] = 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[expectedCtr] = ITEM_DONT_CARE;
+ }
+ // If we have a list of expected items, increment
+ if (expected.length > 1)
+ {
+ expectedCtr++;
+ // If we run off the end, reset all expected
+ if (expectedCtr >= expected.length)
+ {
+ expected = new String[1];
+ expected[0] = ITEM_DONT_CARE;
+ expectedCtr = 0;
+ }
+ }
}
+ ////////////////// Implement URIResolver //////////////////
/**
* This will be called by the processor when it encounters
* an xsl:include, xsl:import, or document() function.
@@ -272,18 +356,23 @@
public Source resolve(String href, String base)
throws TransformerException
{
-
- URICtr++;
-
- setLastURI("{" + base + "}" + href);
-
- if (reporter != null)
+ counters[0]++;
+ setLastItem("{" + base + "}" + href);
+ logOrCheck(getLast());
+ if (null != defaultHandler)
{
- reporter.logMsg(level,
- prefix + getLastURI() + " " +
getCounterString());
+ logger.logMsg(level, prefix + " resolved by: " + defaultHandler);
+ return defaultHandler.resolve(href, base);
}
-
- return null; // @todo do we need to return anything here?
-
+ else
+ {
+ // Note that we don't currently have a default URIResolver,
+ // so the LoggingURIResolver class will just attempt
+ // to use the SystemIDResolver class instead
+ String sysId = SystemIDResolver.getAbsoluteURI(href, base);
+ logger.logMsg(level, prefix + " resolved into new StreamSource("
+ + sysId + ")");
+ return new StreamSource(sysId);
+ }
}
}
1.12 +2 -2
xml-xalan/test/java/src/org/apache/qetest/trax/TransformerAPITest.java
Index: TransformerAPITest.java
===================================================================
RCS file:
/home/cvs/xml-xalan/test/java/src/org/apache/qetest/trax/TransformerAPITest.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- TransformerAPITest.java 2001/02/13 21:28:52 1.11
+++ TransformerAPITest.java 2001/03/09 01:10:07 1.12
@@ -1084,7 +1084,7 @@
LoggingURIResolver myURIResolver = new
LoggingURIResolver(reporter);
transformer.setURIResolver(myURIResolver);
reporter.checkObject(transformer.getURIResolver(),
myURIResolver, "set/getURIResolver API coverage");
- reporter.logTraceMsg("myURIres.getCounterString = " +
myURIResolver.getCounterString());
+ reporter.logTraceMsg("myURIres.getQuickCounters = " +
myURIResolver.getQuickCounters());
// Assumes we support Streams
FileOutputStream fos = new FileOutputStream(outNames.nextName());
@@ -1101,7 +1101,7 @@
)
reporter.logInfoMsg("transform(Stream, Stream) failure
reason:" + fileChecker.getExtendedInfo());
}
- reporter.logTraceMsg("myURIres.getCounterString = " +
myURIResolver.getCounterString());
+ reporter.logTraceMsg("myURIres.getQuickCounters = " +
myURIResolver.getQuickCounters());
reporter.logStatusMsg("@todo basic URIResolver functionality
test (i.e. does it get used in a transform)");
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]