You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SecureChartDataEncoding.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2007 Daniel Spiewak.
  3. * Copyright 2013 gitblit.com.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.gitblit.wicket.charting;
  18. /**
  19. * This class is a pristine fork of org.wicketstuff.googlecharts.ChartDataEncoding
  20. * to bring the package-protected convert methods to SecureChart.
  21. *
  22. * @author Daniel Spiewak
  23. */
  24. public enum SecureChartDataEncoding {
  25. SIMPLE("s", "", ",") {
  26. @Override
  27. CharSequence convert(double value, double max) {
  28. if (value < 0) {
  29. return "_";
  30. }
  31. value = Math.round((CHARS.length() - 1) * value / max);
  32. if (value > CHARS.length() - 1) {
  33. throw new IllegalArgumentException(value + " is out of range for SIMPLE encoding");
  34. }
  35. return Character.toString(CHARS.charAt((int) value));
  36. }
  37. },
  38. TEXT("t", ",", "|") {
  39. @Override
  40. CharSequence convert(double value, double max) {
  41. if (value < 0) {
  42. value = -1;
  43. }
  44. if (value > 100) {
  45. throw new IllegalArgumentException(value + " is out of range for TEXT encoding");
  46. }
  47. return Double.toString(value);
  48. }
  49. },
  50. EXTENDED("e", "", ",") {
  51. @Override
  52. CharSequence convert(double value, double max) {
  53. if (value < 0) {
  54. return "__";
  55. }
  56. value = Math.round(value);
  57. if (value > (EXT_CHARS.length() - 1) * (EXT_CHARS.length() - 1)) {
  58. throw new IllegalArgumentException(value + " is out of range for EXTENDED encoding");
  59. }
  60. int rem = (int) (value % EXT_CHARS.length());
  61. int exp = (int) (value / EXT_CHARS.length());
  62. return new StringBuilder().append(EXT_CHARS.charAt(exp)).append(EXT_CHARS.charAt(rem));
  63. }
  64. };
  65. private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  66. private static final String EXT_CHARS = CHARS + "-_.";
  67. private final String rendering, valueSeparator, setSeparator;
  68. private SecureChartDataEncoding(String rendering, String valueSeparator, String setSeparator) {
  69. this.rendering = rendering;
  70. this.valueSeparator = valueSeparator;
  71. this.setSeparator = setSeparator;
  72. }
  73. public String getRendering() {
  74. return rendering;
  75. }
  76. public String getValueSeparator() {
  77. return valueSeparator;
  78. }
  79. public String getSetSeparator() {
  80. return setSeparator;
  81. }
  82. abstract CharSequence convert(double value, double max);
  83. }