⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 charactertranslationtest.java

📁 html解析包 可以很方便的解析html 纯java 实现
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    }    public void check (byte[] reference, byte[] result)        throws            IOException    {        InputStream ref;        InputStream in;        int i;        int i1;        int i2;        ref = new ByteArrayInputStream (reference);        in = new ByteArrayInputStream (result);        i = 0;        do        {            i1 = ref.read ();            i2 = in.read ();            if (i1 != i2)                fail ("byte difference detected at offset " + i + " expected " + i1 + ", actual " + i2);            i++;        }        while (-1 != i1);        ref.close ();        in.close ();    }    public void testHexNumericEncoding ()        throws            IOException    {        try        {            Translate.ENCODE_HEXADECIMAL = true;            assertEquals (                "hex value incorrect",                "&#x5ab; is a non-existant character.",                Translate.encode ("\u05AB is a non-existant character."));        }        finally        {            Translate.ENCODE_HEXADECIMAL = false;        }    }    public void testLastCharacterEntityReference ()        throws            IOException    {        assertEquals (            "poorly terminated numeric character reference doesn't work",            "The character entity reference\u200cshould be decoded",            Translate.decode ("The character entity reference&zwnjshould be decoded"));    }    public void testEncodeDecodePage () throws IOException    {        URL url;        URLConnection connection;        InputStream in;        ByteArrayOutputStream out;        byte[] bytes;        byte[] result;        int c;        // get some bytes        url = new URL ("http://sourceforge.net/projects/htmlparser");        connection = url.openConnection ();        in = connection.getInputStream ();        out = new ByteArrayOutputStream ();        while (-1 != (c = in.read ()))            out.write (c);        in.close ();        out.close ();        bytes = out.toByteArray ();        // run it through        result = encodedecode (bytes);                // check        check (bytes, result);    }    /**     * Check all references read in from the w3.org site.     * If this test fails but the others pass, suspect that the list of     * entity references has been augmented. The updated list is in the     * CharacterEntityReferenceList.java file in your home directory.     */    public void testEncodeDecodeAll ()    {        CharacterReference[] list;        StringBuffer stimulus;        StringBuffer response;        CharacterReference ref;        String string;        list = getReferences ();        stimulus = new StringBuffer ();        response = new StringBuffer ();        for (int i = 0; i < list.length; i++)        {            ref = list[i];            stimulus.append ((char)ref.getCharacter ());            response.append ("&");            response.append (ref.getKernel ());            response.append (";");        }        string = Translate.encode (stimulus.toString ());        if (!string.equals (response.toString ()))            fail ("encoding incorrect, expected \n\"" + response.toString () + "\", encoded \n\"" + string + "\"");         string = Translate.decode (string);        if (!string.equals (stimulus.toString ()))            fail ("decoding incorrect, expected \n\"" + stimulus.toString () + "\", decoded \n\"" + string + "\", encoded \n\"" + response.toString () + "\"");     }    public void testEncodeDecodeRandom ()    {        Random random;        CharacterReference[] list;        StringBuffer stimulus;        StringBuffer response;        char character;        CharacterReference ref;        String string;        random = new Random ();        list = getReferences ();        stimulus = new StringBuffer ();        response = new StringBuffer ();        for (int i = 0; i < 1000; i++)        {            for (int j = 0; j < 10; j++)            {                // some random characters                for (int k = 0; k < 10; k++)                {                    character = (char)random.nextInt (127);                    if (character >= ' ')                    {                        if ('&' == character)                        {                            stimulus.append (character);                            response.append ("&amp;");                        }                        else if ('"' == character)                        {                            stimulus.append (character);                            response.append ("&quot;");                        }                        else if ('<' == character)                        {                            stimulus.append (character);                            response.append ("&lt;");                        }                        else if ('>' == character)                        {                            stimulus.append (character);                            response.append ("&gt;");                        }                        else                        {                            stimulus.append (character);                            response.append (character);                        }                    }                }                ref = list[random.nextInt (list.length)];                stimulus.append ((char)ref.getCharacter ());                response.append ("&");                response.append (ref.getKernel ());                response.append (";");                // some more random characters                for (int k = 0; k < 10; k++)                {                    character = (char)random.nextInt (127);                    if (character >= ' ')                    {                        if ('&' == character)                        {                            stimulus.append (character);                            response.append ("&amp;");                        }                        else if ('"' == character)                        {                            stimulus.append (character);                            response.append ("&quot;");                        }                        else if ('<' == character)                        {                            stimulus.append (character);                            response.append ("&lt;");                        }                        else if ('>' == character)                        {                            stimulus.append (character);                            response.append ("&gt;");                        }                        else                        {                            stimulus.append (character);                            response.append (character);                        }                    }                }            }            string = Translate.encode (stimulus.toString ());            if (!string.equals (response.toString ()))                fail ("encoding incorrect, expected \n\"" + response.toString () + "\", encoded \n\"" + string + "\"");             string = Translate.decode (string);            if (!string.equals (stimulus.toString ()))                fail ("decoding incorrect, expected \n\"" + stimulus.toString () + "\", decoded \n\"" + string + "\", encoded \n\"" + response.toString () + "\"");             stimulus.setLength (0);            response.setLength (0);        }               }    public void testEncodeDecodeRandomNoSemi ()    {        Random random;        CharacterReference[] list;        StringBuffer stimulus;        StringBuffer response;        char character;        int index;        CharacterReference ref;        String kernel;        ArrayList forbidden;        String string;        random = new Random ();        list = getReferences ();        stimulus = new StringBuffer ();        response = new StringBuffer ();        for (int i = 0; i < 1000; i++)        {            for (int j = 0; j < 10; j++)            {                // some random characters                for (int k = 0; k < 10; k++)                {                    character = (char)random.nextInt (127);                    if (character >= ' ')                    {                        if ('&' == character)                        {                            stimulus.append (character);                            response.append ("&amp;");                        }                        else if ('"' == character)                        {                            stimulus.append (character);                            response.append ("&quot;");                        }                        else if ('<' == character)                        {                            stimulus.append (character);                            response.append ("&lt;");                        }                        else if ('>' == character)                        {                            stimulus.append (character);                            response.append ("&gt;");                        }                        else                        {                            stimulus.append (character);                            response.append (character);                        }                    }                }                index = random.nextInt (list.length);                ref = list[index];                kernel = ref.getKernel ();                stimulus.append ((char)ref.getCharacter ());                response.append ("&");                response.append (kernel);                // to be fair, we ensure that the next character isn't valid                // for a different reference, i.e. &sup shouldn't be followed                // by a 1, 2, 3 or e                forbidden = new ArrayList ();                for (int k = index + 1; k < list.length; k++)                    if (list[k].getKernel ().regionMatches (                        0,                        kernel,                        0,                        kernel.length ()))                        forbidden.add (new Character (list[k].getKernel ().charAt (kernel.length ())));                    else                        break;                do                {                    character = (char)random.nextInt (127);                    if (   (' ' <= character)                        && ('&' != character)                        && ('"' != character)                        && ('<' != character)                        && ('>' != character)                        && (';' != character)                        && !(forbidden.contains (new Character (character))))                    {                        stimulus.append (character);                        response.append (character);                        character = 0;                    }                    else                        character = ' ';                                        }                while (0 != character);                // some more random characters                for (int k = 0; k < 10; k++)                {                    character = (char)random.nextInt (127);                    if (character >= ' ')                    {                        if ('&' == character)                        {                            stimulus.append (character);                            response.append ("&amp;");                        }                        else if ('"' == character)                        {                            stimulus.append (character);                            response.append ("&quot;");                        }                        else if ('<' == character)                        {                            stimulus.append (character);                            response.append ("&lt;");                        }                        else if ('>' == character)                        {                            stimulus.append (character);                            response.append ("&gt;");                        }                        else                        {                            stimulus.append (character);                            response.append (character);                        }                    }                }            }            string = Translate.decode (response.toString ());            if (!string.equals (stimulus.toString ()))                fail ("decoding incorrect:\nexpected \"" + stimulus.toString () + "\"\n decoded \"" + string + "\"\n encoded \"" + response.toString () + "\"");             stimulus.setLength (0);            response.setLength (0);        }       }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -