KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > LineNumberSAXBuilderDemo


1 /*--
2
3  Copyright 2004 Jason Hunter. All rights reserved.
4
5  Redistribution and use in source and binary forms, with or without modifica-
6  tion, are permitted provided that the following conditions are met:
7
8  1. Redistributions of source code must retain the above copyright notice,
9     this list of conditions, and the following disclaimer.
10
11  2. Redistributions in binary form must reproduce the above copyright notice,
12     this list of conditions, the disclaimer that follows these conditions,
13     and/or other materials provided with the distribution.
14
15  3. The names "JDOM" and "Java Document Object Model" must not be used to
16     endorse or promote products derived from this software without prior
17     written permission. For written permission, please contact
18     license@jdom.org.
19
20  4. Products derived from this software may not be called "JDOM", nor may
21     "JDOM" appear in their name, without prior written permission from the
22     JDOM Project Management (pm@jdom.org).
23
24  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
25  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
26  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  JDOM PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35  This software consists of voluntary contributions made by many individuals
36  on behalf of the Java Document Object Model Project and was originally
37  created by Brett McLaughlin <brett@jdom.org> and
38  Jason Hunter <jhunter@jdom.org>. For more information on the JDOM
39  Project, please see <http://www.jdom.org/>.
40
41  */

42
43 import java.io.StringReader JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 import org.jdom.Document;
47 import org.jdom.input.SAXBuilder;
48 import org.jdom.filter.*;
49
50 import org.jdom.contrib.input.*;
51
52 /**
53  * @author Per Norrman
54  *
55  */

56 public class LineNumberSAXBuilderDemo
57 {
58
59     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
60         SAXBuilder builder = new LineNumberSAXBuilder();
61         Document doc = builder.build(new StringReader JavaDoc(xml));
62
63         for (Iterator JavaDoc iter = doc.getDescendants(new ElementFilter());
64          iter.hasNext(); ) {
65             LineNumberElement e = (LineNumberElement) iter.next();
66             System.out.println(
67                 e.getName() + ": lines " + e.getStartLine() + " to " + e.getEndLine());
68         }
69
70     }
71
72     private static String JavaDoc xml =
73         "<a>\n<b/>\n<c/>\n<d>\n<e/>\n<f/>\n</d>\n</a>\n";
74
75 }
76
Popular Tags