01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10: package org.mmbase.util.transformers;
11:
12: import java.io.Reader;
13: import java.io.Writer;
14:
15: import org.mmbase.util.logging.*;
16:
17: /**
18: * Rot13 implementation. Letter a-m are shifted 13 positions forward, n-z 13 backwards and other
19: * characters are untouched, which results in scrambled - but easily decoded - strings.
20: *
21: * @author Michiel Meeuwissen
22: * @since MMBase-1.8
23: */
24:
25: public class Rot13 extends ReaderTransformer implements CharTransformer {
26:
27: private static final Logger log = Logging
28: .getLoggerInstance(Rot13.class);
29:
30: protected Writer rot13(Reader r, Writer w) {
31: try {
32: int c = r.read();
33: while (c != -1) {
34: if (c >= 'a' && c <= 'm') {
35: c += 13;
36: } else if (c >= 'n' && c <= 'z') {
37: c -= 13;
38: } else if (c >= 'A' && c <= 'M') {
39: c += 13;
40: } else if (c >= 'A' && c <= 'Z') {
41: c -= 13;
42: }
43: w.write(c);
44: c = r.read();
45: }
46: } catch (java.io.IOException ioe) {
47: log.error(ioe);
48: }
49: return w;
50: }
51:
52: public Writer transform(Reader r, Writer w) {
53: return rot13(r, w);
54: }
55:
56: /**
57: * For Rot13, transformBack does the same as {@link #transform}
58: **/
59: public Writer transformBack(Reader r, Writer w) {
60: return rot13(r, w);
61: }
62:
63: public String toString() {
64: return "ROT-13";
65: }
66: }
|