PetNameTable.java 3.2 KB

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