001: package com.nwalsh.saxon;
002:
003: import org.xml.sax.*;
004: import javax.xml.transform.TransformerException;
005: import com.icl.saxon.output.*;
006: import com.icl.saxon.om.*;
007: import com.icl.saxon.expr.FragmentValue;
008:
009: /**
010: * <p>Saxon extension to count the lines in a result tree fragment.</p>
011: *
012: * <p>$Id: LineCountEmitter.java,v 1.4 2005-08-30 08:14:58 draganr Exp $</p>
013: *
014: * <p>Copyright (C) 2000 Norman Walsh.</p>
015: *
016: * <p>This class provides a
017: * <a href="http://users.iclway.co.uk/mhkay/saxon/">Saxon 6.*</a>
018: * implementation to count the number of lines in a result tree
019: * fragment.</p>
020: *
021: * <p>The general design is this: the stylesheets construct a result tree
022: * fragment for some verbatim environment. That result tree fragment
023: * is "replayed" through the LineCountEmitter; the LineCountEmitter watches
024: * characters go by and counts the number of line feeds that it sees.
025: * That number is then returned.</p>
026: *
027: * <p><b>Change Log:</b></p>
028: * <dl>
029: * <dt>1.0</dt>
030: * <dd><p>Initial release.</p></dd>
031: * </dl>
032: *
033: * @see Verbatim
034: *
035: * @author Norman Walsh
036: * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
037: *
038: * @version $Id: LineCountEmitter.java,v 1.4 2005-08-30 08:14:58 draganr Exp $
039: *
040: */
041: public class LineCountEmitter extends com.icl.saxon.output.Emitter {
042: /** The number of lines seen. */
043: protected int numLines = 0;
044:
045: /** Construct a new LineCountEmitter. */
046: public LineCountEmitter() {
047: numLines = 0;
048: }
049:
050: /** Reset the number of lines. */
051: public void reset() {
052: numLines = 0;
053: }
054:
055: /** Return the number of lines. */
056: public int lineCount() {
057: return numLines;
058: }
059:
060: /** Process characters. */
061: public void characters(char[] chars, int start, int len)
062: throws javax.xml.transform.TransformerException {
063:
064: if (numLines == 0) {
065: // If there are any characters at all, there's at least one line
066: numLines++;
067: }
068:
069: for (int count = start; count < start + len; count++) {
070: if (chars[count] == '\n') {
071: numLines++;
072: }
073: }
074: }
075:
076: /** Discarded. */
077: public void comment(char[] chars, int start, int length)
078: throws javax.xml.transform.TransformerException {
079: // nop
080: }
081:
082: /** Discarded. */
083: public void endDocument()
084: throws javax.xml.transform.TransformerException {
085: // nop
086: }
087:
088: /** Discarded. */
089: public void endElement(int nameCode)
090: throws javax.xml.transform.TransformerException {
091: // nop
092: }
093:
094: /** Discarded. */
095: public void processingInstruction(java.lang.String name,
096: java.lang.String data)
097: throws javax.xml.transform.TransformerException {
098: // nop
099: }
100:
101: /** Discarded. */
102: public void setDocumentLocator(org.xml.sax.Locator locator) {
103: // nop
104: }
105:
106: /** Discarded. */
107: public void setEscaping(boolean escaping)
108: throws javax.xml.transform.TransformerException {
109: // nop
110: }
111:
112: /** Discarded. */
113: public void setNamePool(NamePool namePool) {
114: // nop
115: }
116:
117: /** Discarded. */
118: public void setUnparsedEntity(java.lang.String name,
119: java.lang.String uri)
120: throws javax.xml.transform.TransformerException {
121: // nop
122: }
123:
124: /** Discarded. */
125: public void setWriter(java.io.Writer writer) {
126: // nop
127: }
128:
129: /** Discarded. */
130: public void startDocument()
131: throws javax.xml.transform.TransformerException {
132: // nop
133: }
134:
135: /** Discarded. */
136: public void startElement(int nameCode,
137: org.xml.sax.Attributes attributes, int[] namespaces,
138: int nscount)
139: throws javax.xml.transform.TransformerException {
140: // nop
141: }
142: }
|