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:
27: import java.util.regex.Matcher;
28: import java.util.regex.Pattern;
29:
30: /**
31: * Detect XML 1.0 character entity references.
32: *
33: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
34: * @version $Revision: 8784 $
35: */
36: public class XML1_0CharacterEntityReferenceParser extends
37: AbstractParser {
38:
39: private final TextEvent textEvent = new TextEvent();
40: private final CharacterEntityReferenceEvent cere = new CharacterEntityReferenceEvent();
41:
42: private static Pattern pattern = Pattern
43: .compile("&#(?:(?:x[0-9a-fA-F]{1,4})|(?:[0-9]{1,5}));");
44:
45: public void parse(char[] chars, int offset, int length) {
46: CharSequence seq = new CharSequenceImpl(chars, offset, length);
47: Matcher matcher = pattern.matcher(seq);
48: int from = offset;
49: while (matcher.find()) {
50: int to = matcher.start();
51: textEvent.setText(chars, from, to - from);
52: handler.handle(textEvent);
53: from = matcher.end();
54: cere.setText(chars, to, from - to);
55: handler.handle(cere);
56: }
57: if (from < offset + length) {
58: textEvent.setText(chars, from, offset + length - from);
59: handler.handle(textEvent);
60: }
61: }
62:
63: private static class CharSequenceImpl implements CharSequence {
64: private final char[] chars;
65: private final int offset;
66: private final int length;
67:
68: public CharSequenceImpl(char[] chars, int offset, int length) {
69: this .chars = chars;
70: this .offset = offset;
71: this .length = length;
72: }
73:
74: public int length() {
75: return length;
76: }
77:
78: public char charAt(int index) {
79: return chars[offset + index];
80: }
81:
82: public CharSequence subSequence(int start, int end) {
83: return new CharSequenceImpl(chars, start, end - start);
84: }
85: }
86: }
|