summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2006-04-27 10:31:17 (EDT)
committerpelder2006-04-27 10:31:17 (EDT)
commit3fd74192591bd6428af42636b5822469d00b69ce (patch)
tree610ec419f0b9b7d1f6cd40960a3b0b1236926c87
parent3ea8b7e4b8700a34a21de353504e0a230863c93f (diff)
downloadorg.eclipse.jet-3fd74192591bd6428af42636b5822469d00b69ce.zip
org.eclipse.jet-3fd74192591bd6428af42636b5822469d00b69ce.tar.gz
org.eclipse.jet-3fd74192591bd6428af42636b5822469d00b69ce.tar.bz2
[138926] Correct offset / length errors, add detection of invalid offset/length values, refactor uc/lc common code to AbstractCaseShiftTag.
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/AbstractCaseShiftTag.java99
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/LowerCaseTag.java53
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/UpperCaseTag.java53
3 files changed, 110 insertions, 95 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/AbstractCaseShiftTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/AbstractCaseShiftTag.java
new file mode 100644
index 0000000..efc74c4
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/AbstractCaseShiftTag.java
@@ -0,0 +1,99 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id$
+ */
+package org.eclipse.jet.internal.taglib.format;
+
+import java.text.MessageFormat;
+
+import org.eclipse.jet.JET2Context;
+import org.eclipse.jet.internal.l10n.JET2Messages;
+import org.eclipse.jet.taglib.AbstractFunctionTag;
+import org.eclipse.jet.taglib.FunctionTag;
+import org.eclipse.jet.taglib.JET2TagException;
+import org.eclipse.jet.taglib.TagInfo;
+
+/**
+ * Abstract tag that serves as common ancester to all case-shifting tags (upper-case, lower-case ...).
+ * <p>
+ * Each tag is assumed to be a Function tag with two optional attributes 'offset' and 'length'. Implementors
+ * must provide an implementation of doCaseShift
+ * </p>
+ */
+public abstract class AbstractCaseShiftTag extends AbstractFunctionTag implements FunctionTag
+{
+
+ private static final String LENGTH_ATTR = "length"; //$NON-NLS-1$
+
+ private static final String OFFSET_ATTR = "offset"; //$NON-NLS-1$
+
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.taglib.FunctionTag#doFunction(org.eclipse.jet.taglib.TagInfo, org.eclipse.jet.JET2Context, java.lang.String)
+ */
+ public final String doFunction(TagInfo td, JET2Context context, String bodyContent) throws JET2TagException
+ {
+ int offset = 0;
+ if (td.hasAttribute(OFFSET_ATTR))
+ {
+ try
+ {
+ offset = Integer.valueOf(getAttribute(OFFSET_ATTR)).intValue();
+ if(offset < 0)
+ {
+ throw new JET2TagException(MessageFormat.format(JET2Messages.AnyTag_AttributeMustBeInteger, new Object []{ OFFSET_ATTR }));
+ }
+ }
+ catch (NumberFormatException e)
+ {
+ throw new JET2TagException(MessageFormat.format(JET2Messages.AnyTag_AttributeMustBeInteger, new Object []{ OFFSET_ATTR }), e);
+ }
+ }
+
+ int length = bodyContent.length() - offset;
+ if (td.hasAttribute(LENGTH_ATTR))
+ {
+ try
+ {
+ length = Integer.valueOf(getAttribute(LENGTH_ATTR)).intValue();
+ if(length < 0)
+ {
+ throw new JET2TagException(MessageFormat.format(JET2Messages.AnyTag_AttributeMustBeInteger, new Object []{ LENGTH_ATTR }));
+ }
+ }
+ catch (NumberFormatException e)
+ {
+ throw new JET2TagException(MessageFormat.format(JET2Messages.AnyTag_AttributeMustBeInteger, new Object []{ LENGTH_ATTR }), e);
+ }
+ }
+
+ StringBuffer buffer = new StringBuffer(bodyContent);
+
+ // fix offset and length to avoid out of bounds exceptions
+ if(length < 0) length = 0;
+ if(length > buffer.length() - offset) length = buffer.length() - offset;
+
+ if(offset < buffer.length())
+ {
+ final String textToCaseShift = bodyContent.substring(offset, offset + length);
+ buffer.replace(offset, offset + length, doCaseShift(textToCaseShift));
+ }
+ return buffer.toString();
+ }
+
+
+ protected abstract String doCaseShift(String textToCaseShift);
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/LowerCaseTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/LowerCaseTag.java
index 09bd095..c890f42 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/LowerCaseTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/LowerCaseTag.java
@@ -17,27 +17,13 @@
package org.eclipse.jet.internal.taglib.format;
-
-import java.text.MessageFormat;
-
-import org.eclipse.jet.JET2Context;
-import org.eclipse.jet.internal.l10n.JET2Messages;
-import org.eclipse.jet.taglib.AbstractFunctionTag;
-import org.eclipse.jet.taglib.JET2TagException;
-import org.eclipse.jet.taglib.TagInfo;
-
-
/**
* Implement the Standard JET Format Tag 'lc' (lowercase).
*
*/
-public class LowerCaseTag extends AbstractFunctionTag
+public class LowerCaseTag extends AbstractCaseShiftTag
{
- private static final String OFFSET_ATTR = "offset"; //$NON-NLS-1$
-
- private static final String LENGTH_ATTR = "length"; //$NON-NLS-1$
-
/**
*
*/
@@ -46,41 +32,10 @@ public class LowerCaseTag extends AbstractFunctionTag
super();
}
- /* (non-Javadoc)
- * @see org.eclipse.jet.taglib.FunctionTag#doFunction(org.eclipse.jet.taglib.TagInfo, org.eclipse.jet.JET2Context, java.lang.String)
- */
- public String doFunction(TagInfo td, JET2Context context, String bodyContent) throws JET2TagException
+ protected String doCaseShift(String textToCaseShift)
{
-
- int offset = 0;
- if (td.hasAttribute(OFFSET_ATTR))
- {
- try
- {
- offset = Integer.valueOf(getAttribute(OFFSET_ATTR)).intValue();
- }
- catch (NumberFormatException e)
- {
- throw new JET2TagException(MessageFormat.format(JET2Messages.AnyTag_AttributeMustBeInteger, new Object []{ OFFSET_ATTR }), e);
- }
- }
-
- int length = bodyContent.length();
- if (td.hasAttribute(LENGTH_ATTR))
- {
- try
- {
- length = Integer.valueOf(getAttribute(LENGTH_ATTR)).intValue();
- }
- catch (NumberFormatException e)
- {
- throw new JET2TagException(MessageFormat.format(JET2Messages.AnyTag_AttributeMustBeInteger, new Object []{ LENGTH_ATTR }), e);
- }
- }
-
- StringBuffer buffer = new StringBuffer(bodyContent);
- buffer.replace(offset, offset + length, bodyContent.substring(offset, offset + length).toLowerCase());
- return buffer.toString();
+ return textToCaseShift.toLowerCase();
}
+
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/UpperCaseTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/UpperCaseTag.java
index de7c999..0a1459e 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/UpperCaseTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/UpperCaseTag.java
@@ -18,26 +18,13 @@
package org.eclipse.jet.internal.taglib.format;
-import java.text.MessageFormat;
-
-import org.eclipse.jet.JET2Context;
-import org.eclipse.jet.internal.l10n.JET2Messages;
-import org.eclipse.jet.taglib.AbstractFunctionTag;
-import org.eclipse.jet.taglib.JET2TagException;
-import org.eclipse.jet.taglib.TagInfo;
-
-
/**
* Implement the Standard JET Format Tag 'uc' (uppercase).
*
*/
-public class UpperCaseTag extends AbstractFunctionTag
+public class UpperCaseTag extends AbstractCaseShiftTag
{
- private static final String LENGTH_ATTR = "length"; //$NON-NLS-1$
-
- private static final String OFFSET_ATTR = "offset"; //$NON-NLS-1$
-
/**
*
*/
@@ -46,40 +33,14 @@ public class UpperCaseTag extends AbstractFunctionTag
super();
}
- /* (non-Javadoc)
- * @see org.eclipse.jet.taglib.FunctionTag#doFunction(org.eclipse.jet.taglib.TagInfo, org.eclipse.jet.JET2Context, java.lang.String)
+
+ /**
+ * @param textToCaseShift
+ * @return
*/
- public String doFunction(TagInfo td, JET2Context context, String bodyContent) throws JET2TagException
+ protected String doCaseShift(final String textToCaseShift)
{
- int offset = 0;
- if (td.hasAttribute(OFFSET_ATTR))
- {
- try
- {
- offset = Integer.valueOf(getAttribute(OFFSET_ATTR)).intValue();
- }
- catch (NumberFormatException e)
- {
- throw new JET2TagException(MessageFormat.format(JET2Messages.AnyTag_AttributeMustBeInteger, new Object []{ OFFSET_ATTR }), e);
- }
- }
-
- int length = bodyContent.length();
- if (td.hasAttribute(LENGTH_ATTR))
- {
- try
- {
- length = Integer.valueOf(getAttribute(LENGTH_ATTR)).intValue();
- }
- catch (NumberFormatException e)
- {
- throw new JET2TagException(MessageFormat.format(JET2Messages.AnyTag_AttributeMustBeInteger, new Object []{ LENGTH_ATTR }), e);
- }
- }
-
- StringBuffer buffer = new StringBuffer(bodyContent);
- buffer.replace(offset, offset + length, bodyContent.substring(offset, offset + length).toUpperCase());
- return buffer.toString();
+ return textToCaseShift.toUpperCase();
}
}