001: package net.sourceforge.squirrel_sql.plugins.editextras;
002:
003: /*
004: * Copyright (C) 2003 Gerd Wagner
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020: import java.util.StringTokenizer;
021:
022: class Utilities {
023: static String quoteText(String textToQuote, boolean sbAppend) {
024: if (null == textToQuote) {
025: throw new IllegalArgumentException(
026: "textToQuote can not be null");
027: }
028:
029: String[] lines = textToQuote.split("\n");
030:
031: StringBuffer ret = new StringBuffer();
032:
033: if (sbAppend) {
034: ret.append("sb.append(\"").append(
035: trimRight(lines[0].replaceAll("\"", "\\\\\"")));
036: } else {
037: ret.append("\"").append(
038: trimRight(lines[0].replaceAll("\"", "\\\\\"")));
039: }
040:
041: for (int i = 1; i < lines.length; ++i) {
042: if (sbAppend) {
043: ret.append(" \"); \nsb.append(\"").append(
044: trimRight(lines[i].replaceAll("\"", "\\\\\"")));
045: } else {
046: ret.append(" \" +\n\"").append(
047: trimRight(lines[i].replaceAll("\"", "\\\\\"")));
048: }
049: }
050:
051: if (sbAppend) {
052: ret.append(" \");");
053: } else {
054: ret.append(" \";");
055: }
056:
057: return ret.toString();
058: }
059:
060: /**
061: * textToUnquote is seen as a tokens separated by quotes. All tokens
062: * that contain a new line character are left out.
063: *
064: * @param textToUnquote Text to be unquoted.
065: *
066: * @return The unquoted text.
067: */
068: static String unquoteText(String textToUnquote) {
069: // new line to the begining so that sb.append( will be removed
070: // new line to the end so that a semi colon at the end will be removed.
071: textToUnquote = "\n" + textToUnquote + "\n";
072:
073: StringTokenizer st = new StringTokenizer(textToUnquote, "\"");
074:
075: StringBuffer ret = new StringBuffer();
076: while (st.hasMoreTokens()) {
077: String token = st.nextToken();
078: String trimmedToken = token;
079: if (0 != token.trim().length() && -1 == token.indexOf('\n')) {
080: if (trimmedToken.endsWith("\\n")) {
081: // Some people put new line characters in their SQL to have nice debug output.
082: // Remove these new line characters too.
083: trimmedToken = trimmedToken.substring(0,
084: trimmedToken.length() - 2);
085: }
086:
087: if (trimmedToken.endsWith("\\")) {
088: ret.append(
089: trimmedToken.substring(0, trimmedToken
090: .length() - 1)).append("\"");
091: } else {
092: ret.append(trimmedToken).append("\n");
093: }
094: }
095: }
096: if (ret.toString().endsWith("\n")) {
097: ret.setLength(ret.length() - 1);
098: }
099: return ret.toString();
100: }
101:
102: static String trimRight(String toTrim) {
103: if (0 >= toTrim.length()) {
104: return toTrim;
105: }
106:
107: int i;
108: for (i = toTrim.length(); i > 0; --i) {
109: if (!Character.isWhitespace(toTrim.charAt(i - 1))) {
110: break;
111: }
112: }
113:
114: return toTrim.substring(0, i);
115: }
116:
117: }
|