001/**
002 *
003 * Copyright © 2015-2019 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jxmpp.stringprep.icu4j;
018
019import org.jxmpp.JxmppContext;
020import org.jxmpp.stringprep.XmppStringprep;
021import org.jxmpp.stringprep.XmppStringprepException;
022
023import com.ibm.icu.text.StringPrep;
024import com.ibm.icu.text.StringPrepParseException;
025
026public class Icu4jXmppStringprep implements XmppStringprep {
027
028        public static final String NAME = "icu4j";
029
030        private static final StringPrep NODEPREP = StringPrep.getInstance(StringPrep.RFC3920_NODEPREP);
031        private static final StringPrep DOMAINPREP = StringPrep.getInstance(StringPrep.RFC3491_NAMEPREP);
032        private static final StringPrep RESOURCEPREP = StringPrep.getInstance(StringPrep.RFC3920_RESOURCEPREP);
033
034        private static Icu4jXmppStringprep instance;
035
036        /**
037         * Setup the ICU4J Stringprep implementation as active Stringprep implementation.
038         */
039        public static void setup() {
040                JxmppContext.setDefaultXmppStringprep(getInstance());
041        }
042
043        /**
044         * Get the ICU4J Stringprep implementation singleton.
045         * @return the ICU4J Stringprep implementation.
046         */
047        public static Icu4jXmppStringprep getInstance() {
048                if (instance == null) {
049                        instance = new Icu4jXmppStringprep();
050                }
051                return instance;
052        }
053
054        private Icu4jXmppStringprep() {
055        }
056
057        @Override
058        public String localprep(String string) throws XmppStringprepException {
059                try {
060                        return NODEPREP.prepare(string, StringPrep.DEFAULT);
061                } catch (StringPrepParseException e) {
062                        throw new XmppStringprepException(string, e);
063                }
064        }
065
066        @Override
067        public String domainprep(String string) throws XmppStringprepException {
068                try {
069                        return DOMAINPREP.prepare(string, StringPrep.DEFAULT);
070                } catch (StringPrepParseException e) {
071                        throw new XmppStringprepException(string, e);
072                }
073        }
074
075        @Override
076        public String resourceprep(String string) throws XmppStringprepException {
077                try {
078                        return RESOURCEPREP.prepare(string, StringPrep.DEFAULT);
079                } catch (StringPrepParseException e) {
080                        throw new XmppStringprepException(string, e);
081                }
082        }
083}