001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.utils;
007:
008: import java.io.IOException;
009: import java.io.Writer;
010:
011: /**
012: * A filter presenting a <code>Writer</code> that performs
013: * word substitution (search and replace) on the fly.
014: *
015: * 7/25/05 - UP-1180 - dmindler@rutgers.edu
016: * Modified to make use of optimized SubstitutionIntegerFilter
017: *
018: * @author Peter Kharchenko {@link <a href="mailto:pkharchenko@interactivebusiness.com"">pkharchenko@interactivebusiness.com"</a>}
019: * @version $Revision: 36690 $
020: */
021: public class SubstitutionWriter extends Writer {
022: SubstitutionIntegerFilter filter;
023:
024: /**
025: * Creates a new <code>SubstitutionWriter</code> instance.
026: *
027: * @param out a true <code>Writer</code> value where processed stream should be directed
028: * @param target a <code>byte[]</code> value of a target to be replaced
029: * @param substitute a <code>byte[]</code> value with which the target will be replaced
030: * @param bufferSize a size of the buffer
031: */
032: public SubstitutionWriter(Writer out, char[] target,
033: char[] substitute, int bufferSize) {
034: filter = new SubstitutionIntegerFilter(out, target, substitute,
035: bufferSize);
036: }
037:
038: /**
039: * Creates a new <code>SubstitutionWriter</code> instance.
040: *
041: * @param out a true <code>Writer</code> value where processed stream should be directed
042: * @param target a <code>byte[]</code> value of a target to be replaced
043: * @param substitute a <code>byte[]</code> value with which the target will be replaced
044: */
045: public SubstitutionWriter(Writer out, char[] target,
046: char[] substitute) {
047: filter = new SubstitutionIntegerFilter(out, target, substitute);
048: }
049:
050: public void write(int i) throws IOException {
051: filter.write((char) i);
052: }
053:
054: public void flush() throws IOException {
055: filter.flush();
056: }
057:
058: public void close() throws IOException {
059: filter.close();
060: }
061:
062: public void write(char[] cbuf, int off, int len) throws IOException {
063: // check boundaries
064: if (off + len > cbuf.length)
065: throw new IOException("Invalid offsent or length specified");
066:
067: for (int i = 0; i < len; i++) {
068: filter.write(cbuf[i]);
069: }
070: }
071:
072: /**
073: * A helper method to convert char array to int array.
074: * I am sure there's a way to cast it correctly, but I don't want to take my chances :)
075: * @param c a <code>char[]</code> value
076: * @return an <code>int[]</code> value
077: */
078: private static int[] getIntArrayFromCharArray(char[] c) {
079: int[] ic = new int[c.length];
080: for (int i = 0; i < c.length; i++) {
081: ic[i] = (int) c[i];
082: }
083: return ic;
084: }
085:
086: /**
087: * A test self-test method for the class.
088: *
089: */
090: public static void main(String[] args) {
091: // construct a string
092: String inputString = "Marry had a little lamb, little lamb, little lamb.";
093:
094: // set out the sink
095: java.io.StringWriter sw = new java.io.StringWriter();
096: SubstitutionWriter substw = new SubstitutionWriter(sw,
097: (new String("lamb")).toCharArray(),
098: (new String("rump")).toCharArray());
099: try {
100: substw.write(inputString);
101: substw.flush();
102: String resultString = sw.toString();
103: if (resultString
104: .equals("Marry had a little rump, little rump, little rump.")) {
105: System.out.println("Test passed.");
106: } else {
107: System.out.println("Test failed!");
108: }
109: } catch (Exception e) {
110: System.out.println("Test failed:");
111: e.printStackTrace();
112: }
113:
114: }
115:
116: }
|