You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ch...@apache.org on 2017/02/10 13:17:25 UTC
[12/25] [text] chore: update packages back to
org.apache.commons.text.*
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/EntityArrays.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/EntityArrays.java b/src/main/java/org/apache/commons/text/translate/EntityArrays.java
new file mode 100644
index 0000000..f5876f2
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/EntityArrays.java
@@ -0,0 +1,445 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * Class holding various entity data for HTML and XML - generally for use with
+ * the LookupTranslator.
+ * All Maps are generated using <code>java.util.Collections.unmodifiableMap()</code>.
+ *
+ * @since 1.0
+ */
+public class EntityArrays {
+
+ /**
+ * A Map<CharSequence, CharSequence> to to escape
+ * <a href="https://secure.wikimedia.org/wikipedia/en/wiki/ISO/IEC_8859-1">ISO-8859-1</a>
+ * characters to their named HTML 3.x equivalents.
+ */
+ public static final Map<CharSequence, CharSequence> ISO8859_1_ESCAPE;
+ static {
+ Map<CharSequence, CharSequence> initialMap = new HashMap<>();
+ initialMap.put("\u00A0", " "); // non-breaking space
+ initialMap.put("\u00A1", "¡"); // inverted exclamation mark
+ initialMap.put("\u00A2", "¢"); // cent sign
+ initialMap.put("\u00A3", "£"); // pound sign
+ initialMap.put("\u00A4", "¤"); // currency sign
+ initialMap.put("\u00A5", "¥"); // yen sign = yuan sign
+ initialMap.put("\u00A6", "¦"); // broken bar = broken vertical bar
+ initialMap.put("\u00A7", "§"); // section sign
+ initialMap.put("\u00A8", "¨"); // diaeresis = spacing diaeresis
+ initialMap.put("\u00A9", "©"); // � - copyright sign
+ initialMap.put("\u00AA", "ª"); // feminine ordinal indicator
+ initialMap.put("\u00AB", "«"); // left-pointing double angle quotation mark = left pointing guillemet
+ initialMap.put("\u00AC", "¬"); // not sign
+ initialMap.put("\u00AD", "­"); // soft hyphen = discretionary hyphen
+ initialMap.put("\u00AE", "®"); // � - registered trademark sign
+ initialMap.put("\u00AF", "¯"); // macron = spacing macron = overline = APL overbar
+ initialMap.put("\u00B0", "°"); // degree sign
+ initialMap.put("\u00B1", "±"); // plus-minus sign = plus-or-minus sign
+ initialMap.put("\u00B2", "²"); // superscript two = superscript digit two = squared
+ initialMap.put("\u00B3", "³"); // superscript three = superscript digit three = cubed
+ initialMap.put("\u00B4", "´"); // acute accent = spacing acute
+ initialMap.put("\u00B5", "µ"); // micro sign
+ initialMap.put("\u00B6", "¶"); // pilcrow sign = paragraph sign
+ initialMap.put("\u00B7", "·"); // middle dot = Georgian comma = Greek middle dot
+ initialMap.put("\u00B8", "¸"); // cedilla = spacing cedilla
+ initialMap.put("\u00B9", "¹"); // superscript one = superscript digit one
+ initialMap.put("\u00BA", "º"); // masculine ordinal indicator
+ initialMap.put("\u00BB", "»"); // right-pointing double angle quotation mark = right pointing guillemet
+ initialMap.put("\u00BC", "¼"); // vulgar fraction one quarter = fraction one quarter
+ initialMap.put("\u00BD", "½"); // vulgar fraction one half = fraction one half
+ initialMap.put("\u00BE", "¾"); // vulgar fraction three quarters = fraction three quarters
+ initialMap.put("\u00BF", "¿"); // inverted question mark = turned question mark
+ initialMap.put("\u00C0", "À"); // � - uppercase A, grave accent
+ initialMap.put("\u00C1", "Á"); // � - uppercase A, acute accent
+ initialMap.put("\u00C2", "Â"); // � - uppercase A, circumflex accent
+ initialMap.put("\u00C3", "Ã"); // � - uppercase A, tilde
+ initialMap.put("\u00C4", "Ä"); // � - uppercase A, umlaut
+ initialMap.put("\u00C5", "Å"); // � - uppercase A, ring
+ initialMap.put("\u00C6", "Æ"); // � - uppercase AE
+ initialMap.put("\u00C7", "Ç"); // � - uppercase C, cedilla
+ initialMap.put("\u00C8", "È"); // � - uppercase E, grave accent
+ initialMap.put("\u00C9", "É"); // � - uppercase E, acute accent
+ initialMap.put("\u00CA", "Ê"); // � - uppercase E, circumflex accent
+ initialMap.put("\u00CB", "Ë"); // � - uppercase E, umlaut
+ initialMap.put("\u00CC", "Ì"); // � - uppercase I, grave accent
+ initialMap.put("\u00CD", "Í"); // � - uppercase I, acute accent
+ initialMap.put("\u00CE", "Î"); // � - uppercase I, circumflex accent
+ initialMap.put("\u00CF", "Ï"); // � - uppercase I, umlaut
+ initialMap.put("\u00D0", "Ð"); // � - uppercase Eth, Icelandic
+ initialMap.put("\u00D1", "Ñ"); // � - uppercase N, tilde
+ initialMap.put("\u00D2", "Ò"); // � - uppercase O, grave accent
+ initialMap.put("\u00D3", "Ó"); // � - uppercase O, acute accent
+ initialMap.put("\u00D4", "Ô"); // � - uppercase O, circumflex accent
+ initialMap.put("\u00D5", "Õ"); // � - uppercase O, tilde
+ initialMap.put("\u00D6", "Ö"); // � - uppercase O, umlaut
+ initialMap.put("\u00D7", "×"); // multiplication sign
+ initialMap.put("\u00D8", "Ø"); // � - uppercase O, slash
+ initialMap.put("\u00D9", "Ù"); // � - uppercase U, grave accent
+ initialMap.put("\u00DA", "Ú"); // � - uppercase U, acute accent
+ initialMap.put("\u00DB", "Û"); // � - uppercase U, circumflex accent
+ initialMap.put("\u00DC", "Ü"); // � - uppercase U, umlaut
+ initialMap.put("\u00DD", "Ý"); // � - uppercase Y, acute accent
+ initialMap.put("\u00DE", "Þ"); // � - uppercase THORN, Icelandic
+ initialMap.put("\u00DF", "ß"); // � - lowercase sharps, German
+ initialMap.put("\u00E0", "à"); // � - lowercase a, grave accent
+ initialMap.put("\u00E1", "á"); // � - lowercase a, acute accent
+ initialMap.put("\u00E2", "â"); // � - lowercase a, circumflex accent
+ initialMap.put("\u00E3", "ã"); // � - lowercase a, tilde
+ initialMap.put("\u00E4", "ä"); // � - lowercase a, umlaut
+ initialMap.put("\u00E5", "å"); // � - lowercase a, ring
+ initialMap.put("\u00E6", "æ"); // � - lowercase ae
+ initialMap.put("\u00E7", "ç"); // � - lowercase c, cedilla
+ initialMap.put("\u00E8", "è"); // � - lowercase e, grave accent
+ initialMap.put("\u00E9", "é"); // � - lowercase e, acute accent
+ initialMap.put("\u00EA", "ê"); // � - lowercase e, circumflex accent
+ initialMap.put("\u00EB", "ë"); // � - lowercase e, umlaut
+ initialMap.put("\u00EC", "ì"); // � - lowercase i, grave accent
+ initialMap.put("\u00ED", "í"); // � - lowercase i, acute accent
+ initialMap.put("\u00EE", "î"); // � - lowercase i, circumflex accent
+ initialMap.put("\u00EF", "ï"); // � - lowercase i, umlaut
+ initialMap.put("\u00F0", "ð"); // � - lowercase eth, Icelandic
+ initialMap.put("\u00F1", "ñ"); // � - lowercase n, tilde
+ initialMap.put("\u00F2", "ò"); // � - lowercase o, grave accent
+ initialMap.put("\u00F3", "ó"); // � - lowercase o, acute accent
+ initialMap.put("\u00F4", "ô"); // � - lowercase o, circumflex accent
+ initialMap.put("\u00F5", "õ"); // � - lowercase o, tilde
+ initialMap.put("\u00F6", "ö"); // � - lowercase o, umlaut
+ initialMap.put("\u00F7", "÷"); // division sign
+ initialMap.put("\u00F8", "ø"); // � - lowercase o, slash
+ initialMap.put("\u00F9", "ù"); // � - lowercase u, grave accent
+ initialMap.put("\u00FA", "ú"); // � - lowercase u, acute accent
+ initialMap.put("\u00FB", "û"); // � - lowercase u, circumflex accent
+ initialMap.put("\u00FC", "ü"); // � - lowercase u, umlaut
+ initialMap.put("\u00FD", "ý"); // � - lowercase y, acute accent
+ initialMap.put("\u00FE", "þ"); // � - lowercase thorn, Icelandic
+ initialMap.put("\u00FF", "ÿ"); // � - lowercase y, umlaut
+ ISO8859_1_ESCAPE = Collections.unmodifiableMap(initialMap);
+ }
+
+ /**
+ * Reverse of {@link #ISO8859_1_ESCAPE} for unescaping purposes.
+ */
+ public static final Map<CharSequence, CharSequence> ISO8859_1_UNESCAPE;
+ static {
+ ISO8859_1_UNESCAPE = Collections.unmodifiableMap(invert(ISO8859_1_ESCAPE));
+ }
+
+ /**
+ * A Map<CharSequence, CharSequence> to escape additional
+ * <a href="http://www.w3.org/TR/REC-html40/sgml/entities.html">character entity
+ * references</a>. Note that this must be used with {@link #ISO8859_1_ESCAPE} to get the full list of
+ * HTML 4.0 character entities.
+ */
+ public static final Map<CharSequence, CharSequence> HTML40_EXTENDED_ESCAPE;
+ static {
+ Map<CharSequence, CharSequence> initialMap = new HashMap<>();
+ // <!-- Latin Extended-B -->
+ initialMap.put("\u0192", "ƒ"); // latin small f with hook = function= florin, U+0192 ISOtech -->
+ // <!-- Greek -->
+ initialMap.put("\u0391", "Α"); // greek capital letter alpha, U+0391 -->
+ initialMap.put("\u0392", "Β"); // greek capital letter beta, U+0392 -->
+ initialMap.put("\u0393", "Γ"); // greek capital letter gamma,U+0393 ISOgrk3 -->
+ initialMap.put("\u0394", "Δ"); // greek capital letter delta,U+0394 ISOgrk3 -->
+ initialMap.put("\u0395", "Ε"); // greek capital letter epsilon, U+0395 -->
+ initialMap.put("\u0396", "Ζ"); // greek capital letter zeta, U+0396 -->
+ initialMap.put("\u0397", "Η"); // greek capital letter eta, U+0397 -->
+ initialMap.put("\u0398", "Θ"); // greek capital letter theta,U+0398 ISOgrk3 -->
+ initialMap.put("\u0399", "Ι"); // greek capital letter iota, U+0399 -->
+ initialMap.put("\u039A", "Κ"); // greek capital letter kappa, U+039A -->
+ initialMap.put("\u039B", "Λ"); // greek capital letter lambda,U+039B ISOgrk3 -->
+ initialMap.put("\u039C", "Μ"); // greek capital letter mu, U+039C -->
+ initialMap.put("\u039D", "Ν"); // greek capital letter nu, U+039D -->
+ initialMap.put("\u039E", "Ξ"); // greek capital letter xi, U+039E ISOgrk3 -->
+ initialMap.put("\u039F", "Ο"); // greek capital letter omicron, U+039F -->
+ initialMap.put("\u03A0", "Π"); // greek capital letter pi, U+03A0 ISOgrk3 -->
+ initialMap.put("\u03A1", "Ρ"); // greek capital letter rho, U+03A1 -->
+ // <!-- there is no Sigmaf, and no U+03A2 character either -->
+ initialMap.put("\u03A3", "Σ"); // greek capital letter sigma,U+03A3 ISOgrk3 -->
+ initialMap.put("\u03A4", "Τ"); // greek capital letter tau, U+03A4 -->
+ initialMap.put("\u03A5", "Υ"); // greek capital letter upsilon,U+03A5 ISOgrk3 -->
+ initialMap.put("\u03A6", "Φ"); // greek capital letter phi,U+03A6 ISOgrk3 -->
+ initialMap.put("\u03A7", "Χ"); // greek capital letter chi, U+03A7 -->
+ initialMap.put("\u03A8", "Ψ"); // greek capital letter psi,U+03A8 ISOgrk3 -->
+ initialMap.put("\u03A9", "Ω"); // greek capital letter omega,U+03A9 ISOgrk3 -->
+ initialMap.put("\u03B1", "α"); // greek small letter alpha,U+03B1 ISOgrk3 -->
+ initialMap.put("\u03B2", "β"); // greek small letter beta, U+03B2 ISOgrk3 -->
+ initialMap.put("\u03B3", "γ"); // greek small letter gamma,U+03B3 ISOgrk3 -->
+ initialMap.put("\u03B4", "δ"); // greek small letter delta,U+03B4 ISOgrk3 -->
+ initialMap.put("\u03B5", "ε"); // greek small letter epsilon,U+03B5 ISOgrk3 -->
+ initialMap.put("\u03B6", "ζ"); // greek small letter zeta, U+03B6 ISOgrk3 -->
+ initialMap.put("\u03B7", "η"); // greek small letter eta, U+03B7 ISOgrk3 -->
+ initialMap.put("\u03B8", "θ"); // greek small letter theta,U+03B8 ISOgrk3 -->
+ initialMap.put("\u03B9", "ι"); // greek small letter iota, U+03B9 ISOgrk3 -->
+ initialMap.put("\u03BA", "κ"); // greek small letter kappa,U+03BA ISOgrk3 -->
+ initialMap.put("\u03BB", "λ"); // greek small letter lambda,U+03BB ISOgrk3 -->
+ initialMap.put("\u03BC", "μ"); // greek small letter mu, U+03BC ISOgrk3 -->
+ initialMap.put("\u03BD", "ν"); // greek small letter nu, U+03BD ISOgrk3 -->
+ initialMap.put("\u03BE", "ξ"); // greek small letter xi, U+03BE ISOgrk3 -->
+ initialMap.put("\u03BF", "ο"); // greek small letter omicron, U+03BF NEW -->
+ initialMap.put("\u03C0", "π"); // greek small letter pi, U+03C0 ISOgrk3 -->
+ initialMap.put("\u03C1", "ρ"); // greek small letter rho, U+03C1 ISOgrk3 -->
+ initialMap.put("\u03C2", "ς"); // greek small letter final sigma,U+03C2 ISOgrk3 -->
+ initialMap.put("\u03C3", "σ"); // greek small letter sigma,U+03C3 ISOgrk3 -->
+ initialMap.put("\u03C4", "τ"); // greek small letter tau, U+03C4 ISOgrk3 -->
+ initialMap.put("\u03C5", "υ"); // greek small letter upsilon,U+03C5 ISOgrk3 -->
+ initialMap.put("\u03C6", "φ"); // greek small letter phi, U+03C6 ISOgrk3 -->
+ initialMap.put("\u03C7", "χ"); // greek small letter chi, U+03C7 ISOgrk3 -->
+ initialMap.put("\u03C8", "ψ"); // greek small letter psi, U+03C8 ISOgrk3 -->
+ initialMap.put("\u03C9", "ω"); // greek small letter omega,U+03C9 ISOgrk3 -->
+ initialMap.put("\u03D1", "ϑ"); // greek small letter theta symbol,U+03D1 NEW -->
+ initialMap.put("\u03D2", "ϒ"); // greek upsilon with hook symbol,U+03D2 NEW -->
+ initialMap.put("\u03D6", "ϖ"); // greek pi symbol, U+03D6 ISOgrk3 -->
+ // <!-- General Punctuation -->
+ initialMap.put("\u2022", "•"); // bullet = black small circle,U+2022 ISOpub -->
+ // <!-- bullet is NOT the same as bullet operator, U+2219 -->
+ initialMap.put("\u2026", "…"); // horizontal ellipsis = three dot leader,U+2026 ISOpub -->
+ initialMap.put("\u2032", "′"); // prime = minutes = feet, U+2032 ISOtech -->
+ initialMap.put("\u2033", "″"); // double prime = seconds = inches,U+2033 ISOtech -->
+ initialMap.put("\u203E", "‾"); // overline = spacing overscore,U+203E NEW -->
+ initialMap.put("\u2044", "⁄"); // fraction slash, U+2044 NEW -->
+ // <!-- Letterlike Symbols -->
+ initialMap.put("\u2118", "℘"); // script capital P = power set= Weierstrass p, U+2118 ISOamso -->
+ initialMap.put("\u2111", "ℑ"); // blackletter capital I = imaginary part,U+2111 ISOamso -->
+ initialMap.put("\u211C", "ℜ"); // blackletter capital R = real part symbol,U+211C ISOamso -->
+ initialMap.put("\u2122", "™"); // trade mark sign, U+2122 ISOnum -->
+ initialMap.put("\u2135", "ℵ"); // alef symbol = first transfinite cardinal,U+2135 NEW -->
+ // <!-- alef symbol is NOT the same as hebrew letter alef,U+05D0 although the
+ // same glyph could be used to depict both characters -->
+ // <!-- Arrows -->
+ initialMap.put("\u2190", "←"); // leftwards arrow, U+2190 ISOnum -->
+ initialMap.put("\u2191", "↑"); // upwards arrow, U+2191 ISOnum-->
+ initialMap.put("\u2192", "→"); // rightwards arrow, U+2192 ISOnum -->
+ initialMap.put("\u2193", "↓"); // downwards arrow, U+2193 ISOnum -->
+ initialMap.put("\u2194", "↔"); // left right arrow, U+2194 ISOamsa -->
+ initialMap.put("\u21B5", "↵"); // downwards arrow with corner leftwards= carriage return, U+21B5 NEW -->
+ initialMap.put("\u21D0", "⇐"); // leftwards double arrow, U+21D0 ISOtech -->
+ // <!-- ISO 10646 does not say that lArr is the same as the 'is implied by'
+ // arrow but also does not have any other character for that function.
+ // So ? lArr canbe used for 'is implied by' as ISOtech suggests -->
+ initialMap.put("\u21D1", "⇑"); // upwards double arrow, U+21D1 ISOamsa -->
+ initialMap.put("\u21D2", "⇒"); // rightwards double arrow,U+21D2 ISOtech -->
+ // <!-- ISO 10646 does not say this is the 'implies' character but does not
+ // have another character with this function so ?rArr can be used for
+ // 'implies' as ISOtech suggests -->
+ initialMap.put("\u21D3", "⇓"); // downwards double arrow, U+21D3 ISOamsa -->
+ initialMap.put("\u21D4", "⇔"); // left right double arrow,U+21D4 ISOamsa -->
+ // <!-- Mathematical Operators -->
+ initialMap.put("\u2200", "∀"); // for all, U+2200 ISOtech -->
+ initialMap.put("\u2202", "∂"); // partial differential, U+2202 ISOtech -->
+ initialMap.put("\u2203", "∃"); // there exists, U+2203 ISOtech -->
+ initialMap.put("\u2205", "∅"); // empty set = null set = diameter,U+2205 ISOamso -->
+ initialMap.put("\u2207", "∇"); // nabla = backward difference,U+2207 ISOtech -->
+ initialMap.put("\u2208", "∈"); // element of, U+2208 ISOtech -->
+ initialMap.put("\u2209", "∉"); // not an element of, U+2209 ISOtech -->
+ initialMap.put("\u220B", "∋"); // contains as member, U+220B ISOtech -->
+ // <!-- should there be a more memorable name than 'ni'? -->
+ initialMap.put("\u220F", "∏"); // n-ary product = product sign,U+220F ISOamsb -->
+ // <!-- prod is NOT the same character as U+03A0 'greek capital letter pi'
+ // though the same glyph might be used for both -->
+ initialMap.put("\u2211", "∑"); // n-ary summation, U+2211 ISOamsb -->
+ // <!-- sum is NOT the same character as U+03A3 'greek capital letter sigma'
+ // though the same glyph might be used for both -->
+ initialMap.put("\u2212", "−"); // minus sign, U+2212 ISOtech -->
+ initialMap.put("\u2217", "∗"); // asterisk operator, U+2217 ISOtech -->
+ initialMap.put("\u221A", "√"); // square root = radical sign,U+221A ISOtech -->
+ initialMap.put("\u221D", "∝"); // proportional to, U+221D ISOtech -->
+ initialMap.put("\u221E", "∞"); // infinity, U+221E ISOtech -->
+ initialMap.put("\u2220", "∠"); // angle, U+2220 ISOamso -->
+ initialMap.put("\u2227", "∧"); // logical and = wedge, U+2227 ISOtech -->
+ initialMap.put("\u2228", "∨"); // logical or = vee, U+2228 ISOtech -->
+ initialMap.put("\u2229", "∩"); // intersection = cap, U+2229 ISOtech -->
+ initialMap.put("\u222A", "∪"); // union = cup, U+222A ISOtech -->
+ initialMap.put("\u222B", "∫"); // integral, U+222B ISOtech -->
+ initialMap.put("\u2234", "∴"); // therefore, U+2234 ISOtech -->
+ initialMap.put("\u223C", "∼"); // tilde operator = varies with = similar to,U+223C ISOtech -->
+ // <!-- tilde operator is NOT the same character as the tilde, U+007E,although
+ // the same glyph might be used to represent both -->
+ initialMap.put("\u2245", "≅"); // approximately equal to, U+2245 ISOtech -->
+ initialMap.put("\u2248", "≈"); // almost equal to = asymptotic to,U+2248 ISOamsr -->
+ initialMap.put("\u2260", "≠"); // not equal to, U+2260 ISOtech -->
+ initialMap.put("\u2261", "≡"); // identical to, U+2261 ISOtech -->
+ initialMap.put("\u2264", "≤"); // less-than or equal to, U+2264 ISOtech -->
+ initialMap.put("\u2265", "≥"); // greater-than or equal to,U+2265 ISOtech -->
+ initialMap.put("\u2282", "⊂"); // subset of, U+2282 ISOtech -->
+ initialMap.put("\u2283", "⊃"); // superset of, U+2283 ISOtech -->
+ // <!-- note that nsup, 'not a superset of, U+2283' is not covered by the
+ // Symbol font encoding and is not included. Should it be, for symmetry?
+ // It is in ISOamsn -->,
+ initialMap.put("\u2284", "⊄"); // not a subset of, U+2284 ISOamsn -->
+ initialMap.put("\u2286", "⊆"); // subset of or equal to, U+2286 ISOtech -->
+ initialMap.put("\u2287", "⊇"); // superset of or equal to,U+2287 ISOtech -->
+ initialMap.put("\u2295", "⊕"); // circled plus = direct sum,U+2295 ISOamsb -->
+ initialMap.put("\u2297", "⊗"); // circled times = vector product,U+2297 ISOamsb -->
+ initialMap.put("\u22A5", "⊥"); // up tack = orthogonal to = perpendicular,U+22A5 ISOtech -->
+ initialMap.put("\u22C5", "⋅"); // dot operator, U+22C5 ISOamsb -->
+ // <!-- dot operator is NOT the same character as U+00B7 middle dot -->
+ // <!-- Miscellaneous Technical -->
+ initialMap.put("\u2308", "⌈"); // left ceiling = apl upstile,U+2308 ISOamsc -->
+ initialMap.put("\u2309", "⌉"); // right ceiling, U+2309 ISOamsc -->
+ initialMap.put("\u230A", "⌊"); // left floor = apl downstile,U+230A ISOamsc -->
+ initialMap.put("\u230B", "⌋"); // right floor, U+230B ISOamsc -->
+ initialMap.put("\u2329", "⟨"); // left-pointing angle bracket = bra,U+2329 ISOtech -->
+ // <!-- lang is NOT the same character as U+003C 'less than' or U+2039 'single left-pointing angle quotation
+ // mark' -->
+ initialMap.put("\u232A", "⟩"); // right-pointing angle bracket = ket,U+232A ISOtech -->
+ // <!-- rang is NOT the same character as U+003E 'greater than' or U+203A
+ // 'single right-pointing angle quotation mark' -->
+ // <!-- Geometric Shapes -->
+ initialMap.put("\u25CA", "◊"); // lozenge, U+25CA ISOpub -->
+ // <!-- Miscellaneous Symbols -->
+ initialMap.put("\u2660", "♠"); // black spade suit, U+2660 ISOpub -->
+ // <!-- black here seems to mean filled as opposed to hollow -->
+ initialMap.put("\u2663", "♣"); // black club suit = shamrock,U+2663 ISOpub -->
+ initialMap.put("\u2665", "♥"); // black heart suit = valentine,U+2665 ISOpub -->
+ initialMap.put("\u2666", "♦"); // black diamond suit, U+2666 ISOpub -->
+
+ // <!-- Latin Extended-A -->
+ initialMap.put("\u0152", "Œ"); // -- latin capital ligature OE,U+0152 ISOlat2 -->
+ initialMap.put("\u0153", "œ"); // -- latin small ligature oe, U+0153 ISOlat2 -->
+ // <!-- ligature is a misnomer, this is a separate character in some languages -->
+ initialMap.put("\u0160", "Š"); // -- latin capital letter S with caron,U+0160 ISOlat2 -->
+ initialMap.put("\u0161", "š"); // -- latin small letter s with caron,U+0161 ISOlat2 -->
+ initialMap.put("\u0178", "Ÿ"); // -- latin capital letter Y with diaeresis,U+0178 ISOlat2 -->
+ // <!-- Spacing Modifier Letters -->
+ initialMap.put("\u02C6", "ˆ"); // -- modifier letter circumflex accent,U+02C6 ISOpub -->
+ initialMap.put("\u02DC", "˜"); // small tilde, U+02DC ISOdia -->
+ // <!-- General Punctuation -->
+ initialMap.put("\u2002", " "); // en space, U+2002 ISOpub -->
+ initialMap.put("\u2003", " "); // em space, U+2003 ISOpub -->
+ initialMap.put("\u2009", " "); // thin space, U+2009 ISOpub -->
+ initialMap.put("\u200C", "‌"); // zero width non-joiner,U+200C NEW RFC 2070 -->
+ initialMap.put("\u200D", "‍"); // zero width joiner, U+200D NEW RFC 2070 -->
+ initialMap.put("\u200E", "‎"); // left-to-right mark, U+200E NEW RFC 2070 -->
+ initialMap.put("\u200F", "‏"); // right-to-left mark, U+200F NEW RFC 2070 -->
+ initialMap.put("\u2013", "–"); // en dash, U+2013 ISOpub -->
+ initialMap.put("\u2014", "—"); // em dash, U+2014 ISOpub -->
+ initialMap.put("\u2018", "‘"); // left single quotation mark,U+2018 ISOnum -->
+ initialMap.put("\u2019", "’"); // right single quotation mark,U+2019 ISOnum -->
+ initialMap.put("\u201A", "‚"); // single low-9 quotation mark, U+201A NEW -->
+ initialMap.put("\u201C", "“"); // left double quotation mark,U+201C ISOnum -->
+ initialMap.put("\u201D", "”"); // right double quotation mark,U+201D ISOnum -->
+ initialMap.put("\u201E", "„"); // double low-9 quotation mark, U+201E NEW -->
+ initialMap.put("\u2020", "†"); // dagger, U+2020 ISOpub -->
+ initialMap.put("\u2021", "‡"); // double dagger, U+2021 ISOpub -->
+ initialMap.put("\u2030", "‰"); // per mille sign, U+2030 ISOtech -->
+ initialMap.put("\u2039", "‹"); // single left-pointing angle quotation mark,U+2039 ISO proposed -->
+ // <!-- lsaquo is proposed but not yet ISO standardized -->
+ initialMap.put("\u203A", "›"); // single right-pointing angle quotation mark,U+203A ISO proposed -->
+ // <!-- rsaquo is proposed but not yet ISO standardized -->
+ initialMap.put("\u20AC", "€"); // -- euro sign, U+20AC NEW -->
+ HTML40_EXTENDED_ESCAPE = Collections.unmodifiableMap(initialMap);
+ }
+
+ /**
+ * Reverse of {@link #HTML40_EXTENDED_ESCAPE} for unescaping purposes.
+ */
+ public static final Map<CharSequence, CharSequence> HTML40_EXTENDED_UNESCAPE;
+ static {
+ HTML40_EXTENDED_UNESCAPE = Collections.unmodifiableMap(invert(HTML40_EXTENDED_ESCAPE));
+ }
+
+ /**
+ * A Map<CharSequence, CharSequence> to escape the basic XML and HTML
+ * character entities.
+ *
+ * Namely: {@code " & < >}
+ */
+ public static final Map<CharSequence, CharSequence> BASIC_ESCAPE;
+ static {
+ Map<CharSequence, CharSequence> initialMap = new HashMap<>();
+ initialMap.put("\"", """); // " - double-quote
+ initialMap.put("&", "&"); // & - ampersand
+ initialMap.put("<", "<"); // < - less-than
+ initialMap.put(">", ">"); // > - greater-than
+ BASIC_ESCAPE = Collections.unmodifiableMap(initialMap);
+ }
+
+ /**
+ * Reverse of {@link #BASIC_ESCAPE} for unescaping purposes.
+ */
+ public static final Map<CharSequence, CharSequence> BASIC_UNESCAPE;
+ static {
+ BASIC_UNESCAPE = Collections.unmodifiableMap(invert(BASIC_ESCAPE));
+ }
+
+ /**
+ * A Map<CharSequence, CharSequence> to escape the apostrophe character to
+ * its XML character entity.
+ */
+ public static final Map<CharSequence, CharSequence> APOS_ESCAPE;
+ static {
+ Map<CharSequence, CharSequence> initialMap = new HashMap<>();
+ initialMap.put("'","'"); // XML apostrophe
+ APOS_ESCAPE = Collections.unmodifiableMap(initialMap);
+ }
+
+ /**
+ * Reverse of {@link #APOS_ESCAPE} for unescaping purposes.
+ */
+ public static final Map<CharSequence, CharSequence> APOS_UNESCAPE;
+ static {
+ APOS_UNESCAPE = Collections.unmodifiableMap(invert(APOS_ESCAPE));
+ }
+
+ /**
+ * A Map<CharSequence, CharSequence> to escape the Java
+ * control characters.
+ *
+ * Namely: {@code \b \n \t \f \r}
+ */
+ public static final Map<CharSequence, CharSequence> JAVA_CTRL_CHARS_ESCAPE;
+ static {
+ Map<CharSequence, CharSequence> initialMap = new HashMap<>();
+ initialMap.put("\b", "\\b");
+ initialMap.put("\n", "\\n");
+ initialMap.put("\t", "\\t");
+ initialMap.put("\f", "\\f");
+ initialMap.put("\r", "\\r");
+ JAVA_CTRL_CHARS_ESCAPE = Collections.unmodifiableMap(initialMap);
+ }
+
+ /**
+ * Reverse of {@link #JAVA_CTRL_CHARS_ESCAPE} for unescaping purposes.
+ */
+ public static final Map<CharSequence, CharSequence> JAVA_CTRL_CHARS_UNESCAPE;
+ static {
+ JAVA_CTRL_CHARS_UNESCAPE = Collections.unmodifiableMap(invert(JAVA_CTRL_CHARS_ESCAPE));
+ }
+
+ /**
+ * Used to invert an escape Map into an unescape Map
+ * @param map Map<String, String> to be inverted
+ * @return Map<String, String> inverted array
+ */
+ public static Map<CharSequence, CharSequence> invert(final Map<CharSequence, CharSequence> map) {
+ Map<CharSequence, CharSequence> newMap = new HashMap<>();
+ Iterator<Map.Entry<CharSequence, CharSequence>> it = map.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<CharSequence, CharSequence> pair = it.next();
+ newMap.put((CharSequence) pair.getValue(), (CharSequence) pair.getKey());
+ }
+ return newMap;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/JavaUnicodeEscaper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/JavaUnicodeEscaper.java b/src/main/java/org/apache/commons/text/translate/JavaUnicodeEscaper.java
new file mode 100644
index 0000000..22549fa
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/JavaUnicodeEscaper.java
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+/**
+ * Translates codepoints to their Unicode escaped value suitable for Java source.
+ *
+ * @since 1.0
+ */
+public class JavaUnicodeEscaper extends UnicodeEscaper {
+
+ /**
+ * <p>
+ * Constructs a <code>JavaUnicodeEscaper</code> above the specified value (exclusive).
+ * </p>
+ *
+ * @param codepoint
+ * above which to escape
+ * @return the newly created {@code UnicodeEscaper} instance
+ */
+ public static JavaUnicodeEscaper above(final int codepoint) {
+ return outsideOf(0, codepoint);
+ }
+
+ /**
+ * <p>
+ * Constructs a <code>JavaUnicodeEscaper</code> below the specified value (exclusive).
+ * </p>
+ *
+ * @param codepoint
+ * below which to escape
+ * @return the newly created {@code UnicodeEscaper} instance
+ */
+ public static JavaUnicodeEscaper below(final int codepoint) {
+ return outsideOf(codepoint, Integer.MAX_VALUE);
+ }
+
+ /**
+ * <p>
+ * Constructs a <code>JavaUnicodeEscaper</code> between the specified values (inclusive).
+ * </p>
+ *
+ * @param codepointLow
+ * above which to escape
+ * @param codepointHigh
+ * below which to escape
+ * @return the newly created {@code UnicodeEscaper} instance
+ */
+ public static JavaUnicodeEscaper between(final int codepointLow, final int codepointHigh) {
+ return new JavaUnicodeEscaper(codepointLow, codepointHigh, true);
+ }
+
+ /**
+ * <p>
+ * Constructs a <code>JavaUnicodeEscaper</code> outside of the specified values (exclusive).
+ * </p>
+ *
+ * @param codepointLow
+ * below which to escape
+ * @param codepointHigh
+ * above which to escape
+ * @return the newly created {@code UnicodeEscaper} instance
+ */
+ public static JavaUnicodeEscaper outsideOf(final int codepointLow, final int codepointHigh) {
+ return new JavaUnicodeEscaper(codepointLow, codepointHigh, false);
+ }
+
+ /**
+ * <p>
+ * Constructs a <code>JavaUnicodeEscaper</code> for the specified range. This is the underlying method for the
+ * other constructors/builders. The <code>below</code> and <code>above</code> boundaries are inclusive when
+ * <code>between</code> is <code>true</code> and exclusive when it is <code>false</code>.
+ * </p>
+ *
+ * @param below
+ * int value representing the lowest codepoint boundary
+ * @param above
+ * int value representing the highest codepoint boundary
+ * @param between
+ * whether to escape between the boundaries or outside them
+ */
+ public JavaUnicodeEscaper(final int below, final int above, final boolean between) {
+ super(below, above, between);
+ }
+
+ /**
+ * Converts the given codepoint to a hex string of the form {@code "\\uXXXX\\uXXXX"}
+ *
+ * @param codepoint
+ * a Unicode code point
+ * @return the hex string for the given codepoint
+ */
+ @Override
+ protected String toUtf16Escape(final int codepoint) {
+ final char[] surrogatePair = Character.toChars(codepoint);
+ return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/LookupTranslator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/LookupTranslator.java b/src/main/java/org/apache/commons/text/translate/LookupTranslator.java
new file mode 100644
index 0000000..ab3a7f6
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/LookupTranslator.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.security.InvalidParameterException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * Translates a value using a lookup table.
+ *
+ * @since 1.0
+ */
+public class LookupTranslator extends CharSequenceTranslator {
+
+ private final Map<String, String> lookupMap;
+ private final HashSet<Character> prefixSet;
+ private final int shortest;
+ private final int longest;
+
+ /**
+ * Define the lookup table to be used in translation
+ *
+ * Note that, as of Lang 3.1 (the orgin of this code), the key to the lookup
+ * table is converted to a java.lang.String. This is because we need the key
+ * to support hashCode and equals(Object), allowing it to be the key for a
+ * HashMap. See LANG-882.
+ *
+ * @param lookupMap Map<CharSequence, CharSequence> table of translator
+ * mappings
+ */
+ public LookupTranslator(final Map<CharSequence, CharSequence> lookupMap) {
+ if (lookupMap == null) {
+ throw new InvalidParameterException("lookupMap cannot be null");
+ }
+ this.lookupMap = new HashMap<>();
+ prefixSet = new HashSet<>();
+ int _shortest = Integer.MAX_VALUE;
+ int _longest = 0;
+ Iterator<Map.Entry<CharSequence, CharSequence>> it = lookupMap.entrySet().iterator();
+
+ while (it.hasNext()) {
+ Map.Entry<CharSequence, CharSequence> pair = it.next();
+ this.lookupMap.put(pair.getKey().toString(), pair.getValue().toString());
+ this.prefixSet.add((pair.getKey()).charAt(0));
+ final int sz = pair.getKey().length();
+ if (sz < _shortest) {
+ _shortest = sz;
+ }
+ if (sz > _longest) {
+ _longest = sz;
+ }
+ }
+ shortest = _shortest;
+ longest = _longest;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
+ // check if translation exists for the input at position index
+ if (prefixSet.contains(input.charAt(index))) {
+ int max = longest;
+ if (index + longest > input.length()) {
+ max = input.length() - index;
+ }
+ // implement greedy algorithm by trying maximum match first
+ for (int i = max; i >= shortest; i--) {
+ final CharSequence subSeq = input.subSequence(index, index + i);
+ final String result = lookupMap.get(subSeq.toString());
+
+ if (result != null) {
+ out.write(result);
+ return i;
+ }
+ }
+ }
+ return 0;
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/NumericEntityEscaper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/NumericEntityEscaper.java b/src/main/java/org/apache/commons/text/translate/NumericEntityEscaper.java
new file mode 100644
index 0000000..a852964
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/NumericEntityEscaper.java
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Translates codepoints to their XML numeric entity escaped value.
+ *
+ * @since 1.0
+ */
+public class NumericEntityEscaper extends CodePointTranslator {
+
+ private final int below;
+ private final int above;
+ private final boolean between;
+
+ /**
+ * <p>Constructs a <code>NumericEntityEscaper</code> for the specified range. This is
+ * the underlying method for the other constructors/builders. The <code>below</code>
+ * and <code>above</code> boundaries are inclusive when <code>between</code> is
+ * <code>true</code> and exclusive when it is <code>false</code>. </p>
+ *
+ * @param below int value representing the lowest codepoint boundary
+ * @param above int value representing the highest codepoint boundary
+ * @param between whether to escape between the boundaries or outside them
+ */
+ private NumericEntityEscaper(final int below, final int above, final boolean between) {
+ this.below = below;
+ this.above = above;
+ this.between = between;
+ }
+
+ /**
+ * <p>Constructs a <code>NumericEntityEscaper</code> for all characters. </p>
+ */
+ public NumericEntityEscaper() {
+ this(0, Integer.MAX_VALUE, true);
+ }
+
+ /**
+ * <p>Constructs a <code>NumericEntityEscaper</code> below the specified value (exclusive). </p>
+ *
+ * @param codepoint below which to escape
+ * @return the newly created {@code NumericEntityEscaper} instance
+ */
+ public static NumericEntityEscaper below(final int codepoint) {
+ return outsideOf(codepoint, Integer.MAX_VALUE);
+ }
+
+ /**
+ * <p>Constructs a <code>NumericEntityEscaper</code> above the specified value (exclusive). </p>
+ *
+ * @param codepoint above which to escape
+ * @return the newly created {@code NumericEntityEscaper} instance
+ */
+ public static NumericEntityEscaper above(final int codepoint) {
+ return outsideOf(0, codepoint);
+ }
+
+ /**
+ * <p>Constructs a <code>NumericEntityEscaper</code> between the specified values (inclusive). </p>
+ *
+ * @param codepointLow above which to escape
+ * @param codepointHigh below which to escape
+ * @return the newly created {@code NumericEntityEscaper} instance
+ */
+ public static NumericEntityEscaper between(final int codepointLow, final int codepointHigh) {
+ return new NumericEntityEscaper(codepointLow, codepointHigh, true);
+ }
+
+ /**
+ * <p>Constructs a <code>NumericEntityEscaper</code> outside of the specified values (exclusive). </p>
+ *
+ * @param codepointLow below which to escape
+ * @param codepointHigh above which to escape
+ * @return the newly created {@code NumericEntityEscaper} instance
+ */
+ public static NumericEntityEscaper outsideOf(final int codepointLow, final int codepointHigh) {
+ return new NumericEntityEscaper(codepointLow, codepointHigh, false);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean translate(final int codepoint, final Writer out) throws IOException {
+ if(between) {
+ if (codepoint < below || codepoint > above) {
+ return false;
+ }
+ } else {
+ if (codepoint >= below && codepoint <= above) {
+ return false;
+ }
+ }
+
+ out.write("&#");
+ out.write(Integer.toString(codepoint, 10));
+ out.write(';');
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/NumericEntityUnescaper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/NumericEntityUnescaper.java b/src/main/java/org/apache/commons/text/translate/NumericEntityUnescaper.java
new file mode 100644
index 0000000..8b3d7c7
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/NumericEntityUnescaper.java
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Arrays;
+import java.util.EnumSet;
+
+/**
+ * Translate XML numeric entities of the form &#[xX]?\d+;? to
+ * the specific codepoint.
+ *
+ * Note that the semi-colon is optional.
+ *
+ * @since 1.0
+ */
+public class NumericEntityUnescaper extends CharSequenceTranslator {
+
+ public static enum OPTION { semiColonRequired, semiColonOptional, errorIfNoSemiColon }
+
+ // TODO?: Create an OptionsSet class to hide some of the conditional logic below
+ private final EnumSet<OPTION> options;
+
+ /**
+ * Create a UnicodeUnescaper.
+ *
+ * The constructor takes a list of options, only one type of which is currently
+ * available (whether to allow, error or ignore the semi-colon on the end of a
+ * numeric entity to being missing).
+ *
+ * For example, to support numeric entities without a ';':
+ * new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional)
+ * and to throw an IllegalArgumentException when they're missing:
+ * new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon)
+ *
+ * Note that the default behaviour is to ignore them.
+ *
+ * @param options to apply to this unescaper
+ */
+ public NumericEntityUnescaper(final OPTION... options) {
+ if(options.length > 0) {
+ this.options = EnumSet.copyOf(Arrays.asList(options));
+ } else {
+ this.options = EnumSet.copyOf(Arrays.asList(new OPTION[] { OPTION.semiColonRequired }));
+ }
+ }
+
+ /**
+ * Whether the passed in option is currently set.
+ *
+ * @param option to check state of
+ * @return whether the option is set
+ */
+ public boolean isSet(final OPTION option) {
+ return options == null ? false : options.contains(option);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
+ final int seqEnd = input.length();
+ // Uses -2 to ensure there is something after the &#
+ if(input.charAt(index) == '&' && index < seqEnd - 2 && input.charAt(index + 1) == '#') {
+ int start = index + 2;
+ boolean isHex = false;
+
+ final char firstChar = input.charAt(start);
+ if(firstChar == 'x' || firstChar == 'X') {
+ start++;
+ isHex = true;
+
+ // Check there's more than just an x after the &#
+ if(start == seqEnd) {
+ return 0;
+ }
+ }
+
+ int end = start;
+ // Note that this supports character codes without a ; on the end
+ while(end < seqEnd && ( input.charAt(end) >= '0' && input.charAt(end) <= '9' ||
+ input.charAt(end) >= 'a' && input.charAt(end) <= 'f' ||
+ input.charAt(end) >= 'A' && input.charAt(end) <= 'F' ) )
+ {
+ end++;
+ }
+
+ final boolean semiNext = end != seqEnd && input.charAt(end) == ';';
+
+ if(!semiNext) {
+ if(isSet(OPTION.semiColonRequired)) {
+ return 0;
+ } else
+ if(isSet(OPTION.errorIfNoSemiColon)) {
+ throw new IllegalArgumentException("Semi-colon required at end of numeric entity");
+ }
+ }
+
+ int entityValue;
+ try {
+ if(isHex) {
+ entityValue = Integer.parseInt(input.subSequence(start, end).toString(), 16);
+ } else {
+ entityValue = Integer.parseInt(input.subSequence(start, end).toString(), 10);
+ }
+ } catch(final NumberFormatException nfe) {
+ return 0;
+ }
+
+ if(entityValue > 0xFFFF) {
+ final char[] chrs = Character.toChars(entityValue);
+ out.write(chrs[0]);
+ out.write(chrs[1]);
+ } else {
+ out.write(entityValue);
+ }
+
+ return 2 + end - start + (isHex ? 1 : 0) + (semiNext ? 1 : 0);
+ }
+ return 0;
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/OctalUnescaper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/OctalUnescaper.java b/src/main/java/org/apache/commons/text/translate/OctalUnescaper.java
new file mode 100644
index 0000000..e49cdd5
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/OctalUnescaper.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Translate escaped octal Strings back to their octal values.
+ *
+ * For example, "\45" should go back to being the specific value (a %).
+ *
+ * Note that this currently only supports the viable range of octal for Java; namely
+ * 1 to 377. This is because parsing Java is the main use case.
+ *
+ * @since 1.0
+ */
+public class OctalUnescaper extends CharSequenceTranslator {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
+ final int remaining = input.length() - index - 1; // how many characters left, ignoring the first \
+ final StringBuilder builder = new StringBuilder();
+ if(input.charAt(index) == '\\' && remaining > 0 && isOctalDigit(input.charAt(index + 1)) ) {
+ final int next = index + 1;
+ final int next2 = index + 2;
+ final int next3 = index + 3;
+
+ // we know this is good as we checked it in the if block above
+ builder.append(input.charAt(next));
+
+ if(remaining > 1 && isOctalDigit(input.charAt(next2))) {
+ builder.append(input.charAt(next2));
+ if(remaining > 2 && isZeroToThree(input.charAt(next)) && isOctalDigit(input.charAt(next3))) {
+ builder.append(input.charAt(next3));
+ }
+ }
+
+ out.write( Integer.parseInt(builder.toString(), 8) );
+ return 1 + builder.length();
+ }
+ return 0;
+ }
+
+ /**
+ * Checks if the given char is an octal digit. Octal digits are the character representations of the digits 0 to 7.
+ * @param ch the char to check
+ * @return true if the given char is the character representation of one of the digits from 0 to 7
+ */
+ private boolean isOctalDigit(final char ch) {
+ return ch >= '0' && ch <= '7';
+ }
+
+ /**
+ * Checks if the given char is the character representation of one of the digit from 0 to 3.
+ * @param ch the char to check
+ * @return true if the given char is the character representation of one of the digits from 0 to 3
+ */
+ private boolean isZeroToThree(final char ch) {
+ return ch >= '0' && ch <= '3';
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/SingleLookupTranslator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/SingleLookupTranslator.java b/src/main/java/org/apache/commons/text/translate/SingleLookupTranslator.java
new file mode 100644
index 0000000..7af0579
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/SingleLookupTranslator.java
@@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * Translates a value using a lookup table.
+ * But doesn't translate if that value is already translated.
+ *
+ * @since 1.0
+ */
+public class SingleLookupTranslator extends CharSequenceTranslator {
+
+ private final Map<String, String> lookupMap;
+ private final HashSet<Character> prefixSet;
+ private final int shortest;
+ private final int longest;
+ private final int shortestValue;
+ private final int longestValue;
+
+ /**
+ * Define the look tables to be used in translation.
+ * <p>
+ * Note that, as of Lang 3.1, the key to the lookup table is converted to a
+ * java.lang.String. This is because we need the key to support hashCode and
+ * equals(Object), allowing it to be the key for a HashMap. See LANG-882.
+ * <p>
+ * Also note that, multiple lookup tables should be passed to this translator
+ * instead of passing multiple instances of this translator to the
+ * AggregateTranslator. Because, this translator only checks the values of the
+ * lookup table passed to this instance while deciding whether a value is
+ * already translated or not.
+ *
+ * @param inputMaps, an array of Map<CharSequence, CharSequence>.
+ */
+ public SingleLookupTranslator(Map<CharSequence, CharSequence>... inputMaps) {
+ Map<CharSequence, CharSequence> lookup = new HashMap<>();
+ for (Map<CharSequence, CharSequence> input : inputMaps) {
+ Iterator<Map.Entry<CharSequence, CharSequence>> it = input.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<CharSequence, CharSequence> pair = it.next();
+ lookup.put(pair.getKey(), pair.getValue());
+ }
+ }
+ lookupMap = new HashMap<String, String>();
+ prefixSet = new HashSet<Character>();
+ int _shortest = Integer.MAX_VALUE;
+ int _longest = 0;
+ int _shortestValue = Integer.MAX_VALUE;
+ int _longestValue = 0;
+ if (lookup != null) {
+ Iterator<Map.Entry<CharSequence, CharSequence>> it = lookup.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<CharSequence, CharSequence> pair = it.next();
+ this.lookupMap.put(pair.getKey().toString(), pair.getValue().toString());
+ this.prefixSet.add(pair.getKey().charAt(0));
+ final int sz = pair.getKey().length();
+ if (sz < _shortest) {
+ _shortest = sz;
+ }
+ if (sz > _longest) {
+ _longest = sz;
+ }
+ final int sizeOfValue = lookup.get(pair.getKey()).length();
+ if (sizeOfValue < _shortestValue) {
+ _shortestValue = sizeOfValue;
+ }
+ if (sizeOfValue > _longestValue) {
+ _longestValue = sizeOfValue;
+ }
+ }
+ }
+ shortest = _shortest;
+ longest = _longest;
+ shortestValue = _shortestValue;
+ longestValue = _longestValue;
+ }
+
+ /**
+ * Translate a set of codepoints, represented by an int index into a CharSequence,
+ * into another set of codepoints. The number of codepoints consumed must be returned,
+ * and the only IOExceptions thrown must be from interacting with the Writer so that
+ * the top level API may reliably ignore StringWriter IOExceptions.
+ *
+ * @param input CharSequence that is being translated
+ * @param index int representing the current point of translation
+ * @param out Writer to translate the text to
+ * @return int count of codepoints consumed
+ * @throws IOException if and only if the Writer produces an IOException
+ */
+ @Override
+ public int translate(CharSequence input, int index, Writer out) throws IOException {
+ // check if already translated
+ int maxValue = longestValue;
+ if (index + maxValue > input.length()) {
+ maxValue = input.length() - index;
+ }
+ // implement greedy algorithm to check all the possible 'value' matches
+ // for which we need to skip translation.
+ for (int i = maxValue; i >= shortestValue; i--) {
+ final CharSequence subSeq = input.subSequence(index, index + i);
+ // If the sub-string is already translated, return without translating.
+ if (lookupMap.containsValue(subSeq.toString())) {
+ return 0;
+ }
+ }
+
+ // check if translation exists for the input at position index
+ if (prefixSet.contains(input.charAt(index))) {
+ int max = longest;
+ if (index + longest > input.length()) {
+ max = input.length() - index;
+ }
+ // implement greedy algorithm by trying maximum match first
+ for (int i = max; i >= shortest; i--) {
+ final CharSequence subSeq = input.subSequence(index, index + i);
+ final String result = lookupMap.get(subSeq.toString());
+
+ if (result != null) {
+ out.write(result);
+ return i;
+ }
+ }
+ }
+ return 0;
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java b/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
new file mode 100644
index 0000000..721e727
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Abstract translator for processing whole input in single pass.
+ * Handles initial index checking and counting of returned code points.
+ */
+abstract class SinglePassTranslator extends CharSequenceTranslator {
+
+ @Override
+ public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
+ if (index != 0) {
+ throw new IllegalArgumentException(getClassName() + ".translate(final CharSequence input, final int " +
+ "index, final Writer out) can not handle a non-zero index.");
+ }
+
+ translateWhole(input, out);
+
+ return Character.codePointCount(input, index, input.length());
+ }
+
+ private String getClassName() {
+ final Class<? extends SinglePassTranslator> clazz = this.getClass();
+ return clazz.isAnonymousClass() ? clazz.getName() : clazz.getSimpleName();
+ }
+
+ /**
+ * Translate whole set of code points passed in input.
+ *
+ * @param input CharSequence that is being translated
+ * @param out Writer to translate the text to
+ * @return total count of codepoints in input
+ * @throws IOException if and only if the Writer produces an IOException
+ */
+ abstract void translateWhole(final CharSequence input, final Writer out) throws IOException;
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/UnicodeEscaper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/UnicodeEscaper.java b/src/main/java/org/apache/commons/text/translate/UnicodeEscaper.java
new file mode 100644
index 0000000..04b24e2
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/UnicodeEscaper.java
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Translates codepoints to their Unicode escaped value.
+ *
+ * @since 1.0
+ */
+public class UnicodeEscaper extends CodePointTranslator {
+
+ private final int below;
+ private final int above;
+ private final boolean between;
+
+ /**
+ * <p>Constructs a <code>UnicodeEscaper</code> for all characters. </p>
+ */
+ public UnicodeEscaper(){
+ this(0, Integer.MAX_VALUE, true);
+ }
+
+ /**
+ * <p>Constructs a <code>UnicodeEscaper</code> for the specified range. This is
+ * the underlying method for the other constructors/builders. The <code>below</code>
+ * and <code>above</code> boundaries are inclusive when <code>between</code> is
+ * <code>true</code> and exclusive when it is <code>false</code>. </p>
+ *
+ * @param below int value representing the lowest codepoint boundary
+ * @param above int value representing the highest codepoint boundary
+ * @param between whether to escape between the boundaries or outside them
+ */
+ protected UnicodeEscaper(final int below, final int above, final boolean between) {
+ this.below = below;
+ this.above = above;
+ this.between = between;
+ }
+
+ /**
+ * <p>Constructs a <code>UnicodeEscaper</code> below the specified value (exclusive). </p>
+ *
+ * @param codepoint below which to escape
+ * @return the newly created {@code UnicodeEscaper} instance
+ */
+ public static UnicodeEscaper below(final int codepoint) {
+ return outsideOf(codepoint, Integer.MAX_VALUE);
+ }
+
+ /**
+ * <p>Constructs a <code>UnicodeEscaper</code> above the specified value (exclusive). </p>
+ *
+ * @param codepoint above which to escape
+ * @return the newly created {@code UnicodeEscaper} instance
+ */
+ public static UnicodeEscaper above(final int codepoint) {
+ return outsideOf(0, codepoint);
+ }
+
+ /**
+ * <p>Constructs a <code>UnicodeEscaper</code> outside of the specified values (exclusive). </p>
+ *
+ * @param codepointLow below which to escape
+ * @param codepointHigh above which to escape
+ * @return the newly created {@code UnicodeEscaper} instance
+ */
+ public static UnicodeEscaper outsideOf(final int codepointLow, final int codepointHigh) {
+ return new UnicodeEscaper(codepointLow, codepointHigh, false);
+ }
+
+ /**
+ * <p>Constructs a <code>UnicodeEscaper</code> between the specified values (inclusive). </p>
+ *
+ * @param codepointLow above which to escape
+ * @param codepointHigh below which to escape
+ * @return the newly created {@code UnicodeEscaper} instance
+ */
+ public static UnicodeEscaper between(final int codepointLow, final int codepointHigh) {
+ return new UnicodeEscaper(codepointLow, codepointHigh, true);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean translate(final int codepoint, final Writer out) throws IOException {
+ if (between) {
+ if (codepoint < below || codepoint > above) {
+ return false;
+ }
+ } else {
+ if (codepoint >= below && codepoint <= above) {
+ return false;
+ }
+ }
+
+ // TODO: Handle potential + sign per various Unicode escape implementations
+ if (codepoint > 0xffff) {
+ out.write(toUtf16Escape(codepoint));
+ } else {
+ out.write("\\u");
+ out.write(HEX_DIGITS[(codepoint >> 12) & 15]);
+ out.write(HEX_DIGITS[(codepoint >> 8) & 15]);
+ out.write(HEX_DIGITS[(codepoint >> 4) & 15]);
+ out.write(HEX_DIGITS[(codepoint) & 15]);
+ }
+ return true;
+ }
+
+ /**
+ * Converts the given codepoint to a hex string of the form {@code "\\uXXXX"}
+ *
+ * @param codepoint
+ * a Unicode code point
+ * @return the hex string for the given codepoint
+ *
+ */
+ protected String toUtf16Escape(final int codepoint) {
+ return "\\u" + hex(codepoint);
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/UnicodeUnescaper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/UnicodeUnescaper.java b/src/main/java/org/apache/commons/text/translate/UnicodeUnescaper.java
new file mode 100644
index 0000000..3a91d6c
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/UnicodeUnescaper.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Translates escaped Unicode values of the form \\u+\d\d\d\d back to
+ * Unicode. It supports multiple 'u' characters and will work with or
+ * without the +.
+ *
+ * @since 1.0
+ */
+public class UnicodeUnescaper extends CharSequenceTranslator {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
+ if (input.charAt(index) == '\\' && index + 1 < input.length() && input.charAt(index + 1) == 'u') {
+ // consume optional additional 'u' chars
+ int i = 2;
+ while (index + i < input.length() && input.charAt(index + i) == 'u') {
+ i++;
+ }
+
+ if (index + i < input.length() && input.charAt(index + i) == '+') {
+ i++;
+ }
+
+ if (index + i + 4 <= input.length()) {
+ // Get 4 hex digits
+ final CharSequence unicode = input.subSequence(index + i, index + i + 4);
+
+ try {
+ final int value = Integer.parseInt(unicode.toString(), 16);
+ out.write((char) value);
+ } catch (final NumberFormatException nfe) {
+ throw new IllegalArgumentException("Unable to parse unicode value: " + unicode, nfe);
+ }
+ return i + 4;
+ }
+ throw new IllegalArgumentException("Less than 4 hex digits in unicode value: '" + input.subSequence(index, input.length())
+ + "' due to end of CharSequence");
+ }
+ return 0;
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/UnicodeUnpairedSurrogateRemover.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/UnicodeUnpairedSurrogateRemover.java b/src/main/java/org/apache/commons/text/translate/UnicodeUnpairedSurrogateRemover.java
new file mode 100644
index 0000000..ca90b89
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/UnicodeUnpairedSurrogateRemover.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Helper subclass to CharSequenceTranslator to remove unpaired surrogates.
+ *
+ * @since 1.0
+ */
+public class UnicodeUnpairedSurrogateRemover extends CodePointTranslator {
+ /**
+ * Implementation of translate that throws out unpaired surrogates.
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean translate(final int codepoint, final Writer out) throws IOException {
+ if (codepoint >= Character.MIN_SURROGATE && codepoint <= Character.MAX_SURROGATE) {
+ // It's a surrogate. Write nothing and say we've translated.
+ return true;
+ }
+ // It's not a surrogate. Don't translate it.
+ return false;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/main/java/org/apache/commons/text/translate/package-info.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/package-info.java b/src/main/java/org/apache/commons/text/translate/package-info.java
new file mode 100644
index 0000000..5ccb000
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/package-info.java
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * <p> An API for creating text translation routines from a set of smaller building blocks. Initially created to make it
+ * possible for the user to customize the rules in the StringEscapeUtils class.</p>
+ * <p>These classes are immutable, and therefore thread-safe.</p>
+ *
+ * @since 1.0
+ */
+package org.apache.commons.text.translate;
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/AlphabetConverterTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/AlphabetConverterTest.java b/src/test/java/org/apache/commons/text/AlphabetConverterTest.java
new file mode 100644
index 0000000..d235c0e
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/AlphabetConverterTest.java
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+/**
+ * Unit tests for {@link AlphabetConverter}.
+ */
+public class AlphabetConverterTest {
+
+ private static Character[] lower_case_english = {' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
+ private static Character[] english_and_numbers = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ' };
+ private static Character[] lower_case_english_and_numbers = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ' };
+ private static Character[] numbers = {'0','1','2','3','4','5','6','7','8','9'};
+ private static Character[] binary = {'0','1'};
+ private static Character[] hebrew = {'_', ' ', '\u05e7','\u05e8','\u05d0','\u05d8','\u05d5','\u05df','\u05dd','\u05e4','\u05e9','\u05d3','\u05d2','\u05db','\u05e2','\u05d9','\u05d7','\u05dc','\u05da','\u05e3','\u05d6','\u05e1','\u05d1','\u05d4','\u05e0','\u05de','\u05e6','\u05ea','\u05e5'};
+ private static Character[] empty = {};
+
+ private static Integer[] unicode = {32,35395,35397,36302,36291,35203,35201,35215,35219,35268,97,98,99,100,101,102,103,104,105,106,107,108,109,110,1001,1002,1003,1004,1005};
+ private static Integer[] lower_case_english_codepoints = {32,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122};
+ private static Integer[] doNotEncodePoints = {32,97,98,99}; // space, a, b, c
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void encodeFailureTest() throws UnsupportedEncodingException {
+ thrown.expect(UnsupportedEncodingException.class);
+ thrown.expectMessage("Couldn't find encoding for '3'");
+ test(binary, numbers, empty, "3");
+ }
+
+ @Test
+ public void binaryTest() throws UnsupportedEncodingException {
+ test(binary, numbers, empty, "0", "1", "10", "11");
+ test(numbers, binary, empty, "12345", "0");
+ test(lower_case_english, binary, empty, "abc", "a");
+ }
+
+ @Test
+ public void hebrewTest() throws UnsupportedEncodingException {
+ test(hebrew, binary, empty, "\u05d0", "\u05e2", "\u05d0\u05dc\u05e3_\u05d0\u05d5\u05d4\u05d1\u05dc_\u05d1\u05d9\u05ea_\u05d6\u05d4_\u05d1\u05d9\u05ea_\u05d2\u05d9\u05de\u05dc_\u05d6\u05d4_\u05db\u05de\u05dc_\u05d2\u05d3\u05d5\u05dc");
+ test(hebrew, numbers, empty, "\u05d0", "\u05e2", "\u05d0\u05dc\u05e3_\u05d0\u05d5\u05d4\u05d1\u05dc_\u05d1\u05d9\u05ea_\u05d6\u05d4_\u05d1\u05d9\u05ea_\u05d2\u05d9\u05de\u05dc_\u05d6\u05d4_\u05db\u05de\u05dc_\u05d2\u05d3\u05d5\u05dc");
+ test(numbers, hebrew, empty, "123456789", "1", "5");
+ test(lower_case_english, hebrew, empty, "this is a test");
+ }
+
+ @Test
+ public void doNotEncodeTest() throws UnsupportedEncodingException {
+ test(english_and_numbers, lower_case_english_and_numbers, lower_case_english, "1", "456", "abc", "ABC", "this will not be converted but THIS WILL");
+ test(english_and_numbers, lower_case_english_and_numbers, numbers, "1", "456", "abc", "ABC", "this will be converted but 12345 and this will be");
+ }
+
+ private AlphabetConverter createJavadocExample() {
+ final Character[] original = {'a','b','c','d'};
+ final Character[] encoding = {'0','1','d'};
+ final Character[] doNotEncode = {'d'};
+
+ return AlphabetConverter.createConverterFromChars(original, encoding, doNotEncode);
+ }
+
+ /*
+ * Test example in javadocs for consistency
+ */
+ @Test
+ public void javadocExampleTest() throws UnsupportedEncodingException {
+ final AlphabetConverter ac = createJavadocExample();
+
+ Assert.assertEquals("00", ac.encode("a"));
+ Assert.assertEquals("01", ac.encode("b"));
+ Assert.assertEquals("0d", ac.encode("c"));
+ Assert.assertEquals("d", ac.encode("d"));
+ Assert.assertEquals("00010dd", ac.encode("abcd"));
+ }
+
+ @Test
+ public void unexpectedEndwhileDecodingTest() throws UnsupportedEncodingException {
+ final String toDecode = "00d01d0";
+
+ thrown.expect(UnsupportedEncodingException.class);
+ thrown.expectMessage("Unexpected end of string while decoding " + toDecode);
+
+ final AlphabetConverter ac = createJavadocExample();
+ ac.decode(toDecode);
+ }
+
+ @Test
+ public void unexpectedStringWhileDecodingTest() throws UnsupportedEncodingException {
+ final String toDecode = "00XX";
+
+ thrown.expect(UnsupportedEncodingException.class);
+ thrown.expectMessage("Unexpected string without decoding (XX) in " + toDecode);
+
+ final AlphabetConverter ac = createJavadocExample();
+ ac.decode(toDecode);
+ }
+
+ /*
+ * Test constructor from code points
+ */
+ @Test
+ public void unicodeTest() throws UnsupportedEncodingException {
+ final AlphabetConverter ac = AlphabetConverter.createConverter(unicode, lower_case_english_codepoints, doNotEncodePoints);
+
+ Assert.assertEquals(2, ac.getEncodedCharLength());
+
+ final String original = "\u8a43\u8a45 \u8dce ab \u8dc3 c \u8983";
+ final String encoded = ac.encode(original);
+ final String decoded = ac.decode(encoded);
+
+ Assert.assertEquals("Encoded '" + original + "' into '" + encoded + "', but decoded into '" + decoded + "'", original, decoded);
+ }
+
+ @Test
+ public void noEncodingLettersTest() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Must have at least two encoding characters (excluding those in the 'do not encode' list), but has 0");
+
+ AlphabetConverter.createConverterFromChars(english_and_numbers, numbers, numbers);
+ }
+
+ @Test
+ public void onlyOneEncodingLettersTest() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Must have at least two encoding characters (excluding those in the 'do not encode' list), but has 1");
+
+ final Character[] numbersPlusUnderscore = Arrays.copyOf(numbers, numbers.length + 1);
+ numbersPlusUnderscore[numbersPlusUnderscore.length -1] = '_';
+
+ AlphabetConverter.createConverterFromChars(english_and_numbers, numbersPlusUnderscore, numbers);
+ }
+
+ @Test
+ public void missingDoNotEncodeLettersFromEncodingTest() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Can not use 'do not encode' list because encoding alphabet does not contain");
+
+ AlphabetConverter.createConverterFromChars(english_and_numbers, lower_case_english, numbers);
+ }
+
+ @Test
+ public void missingDoNotEncodeLettersFromOriginalTest() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Can not use 'do not encode' list because original alphabet does not contain");
+
+ AlphabetConverter.createConverterFromChars(lower_case_english, english_and_numbers, numbers);
+ }
+
+ private void test(final Character[] originalChars, final Character[] encodingChars, final Character[] doNotEncodeChars, final String... strings) throws UnsupportedEncodingException {
+
+ final AlphabetConverter ac = AlphabetConverter.createConverterFromChars(originalChars, encodingChars, doNotEncodeChars);
+
+ final AlphabetConverter reconstructedAlphabetConverter = AlphabetConverter.createConverterFromMap(ac.getOriginalToEncoded());
+
+ Assert.assertEquals(ac, reconstructedAlphabetConverter);
+ Assert.assertEquals(ac.hashCode(), reconstructedAlphabetConverter.hashCode());
+ Assert.assertEquals(ac.toString(), reconstructedAlphabetConverter.toString());
+ Assert.assertEquals(null, ac.encode(null)); // test null conversions
+ Assert.assertEquals("", ac.encode("")); // test empty conversion
+
+ // test all the trial strings
+ for (final String s : strings) {
+ final String encoded = ac.encode(s);
+
+ // test that only encoding chars are used
+ final List<Character> originalEncodingChars = Arrays.asList(encodingChars);
+ for (int i = 0; i < encoded.length(); i++) {
+ Assert.assertTrue(originalEncodingChars.contains(encoded.charAt(i)));
+ }
+
+ final String decoded = ac.decode(encoded);
+
+ // test that only the original alphabet is used after decoding
+ final List<Character> originalCharsList = Arrays.asList(originalChars);
+ for (int i = 0; i < decoded.length(); i++) {
+ Assert.assertTrue(originalCharsList.contains(decoded.charAt(i)));
+ }
+
+ Assert.assertEquals("Encoded '" + s + "' into '" + encoded + "', but decoded into '" + decoded + "'", s, decoded);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/CharacterPredicatesTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/CharacterPredicatesTest.java b/src/test/java/org/apache/commons/text/CharacterPredicatesTest.java
new file mode 100644
index 0000000..a22b466
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/CharacterPredicatesTest.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests for {@link CharacterPredicates}.
+ */
+public class CharacterPredicatesTest {
+
+ @Test
+ public void testLetters() throws Exception {
+ assertTrue(CharacterPredicates.LETTERS.test('a'));
+ assertTrue(CharacterPredicates.LETTERS.test('Z'));
+
+ assertFalse(CharacterPredicates.LETTERS.test('1'));
+ assertFalse(CharacterPredicates.LETTERS.test('?'));
+ assertFalse(CharacterPredicates.LETTERS.test('@'));
+ }
+
+ @Test
+ public void testDigits() {
+ assertTrue(CharacterPredicates.DIGITS.test('0'));
+ assertTrue(CharacterPredicates.DIGITS.test('9'));
+
+ assertFalse(CharacterPredicates.DIGITS.test('-'));
+ assertFalse(CharacterPredicates.DIGITS.test('.'));
+ assertFalse(CharacterPredicates.DIGITS.test('L'));
+ }
+}
\ No newline at end of file