source: trunk/jabberit_messenger/java_source/src/nu/fw/jeti/util/JIDPrep.java @ 1001

Revision 1001, 3.2 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #552 - Inclusão do projeto Java referente ao applet do módulo.

Line 
1/*
2 *      Jeti, a Java Jabber client, Copyright (C) 2007 E.S. de Boer 
3 *
4 *  This program is free software; you can redistribute it and/or modify
5 *  it under the terms of the GNU General Public License as published by
6 *  the Free Software Foundation; either version 2 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *      GNU General Public License for more details.
13 *
14 *  You should have received a copy of the GNU General Public License
15 *  along with this program; if not, write to the Free Software
16 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 *
18 *      For questions, comments etc,
19 *      use the website at http://jeti.jabberstudio.org
20 */
21
22//created on May 7, 2007
23package nu.fw.jeti.util;
24
25import java.io.IOException;
26import java.io.InputStream;
27import java.net.IDN;
28
29import sun.net.idn.StringPrep;
30import sun.text.normalizer.UCharacterIterator;
31
32/**
33 * stringprep JIDs See rfc3920 - 3.2 / 3.4
34 *
35 */
36public class JIDPrep {
37
38        private static StringPrep nodePrep;
39        private static StringPrep resourcePrep;
40           
41    static {
42        InputStream stream = null;
43       
44        try {
45            stream = JIDPrep.class.getResourceAsStream("nodeprep.spp");
46            nodePrep = new StringPrep(stream);
47            stream.close();
48            stream = JIDPrep.class.getResourceAsStream("resourceprep.spp");
49            resourcePrep = new StringPrep(stream);
50            stream.close();
51        } catch (IOException e) {
52            // should never reach here
53                e.printStackTrace();
54            assert false;
55        }
56    }
57       
58       
59       
60        /**
61         * Nameprep the server/domain part of a JID
62         * @param server
63         * @return namepreped server part
64         * @throws a IllegalArgumentException if it is an illegal domain
65         */
66        public static String namePrep(String server)
67        {
68                return IDN.toASCII(server, IDN.USE_STD3_ASCII_RULES);
69        }
70       
71        /**
72         * Nodeprep the node part of a JID
73         * @param node
74         * @return nodepreped part of a JID
75         * @throws a IllegalArgumentException if it is an illegal Node
76         */
77        public static String nodePrep(String node)
78        {
79                //TODO add http://www.icu-project.org/icu4j_faq.html for java 1.4 compatibility and non sun java
80                if(node == null) return null;
81                UCharacterIterator iter = UCharacterIterator.getInstance(node);
82        try {
83            return nodePrep.prepare(iter, IDN.USE_STD3_ASCII_RULES).toString();
84        } catch (java.text.ParseException e) {
85            throw new IllegalArgumentException(e);
86        }
87        }
88       
89       
90        /**
91         * Resourceprep the node part of a JID
92         * @param resource
93         * @return resourcepreped part of a JID
94         * @throws a IllegalArgumentException if it is an illegal Resource
95         */
96        public static String resourcePrep(String resource)
97        {
98                if(resource == null) return null;
99                UCharacterIterator iter = UCharacterIterator.getInstance(resource);
100        try {
101            return resourcePrep.prepare(iter, IDN.USE_STD3_ASCII_RULES).toString();
102        } catch (java.text.ParseException e) {
103            throw new IllegalArgumentException(e);
104        }
105        }
106       
107}
Note: See TracBrowser for help on using the repository browser.