PetNameTable.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Logger;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. import java.util.regex.PatternSyntaxException;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.L2DatabaseFactory;
  26. public class PetNameTable
  27. {
  28. private static Logger _log = Logger.getLogger(PetNameTable.class.getName());
  29. private PetNameTable()
  30. {
  31. }
  32. public static PetNameTable getInstance()
  33. {
  34. return SingletonHolder._instance;
  35. }
  36. public boolean doesPetNameExist(String name, int petNpcId)
  37. {
  38. boolean result = true;
  39. Connection con = null;
  40. try
  41. {
  42. con = L2DatabaseFactory.getInstance().getConnection();
  43. 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 (?)");
  44. statement.setString(1, name);
  45. String cond = "";
  46. for (int it : PetDataTable.getPetItemsByNpc(petNpcId))
  47. {
  48. if (!cond.isEmpty())
  49. cond += ", ";
  50. cond += it;
  51. }
  52. statement.setString(2, cond);
  53. ResultSet rset = statement.executeQuery();
  54. result = rset.next();
  55. rset.close();
  56. statement.close();
  57. }
  58. catch (SQLException e)
  59. {
  60. _log.warning("could not check existing petname:" + e.getMessage());
  61. }
  62. finally
  63. {
  64. try
  65. {
  66. con.close();
  67. }
  68. catch (Exception e)
  69. {
  70. }
  71. }
  72. return result;
  73. }
  74. public boolean isValidPetName(String name)
  75. {
  76. boolean result = true;
  77. if (!isAlphaNumeric(name))
  78. return result;
  79. Pattern pattern;
  80. try
  81. {
  82. pattern = Pattern.compile(Config.PET_NAME_TEMPLATE);
  83. }
  84. catch (PatternSyntaxException e) // case of illegal pattern
  85. {
  86. _log.warning("ERROR : Pet name pattern of config is wrong!");
  87. pattern = Pattern.compile(".*");
  88. }
  89. Matcher regexp = pattern.matcher(name);
  90. if (!regexp.matches())
  91. {
  92. result = false;
  93. }
  94. return result;
  95. }
  96. private boolean isAlphaNumeric(String text)
  97. {
  98. boolean result = true;
  99. char[] chars = text.toCharArray();
  100. for (int i = 0; i < chars.length; i++)
  101. {
  102. if (!Character.isLetterOrDigit(chars[i]))
  103. {
  104. result = false;
  105. break;
  106. }
  107. }
  108. return result;
  109. }
  110. @SuppressWarnings("synthetic-access")
  111. private static class SingletonHolder
  112. {
  113. protected static final PetNameTable _instance = new PetNameTable();
  114. }
  115. }