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.

CellFormatter.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.format;
  16. import java.util.Locale;
  17. import java.util.logging.Logger;
  18. import org.apache.poi.util.LocaleUtil;
  19. /**
  20. * This is the abstract supertype for the various cell formatters.
  21. *
  22. * @author Ken Arnold, Industrious Media LLC
  23. */
  24. public abstract class CellFormatter {
  25. /** The original specified format. */
  26. protected final String format;
  27. protected final Locale locale;
  28. /**
  29. * Creates a new formatter object, storing the format in {@link #format}.
  30. *
  31. * @param format The format.
  32. */
  33. public CellFormatter(String format) {
  34. this(LocaleUtil.getUserLocale(), format);
  35. }
  36. /**
  37. * Creates a new formatter object, storing the format in {@link #format}.
  38. *
  39. * @param locale The locale.
  40. * @param format The format.
  41. */
  42. public CellFormatter(Locale locale, String format) {
  43. this.locale = locale;
  44. this.format = format;
  45. }
  46. /** The logger to use in the formatting code. */
  47. static final Logger logger = Logger.getLogger(
  48. CellFormatter.class.getName());
  49. /**
  50. * Format a value according the format string.
  51. * <p/>
  52. * NOTE: this method must be thread safe! In particular, if it uses a
  53. * Format instance that is not thread safe, i.e. DateFormat, this method
  54. * must be synchronized, either on the method, if the format is a final
  55. * property, or on the format instance itself.
  56. *
  57. * @param toAppendTo The buffer to append to.
  58. * @param value The value to format.
  59. */
  60. public abstract void formatValue(StringBuffer toAppendTo, Object value);
  61. /**
  62. * Format a value according to the type, in the most basic way.
  63. * <p/>
  64. * NOTE: this method must be thread safe! In particular, if it uses a
  65. * Format instance that is not thread safe, i.e. DateFormat, this method
  66. * must be synchronized, either on the method, if the format is a final
  67. * property, or on the format instance itself.
  68. *
  69. * @param toAppendTo The buffer to append to.
  70. * @param value The value to format.
  71. */
  72. public abstract void simpleValue(StringBuffer toAppendTo, Object value);
  73. /**
  74. * Formats the value, returning the resulting string.
  75. *
  76. * @param value The value to format.
  77. *
  78. * @return The value, formatted.
  79. */
  80. public String format(Object value) {
  81. StringBuffer sb = new StringBuffer();
  82. formatValue(sb, value);
  83. return sb.toString();
  84. }
  85. /**
  86. * Formats the value in the most basic way, returning the resulting string.
  87. *
  88. * @param value The value to format.
  89. *
  90. * @return The value, formatted.
  91. */
  92. public String simpleFormat(Object value) {
  93. StringBuffer sb = new StringBuffer();
  94. simpleValue(sb, value);
  95. return sb.toString();
  96. }
  97. /**
  98. * Returns the input string, surrounded by quotes.
  99. *
  100. * @param str The string to quote.
  101. *
  102. * @return The input string, surrounded by quotes.
  103. */
  104. static String quote(String str) {
  105. return '"' + str + '"';
  106. }
  107. }