Skip to main content
summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorNan Li2012-09-24 16:57:50 -0400
committerBrian Vosburgh2012-09-24 17:40:40 -0400
commit93ea11f7623b388b2c1149b7ff66b6e10fb57c1a (patch)
tree0a5fe57605bd3b564aed13855a7d5092e973fc9f /common
parent8741e266f5eb8aea773ae450fec7722772220423 (diff)
downloadwebtools.dali-93ea11f7623b388b2c1149b7ff66b6e10fb57c1a.tar.gz
webtools.dali-93ea11f7623b388b2c1149b7ff66b6e10fb57c1a.tar.xz
webtools.dali-93ea11f7623b388b2c1149b7ff66b6e10fb57c1a.zip
[382644] XML code assist for database identifiers with special
characters
Diffstat (limited to 'common')
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringTools.java70
1 files changed, 62 insertions, 8 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringTools.java
index 6e27dbeca4..affdf300e7 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringTools.java
@@ -36,7 +36,13 @@ public final class StringTools {
/** XML double quote */
public static final String XML_QUOTE = """; //$NON-NLS-1$
-
+
+ /** XML apostrophe */
+ public static final String XML_APOSTROPHE = "'";
+
+ /** XML ampersand */
+ public static final String XML_AMPERSAND = "&";
+
/** parenthesis */
public static final char OPEN_PARENTHESIS = '(';
public static final char CLOSE_PARENTHESIS = ')';
@@ -53,6 +59,10 @@ public final class StringTools {
public static final char OPEN_CHEVRON = '<';
public static final char CLOSE_CHEVRON = '>';
+ /** XML angle brackets */
+ public static final String XML_OPEN_CHEVRON= "&lt;";
+ public static final String XML_CLOSE_CHEVRON = "&gt;";
+
/** empty string */
public static final String EMPTY_STRING = ""; //$NON-NLS-1$
@@ -5140,32 +5150,76 @@ public final class StringTools {
// ********** convert to XML string literal **********
public static String convertToXmlStringLiteral(String string) {
+ return convertToXmlStringLiteralQuote(string);
+ }
+
+ public static String convertToXmlStringLiteralQuote(String string) {
+ return convertToXmlStringLiteral(string, '"');
+ }
+
+ public static String convertToXmlStringLiteralApostrophe(String string) {
+ return convertToXmlStringLiteral(string, '\'');
+ }
+
+ public static String convertToXmlStringLiteral(String string, char delimiter) {
int len = string.length();
if (len == 0) {
return EMPTY_JAVA_STRING_LITERAL;
}
StringBuilder sb = new StringBuilder(len + 5);
- convertToXmlStringLiteralOn_(string.toCharArray(), sb, len);
+ convertToXmlStringLiteralOn_(string.toCharArray(), sb, len, delimiter);
return sb.toString();
}
- //TODO need to add the rest of the predifende entities to this switch (amp, apos, lt, and gt)
- private static void convertToXmlStringLiteralOn_(char[] string, StringBuilder sb, int len) {
+ //TODO need to add the rest of the predifende entities to this switch (amp, lt, and gt)
+ private static void convertToXmlStringLiteralOn_(char[] string, StringBuilder sb, int len, char delimiter) {
sb.ensureCapacity(sb.length() + len + 5);
- sb.append(QUOTE);
+ sb.append(delimiter);
for (char c : string) {
switch (c) {
case '"': // double-quote
- sb.append(XML_QUOTE);
- break;
+ sb.append((delimiter == '"') ? XML_QUOTE : '"');
+ break;
+ case '\'': // apostrophe
+ sb.append((delimiter == '\'') ? XML_APOSTROPHE : '\'');
+ break;
default:
sb.append(c);
break;
}
}
- sb.append(QUOTE);
+ sb.append(delimiter);
}
+ public static String convertToXmlElementStringLiteral(String string) {
+ int len = string.length();
+ if (len == 0) {
+ return EMPTY_JAVA_STRING_LITERAL;
+ }
+ StringBuilder sb = new StringBuilder(len + 5);
+ convertToXmlElementStringLiteralOn_(string.toCharArray(), sb, len);
+ return sb.toString();
+ }
+
+ private static void convertToXmlElementStringLiteralOn_(char[] string, StringBuilder sb, int len) {
+ sb.ensureCapacity(sb.length() + len + 5);
+ for (char c : string) {
+ switch (c) {
+ case '&': // ampersand
+ sb.append(XML_AMPERSAND);
+ break;
+ case '<': // less-than sign
+ sb.append(XML_OPEN_CHEVRON);
+ break;
+ case '>': // greater-than sign
+ sb.append(XML_CLOSE_CHEVRON);
+ break;
+ default:
+ sb.append(c);
+ break;
+ }
+ }
+ }
// ********** convenience **********

Back to the top