package nu.fw.jeti.jabber; import nu.fw.jeti.util.I18N; /** * * @author E.S. de Boer * @version 1.0 */ public final class JID { private String domain; private String user; private String resource; private String domainPreped; private String userPreped; private String resourcePreped; public JID(String server) { // if(server == null || server.equals("")) throw new NullPointerException(I18N.gettext("main.error.Server_has_no_value")); // if(Utils.isAtleastJava(6)){ // domainPreped = JIDPrep.namePrep(server); // } // else domainPreped = server.toLowerCase(); this.domain = server; } public JID(String user,String server) { this(server); if(user == null) return; // if(Utils.isAtleastJava(6)){ // userPreped = JIDPrep.nodePrep(user); // } // else userPreped = user.toLowerCase(); this.user = user; } public JID(String user,String server,String resource) { this(user,server); // if(Utils.isAtleastJava(6)){ // resourcePreped = JIDPrep.resourcePrep(resource); // // } // else resourcePreped = resource; this.resource = resource; } public JID() { } public static JID jidFromString(String jid) { if(jid == null || jid.equals("")) return null; String node=null, domain=null, resource=null; int loc = jid.indexOf("@"); if (loc != -1) { node = jid.substring(0, loc); jid = jid.substring(loc + 1); } loc = jid.indexOf("/"); if (loc == -1) domain = jid; else { domain = jid.substring(0, loc); resource = jid.substring(loc + 1); } if(domain ==null) return null; return new JID(node,domain,resource); } public static JID checkedJIDFromString(String jid) throws InstantiationException { if(jid == null || jid.equals("")) return null; String node=null, domain=null, resource=null; int loc = jid.indexOf("@"); if (loc != -1) { node = jid.substring(0, loc); jid = jid.substring(loc + 1); } loc = jid.indexOf("/"); if (loc == -1) domain = jid; else { domain = jid.substring(0, loc); resource = jid.substring(loc + 1); } if (node != null && node.length() > 255) throw new InstantiationException (I18N.gettext("main.error.Username_>_255_Characters")); if(domain.indexOf("@") != -1) throw new InstantiationException (I18N.gettext("main.error.Server_or_Username_contains_a_'@'")); if(!isValidUser(node)) throw new InstantiationException(I18N.getTextWithAmp("main.error.Username_contains_illegal_chars_(see_english_translation)")); if(!isValidServer(domain)) throw new InstantiationException(I18N.gettext("main.error.Server_must_start_with_a_letter_(see_english_translation)")); return new JID(node,domain,resource); } public static boolean isValidUser(String user) { if(user == null) return true; int len = user.length(); if (len > 255) return false; char c; for(int i=0; i') return false; if (c == '<') return false; if (c == '/') return false; if (c == '\'') return false; if (c == '&') return false; if (c == '\u077F') return false; if (c == '\u0FFE') return false; if (c == '\u0FFF') return false; } return true; } //check dns name public static boolean isValidServer(String server) { if(server == null || server.equals("")) return false; int len = server.length(); //if (len > 255) return false; //first leter must be alphanumeric if(!Character.isLetterOrDigit(server.charAt(0)))return false; char c; for(int i=0; i