Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/DumpTag.java109
1 files changed, 84 insertions, 25 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/DumpTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/DumpTag.java
index 4bff46a..b04967d 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/DumpTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/DumpTag.java
@@ -147,7 +147,7 @@ public class DumpTag extends AbstractEmptyTag {
out.append("="); //$NON-NLS-1$
String value = xpc.resolveAsString(attrs[i], NODE_VALUE_XPATH);
if(value == null) value = ""; //$NON-NLS-1$
- out.append(useRightQuotes(value));
+ out.append(useRightQuotes(value, false));
}
final Object[] children = xpc.resolve(node, CHILD_NODES_XPATH);
if(children != null && children.length > 0)
@@ -167,7 +167,7 @@ public class DumpTag extends AbstractEmptyTag {
{
String value = xpc.resolveAsString(node, NODE_VALUE_XPATH);
if(value == null) value = ""; //$NON-NLS-1$
- if (entities) { value = insertEntities(value); }
+ if (entities) { value = insertEntitiesInContent(value); }
out.append(value);
}
else if(nodeKind == NodeKind.COMMENT)
@@ -290,7 +290,7 @@ public class DumpTag extends AbstractEmptyTag {
// out.write("<![CDATA["+buffer+"]]>");
// written = true;
// } else {
- if (entities) { buffer = insertEntities(buffer); }
+ if (entities) { buffer = insertEntitiesInContent(buffer); }
out.write(buffer);
// }
written = true;
@@ -313,38 +313,97 @@ public class DumpTag extends AbstractEmptyTag {
}
/**
+ * Method useRightQuotes.
+ * @param string
+ * @return String
+ * @deprecated Use {@link #useRightQuotes(String,boolean)} instead
+ */
+ private String useRightQuotes(String string) {
+ return useRightQuotes(string, false);
+ }
+
+ /**
* Method useRightQuotes.
* @param string
+ * @param entities TODO
* @return String
*/
- private String useRightQuotes(String string) {
- if (string.indexOf("\"") > -1) { //$NON-NLS-1$
- return "'"+string+"'"; //$NON-NLS-1$ //$NON-NLS-2$
+ private String useRightQuotes(String string, boolean entities) {
+ final boolean containsDoubleQuote = string.indexOf("\"") > -1; //$NON-NLS-1$
+ if(entities) {
+ if (containsDoubleQuote) {
+ return "'"+insertEntitiesInAttribute(string, '\'')+"'"; //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ return "\""+insertEntitiesInAttribute(string, '"')+"\""; //$NON-NLS-1$ //$NON-NLS-2$
+ } else {
+ if (containsDoubleQuote) {
+ return "'"+string+"'"; //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ return "\""+string+"\""; //$NON-NLS-1$ //$NON-NLS-2$
}
- return "\""+string+"\""; //$NON-NLS-1$ //$NON-NLS-2$
}
-
/**
- * Method insertEntities.
- * @param buffer
- * @return String
+ * Insert required XML entities for an attribute value
+ * @param value the attribute value
+ * @param delimiter the attribute delimiter (' or ")
+ * @return the modified string
*/
- private String insertEntities(String string) {
- StringBuffer sb = new StringBuffer(string.length() * 2);
- StringTokenizer st = new StringTokenizer(string,"<>&'\"",true); //$NON-NLS-1$
- while (st.hasMoreTokens()) {
- String token = st.nextToken();
- if (token.equals("<")) { token = "&lt;"; } //$NON-NLS-1$ //$NON-NLS-2$
- if (token.equals(">")) { token = "&gt;"; } //$NON-NLS-1$ //$NON-NLS-2$
- if (token.equals("&")) { token = "&amp;"; } //$NON-NLS-1$ //$NON-NLS-2$
- if (token.equals("'")) { token = "&apos;"; } //$NON-NLS-1$ //$NON-NLS-2$
- if (token.equals("\"")) { token = "&quot;"; } //$NON-NLS-1$//$NON-NLS-2$
- sb.append(token);
- }
- String buffer = sb.toString();
- return buffer;
+ private String insertEntitiesInAttribute(String value, char delimiter) {
+ final String charsToReplace = delimiter == '\'' ? "<&'" : "<&\""; //$NON-NLS-1$ //$NON-NLS-2$
+
+ return insertEntities(value, charsToReplace);
+
}
+
+ /**
+ * Insert XML entities in XML content (text contained by a tag).
+ * @param text the text to modify
+ * @return the modified text
+ */
+ private String insertEntitiesInContent(String text) {
+ return insertEntities(text, "<>&'\""); //$NON-NLS-1$
+ }
+
+ /**
+ * Insert XML entities for the characters specified in charsToReplace.
+ * @param text the text to modify
+ * @param charsToReplace the chars for which entities will be replaced
+ * @return the modified string
+ */
+ private String insertEntities(String text, final String charsToReplace)
+ {
+ StringBuffer sb = new StringBuffer(text.length() * 2);
+ StringTokenizer st = new StringTokenizer(text,charsToReplace ,true);
+ while (st.hasMoreTokens()) {
+ String token = st.nextToken();
+ if(token.length() == 1) {
+ switch(token.charAt(0)) {
+ case '<':
+ sb.append("&lt;"); //$NON-NLS-1$
+ break;
+ case '>':
+ sb.append("&gt;"); //$NON-NLS-1$
+ break;
+ case '&':
+ sb.append("&amp;"); //$NON-NLS-1$
+ break;
+ case '\'':
+ sb.append("&apos;"); //$NON-NLS-1$
+ break;
+ case '"':
+ sb.append("&quot;"); //$NON-NLS-1$
+ break;
+ default:
+ sb.append(token);
+ }
+ } else {
+ sb.append(token);
+ }
+ }
+ String buffer = sb.toString();
+ return buffer;
+ }
/**
* End custom methods

Back to the top