Builder.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2004-2014 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: for (; index1 < arrayLength; index1++)
  37. {
  38. c = array[index1];
  39. if ((c == '$') && (index1 < (arrayLength - 2)))
  40. {
  41. c2 = array[index1 + 1];
  42. if ((c2 == 'c') || (c2 == 's') || (c2 == 'p') || (c2 == 'C') || (c2 == 'S') || (c2 == 'P'))
  43. {
  44. c3 = array[index1 + 2];
  45. if (Character.isDigit(c3))
  46. {
  47. paramId = Character.getNumericValue(c3);
  48. subTextLen = index1 - index2;
  49. if (subTextLen != 0)
  50. {
  51. builders.add(new BuilderText(new String(array, index2, subTextLen)));
  52. }
  53. builders.add(new BuilderObject(paramId));
  54. index1 += 2;
  55. index2 = index1 + 1;
  56. continue LOOP;
  57. }
  58. }
  59. }
  60. }
  61. if (arrayLength >= index1)
  62. {
  63. subTextLen = index1 - index2;
  64. if (subTextLen != 0)
  65. {
  66. builders.add(new BuilderText(new String(array, index2, subTextLen)));
  67. }
  68. }
  69. if (builders.size() == 1)
  70. {
  71. return builders.get(0);
  72. }
  73. return new BuilderContainer(builders.toArray(new Builder[builders.size()]));
  74. }
  75. }