コード例 #1
0
        //
        // toUnicode operation; should only apply to a single label
        //
        private static String ToUnicodeInternal(String label, int flag)
        {
            bool[]       caseFlags = null;
            StringBuffer dest;

            // step 1
            // find out if all the codepoints in input are ASCII
            bool isASCII = IsAllASCII(label);

            if (!isASCII)
            {
                // step 2
                // perform the nameprep operation; flag ALLOW_UNASSIGNED is used here
                try
                {
                    UCharacterIterator iter = UCharacterIterator.getInstance(label);
                    dest = NamePrep.prepare(iter, flag);
                }
                catch (Exception)
                {
                    // toUnicode never fails; if any step fails, return the input string
                    return(label);
                }
            }
            else
            {
                dest = new StringBuffer(label);
            }

            // step 3
            // verify ACE Prefix
            if (StartsWithACEPrefix(dest))
            {
                // step 4
                // Remove the ACE Prefix
                String temp = dest.Substring(ACE_PREFIX_LENGTH, dest.Length() - ACE_PREFIX_LENGTH);

                try
                {
                    // step 5
                    // Decode using punycode
                    StringBuffer decodeOut = Punycode.decode(new StringBuffer(temp), null);

                    // step 6
                    // Apply toASCII
                    String toASCIIOut = ToASCII(decodeOut.ToString(), flag);

                    // step 7
                    // verify
                    if (toASCIIOut.EqualsIgnoreCase(dest.ToString()))
                    {
                        // step 8
                        // return output of step 5
                        return(decodeOut.ToString());
                    }
                }
                catch (Exception)
                {
                    // no-op
                }
            }

            // just return the input
            return(label);
        }
コード例 #2
0
        //
        // toASCII operation; should only apply to a single label
        //
        private static String ToASCIIInternal(String label, int flag)
        {
            // step 1
            // Check if the string contains code points outside the ASCII range 0..0x7c.
            bool         isASCII = IsAllASCII(label);
            StringBuffer dest;

            // step 2
            // perform the nameprep operation; flag ALLOW_UNASSIGNED is used here
            if (!isASCII)
            {
                UCharacterIterator iter = UCharacterIterator.getInstance(label);
                try
                {
                    dest = NamePrep.prepare(iter, flag);
                }
                catch (java.text.ParseException e)
                {
                    throw new IllegalArgumentException(e);
                }
            }
            else
            {
                dest = new StringBuffer(label);
            }

            // step 8, move forward to check the smallest number of the code points
            // the length must be inside 1..63
            if (dest.Length() == 0)
            {
                throw new IllegalArgumentException("Empty label is not a legal name");
            }

            // step 3
            // Verify the absence of non-LDH ASCII code points
            //   0..0x2c, 0x2e..0x2f, 0x3a..0x40, 0x5b..0x60, 0x7b..0x7f
            // Verify the absence of leading and trailing hyphen
            bool useSTD3ASCIIRules = ((flag & USE_STD3_ASCII_RULES) != 0);

            if (useSTD3ASCIIRules)
            {
                for (int i = 0; i < dest.Length(); i++)
                {
                    int c = dest.CharAt(i);
                    if (IsNonLDHAsciiCodePoint(c))
                    {
                        throw new IllegalArgumentException("Contains non-LDH ASCII characters");
                    }
                }

                if (dest.CharAt(0) == '-' || dest.CharAt(dest.Length() - 1) == '-')
                {
                    throw new IllegalArgumentException("Has leading or trailing hyphen");
                }
            }

            if (!isASCII)
            {
                // step 4
                // If all code points are inside 0..0x7f, skip to step 8
                if (!IsAllASCII(dest.ToString()))
                {
                    // step 5
                    // verify the sequence does not begin with ACE prefix
                    if (!StartsWithACEPrefix(dest))
                    {
                        // step 6
                        // encode the sequence with punycode
                        try
                        {
                            dest = Punycode.encode(dest, null);
                        }
                        catch (java.text.ParseException e)
                        {
                            throw new IllegalArgumentException(e);
                        }

                        dest = ToASCIILower(dest);

                        // step 7
                        // prepend the ACE prefix
                        dest.Insert(0, ACE_PREFIX);
                    }
                    else
                    {
                        throw new IllegalArgumentException("The input starts with the ACE Prefix");
                    }
                }
            }

            // step 8
            // the length must be inside 1..63
            if (dest.Length() > MAX_LABEL_LENGTH)
            {
                throw new IllegalArgumentException("The label in the input is too long");
            }

            return(dest.ToString());
        }