summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2006-03-29 09:37:14 (EST)
committer pelder2006-03-29 09:37:14 (EST)
commit4cf6b31bd6eb237678e6a99ca9135c56562fc5de (patch)
tree1249ab6c58c0ff385e4f30bbf97edaddd21d9bc4
parent7cac0ab938465a71db601ef7b44b9c6c80ff1f2f (diff)
downloadorg.eclipse.jet-4cf6b31bd6eb237678e6a99ca9135c56562fc5de.zip
org.eclipse.jet-4cf6b31bd6eb237678e6a99ca9135c56562fc5de.tar.gz
org.eclipse.jet-4cf6b31bd6eb237678e6a99ca9135c56562fc5de.tar.bz2
[133843] Implement normalize-space() and translate() XPath functions.
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/BuiltinXPathFunctionResolver.java4
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/NormalizeSpaceFunction.java97
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/TranslateFunction.java58
3 files changed, 157 insertions, 2 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/BuiltinXPathFunctionResolver.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/BuiltinXPathFunctionResolver.java
index 0e75151..dcec07f 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/BuiltinXPathFunctionResolver.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/BuiltinXPathFunctionResolver.java
@@ -69,8 +69,8 @@ public class BuiltinXPathFunctionResolver
instance.addFunction(SubstringAfterFunction.FUNCTION_META_DATA);
instance.addFunction(SubstringFunction.FUNCTION_META_DATA);
instance.addFunction(StringLengthFunction.FUNCTION_META_DATA);
- // TODO Implement 'normalize-space' new XPathFunctionMetaData("normalize-space", null, 0, 1),
- // TODO Implement 'translate' new XPathFunctionMetaData("translate", null, 3, 3),
+ instance.addFunction(NormalizeSpaceFunction.FUNCTION_META_DATA);
+ instance.addFunction(TranslateFunction.FUNCTION_META_DATA);
// *** Boolean functions
instance.addFunction(BooleanFunction.FUNCTION_META_DATA);
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/NormalizeSpaceFunction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/NormalizeSpaceFunction.java
new file mode 100644
index 0000000..c51db31
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/NormalizeSpaceFunction.java
@@ -0,0 +1,97 @@
+/**
+ * <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.xpath.functions;
+
+import java.util.List;
+
+import org.eclipse.jet.xpath.Context;
+import org.eclipse.jet.xpath.XPathFunction;
+import org.eclipse.jet.xpath.XPathFunctionMetaData;
+import org.eclipse.jet.xpath.XPathFunctionWithContext;
+import org.eclipse.jet.xpath.XPathUtil;
+
+/**
+ * Implement the XPath Function 'normalize-space'
+ */
+public class NormalizeSpaceFunction implements XPathFunction, XPathFunctionWithContext
+{
+
+ public static final XPathFunctionMetaData FUNCTION_META_DATA = new XPathFunctionMetaData("normalize-space", new NormalizeSpaceFunction(), 0, 1); //$NON-NLS-1$
+
+ private Context context;
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.XPathFunction#evaluate(java.util.List)
+ */
+ public Object evaluate(List args)
+ {
+ String arg;
+ if(args.size() == 0)
+ {
+ arg = XPathUtil.xpathString(context.getContextNode());
+ }
+ else
+ {
+ arg = XPathUtil.xpathString(args.get(0));
+ }
+
+ return normalizeSpace(arg);
+ }
+
+ /**
+ * Normalize a string, accoring to the XPath normalize-space function.
+ * @param arg the original string.
+ * @return the normalized string
+ */
+ public static String normalizeSpace(String arg)
+ {
+ boolean stripNextSpace = true;
+ StringBuffer result = new StringBuffer(arg);
+ for(int i = 0; i < result.length(); i++)
+ {
+ if(Character.isWhitespace(result.charAt(i)))
+ {
+ if(stripNextSpace)
+ {
+ result.deleteCharAt(i);
+ i--;
+ }
+ else
+ {
+ stripNextSpace = true;
+ }
+ }
+ else
+ {
+ stripNextSpace = false;
+ }
+ }
+
+ final int lastCharIndex = result.length() - 1;
+ if(lastCharIndex >= 0 && Character.isWhitespace(result.charAt(lastCharIndex)))
+ {
+ result.deleteCharAt(lastCharIndex);
+ }
+ return result.toString();
+ }
+
+ public void setContext(Context context)
+ {
+ this.context = context;
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/TranslateFunction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/TranslateFunction.java
new file mode 100644
index 0000000..6034fff
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/TranslateFunction.java
@@ -0,0 +1,58 @@
+/**
+ * <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.xpath.functions;
+
+import java.util.List;
+
+import org.eclipse.jet.xpath.XPathFunction;
+import org.eclipse.jet.xpath.XPathFunctionMetaData;
+import org.eclipse.jet.xpath.XPathUtil;
+
+/**
+ * Implement the XPath 1.0 function 'translate'
+ */
+public class TranslateFunction implements XPathFunction
+{
+
+ public static final XPathFunctionMetaData FUNCTION_META_DATA = new XPathFunctionMetaData("translate", new TranslateFunction(), 3, 3); //$NON-NLS-1$
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.XPathFunction#evaluate(java.util.List)
+ */
+ public Object evaluate(List args)
+ {
+ String arg = XPathUtil.xpathString(args.get(0));
+ String mapString = XPathUtil.xpathString(args.get(1));
+ String transString = XPathUtil.xpathString(args.get(2));
+
+ for (int i = 0; i < mapString.length() && arg.length() > 0; i++)
+ {
+ char c = mapString.charAt(i);
+ char replacement = i < transString.length() ? transString.charAt(i) : 0;
+ if(replacement != 0)
+ {
+ arg = arg.replace(c, replacement);
+ }
+ else
+ {
+ arg = arg.replaceAll("\\Q" + c + "\\E", ""); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+ }
+ }
+ return arg;
+ }
+
+}