• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id$
20  */
21 
22 /*
23  *
24  * LoggingContentHandler.java
25  *
26  */
27 package org.apache.qetest.xsl;
28 
29 import org.apache.qetest.Logger;
30 import org.apache.qetest.LoggingHandler;
31 import org.xml.sax.Attributes;
32 import org.xml.sax.ContentHandler;
33 import org.xml.sax.Locator;
34 import org.xml.sax.SAXException;
35 
36 /**
37  * Cheap-o ContentHandler for use by API tests.
38  * <p>Implements ContentHandler and dumps simplistic info
39  * everything to a Logger; a way to debug SAX stuff.</p>
40  * @author shane_curcuru@lotus.com
41  * @version $Id$
42  */
43 public class LoggingContentHandler extends LoggingHandler implements ContentHandler
44 {
45 
46     /** No-op sets logger to default.  */
LoggingContentHandler()47     public LoggingContentHandler()
48     {
49         setLogger(getDefaultLogger());
50     }
51 
52     /**
53      * Ctor that calls setLogger automatically.
54      *
55      * @param r Logger we should log to
56      */
LoggingContentHandler(Logger l)57     public LoggingContentHandler(Logger l)
58     {
59         setLogger(l);
60     }
61 
62 
63     /**
64      * Our default handler that we pass all events through to.
65      */
66     protected ContentHandler defaultHandler = null;
67 
68 
69     /**
70      * Set a default handler for us to wrapper.
71      * Set a ContentHandler for us to use.
72      *
73      * @param default Object of the correct type to pass-through to;
74      * throws IllegalArgumentException if null or incorrect type
75      */
setDefaultHandler(Object defaultC)76     public void setDefaultHandler(Object defaultC)
77     {
78         try
79         {
80             defaultHandler = (ContentHandler)defaultC;
81         }
82         catch (Throwable t)
83         {
84             throw new java.lang.IllegalArgumentException("setDefaultHandler illegal type: " + t.toString());
85         }
86     }
87 
88 
89     /**
90      * Accessor method for our default handler.
91      *
92      * @return default (Object) our default handler; null if unset
93      */
getDefaultHandler()94     public Object getDefaultHandler()
95     {
96         return (Object)defaultHandler;
97     }
98 
99 
100     /** Prefixed to all logger msg output.  */
101     public final String prefix = "LCH:";
102 
103 
104     /** Cheap-o string representation of last event we got.  */
105     protected String lastItem = NOTHING_HANDLED;
106 
107 
108     /**
109      * Accessor for string representation of last event we got.
110      * @param s string to set
111      */
setLastItem(String s)112     protected void setLastItem(String s)
113     {
114         lastItem = s;
115     }
116 
117 
118     /**
119      * Accessor for string representation of last event we got.
120      * @return last event string we had
121      */
getLast()122     public String getLast()
123     {
124         return lastItem;
125     }
126 
127 
128     /** setExpected, etc. not yet implemented.  */
129 
130 
131     /** How many characters to report from characters event.  */
132     private int charLimit = 30;
133 
134 
135     /**
136      * How many characters to report from characters event.
137      * @param l charLimit for us to use
138      */
setCharLimit(int l)139     public void setCharLimit(int l)
140     {
141         charLimit = l;
142     }
143 
144 
145     /**
146      * How many characters to report from characters event.
147      * @return charLimit we use
148      */
getCharLimit()149     public int getCharLimit()
150     {
151         return charLimit;
152     }
153 
154 
155     ////////////////// Implement ContentHandler //////////////////
156     protected Locator ourLocator = null;
157 
setDocumentLocator(Locator locator)158     public void setDocumentLocator (Locator locator)
159     {
160         // Note: this implies this class is !not! threadsafe
161         setLastItem("setDocumentLocator");
162         ourLocator = locator; // future use
163         logger.logMsg(level, prefix + getLast());
164         if (null != defaultHandler)
165             defaultHandler.setDocumentLocator(locator);
166     }
167 
168 
startDocument()169     public void startDocument ()
170         throws SAXException
171     {
172         setLastItem("startDocument");
173         logger.logMsg(level, prefix + getLast());
174         if (null != defaultHandler)
175             defaultHandler.startDocument();
176     }
177 
178 
endDocument()179     public void endDocument()
180         throws SAXException
181     {
182         setLastItem("endDocument");
183         logger.logMsg(level, prefix + getLast());
184         if (null != defaultHandler)
185             defaultHandler.endDocument();
186     }
187 
188 
startPrefixMapping(String prefix, String uri)189     public void startPrefixMapping (String prefix, String uri)
190         throws SAXException
191     {
192         setLastItem("startPrefixMapping: " + prefix + ", " + uri);
193         logger.logMsg(level, prefix + getLast());
194         if (null != defaultHandler)
195             defaultHandler.startPrefixMapping(prefix, uri);
196     }
197 
198 
endPrefixMapping(String prefix)199     public void endPrefixMapping (String prefix)
200         throws SAXException
201     {
202         setLastItem("endPrefixMapping: " + prefix);
203         logger.logMsg(level, prefix + getLast());
204         if (null != defaultHandler)
205             defaultHandler.endPrefixMapping(prefix);
206     }
207 
208 
startElement(String namespaceURI, String localName, String qName, Attributes atts)209     public void startElement (String namespaceURI, String localName,
210                                                         String qName, Attributes atts)
211         throws SAXException
212     {
213         StringBuffer buf = new StringBuffer();
214         buf.append("startElement: " + namespaceURI + ", "
215                    + namespaceURI + ", " + qName);
216 
217         int n = atts.getLength();
218         for(int i = 0; i < n; i++)
219         {
220             buf.append(", " + atts.getQName(i));
221         }
222         setLastItem(buf.toString());
223         logger.logMsg(level, prefix + getLast());
224         if (null != defaultHandler)
225             defaultHandler.startElement(namespaceURI, localName, qName, atts);
226     }
227 
228 
endElement(String namespaceURI, String localName, String qName)229     public void endElement (String namespaceURI, String localName, String qName)
230         throws SAXException
231     {
232         setLastItem("endElement: " + namespaceURI + ", " + namespaceURI + ", " + qName);
233         logger.logMsg(level, prefix + getLast());
234         if (null != defaultHandler)
235             defaultHandler.endElement(namespaceURI, localName, qName);
236     }
237 
238 
characters(char ch[], int start, int length)239     public void characters (char ch[], int start, int length)
240         throws SAXException
241     {
242         String s = new String(ch, start, (length > charLimit) ? charLimit : length);
243         if(length > charLimit)
244             setLastItem("characters: \"" + s + "\"...");
245         else
246             setLastItem("characters: \"" + s + "\"");
247         logger.logMsg(level, prefix + getLast());
248         if (null != defaultHandler)
249             defaultHandler.characters(ch, start, length);
250     }
251 
252 
ignorableWhitespace(char ch[], int start, int length)253     public void ignorableWhitespace (char ch[], int start, int length)
254         throws SAXException
255     {
256         setLastItem("ignorableWhitespace: len " + length);
257         logger.logMsg(level, prefix + getLast());
258         if (null != defaultHandler)
259             defaultHandler.ignorableWhitespace(ch, start, length);
260     }
261 
262 
processingInstruction(String target, String data)263     public void processingInstruction (String target, String data)
264         throws SAXException
265     {
266         setLastItem("processingInstruction: " + target + ", " + data);
267         logger.logMsg(level, prefix + getLast());
268         if (null != defaultHandler)
269             defaultHandler.processingInstruction(target, data);
270     }
271 
272 
skippedEntity(String name)273     public void skippedEntity (String name)
274         throws SAXException
275     {
276         setLastItem("skippedEntity: " + name);
277         logger.logMsg(level, prefix + getLast());
278         if (null != defaultHandler)
279             defaultHandler.skippedEntity(name);
280     }
281 
282 }
283