Package nu.fw.jeti.util

Source Code of nu.fw.jeti.util.JIDPrep

/*
*  Jeti, a Java Jabber client, Copyright (C) 2007 E.S. de Boer 
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*  For questions, comments etc,
*  use the website at http://jeti.jabberstudio.org
*/

//created on May 7, 2007
package nu.fw.jeti.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.IDN;

import sun.net.idn.StringPrep;
import sun.text.normalizer.UCharacterIterator;

/**
* stringprep JIDs See rfc3920 - 3.2 / 3.4
*
*/
public class JIDPrep {

  private static StringPrep nodePrep;
  private static StringPrep resourcePrep;
     
    static {
        InputStream stream = null;
       
        try {
            stream = JIDPrep.class.getResourceAsStream("nodeprep.spp");
            nodePrep = new StringPrep(stream);
            stream.close();
            stream = JIDPrep.class.getResourceAsStream("resourceprep.spp");
            resourcePrep = new StringPrep(stream);
            stream.close();
        } catch (IOException e) {
            // should never reach here
          e.printStackTrace();
            assert false;
        }
    }
 
 
 
  /**
   * Nameprep the server/domain part of a JID
   * @param server
   * @return namepreped server part
   * @throws a IllegalArgumentException if it is an illegal domain
   */
  public static String namePrep(String server)
  {
    return IDN.toASCII(server, IDN.USE_STD3_ASCII_RULES);
  }
 
  /**
   * Nodeprep the node part of a JID
   * @param node
   * @return nodepreped part of a JID
   * @throws a IllegalArgumentException if it is an illegal Node
   */
  public static String nodePrep(String node)
  {
    //TODO add http://www.icu-project.org/icu4j_faq.html for java 1.4 compatibility and non sun java
    if(node == null) return null;
    UCharacterIterator iter = UCharacterIterator.getInstance(node);
        try {
            return nodePrep.prepare(iter, IDN.USE_STD3_ASCII_RULES).toString();
        } catch (java.text.ParseException e) {
            throw new IllegalArgumentException(e);
        }
  }
 
 
  /**
   * Resourceprep the node part of a JID
   * @param resource
   * @return resourcepreped part of a JID
   * @throws a IllegalArgumentException if it is an illegal Resource
   */
  public static String resourcePrep(String resource)
  {
    if(resource == null) return null;
    UCharacterIterator iter = UCharacterIterator.getInstance(resource);
        try {
            return resourcePrep.prepare(iter, IDN.USE_STD3_ASCII_RULES).toString();
        } catch (java.text.ParseException e) {
            throw new IllegalArgumentException(e);
        }
  }
 
}
TOP

Related Classes of nu.fw.jeti.util.JIDPrep

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.