001/**
002 *
003 * Copyright © 2015 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.stringprep.XmppStringPrepUtil;
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        private static final StringPrep NODEPREP = StringPrep.getInstance(StringPrep.RFC3920_NODEPREP);
029        private static final StringPrep DOMAINPREP = StringPrep.getInstance(StringPrep.RFC3491_NAMEPREP);
030        private static final StringPrep RESOURCEPREP = StringPrep.getInstance(StringPrep.RFC3920_RESOURCEPREP);
031
032        private static Icu4jXmppStringprep instance;
033
034        /**
035         * Setup the ICU4J Stringprep implementation as active Stringprep implementation.
036         */
037        public static void setup() {
038                XmppStringPrepUtil.setXmppStringprep(getInstance());
039        }
040
041        /**
042         * Get the ICU4J Stringprep implementation singleton.
043         * @return the ICU4J Stringprep implementation.
044         */
045        public static Icu4jXmppStringprep getInstance() {
046                if (instance == null) {
047                        instance = new Icu4jXmppStringprep();
048                }
049                return instance;
050        }
051
052        private Icu4jXmppStringprep() {
053        }
054
055        @Override
056        public String localprep(String string) throws XmppStringprepException {
057                try {
058                        return NODEPREP.prepare(string, StringPrep.DEFAULT);
059                } catch (StringPrepParseException e) {
060                        throw new XmppStringprepException(string, e);
061                }
062        }
063
064        @Override
065        public String domainprep(String string) throws XmppStringprepException {
066                try {
067                        return DOMAINPREP.prepare(string, StringPrep.DEFAULT);
068                } catch (StringPrepParseException e) {
069                        throw new XmppStringprepException(string, e);
070                }
071        }
072
073        @Override
074        public String resourceprep(String string) throws XmppStringprepException {
075                try {
076                        return RESOURCEPREP.prepare(string, StringPrep.DEFAULT);
077                } catch (StringPrepParseException e) {
078                        throw new XmppStringprepException(string, e);
079                }
080        }
081}