Source code: com/nwalsh/saxon/LineCountEmitter.java
1 package com.nwalsh.saxon;
2
3 import org.xml.sax.*;
4 import javax.xml.transform.TransformerException;
5 import com.icl.saxon.output.*;
6 import com.icl.saxon.om.*;
7 import com.icl.saxon.expr.FragmentValue;
8
9 /**
10 * <p>Saxon extension to count the lines in a result tree fragment.</p>
11 *
12 *
13 * <p>Copyright (C) 2000 Norman Walsh.</p>
14 *
15 * <p>This class provides a
16 * <a href="http://users.iclway.co.uk/mhkay/saxon/">Saxon 6.*</a>
17 * implementation to count the number of lines in a result tree
18 * fragment.</p>
19 *
20 * <p>The general design is this: the stylesheets construct a result tree
21 * fragment for some verbatim environment. That result tree fragment
22 * is "replayed" through the LineCountEmitter; the LineCountEmitter watches
23 * characters go by and counts the number of line feeds that it sees.
24 * That number is then returned.</p>
25 *
26 * <p><b>Change Log:</b></p>
27 * <dl>
28 * <dt>1.0</dt>
29 * <dd><p>Initial release.</p></dd>
30 * </dl>
31 *
32 * @see Verbatim
33 *
34 * @author Norman Walsh
35 * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
36 *
37 *
38 */
39 public class LineCountEmitter extends com.icl.saxon.output.Emitter {
40 /** The number of lines seen. */
41 protected int numLines = 0;
42
43 /** Construct a new LineCountEmitter. */
44 public LineCountEmitter() {
45 numLines = 0;
46 }
47
48 /** Reset the number of lines. */
49 public void reset() {
50 numLines = 0;
51 }
52
53 /** Return the number of lines. */
54 public int lineCount() {
55 return numLines;
56 }
57
58 /** Process characters. */
59 public void characters(char[] chars, int start, int len)
60 throws javax.xml.transform.TransformerException {
61
62 if (numLines == 0) {
63 // If there are any characters at all, there's at least one line
64 numLines++;
65 }
66
67 for (int count = start; count < start+len; count++) {
68 if (chars[count] == '\n') {
69 numLines++;
70 }
71 }
72 }
73
74 /** Discarded. */
75 public void comment(char[] chars, int start, int length)
76 throws javax.xml.transform.TransformerException {
77 // nop
78 }
79
80 /** Discarded. */
81 public void endDocument()
82 throws javax.xml.transform.TransformerException {
83 // nop
84 }
85
86 /** Discarded. */
87 public void endElement(int nameCode)
88 throws javax.xml.transform.TransformerException {
89 // nop
90 }
91
92 /** Discarded. */
93 public void processingInstruction(java.lang.String name,
94 java.lang.String data)
95 throws javax.xml.transform.TransformerException {
96 // nop
97 }
98
99 /** Discarded. */
100 public void setDocumentLocator(org.xml.sax.Locator locator) {
101 // nop
102 }
103
104 /** Discarded. */
105 public void setEscaping(boolean escaping)
106 throws javax.xml.transform.TransformerException {
107 // nop
108 }
109
110 /** Discarded. */
111 public void setNamePool(NamePool namePool) {
112 // nop
113 }
114
115 /** Discarded. */
116 public void setUnparsedEntity(java.lang.String name, java.lang.String uri)
117 throws javax.xml.transform.TransformerException {
118 // nop
119 }
120
121 /** Discarded. */
122 public void setWriter(java.io.Writer writer) {
123 // nop
124 }
125
126 /** Discarded. */
127 public void startDocument()
128 throws javax.xml.transform.TransformerException {
129 // nop
130 }
131
132 /** Discarded. */
133 public void startElement(int nameCode,
134 org.xml.sax.Attributes attributes,
135 int[] namespaces, int nscount)
136 throws javax.xml.transform.TransformerException {
137 // nop
138 }
139 }