Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.fibs.javafibs.model;
- import javax.swing.ImageIcon;
- import com.fibs.javafibs.gui.util.IconHandler;
- public class PPPInfo {
- // ////////////////////////////////////////////////////////
- // Constructor, Object state and defaulting
- // ////////////////////////////////////////////////////////
- final private Gender gender;
- final private PPPClient client;
- final private String countryCode;
- final private String givenName;
- final private String familyName;
- public PPPInfo() {
- this(null, null, null, null, null);
- }
- public PPPInfo(String rawUserName) {
- this(parseGivenName(rawUserName), parseFamilyName(rawUserName), parseCountry(rawUserName),
- parseGender(rawUserName), parsePPPClient(rawUserName));
- }
- public PPPInfo(String givenName, String familyName, String countryCode, Gender gender) {
- this(givenName, familyName, countryCode, gender, PPPInfo.PPPClient.JAVAFIBS2001);
- }
- public PPPInfo(String givenName, String familyName, String countryCode, Gender gender, PPPClient client) {
- this.givenName = givenName == null ? "" : givenName;
- this.familyName = familyName == null ? "" : familyName;
- this.gender = gender == null ? Gender.GENDER_UNKNOWN : gender;
- this.client = (client == null) ? PPPClient.JAVAFIBS2001 : client;
- if (countryCode == null) {
- this.countryCode = "NT";
- } else if (countryCode.length() == 2) {
- this.countryCode = countryCode;
- } else {
- this.countryCode = countryCode.substring(0, 2);
- }
- }
- // ///////////////////////////////////////////////////////////////////////////////
- // Inner classes, Enum style
- // ///////////////////////////////////////////////////////////////////////////////
- final public static class PPPClient {
- public final static PPPClient JAVAFIBS2001 = new PPPClient('!', "JavaFIBS 2001");
- private final static PPPClient[] KNOWN_PPP_CLIENTS = {
- // JF code range
- // Char20: Specifies the client using this protocol.
- // (0100001 - 0100111 = JavaFIBS)
- // ! = 33 - 0x21 - 041 - %00100001
- // " = 34 - 0x22 - 042 - %00100010
- // # = 35 - 0x23 - 043 - %00100011
- // $ = 36 - 0x24 - 044 - %00100100
- // % = 37 - 0x25 - 045 - %00100101
- // & = 38 - 0x26 - 046 - %00100110 - &
- // ' = 39 - 0x27 - 047 - %00100111
- JAVAFIBS2001, new PPPClient('"', "JavaFIBS Applet"), new PPPClient('#', "JavaFIBS"),
- // "JavaFIBS 2001 Applet", never released
- new PPPClient('$', "JavaFIBS (Reserved)"),
- // JavaFIBS 2001 (Registered), never released
- new PPPClient('%', "JavaFIBS (Reserved)"),
- // JavaFIBS 2oo3D, never released
- new PPPClient('&', "JavaFIBS (Reserved)"), new PPPClient('\'', "JavaFIBS (Reserved)"),
- // Other clients supporting PPP
- // used by boomslang's bots
- new PPPClient('x', "JavaCLIPLib"),
- // Delfibs (as per March 2008)
- new PPPClient('D', "Delfibs"),
- // =NTTourney1rganizer2
- new PPPClient('2', "TourneyBot") };
- private final char id;
- private final String name;
- protected PPPClient(char id, String name) {
- this.name = name;
- this.id = id;
- }
- protected static PPPClient getById(char id) {
- for (int i = 0; i < KNOWN_PPP_CLIENTS.length; i++) {
- if (KNOWN_PPP_CLIENTS[i].id == id) {
- return KNOWN_PPP_CLIENTS[i];
- }
- }
- return JAVAFIBS2001;
- }
- public char getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- // FIXME: JDK 1.5 @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (!(o instanceof PPPClient)) {
- return false;
- }
- return ((PPPClient) o).id == this.id;
- }
- }
- final public static class Gender {
- /*
- * Char1: Identifies this protocol and holds the gender of the player
- *
- * 01111 xy ----- -- | | | specifies the gender (00 = male ">", 10 = female "<",
- * 01 = computer "=", 11 = unknown "?") specifies that this protocol is used
- * (60) (along with a string length of 20).
- */
- private static final int GENDER_MASK = 3;
- public final static Gender GENDER_MALE = new Gender(0, "Male", IconHandler
- .getIcon(IconHandler.GENDER_MALE_ICON));
- public final static Gender GENDER_COMPUTER = new Gender(1, "Computer", IconHandler
- .getIcon(IconHandler.GENDER_COMPUTER_ICON));
- public final static Gender GENDER_FEMALE = new Gender(2, "Female", IconHandler
- .getIcon(IconHandler.GENDER_FEMALE_ICON));
- public final static Gender GENDER_UNKNOWN = new Gender(3, "Unknown", IconHandler
- .getIcon(IconHandler.GENDER_UNKNOWN_ICON));
- final private int id;
- final private String labelText;
- final private ImageIcon imageIcon;
- protected Gender(int id, String labelText, ImageIcon imageIcon) {
- this.id = id & GENDER_MASK;
- this.labelText = labelText;
- this.imageIcon = imageIcon;
- }
- public static Gender getById(int id) {
- switch (id & GENDER_MASK) {
- case 0:
- return GENDER_MALE;
- case 1:
- return GENDER_COMPUTER;
- case 2:
- return GENDER_FEMALE;
- default:
- return GENDER_UNKNOWN;
- }
- }
- // FIXME: make Locale dependend
- public String getLabelText() {
- return labelText;
- }
- public ImageIcon getIcon() {
- return imageIcon;
- }
- public int getId() {
- return id;
- }
- // FIXME: JDK 1.5 @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (!(o instanceof Gender)) {
- return false;
- }
- return ((Gender) o).id == this.id;
- }
- }
- // //////////////////////////////////////////////////////////
- // Parsing constants
- // //////////////////////////////////////////////////////////
- private static final int FLAGS_CHAR_POSITION = 0;
- private static final int NAME_START_POSITION = 3;
- private static final int NAME_END_POSITION = 18;
- private static final int CLIENT_CHAR_POSITION = 19;
- private static final int PROTOCOL_LENGTH = 20;
- private final static int NAME_LENGTH = 16;
- private static final int PROTOCOL_MASK = 124;
- private static final int PROTOCOL_ID = 60;
- private static final int FAMILY_NAME_FIRST_LETTER_CORRECTION = 30;
- // //////////////////////////////////////////////////////////
- // Public Utility methods
- // //////////////////////////////////////////////////////////
- /**
- * Checks to see if this string is a PPP string
- */
- public static boolean isPPP(String pppstring) {
- return pppstring != null && pppstring.length() == PROTOCOL_LENGTH
- && ((int) (pppstring.charAt(0)) & PROTOCOL_MASK) == PROTOCOL_ID;
- }
- // //////////////////////////////////////////////////////////
- // Actual parsing
- // //////////////////////////////////////////////////////////
- public static Gender parseGender(String rawUserName) {
- try {
- return Gender.getById(rawUserName.charAt(FLAGS_CHAR_POSITION));
- } catch (Exception e) {
- return Gender.GENDER_UNKNOWN;
- }
- }
- public static PPPClient parsePPPClient(String rawUserName) {
- try {
- return PPPClient.getById(rawUserName.charAt(CLIENT_CHAR_POSITION));
- } catch (Exception e) {
- return PPPClient.JAVAFIBS2001;
- }
- }
- public static String parseCountry(String rawUserName) {
- try {
- return rawUserName.substring(1, NAME_START_POSITION).trim().substring(0, 2);
- } catch (Exception e) {
- return "NT";
- }
- }
- public static String parseGivenName(String rawUserName) {
- try {
- // Case: empty name
- if (rawUserName.charAt(NAME_START_POSITION) == '_') {
- return "";
- }
- for (int i = NAME_START_POSITION; i <= NAME_END_POSITION; i++) {
- final char c = rawUserName.charAt(i);
- // Given name ends on start of family name
- if (c < 'A') {
- return decodeName(rawUserName.substring(NAME_START_POSITION, i).trim());
- }
- }
- // We did not find a family name, so all the string is the given name
- int givenNameEnd = NAME_END_POSITION;
- while (rawUserName.charAt(--givenNameEnd) == '_') {
- }
- return rawUserName.substring(NAME_START_POSITION, givenNameEnd+1);
- } catch (Exception e) {
- return "";
- }
- }
- public static String parseFamilyName(String rawUserName) {
- try {
- // Case: empty name
- if (rawUserName.charAt(NAME_START_POSITION) == '_') {
- return "";
- }
- // Find end of firstName section
- int familyNameStart;
- for (familyNameStart = NAME_START_POSITION; familyNameStart <= NAME_END_POSITION; familyNameStart++) {
- // Given name ends on either end of name (_), ort start of family name
- if (rawUserName.charAt(familyNameStart) < 'A') {
- // Case: we only got a given name
- break;
- }
- }
- // Check if we do have a familly name at all
- if (familyNameStart+1 >= NAME_END_POSITION) {
- return "";
- }
- int familyNameEnd;
- for (familyNameEnd = NAME_END_POSITION; familyNameEnd >= familyNameStart; familyNameEnd-- ) {
- if (rawUserName.charAt(familyNameEnd) != '_') {
- break;
- }
- }
- return decodeFamilyName(rawUserName.substring(familyNameStart, familyNameEnd+1));
- } catch (Exception e) {
- return "";
- }
- }
- // ///////////////////////////////////////////////////////////////
- // Getter
- // ///////////////////////////////////////////////////////////////
- public Gender getGender() {
- return gender;
- }
- public PPPClient getClient() {
- return client;
- }
- public String getCountryCode() {
- return countryCode;
- }
- public String getGivenName() {
- return givenName;
- }
- public String getFamilyName() {
- return familyName;
- }
- // /////////////////////////////////////////////////////////////////////////
- // Overrides
- // /////////////////////////////////////////////////////////////////////////
- // JDK 1.5: @Override
- public String toString() {
- try {
- String s = new StringBuffer().append((char) (60 + gender.getId()))
- .append(countryCode.substring(0, 2)) // First 2 letters
- // only
- .append(encodeNames()).append(client.getId()).toString();
- return s;
- } catch (Exception e) {
- e.printStackTrace();
- return "?NT________________!";
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- // Dirty detail section for the toString() and parsing methods
- ////////////////////////////////////////////////////////////////////////////////
- protected String encodeNames() throws IndexOutOfBoundsException, NullPointerException {
- if ("".equals(familyName) && !"".equals(givenName)) {
- return padNames(encodeName(givenName));
- } else if (!"".equals(familyName) && "".equals(givenName)) {
- return padNames(encodeFamilyName(familyName));
- }
- if (givenName.length() + familyName.length() <= NAME_LENGTH) {
- return padNames(encodeName(givenName) + encodeFamilyName(familyName));
- }
- if (familyName.length() + 1 <= NAME_LENGTH) {
- // Case: We use 1 initial and the complete family name (P
- // Longstockings)
- return padNames("" + givenName.charAt(0) + encodeFamilyName(familyName));
- }
- if (givenName.length() + 1 <= NAME_LENGTH) {
- // Case: We use the given name and the familiy name initial (Pippi
- // L)
- return padNames(encodeName(givenName) + encodeFamilyName("" + familyName.charAt(0)));
- }
- // last resort, we truncate to initials for both (P L)
- return padNames("" + givenName.charAt(0) + encodeFamilyName("" + familyName.charAt(0)));
- }
- private final static String padNames(String names) throws IndexOutOfBoundsException {
- int len = names.length();
- if (len == 0) {
- return "________________"; // 16 _
- } else if (len < NAME_LENGTH) {
- StringBuffer sb = new StringBuffer(names);
- while (sb.length() < NAME_LENGTH) {
- sb.append('_');
- }
- return sb.toString();
- } else if (len > NAME_LENGTH) {
- return names.substring(0, NAME_LENGTH);
- } else if (len == NAME_LENGTH) {
- return names;
- } else {
- // assert "Can not happen";
- return "________________";
- }
- }
- protected final static String encodeFamilyName(final String name) throws IndexOutOfBoundsException,
- NullPointerException {
- return "".equals(name) ? "" : new StringBuffer(name.length()).append(
- (char) (name.charAt(0) - FAMILY_NAME_FIRST_LETTER_CORRECTION)).append(encodeName(name.substring(1))).toString();
- }
- protected final static String decodeFamilyName(final String name) throws IndexOutOfBoundsException,
- NullPointerException {
- return "".equals(name) ? "" : new StringBuffer(name.length()).append(
- (char) (name.charAt(0) + FAMILY_NAME_FIRST_LETTER_CORRECTION)).append(decodeName(name.substring(1)))
- .toString();
- }
- protected final static String encodeName(final String name) throws NullPointerException {
- return (name.indexOf(' ') != -1) ? name.replace(' ', '_') : name;
- }
- protected final static String decodeName(final String name) throws NullPointerException {
- return (name.indexOf('_') != -1) ? name.replace('_', ' ') : name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement