001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.api.codegen;
042:
043: import org.openide.ErrorManager;
044:
045: /**
046: * @author David Kaspar
047: */
048: public final class CodeSupport {
049:
050: private CodeSupport() {
051: }
052:
053: public static String encryptStringToJavaCode(String value) {
054: if (value == null)
055: return null;
056: StringBuffer sb = new StringBuffer();
057: for (int i = 0; i < value.length(); i++) {
058: final char c = value.charAt(i);
059: switch (c) {
060: case '\\': // NOI18N
061: case '"': // NOI18N
062: case '\'': // NOI18N
063: sb.append('\\'); // NOI18N
064: sb.append(c);
065: break;
066: case '\r': // NOI18N
067: sb.append("\\r"); // NOI18N
068: break;
069: case '\n': // NOI18N
070: sb.append("\\n"); // NOI18N
071: break;
072: case '\t': // NOI18N
073: sb.append("\\t"); // NOI18N
074: break;
075: default:
076: if (c < 32) {
077: sb.append("\\"); // NOI18N
078: sb.append(alignWithZeros(Integer.toOctalString(c),
079: 3));
080: } else if (c >= 128) {
081: sb.append("\\u"); // NOI18N
082: sb.append(alignWithZeros(Integer.toHexString(c)
083: .toUpperCase(), 4));
084: } else
085: sb.append(c);
086: }
087: }
088: return sb.toString();
089: }
090:
091: public static String alignWithZeros(String value, int positions) {
092: if (value == null)
093: return null;
094: StringBuffer sb = new StringBuffer();
095: positions -= value.length();
096: while (positions > 0) {
097: sb.append('0'); // NOI18N
098: positions--;
099: }
100: if (positions < 0) // just to asure alignment to specified positions
101: return value.substring(-positions);
102: return sb.append(value).toString();
103: }
104:
105: public static String decryptStringFromJavaCode(String value) {
106: if (value == null)
107: return null;
108: final int len = value.length();
109: StringBuffer sb = new StringBuffer();
110: int i = 0;
111: while (i < len) {
112: char c = value.charAt(i);
113: i++;
114: if (c != '\\') { // NOI18N
115: sb.append(c);
116: continue;
117: }
118: c = value.charAt(i);
119: i++;
120: switch (c) {
121: case 'r': // NOI18N
122: sb.append('\r'); // NOI18N
123: break;
124: case 'n': // NOI18N
125: sb.append('\n'); // NOI18N
126: break;
127: case 't': // NOI18N
128: sb.append('\t'); // NOI18N
129: break;
130: case 'u': // NOI18N
131: if (i + 4 > len) {
132: ErrorManager.getDefault().log(
133: ErrorManager.INFORMATIONAL,
134: "WARNING: Invalid hex number at the end: "
135: + value.substring(i)); // NOI18N
136: break;
137: }
138: try {
139: sb.append((char) Integer.parseInt(value.substring(
140: i, i + 4), 16));
141: } catch (NumberFormatException e) {
142: ErrorManager.getDefault().log(
143: ErrorManager.INFORMATIONAL,
144: "WARNING: Invalid hex number format: "
145: + value.substring(i, i + 4)); // NOI18N
146: }
147: i += 4;
148: break;
149: case '"': // NOI18N
150: case '\'': // NOI18N
151: case '\\': // NOI18N
152: sb.append(c);
153: break;
154: default:
155: if (c < '0' || c > '9') { // NOI18N
156: ErrorManager.getDefault().log(
157: ErrorManager.INFORMATIONAL,
158: "WARNING: Invalid character after slash: "
159: + c); // NOI18N
160: break;
161: }
162: i--;
163: if (i + 3 > len) {
164: ErrorManager.getDefault().log(
165: ErrorManager.INFORMATIONAL,
166: "WARNING: Invalid octal number at the end: "
167: + value.substring(i)); // NOI18N
168: break;
169: }
170: try {
171: sb.append((char) Integer.parseInt(value.substring(
172: i, i + 3), 8));
173: } catch (NumberFormatException e) {
174: ErrorManager.getDefault().log(
175: ErrorManager.INFORMATIONAL,
176: "WARNING: Invalid octal number format: "
177: + value.substring(i, i + 3)); // NOI18N
178: }
179: i += 3;
180: }
181: }
182: return sb.toString();
183: }
184:
185: public static int compareStrings(String s1, String s2) {
186: if (s1 != null)
187: return s2 != null ? s1.compareTo(s2) : 1;
188: else
189: return s2 != null ? -1 : 0;
190: }
191:
192: public static boolean isNotEmpty(String string) {
193: return string != null && string.length() > 0;
194: }
195:
196: }
|