001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.nls;
011:
012: public class StringHelper {
013:
014: protected static String preparePropertiesString(String s,
015: char[] newLine) {
016: if (s == null)
017: return null;
018: int length = s.length();
019: int nlLength = newLine.length;
020: StringBuffer sb = new StringBuffer(length + nlLength);
021: for (int i = 0; i < length; i++) {
022: char c = s.charAt(i);
023: if (i + nlLength < length) {
024: boolean notNewLine = false;
025: for (int j = 0; j < nlLength; j++)
026: if (s.charAt(i + j) != newLine[j])
027: notNewLine = true;
028: if (!notNewLine)
029: sb.append("\\"); //$NON-NLS-1$
030: }
031: sb.append(c);
032: }
033: return sb.toString();
034: }
035:
036: protected static String unwindEscapeChars(String s) {
037: if (s != null) {
038: int length = s.length();
039: StringBuffer sb = new StringBuffer(length);
040: for (int i = 0; i < length; i++) {
041: char c = s.charAt(i);
042: sb.append(getUnwoundString(c));
043: }
044: return sb.toString();
045: }
046: return null;
047: }
048:
049: protected static String getUnwoundString(char c) {
050: switch (c) {
051: case '\b':
052: return "\\b";//$NON-NLS-1$
053: case '\t':
054: return "\\t";//$NON-NLS-1$
055: case '\n':
056: return "\\n";//$NON-NLS-1$
057: case '\f':
058: return "\\f";//$NON-NLS-1$
059: case '\r':
060: return "\\r";//$NON-NLS-1$
061: case '\\':
062: return "\\\\";//$NON-NLS-1$
063: }
064: return String.valueOf(c);
065: }
066:
067: protected static String windEscapeChars(String s) {
068: if (s == null)
069: return null;
070:
071: char aChar;
072: int len = s.length();
073: StringBuffer outBuffer = new StringBuffer(len);
074:
075: for (int x = 0; x < len;) {
076: aChar = s.charAt(x++);
077: if (aChar == '\\') {
078: aChar = s.charAt(x++);
079: if (aChar == 'u') {
080: // Read the xxxx
081: int value = 0;
082: for (int i = 0; i < 4; i++) {
083: aChar = s.charAt(x++);
084: switch (aChar) {
085: case '0':
086: case '1':
087: case '2':
088: case '3':
089: case '4':
090: case '5':
091: case '6':
092: case '7':
093: case '8':
094: case '9':
095: value = (value << 4) + aChar - '0';
096: break;
097: case 'a':
098: case 'b':
099: case 'c':
100: case 'd':
101: case 'e':
102: case 'f':
103: value = (value << 4) + 10 + aChar - 'a';
104: break;
105: case 'A':
106: case 'B':
107: case 'C':
108: case 'D':
109: case 'E':
110: case 'F':
111: value = (value << 4) + 10 + aChar - 'A';
112: break;
113: default:
114: throw new IllegalArgumentException(
115: "Malformed \\uxxxx encoding."); //$NON-NLS-1$
116: }
117: }
118: outBuffer.append((char) value);
119: } else {
120: if (aChar == 't') {
121: outBuffer.append('\t');
122: } else {
123: if (aChar == 'r') {
124: outBuffer.append('\r');
125: } else {
126: if (aChar == 'n') {
127: outBuffer.append('\n');
128: } else {
129: if (aChar == 'f') {
130: outBuffer.append('\f');
131: } else {
132: outBuffer.append(aChar);
133: }
134: }
135: }
136: }
137: }
138: } else
139: outBuffer.append(aChar);
140: }
141: return outBuffer.toString();
142: }
143:
144: protected static boolean isValidLocalization(String name) {
145: if (name.length() <= 0) {
146: return false;
147: }
148: for (int i = 0; i < name.length(); i++) {
149: char c = name.charAt(i);
150: if ((c < 'A' || 'Z' < c) && (c < 'a' || 'z' < c)
151: && (c < '0' || '9' < c) && c != '_' && c != '-'
152: && c != '/') {
153: return false;
154: }
155: }
156: return true;
157: }
158: }
|