PetNameTable.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 com.l2jserver.gameserver.datatables;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;
  24. import java.util.regex.PatternSyntaxException;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.L2DatabaseFactory;
  27. public class PetNameTable
  28. {
  29. private static Logger _log = Logger.getLogger(PetNameTable.class.getName());
  30. public static PetNameTable getInstance()
  31. {
  32. return SingletonHolder._instance;
  33. }
  34. public boolean doesPetNameExist(String name, int petNpcId)
  35. {
  36. boolean result = true;
  37. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  38. PreparedStatement ps = con.prepareStatement("SELECT name FROM pets p, items i WHERE p.item_obj_id = i.object_id AND name=? AND i.item_id IN (?)"))
  39. {
  40. ps.setString(1, name);
  41. StringBuilder cond = new StringBuilder();
  42. for (int it : PetDataTable.getPetItemsByNpc(petNpcId))
  43. {
  44. if (!cond.toString().isEmpty())
  45. {
  46. cond.append(", ");
  47. }
  48. cond.append(it);
  49. }
  50. ps.setString(2, cond.toString());
  51. try (ResultSet rs = ps.executeQuery())
  52. {
  53. result = rs.next();
  54. }
  55. }
  56. catch (SQLException e)
  57. {
  58. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing petname:" + e.getMessage(), e);
  59. }
  60. return result;
  61. }
  62. public boolean isValidPetName(String name)
  63. {
  64. boolean result = true;
  65. if (!isAlphaNumeric(name))
  66. {
  67. return result;
  68. }
  69. Pattern pattern;
  70. try
  71. {
  72. pattern = Pattern.compile(Config.PET_NAME_TEMPLATE);
  73. }
  74. catch (PatternSyntaxException e) // case of illegal pattern
  75. {
  76. _log.warning(getClass().getSimpleName() + ": Pet name pattern of config is wrong!");
  77. pattern = Pattern.compile(".*");
  78. }
  79. Matcher regexp = pattern.matcher(name);
  80. if (!regexp.matches())
  81. {
  82. result = false;
  83. }
  84. return result;
  85. }
  86. private boolean isAlphaNumeric(String text)
  87. {
  88. boolean result = true;
  89. char[] chars = text.toCharArray();
  90. for (int i = 0; i < chars.length; i++)
  91. {
  92. if (!Character.isLetterOrDigit(chars[i]))
  93. {
  94. result = false;
  95. break;
  96. }
  97. }
  98. return result;
  99. }
  100. private static class SingletonHolder
  101. {
  102. protected static final PetNameTable _instance = new PetNameTable();
  103. }
  104. }