Builder.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) 2004-2013 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.model.clientstrings;
  20. import java.util.ArrayList;
  21. /**
  22. * @author Forsaiken, Zoey76
  23. */
  24. public abstract class Builder
  25. {
  26. public abstract String toString(final Object param);
  27. public abstract String toString(final Object... params);
  28. public abstract int getIndex();
  29. public static final Builder newBuilder(final String text)
  30. {
  31. final ArrayList<Builder> builders = new ArrayList<>();
  32. int index1 = 0, index2 = 0, paramId, subTextLen;
  33. final char[] array = text.toCharArray();
  34. final int arrayLength = array.length;
  35. char c, c2, c3;
  36. LOOP:
  37. for (; index1 < arrayLength; index1++)
  38. {
  39. c = array[index1];
  40. if ((c == '$') && (index1 < (arrayLength - 2)))
  41. {
  42. c2 = array[index1 + 1];
  43. if ((c2 == 'c') || (c2 == 's') || (c2 == 'p') || (c2 == 'C') || (c2 == 'S') || (c2 == 'P'))
  44. {
  45. c3 = array[index1 + 2];
  46. if (Character.isDigit(c3))
  47. {
  48. paramId = Character.getNumericValue(c3);
  49. subTextLen = index1 - index2;
  50. if (subTextLen != 0)
  51. {
  52. builders.add(new BuilderText(new String(array, index2, subTextLen)));
  53. }
  54. builders.add(new BuilderObject(paramId));
  55. index1 += 2;
  56. index2 = index1 + 1;
  57. continue LOOP;
  58. }
  59. }
  60. }
  61. }
  62. if (arrayLength >= index1)
  63. {
  64. subTextLen = index1 - index2;
  65. if (subTextLen != 0)
  66. {
  67. builders.add(new BuilderText(new String(array, index2, subTextLen)));
  68. }
  69. }
  70. if (builders.size() == 1)
  71. {
  72. return builders.get(0);
  73. }
  74. return new BuilderContainer(builders.toArray(new Builder[builders.size()]));
  75. }
  76. }