use of android.icu.text.StringPrepParseException in project j2objc by google.
the class IDNAReference method convertToASCII.
public static StringBuffer convertToASCII(UCharacterIterator srcIter, int options) throws StringPrepParseException {
char[] caseFlags = null;
// the source contains all ascii codepoints
boolean srcIsASCII = true;
// assume the source contains all LDH codepoints
boolean srcIsLDH = true;
// get the options
boolean useSTD3ASCIIRules = ((options & USE_STD3_RULES) != 0);
int ch;
// step 1
while ((ch = srcIter.next()) != UCharacterIterator.DONE) {
if (ch > 0x7f) {
srcIsASCII = false;
}
}
int failPos = -1;
srcIter.setToStart();
StringBuffer processOut = null;
// step 2 is performed only if the source contains non ASCII
if (!srcIsASCII) {
// step 2
processOut = transform.prepare(srcIter, options);
} else {
processOut = new StringBuffer(srcIter.getText());
}
int poLen = processOut.length();
if (poLen == 0) {
throw new StringPrepParseException("Found zero length lable after NamePrep.", StringPrepParseException.ZERO_LENGTH_LABEL);
}
StringBuffer dest = new StringBuffer();
// reset the variable to verify if output of prepare is ASCII or not
srcIsASCII = true;
// step 3 & 4
for (int j = 0; j < poLen; j++) {
ch = processOut.charAt(j);
if (ch > 0x7F) {
srcIsASCII = false;
} else if (isLDHChar(ch) == false) {
// here we do not assemble surrogates
// since we know that LDH code points
// are in the ASCII range only
srcIsLDH = false;
failPos = j;
}
}
if (useSTD3ASCIIRules == true) {
// verify 3a and 3b
if (srcIsLDH == false || /* source contains some non-LDH characters */
processOut.charAt(0) == HYPHEN || processOut.charAt(processOut.length() - 1) == HYPHEN) {
/* populate the parseError struct */
if (srcIsLDH == false) {
throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules", StringPrepParseException.STD3_ASCII_RULES_ERROR, processOut.toString(), (failPos > 0) ? (failPos - 1) : failPos);
} else if (processOut.charAt(0) == HYPHEN) {
throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules", StringPrepParseException.STD3_ASCII_RULES_ERROR, processOut.toString(), 0);
} else {
throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules", StringPrepParseException.STD3_ASCII_RULES_ERROR, processOut.toString(), (poLen > 0) ? poLen - 1 : poLen);
}
}
}
if (srcIsASCII) {
dest = processOut;
} else {
// step 5 : verify the sequence does not begin with ACE prefix
if (!startsWithPrefix(processOut)) {
// step 6: encode the sequence with punycode
StringBuffer punyout = PunycodeReference.encode(processOut, caseFlags);
// convert all codepoints to lower case ASCII
StringBuffer lowerOut = toASCIILower(punyout);
// Step 7: prepend the ACE prefix
dest.append(ACE_PREFIX, 0, ACE_PREFIX_LENGTH);
// Step 6: copy the contents in b2 into dest
dest.append(lowerOut);
} else {
throw new StringPrepParseException("The input does not start with the ACE Prefix.", StringPrepParseException.ACE_PREFIX_ERROR, processOut.toString(), 0);
}
}
if (dest.length() > MAX_LABEL_LENGTH) {
throw new StringPrepParseException("The labels in the input are too long. Length > 64.", StringPrepParseException.LABEL_TOO_LONG_ERROR, dest.toString(), 0);
}
return dest;
}
use of android.icu.text.StringPrepParseException in project j2objc by google.
the class IDNAReference method convertIDNToUnicode.
public static StringBuffer convertIDNToUnicode(String src, int options) throws StringPrepParseException {
char[] srcArr = src.toCharArray();
StringBuffer result = new StringBuffer();
int sepIndex = 0;
int oldSepIndex = 0;
for (; ; ) {
sepIndex = getSeparatorIndex(srcArr, sepIndex, srcArr.length);
String label = new String(srcArr, oldSepIndex, sepIndex - oldSepIndex);
if (label.length() == 0 && sepIndex != srcArr.length) {
throw new StringPrepParseException("Found zero length lable after NamePrep.", StringPrepParseException.ZERO_LENGTH_LABEL);
}
UCharacterIterator iter = UCharacterIterator.getInstance(label);
result.append(convertToUnicode(iter, options));
if (sepIndex == srcArr.length) {
break;
}
// increment the sepIndex to skip past the separator
sepIndex++;
oldSepIndex = sepIndex;
result.append((char) FULL_STOP);
}
return result;
}
use of android.icu.text.StringPrepParseException in project j2objc by google.
the class NamePrepTransform method prepare.
public StringBuffer prepare(String src, int options) throws StringPrepParseException {
int ch;
String mapOut = map(src, options);
UCharacterIterator iter = UCharacterIterator.getInstance(mapOut);
int direction = UCharacterDirection.CHAR_DIRECTION_COUNT, firstCharDir = UCharacterDirection.CHAR_DIRECTION_COUNT;
int rtlPos = -1, ltrPos = -1;
boolean rightToLeft = false, leftToRight = false;
while ((ch = iter.nextCodePoint()) != UCharacterIterator.DONE) {
if (transform.prohibitedSet.contains(ch) == true && ch != 0x0020) {
throw new StringPrepParseException("A prohibited code point was found in the input", StringPrepParseException.PROHIBITED_ERROR, iter.getText(), iter.getIndex());
}
direction = UCharacter.getDirection(ch);
if (firstCharDir == UCharacterDirection.CHAR_DIRECTION_COUNT) {
firstCharDir = direction;
}
if (direction == UCharacterDirection.LEFT_TO_RIGHT) {
leftToRight = true;
ltrPos = iter.getIndex() - 1;
}
if (direction == UCharacterDirection.RIGHT_TO_LEFT || direction == UCharacterDirection.RIGHT_TO_LEFT_ARABIC) {
rightToLeft = true;
rtlPos = iter.getIndex() - 1;
}
}
// satisfy 2
if (leftToRight == true && rightToLeft == true) {
throw new StringPrepParseException("The input does not conform to the rules for BiDi code points.", StringPrepParseException.CHECK_BIDI_ERROR, iter.getText(), (rtlPos > ltrPos) ? rtlPos : ltrPos);
}
// satisfy 3
if (rightToLeft == true && !((firstCharDir == UCharacterDirection.RIGHT_TO_LEFT || firstCharDir == UCharacterDirection.RIGHT_TO_LEFT_ARABIC) && (direction == UCharacterDirection.RIGHT_TO_LEFT || direction == UCharacterDirection.RIGHT_TO_LEFT_ARABIC))) {
throw new StringPrepParseException("The input does not conform to the rules for BiDi code points.", StringPrepParseException.CHECK_BIDI_ERROR, iter.getText(), (rtlPos > ltrPos) ? rtlPos : ltrPos);
}
return new StringBuffer(mapOut);
}
use of android.icu.text.StringPrepParseException in project j2objc by google.
the class TestIDNARef method TestNamePrepConformance.
@Test
public void TestNamePrepConformance() throws Exception {
try {
NamePrepTransform namePrep = NamePrepTransform.getInstance();
if (!namePrep.isReady()) {
logln("Transliterator is not available on this environment.");
return;
}
for (int i = 0; i < TestData.conformanceTestCases.length; i++) {
TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
UCharacterIterator iter = UCharacterIterator.getInstance(testCase.input);
try {
StringBuffer output = namePrep.prepare(iter, NamePrepTransform.NONE);
if (testCase.output != null && output != null && !testCase.output.equals(output.toString())) {
errln("Did not get the expected output. Expected: " + prettify(testCase.output) + " Got: " + prettify(output));
}
if (testCase.expected != null && !unassignedException.equals(testCase.expected)) {
errln("Did not get the expected exception. The operation succeeded!");
}
} catch (StringPrepParseException ex) {
if (testCase.expected == null || !ex.equals(testCase.expected)) {
errln("Did not get the expected exception for source: " + testCase.input + " Got: " + ex.toString());
}
}
try {
iter.setToStart();
StringBuffer output = namePrep.prepare(iter, NamePrepTransform.ALLOW_UNASSIGNED);
if (testCase.output != null && output != null && !testCase.output.equals(output.toString())) {
errln("Did not get the expected output. Expected: " + prettify(testCase.output) + " Got: " + prettify(output));
}
if (testCase.expected != null && !unassignedException.equals(testCase.expected)) {
errln("Did not get the expected exception. The operation succeeded!");
}
} catch (StringPrepParseException ex) {
if (testCase.expected == null || !ex.equals(testCase.expected)) {
errln("Did not get the expected exception for source: " + testCase.input + " Got: " + ex.toString());
}
}
}
} catch (java.lang.ExceptionInInitializerError e) {
warnln("Could not load NamePrepTransformData");
} catch (java.lang.NoClassDefFoundError ex) {
warnln("Could not load NamePrepTransform data");
}
}
use of android.icu.text.StringPrepParseException in project j2objc by google.
the class Punycode method encode.
/**
* Converts Unicode to Punycode.
* The input string must not contain single, unpaired surrogates.
* The output will be represented as an array of ASCII code points.
*
* @param src The source of the String Buffer passed.
* @param caseFlags The boolean array of case flags.
* @return An array of ASCII code points.
*/
public static StringBuilder encode(CharSequence src, boolean[] caseFlags) throws StringPrepParseException {
int n, delta, handledCPCount, basicLength, bias, j, m, q, k, t, srcCPCount;
char c, c2;
int srcLength = src.length();
int[] cpBuffer = new int[srcLength];
StringBuilder dest = new StringBuilder(srcLength);
/*
* Handle the basic code points and
* convert extended ones to UTF-32 in cpBuffer (caseFlag in sign bit):
*/
srcCPCount = 0;
for (j = 0; j < srcLength; ++j) {
c = src.charAt(j);
if (isBasic(c)) {
cpBuffer[srcCPCount++] = 0;
dest.append(caseFlags != null ? asciiCaseMap(c, caseFlags[j]) : c);
} else {
n = ((caseFlags != null && caseFlags[j]) ? 1 : 0) << 31L;
if (!UTF16.isSurrogate(c)) {
n |= c;
} else if (UTF16.isLeadSurrogate(c) && (j + 1) < srcLength && UTF16.isTrailSurrogate(c2 = src.charAt(j + 1))) {
++j;
n |= UCharacter.getCodePoint(c, c2);
} else {
/* error: unmatched surrogate */
throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
}
cpBuffer[srcCPCount++] = n;
}
}
/* Finish the basic string - if it is not empty - with a delimiter. */
basicLength = dest.length();
if (basicLength > 0) {
dest.append(DELIMITER);
}
/*
* handledCPCount is the number of code points that have been handled
* basicLength is the number of basic code points
* destLength is the number of chars that have been output
*/
/* Initialize the state: */
n = INITIAL_N;
delta = 0;
bias = INITIAL_BIAS;
/* Main encoding loop: */
for (handledCPCount = basicLength; handledCPCount < srcCPCount; ) /* no op */
{
/*
* All non-basic code points < n have been handled already.
* Find the next larger one:
*/
for (m = 0x7fffffff, j = 0; j < srcCPCount; ++j) {
q = cpBuffer[j] & 0x7fffffff;
/* remove case flag from the sign bit */
if (n <= q && q < m) {
m = q;
}
}
/*
* Increase delta enough to advance the decoder's
* <n,i> state to <m,0>, but guard against overflow:
*/
if (m - n > (0x7fffffff - delta) / (handledCPCount + 1)) {
throw new IllegalStateException("Internal program error");
}
delta += (m - n) * (handledCPCount + 1);
n = m;
/* Encode a sequence of same code points n */
for (j = 0; j < srcCPCount; ++j) {
q = cpBuffer[j] & 0x7fffffff;
/* remove case flag from the sign bit */
if (q < n) {
++delta;
} else if (q == n) {
/* Represent delta as a generalized variable-length integer: */
for (q = delta, k = BASE; ; /* no condition */
k += BASE) {
/**
* RAM: comment out the old code for conformance with draft-ietf-idn-punycode-03.txt
*
* t=k-bias;
* if(t<TMIN) {
* t=TMIN;
* } else if(t>TMAX) {
* t=TMAX;
* }
*/
t = k - bias;
if (t < TMIN) {
t = TMIN;
} else if (k >= (bias + TMAX)) {
t = TMAX;
}
if (q < t) {
break;
}
dest.append(digitToBasic(t + (q - t) % (BASE - t), false));
q = (q - t) / (BASE - t);
}
dest.append(digitToBasic(q, (cpBuffer[j] < 0)));
bias = adaptBias(delta, handledCPCount + 1, (handledCPCount == basicLength));
delta = 0;
++handledCPCount;
}
}
++delta;
++n;
}
return dest;
}
Aggregations