01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.format.parser.entity;
23:
24: import org.jboss.portal.format.parser.AbstractParser;
25: import org.jboss.portal.format.parser.TextEvent;
26: import org.jboss.portal.format.util.EntityTable;
27:
28: /**
29: * Convert chars that have an an HTML 4 character reference.
30: *
31: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
32: * @version $Revision: 8784 $
33: */
34: public class HTML4_0CharacterEntityReferenceParser extends
35: AbstractParser {
36:
37: private final TextEvent textEvent = new TextEvent();
38: private final CharacterEntityReferenceEvent cere = new CharacterEntityReferenceEvent();
39: private final char[] buffer = new char[10];
40:
41: public void parse(char[] chars, int startOffset, int length) {
42: // Calculate the startOffset of the last chars to process
43: int endOffset = startOffset + length;
44: int previousOffset = startOffset;
45:
46: // Iterates over all the chars
47: for (int currentOffset = startOffset; currentOffset < endOffset; currentOffset++) {
48: char c = chars[currentOffset];
49: String result = EntityTable.FULL.lookup(c);
50: if (result != null) {
51: if (currentOffset > previousOffset) {
52: textEvent.setText(chars, previousOffset,
53: currentOffset - previousOffset);
54: handler.handle(textEvent);
55: }
56:
57: int size = result.length();
58: buffer[0] = '&';
59: result.getChars(0, size, buffer, 1);
60: buffer[size + 1] = ';';
61: cere.setText(buffer, 0, size + 2);
62: handler.handle(cere);
63: previousOffset = currentOffset + 1;
64: }
65: }
66:
67: // Send the last chunck
68: if (endOffset > previousOffset) {
69: textEvent.setText(chars, previousOffset, endOffset
70: - previousOffset);
71: handler.handle(textEvent);
72: }
73: }
74:
75: }
|