001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixxml.config.impl;
020:
021: /**
022: * Provides utility methods for rules reading property style values.
023: *
024: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
025: */
026: class PropertyUtil {
027: /**
028: * Parses a property style values unescaping special sequences.
029: *
030: * @param value string to parse
031: * @return value with unescaped special sequences
032: */
033: static String unesacpePropertyValue(String value) {
034: StringBuffer newValue = new StringBuffer(value.length());
035: char aChar;
036: int off = 0;
037: int end = value.length();
038:
039: while (off < end) {
040: aChar = value.charAt(off++);
041: if (aChar == '\\') {
042: aChar = value.charAt(off++);
043: if (aChar == 'u') {
044: // Read the xxxx
045: int val = 0;
046: for (int i = 0; i < 4; i++) {
047: try {
048: aChar = value.charAt(off++);
049: } catch (StringIndexOutOfBoundsException e) {
050: throw new IllegalArgumentException(
051: "Malformed \\uxxxx encoding.");
052: }
053: switch (aChar) {
054: case '0':
055: case '1':
056: case '2':
057: case '3':
058: case '4':
059: case '5':
060: case '6':
061: case '7':
062: case '8':
063: case '9':
064: val = (val << 4) + aChar - '0';
065: break;
066: case 'a':
067: case 'b':
068: case 'c':
069: case 'd':
070: case 'e':
071: case 'f':
072: val = (val << 4) + 10 + aChar - 'a';
073: break;
074: case 'A':
075: case 'B':
076: case 'C':
077: case 'D':
078: case 'E':
079: case 'F':
080: val = (val << 4) + 10 + aChar - 'A';
081: break;
082: default:
083: throw new IllegalArgumentException(
084: "Malformed \\uxxxx encoding.");
085: }
086: }
087: newValue.append((char) val);
088: } else {
089: if (aChar == 't')
090: aChar = '\t';
091: else if (aChar == 'r')
092: aChar = '\r';
093: else if (aChar == 'n')
094: aChar = '\n';
095: else if (aChar == 'f')
096: aChar = '\f';
097: newValue.append(aChar);
098: }
099: } else {
100: newValue.append(aChar);
101: }
102: }
103:
104: return newValue.toString();
105: }
106:
107: }
|