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.chars;
23:
24: import java.io.IOException;
25: import java.io.Reader;
26: import java.io.Writer;
27:
28: /**
29: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
30: * @version $Revision: 8784 $
31: */
32: public class StackFilter implements Filter {
33:
34: private Filter[] stack = new Filter[0];
35:
36: public StackFilter() {
37: }
38:
39: public void add(Filter filter) {
40: Filter[] tmp = new Filter[1 + stack.length];
41: System.arraycopy(stack, 0, tmp, 1, stack.length);
42: tmp[0] = filter;
43: stack = tmp;
44: }
45:
46: public void filter(Reader in, Writer out) throws IOException {
47: int n = stack.length - 1;
48: if (n > 1) {
49: MutableChars chars1 = new MutableChars();
50: stack[n].filter(in, chars1.getWriter());
51: MutableChars chars2 = new MutableChars();
52: while (n > 0) {
53: // Filter
54: stack[n].filter(chars1.getReader(), chars2.getWriter());
55: // Swap and reset
56: MutableChars tmp = chars1;
57: chars1 = chars2;
58: chars2 = tmp;
59: chars2.reset();
60: n--;
61: }
62: stack[0].filter(chars1.getReader(), out);
63: } else if (n == 1) {
64: MutableChars chars = new MutableChars();
65: stack[1].filter(in, chars.getWriter());
66: stack[0].filter(chars.getReader(), out);
67: } else if (n == 0) {
68: stack[0].filter(in, out);
69: }
70: }
71:
72: }
|