2
0

PetNameTable.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.data.sql.impl;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;
  28. import java.util.regex.PatternSyntaxException;
  29. import com.l2jserver.Config;
  30. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  31. import com.l2jserver.gameserver.data.xml.impl.PetDataTable;
  32. public class PetNameTable
  33. {
  34. private static Logger LOGGER = Logger.getLogger(PetNameTable.class.getName());
  35. public static PetNameTable getInstance()
  36. {
  37. return SingletonHolder._instance;
  38. }
  39. public boolean doesPetNameExist(String name, int petNpcId)
  40. {
  41. boolean result = true;
  42. try (Connection con = ConnectionFactory.getInstance().getConnection();
  43. 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 (?)"))
  44. {
  45. ps.setString(1, name);
  46. StringBuilder cond = new StringBuilder();
  47. if (!cond.toString().isEmpty())
  48. {
  49. cond.append(", ");
  50. }
  51. cond.append(PetDataTable.getInstance().getPetItemsByNpc(petNpcId));
  52. ps.setString(2, cond.toString());
  53. try (ResultSet rs = ps.executeQuery())
  54. {
  55. result = rs.next();
  56. }
  57. }
  58. catch (SQLException e)
  59. {
  60. LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing petname:" + e.getMessage(), e);
  61. }
  62. return result;
  63. }
  64. public boolean isValidPetName(String name)
  65. {
  66. boolean result = true;
  67. if (!isAlphaNumeric(name))
  68. {
  69. return result;
  70. }
  71. Pattern pattern;
  72. try
  73. {
  74. pattern = Pattern.compile(Config.PET_NAME_TEMPLATE);
  75. }
  76. catch (PatternSyntaxException e) // case of illegal pattern
  77. {
  78. LOGGER.warning(getClass().getSimpleName() + ": Pet name pattern of config is wrong!");
  79. pattern = Pattern.compile(".*");
  80. }
  81. Matcher regexp = pattern.matcher(name);
  82. if (!regexp.matches())
  83. {
  84. result = false;
  85. }
  86. return result;
  87. }
  88. private boolean isAlphaNumeric(String text)
  89. {
  90. boolean result = true;
  91. char[] chars = text.toCharArray();
  92. for (int i = 0; i < chars.length; i++)
  93. {
  94. if (!Character.isLetterOrDigit(chars[i]))
  95. {
  96. result = false;
  97. break;
  98. }
  99. }
  100. return result;
  101. }
  102. private static class SingletonHolder
  103. {
  104. protected static final PetNameTable _instance = new PetNameTable();
  105. }
  106. }