PetNameTable.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.datatables;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.logging.Logger;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. import java.util.regex.PatternSyntaxException;
  23. import net.sf.l2j.Config;
  24. import net.sf.l2j.L2DatabaseFactory;
  25. import net.sf.l2j.gameserver.model.L2PetDataTable;
  26. public class PetNameTable
  27. {
  28. private static Logger _log = Logger.getLogger(PetNameTable.class.getName());
  29. private static PetNameTable _instance;
  30. public static PetNameTable getInstance()
  31. {
  32. if (_instance == null)
  33. {
  34. _instance = new PetNameTable();
  35. }
  36. return _instance;
  37. }
  38. public boolean doesPetNameExist(String name, int petNpcId)
  39. {
  40. boolean result = true;
  41. java.sql.Connection con = null;
  42. try
  43. {
  44. con = L2DatabaseFactory.getInstance().getConnection();
  45. PreparedStatement statement = con.prepareStatement("SELECT name FROM pets p, items i WHERE p.item_obj_id = i.object_id AND name=? AND i.item_id IN (?)");
  46. statement.setString(1, name);
  47. String cond = "";
  48. for (int it : L2PetDataTable.getPetItemsByNpc(petNpcId))
  49. {
  50. if (cond != "")
  51. cond += ", ";
  52. cond += it;
  53. }
  54. statement.setString(2, cond);
  55. ResultSet rset = statement.executeQuery();
  56. result = rset.next();
  57. rset.close();
  58. statement.close();
  59. }
  60. catch (SQLException e)
  61. {
  62. _log.warning("could not check existing petname:" + e.getMessage());
  63. }
  64. finally
  65. {
  66. try
  67. {
  68. con.close();
  69. }
  70. catch (Exception e)
  71. {
  72. }
  73. }
  74. return result;
  75. }
  76. public boolean isValidPetName(String name)
  77. {
  78. boolean result = true;
  79. if (!isAlphaNumeric(name))
  80. return result;
  81. Pattern pattern;
  82. try
  83. {
  84. pattern = Pattern.compile(Config.PET_NAME_TEMPLATE);
  85. }
  86. catch (PatternSyntaxException e) // case of illegal pattern
  87. {
  88. _log.warning("ERROR : Pet name pattern of config is wrong!");
  89. pattern = Pattern.compile(".*");
  90. }
  91. Matcher regexp = pattern.matcher(name);
  92. if (!regexp.matches())
  93. {
  94. result = false;
  95. }
  96. return result;
  97. }
  98. private boolean isAlphaNumeric(String text)
  99. {
  100. boolean result = true;
  101. char[] chars = text.toCharArray();
  102. for (int i = 0; i < chars.length; i++)
  103. {
  104. if (!Character.isLetterOrDigit(chars[i]))
  105. {
  106. result = false;
  107. break;
  108. }
  109. }
  110. return result;
  111. }
  112. }