KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > format > parser > linebreak > LineBreakParser


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9
10 package org.jboss.portal.format.parser.linebreak;
11
12 import org.jboss.portal.format.parser.AbstractParser;
13 import org.jboss.portal.format.parser.ParseEvent;
14 import org.jboss.portal.format.parser.TextEvent;
15
16 /**
17  * Break the parsed chars into text events and line break events.
18  *
19  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
20  * @version $Revision: 1.1.1.1 $
21  */

22 public class LineBreakParser extends AbstractParser
23 {
24
25    private static final LineBreakEvent event = new LineBreakEvent();
26
27    private final TextEvent textEvent = new TextEvent();
28
29    public void parse(char[] chars, int startOffset, int length)
30    {
31       int endOffset = startOffset + length;
32       int previousOffset = startOffset;
33       for (int currentOffset = startOffset;currentOffset < endOffset;currentOffset++)
34       {
35          char c = chars[currentOffset];
36          if (c == '\n')
37          {
38             if (currentOffset > previousOffset)
39             {
40                textEvent.setText(chars, previousOffset, currentOffset - previousOffset);
41                handler.handle(textEvent);
42             }
43             handler.handle(event);
44             previousOffset = currentOffset + 1;
45          }
46       }
47       if (endOffset > previousOffset)
48       {
49          textEvent.setText(chars, previousOffset, endOffset - previousOffset);
50          handler.handle(textEvent);
51       }
52    }
53
54    public static class LineBreakEvent implements ParseEvent
55    {
56    }
57 }
58
Popular Tags