You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2006/10/03 23:51:50 UTC
svn commit: r452642 - in
/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util:
io/CharUtils.java iri/IRI.java
Author: jmsnell
Date: Tue Oct 3 14:51:49 2006
New Revision: 452642
URL: http://svn.apache.org/viewvc?view=rev&rev=452642
Log:
Additional bidi support designed to make it easier to work with bidi strings.
For instance, if you'd like to insert right-to-left plain-text characters in the title of an entry,
you can do so using:
entry.setTitle(CharUtils.bidiRLM("text with right-to-left characters"));
Modified:
incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/io/CharUtils.java
incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/iri/IRI.java
Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/io/CharUtils.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/io/CharUtils.java?view=diff&rev=452642&r1=452641&r2=452642
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/io/CharUtils.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/io/CharUtils.java Tue Oct 3 14:51:49 2006
@@ -237,4 +237,81 @@
String.valueOf((char)c);
}
+
+
+ private static final char LRE = 0x202A;
+ private static final char RLE = 0x202B;
+ private static final char LRO = 0x202D;
+ private static final char RLO = 0x202E;
+ private static final char LRM = 0x200E;
+ private static final char RLM = 0x200F;
+ private static final char PDF = 0x202C;
+
+ /**
+ * Removes leading and trailing bidi controls from the string
+ */
+ public static String stripBidi(String s) {
+ if (s == null || s.length() <= 1) return s;
+ if (charIsBidiControl(s.charAt(0)))
+ s = s.substring(1);
+ if (charIsBidiControl(s.charAt(s.length()-1)))
+ s = s.substring(0,s.length()-1);
+ return s;
+ }
+
+ /**
+ * Returns true if the character is a bidi control
+ */
+ public static boolean charIsBidiControl(char c) {
+ return c == 0x202A ||
+ c == LRE ||
+ c == RLE ||
+ c == LRO ||
+ c == RLO ||
+ c == RLM ||
+ c == LRM ||
+ c == PDF;
+ }
+
+ /**
+ * Wrap the string with Bidi Right-to-Left embed
+ */
+ public static String bidiRLE(String s) {
+ return RLE + s + PDF;
+ }
+
+ /**
+ * Wrap the string with Bidi Right-to-Left override
+ */
+ public static String bidiRLO(String s) {
+ return RLO + s + PDF;
+ }
+
+ /**
+ * Wrap the string with Bidi Left-to-Right embed
+ */
+ public static String bidiLRE(String s) {
+ return LRE + s + PDF;
+ }
+
+ /**
+ * Wrap the string with Bidi Left-to-Right override
+ */
+ public static String bidiLRO(String s) {
+ return LRO + s + PDF;
+ }
+
+ /**
+ * Wrap the string with Bidi RML marks
+ */
+ public static String bidiRLM(String s) {
+ return RLM + s + RLM;
+ }
+
+ /**
+ * Wrap the string with Bidi LRM marks
+ */
+ public static String bidiLRM(String s) {
+ return LRM + s + RLM;
+ }
}
Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/iri/IRI.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/iri/IRI.java?view=diff&rev=452642&r1=452641&r2=452642
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/iri/IRI.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/iri/IRI.java Tue Oct 3 14:51:49 2006
@@ -69,7 +69,7 @@
public IRI(String iri) throws IRISyntaxException {
Builder b = new Builder();
- parse(iri, b);
+ parse(CharUtils.stripBidi(iri), b);
init(
b.schemeobj,
b.scheme,
@@ -83,7 +83,7 @@
}
public IRI(String iri, Normalizer.Form nf) throws IRISyntaxException, IOException {
- this(Normalizer.normalize(iri,nf).toString());
+ this(Normalizer.normalize(CharUtils.stripBidi(iri),nf).toString());
}
public IRI(
@@ -637,12 +637,7 @@
}
public String toBIDIString() {
- StringBuffer buf = new StringBuffer(toString());
- if (buf.length() > 0) {
- if (buf.charAt(0) != '\u202A') buf.insert(0,'\u202A');
- if (buf.charAt(buf.length()-1) != '\u202C') buf.append('\u202C');
- }
- return buf.toString();
+ return CharUtils.bidiLRE(toString());
}
public java.net.URI toURI()