001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.common;
010:
011: /**
012: * The class maskes special characters
013: *
014: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
015: * @version CVS $Id: Decoder.java,v 1.9 2003/12/10 16:30:35 benedikta Exp $
016: */
017: public class Decoder {
018: public static final String STRING = "\"";
019: public static final String CHAR = "\'";
020: public static final String CLASS = "[^- ]";
021: public static final String REGEX = "+*?^$() ";
022:
023: /**
024: * Mask special characters
025: *
026: * @param string String
027: *
028: * @return Masked string
029: */
030: public static String toString(String string) {
031: StringBuffer buffer = new StringBuffer();
032:
033: buffer.append("\"");
034: for (int i = 0; i < string.length(); i++)
035: buffer.append(decode(string.charAt(i), STRING));
036:
037: buffer.append("\"");
038: return buffer.toString();
039: }
040:
041: public static String toChar(char character) {
042: return "'" + decode(character, CHAR) + "'";
043: }
044:
045: public static String toClass(char minimum, char maximum) {
046: if (minimum == maximum)
047: return toChar(minimum);
048: else {
049: StringBuffer buffer = new StringBuffer();
050: buffer.append("[");
051: buffer.append(decode(minimum, CLASS));
052: buffer.append("-");
053: buffer.append(decode(maximum, CLASS));
054: buffer.append("]");
055: return buffer.toString();
056: }
057: }
058:
059: public static String toNegativeClass(char minimum, char maximum) {
060: if (minimum == maximum) {
061: StringBuffer buffer = new StringBuffer();
062: buffer.append("[^");
063: buffer.append(decode(minimum, CLASS));
064: buffer.append("]");
065: return buffer.toString();
066: } else {
067: StringBuffer buffer = new StringBuffer();
068: buffer.append("[^");
069: buffer.append(decode(minimum, CLASS));
070: buffer.append("-");
071: buffer.append(decode(maximum, CLASS));
072: buffer.append("]");
073: return buffer.toString();
074: }
075: }
076:
077: public static String decode(char character, String masked) {
078: if (character == '\n')
079: return "\\n";
080: else if (character == '\t')
081: return "\\t";
082: else if (character == '\r')
083: return "\\r";
084: else if (character == '\\')
085: return "\\\\";
086: else if ((character >= ' ') && (character <= '~')
087: && (masked.indexOf(character) < 0))
088: return String.valueOf(character);
089: else if ((character >= ' ') && (character <= '~'))
090: return "\\" + character;
091:
092: String hexstring = Integer.toHexString(character);
093: String zeros = "0000";
094:
095: return "\\u" + zeros.substring(hexstring.length()) + hexstring;
096: }
097:
098: public static String decode(String string, String masked) {
099: StringBuffer buffer = new StringBuffer();
100:
101: for (int i = 0; i < string.length(); i++)
102: buffer.append(decode(string.charAt(i), masked));
103:
104: return buffer.toString();
105: }
106: }
|