KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > format > parser > entity > HTML4_0CharacterEntityReferenceParser


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.entity;
11
12 import org.jboss.portal.format.parser.AbstractParser;
13 import org.jboss.portal.format.parser.TextEvent;
14 import org.jboss.portal.format.util.EntityTable;
15
16 /**
17  * Convert chars that have an an HTML 4 character reference.
18  *
19  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
20  * @version $Revision: 1.1.1.1 $
21  */

22 public class HTML4_0CharacterEntityReferenceParser extends AbstractParser
23 {
24
25    private final TextEvent textEvent = new TextEvent();
26    private final CharacterEntityReferenceEvent cere = new CharacterEntityReferenceEvent();
27    private final char[] buffer = new char[10];
28
29    public void parse(char[] chars, int startOffset, int length)
30    {
31       // Calculate the startOffset of the last chars to process
32
int endOffset = startOffset + length;
33       int previousOffset = startOffset;
34
35       // Iterates over all the chars
36
for (int currentOffset = startOffset;currentOffset < endOffset;currentOffset++)
37       {
38          char c = chars[currentOffset];
39          String JavaDoc result = EntityTable.FULL.lookup(c);
40          if (result != null)
41          {
42             if (currentOffset > previousOffset)
43             {
44                textEvent.setText(chars, previousOffset, currentOffset - previousOffset);
45                handler.handle(textEvent);
46             }
47
48             int size = result.length();
49             buffer[0] = '&';
50             result.getChars(0, size, buffer, 1);
51             buffer[size + 1] = ';';
52             cere.setText(buffer, 0, size + 2);
53             handler.handle(cere);
54             previousOffset = currentOffset + 1;
55          }
56       }
57
58       // Send the last chunck
59
if (endOffset > previousOffset)
60       {
61          textEvent.setText(chars, previousOffset, endOffset - previousOffset);
62          handler.handle(textEvent);
63       }
64    }
65
66 }
67
Popular Tags