Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2008-04-28 12:45:46 -0400
committerpelder2008-04-28 12:45:46 -0400
commit64b4b26c9f208c503b361083c85288794ce4ba1f (patch)
treee74a6ddff8d0fdea612cd692ac0b7c8618eb1fcd
parent87bbab292632f29733733c13d9707361e6a1e198 (diff)
downloadorg.eclipse.jet-64b4b26c9f208c503b361083c85288794ce4ba1f.tar.gz
org.eclipse.jet-64b4b26c9f208c503b361083c85288794ce4ba1f.tar.xz
org.eclipse.jet-64b4b26c9f208c503b361083c85288794ce4ba1f.zip
[229106] Create XPath function to encode content correctly for Properties files.
-rw-r--r--doc/org.eclipse.jet.doc/references/xpathFunctions/overview.html8
-rw-r--r--doc/org.eclipse.jet.doc/references/xpathFunctions/propertiesEncodeFunction.html88
-rwxr-xr-xdoc/org.eclipse.jet.doc/references/xpathFunctions/sortFunction.html2
-rw-r--r--doc/org.eclipse.jet.doc/toc.xml2
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/PropertiesEncodeFunction.java87
5 files changed, 186 insertions, 1 deletions
diff --git a/doc/org.eclipse.jet.doc/references/xpathFunctions/overview.html b/doc/org.eclipse.jet.doc/references/xpathFunctions/overview.html
index e3bbf5f..4c8d7a1 100644
--- a/doc/org.eclipse.jet.doc/references/xpathFunctions/overview.html
+++ b/doc/org.eclipse.jet.doc/references/xpathFunctions/overview.html
@@ -87,6 +87,14 @@ The following functions are included in JET, in addition to the XPath functions
<!-- End packageName description -->
</TD>
</TR>
+ <TR>
+ <TD width="10%"><A href="propertiesEncodeFunction.html">propertiesEncode</A></TD>
+ <TD>
+ <!-- Begin propertiesEncode description -->
+ Encode string content to conform to rules for Java properties files
+ <!-- End propertiesEncode description -->
+ </TD>
+ </TR>
<TR>
<TD width="10%"><A href="removeWhitespaceFunction.html">removeWhitespace</A></TD>
<TD>
diff --git a/doc/org.eclipse.jet.doc/references/xpathFunctions/propertiesEncodeFunction.html b/doc/org.eclipse.jet.doc/references/xpathFunctions/propertiesEncodeFunction.html
new file mode 100644
index 0000000..8419653
--- /dev/null
+++ b/doc/org.eclipse.jet.doc/references/xpathFunctions/propertiesEncodeFunction.html
@@ -0,0 +1,88 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<HTML>
+<HEAD>
+<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
+<META name="GENERATOR" content="IBM Software Development Platform">
+<link rel="STYLESHEET" href="../../book.css" charset="ISO-8859-1" type="text/css"/>
+<TITLE>propertiesEncode XPath function</TITLE>
+</HEAD>
+<BODY>
+<TABLE border="0" cellpadding="0" cellspacing="1" width="100%">
+ <TBODY>
+ <TR>
+ <TH align="left"><P>propertiesEncode()</P></TH>
+ <TH align="right"><A href="overview.html">Additional XPath Functions</A></TH>
+ </TR>
+ </TBODY>
+</TABLE>
+<HR>
+<H2>propertiesEncode</H2>
+<P>Reutnr a string encoded to conform to rules for Java properties files</P>
+<HR>
+<TABLE border="0" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">Function Summary<BR>&nbsp;</TH></TR>
+ <TR>
+ <TD width="20%" valign="top">minimal arguments</TD>
+ <TD><SPAN style="font-family: monospace" valign="top">
+ String propertiesEncode (String text)<BR>&nbsp;<BR>
+ </SPAN></TD>
+ </TR>
+ <TR>
+ <TD width="20%" valign="top">all arguments</TD>
+ <TD><SPAN style="font-family: monospace" valign="top">
+ String propertiesEncode (String text, boolean leadingSpacesOnly)<BR>&nbsp;<BR>
+ </SPAN></TD>
+ </TR>
+ </TBODY>
+</TABLE>
+<HR>
+<TABLE border="0" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">Required Arguments<BR>&nbsp;</TH>
+ </TR>
+ <TR>
+ <TD width="20%" valign="top"><B>String text</B></TD>
+ <TD>The text to be encoded. Should be either a key name or a property value.</TD>
+ </TR>
+ </TBODY>
+</TABLE>
+<HR>
+<TABLE border="0" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">Optional Arguments<BR>&nbsp;</TH>
+ </TR>
+ <TR>
+ <TD width="20%" valign="top"><B>boolean leadingSpacesOnly</B></TD>
+ <TD>If <code>true</code>, then only leading spaces are encode (by adding a \).
+ The default is <code>false</code>.
+ For property value names, readability may be improved by specifying true().
+ Note that property key names containing spaces should not be encoded with leadSpacesOnly set to true.
+ </TD>
+ </TR>
+ </TBODY>
+</TABLE>
+<HR>
+<TABLE border="0" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">Example<BR>&nbsp;</TH></TR>
+ <TR>
+ <TD colspan="2"><SPAN style="font-family: monospace">
+<!-- Begin propertiesEncode example -->
+ <B>propertiesEncode</B>(" a whole bunch of spaces ")<br>
+ <P>returns the string "\ a\ whole\ bunch\ of\ spaces\ "</P>
+ <B>propertiesEncode</B>(" a whole bunch of spaces ", true())<br>
+ <P>returns the string "\ a whole bunch of spaces "</P>
+<!-- End propertiesEncode example -->
+</SPAN></TD>
+ </TR>
+ </TBODY>
+</TABLE>
+<P><BR>
+<FONT color="navy" size="-2">&copy; Copyright IBM Corporation
+2008. All Rights Reserved.</FONT></P></P></BODY>
+</HTML>
diff --git a/doc/org.eclipse.jet.doc/references/xpathFunctions/sortFunction.html b/doc/org.eclipse.jet.doc/references/xpathFunctions/sortFunction.html
index e86db70..b3352e2 100755
--- a/doc/org.eclipse.jet.doc/references/xpathFunctions/sortFunction.html
+++ b/doc/org.eclipse.jet.doc/references/xpathFunctions/sortFunction.html
@@ -4,7 +4,7 @@
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM Software Development Platform">
<link rel="STYLESHEET" href="../../book.css" charset="ISO-8859-1" type="text/css"/>
-<TITLE>removeWhitespace XPath function</TITLE>
+<TITLE>sort XPath function</TITLE>
</HEAD>
<BODY>
<TABLE border="0" cellpadding="0" cellspacing="1" width="100%">
diff --git a/doc/org.eclipse.jet.doc/toc.xml b/doc/org.eclipse.jet.doc/toc.xml
index 013d07c..212e2b1 100644
--- a/doc/org.eclipse.jet.doc/toc.xml
+++ b/doc/org.eclipse.jet.doc/toc.xml
@@ -50,6 +50,8 @@
<topic label="lower-case" href="references/xpathFunctions/lower_caseFunction.html"/>
<topic label="lowercaseFirst" href="references/xpathFunctions/lowercaseFirstFunction.html"/>
<topic label="packageName" href="references/xpathFunctions/packageNameFunction.html"/>
+ <topic href="references/xpathFunctions/propertiesEncodeFunction.html" label="propertiesEncode">
+ </topic>
<topic href="references/xpathFunctions/removeWhitespaceFunction.html" label="removeWhitespace">
</topic>
<topic href="references/xpathFunctions/sortFunction.html" label="sort">
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/PropertiesEncodeFunction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/PropertiesEncodeFunction.java
new file mode 100644
index 0000000..97c0867
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/PropertiesEncodeFunction.java
@@ -0,0 +1,87 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2008 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: PropertiesEncodeFunction.java,v 1.1 2008/04/28 16:45:46 pelder Exp $
+ */
+package org.eclipse.jet.internal.xpath.functions.extras;
+
+import java.util.List;
+
+import org.eclipse.jet.xpath.XPathFunction;
+import org.eclipse.jet.xpath.XPathUtil;
+
+/**
+ * Implement 'String propertiesEncode(String input[, boolean leadingSpacesOnly])'.
+ */
+public class PropertiesEncodeFunction implements XPathFunction
+{
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.XPathFunction#evaluate(java.util.List)
+ */
+ public Object evaluate(List args)
+ {
+ final String input = XPathUtil.xpathString(args.get(0));
+ final boolean leadingSpacesOnly = args.size() == 1 ? false : XPathUtil.xpathBoolean(args.get(1));
+ return PropertiesEncodeFunction.encode(input, leadingSpacesOnly);
+ }
+
+ public static String encode(final String input, final boolean leadingSpacesOnly)
+ {
+ final StringBuffer buffer = new StringBuffer(input.length());
+ // encode all spaces initiallly
+ boolean encodeSpaces = true;
+ for (int i = 0; i < input.length(); i++)
+ {
+ char c = input.charAt(i);
+ switch (c)
+ {
+ case '\\':
+ case '\t':
+ case '\f':
+ case '\n':
+ case '\r':
+ case '#':
+ case '!':
+ case '=':
+ case ':':
+ if (leadingSpacesOnly)
+ encodeSpaces = false;
+ buffer.append('\\');
+ buffer.append(c);
+ break;
+ case ' ':
+ if (encodeSpaces)
+ buffer.append('\\');
+ buffer.append(c);
+ break;
+ default:
+ if (leadingSpacesOnly)
+ encodeSpaces = false;
+ if (0x0020 <= c && c <= 0x007e)
+ {
+ buffer.append(c);
+ }
+ else
+ {
+ buffer.append('\\').append('u').append(Character.forDigit((c >> 12) & 0xF, 16)).append(Character.forDigit((c >> 8) & 0xF, 16)).append(
+ Character.forDigit((c >> 4) & 0xF, 16)).append(Character.forDigit(c & 0xF, 16));
+ }
+ break;
+ }
+ }
+ return buffer.toString();
+ }
+
+}

Back to the top