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.

DataFormatter.java 48KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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. 2012 - Alfresco Software, Ltd.
  15. Alfresco Software has modified source of this file
  16. The details of changes as svn diff can be found in svn at location root/projects/3rd-party/src
  17. ==================================================================== */
  18. package org.apache.poi.ss.usermodel;
  19. import java.math.BigDecimal;
  20. import java.math.RoundingMode;
  21. import java.text.DateFormat;
  22. import java.text.DateFormatSymbols;
  23. import java.text.DecimalFormat;
  24. import java.text.DecimalFormatSymbols;
  25. import java.text.FieldPosition;
  26. import java.text.Format;
  27. import java.text.ParsePosition;
  28. import java.text.SimpleDateFormat;
  29. import java.util.ArrayList;
  30. import java.util.Date;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Locale;
  34. import java.util.Map;
  35. import java.util.Observable;
  36. import java.util.Observer;
  37. import java.util.regex.Matcher;
  38. import java.util.regex.Pattern;
  39. import org.apache.poi.ss.format.CellFormat;
  40. import org.apache.poi.ss.format.CellFormatResult;
  41. import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
  42. import org.apache.poi.ss.util.DateFormatConverter;
  43. import org.apache.poi.ss.util.NumberToTextConverter;
  44. import org.apache.poi.util.LocaleUtil;
  45. import org.apache.poi.util.POILogFactory;
  46. import org.apache.poi.util.POILogger;
  47. /**
  48. * DataFormatter contains methods for formatting the value stored in an
  49. * Cell. This can be useful for reports and GUI presentations when you
  50. * need to display data exactly as it appears in Excel. Supported formats
  51. * include currency, SSN, percentages, decimals, dates, phone numbers, zip
  52. * codes, etc.
  53. * <p>
  54. * Internally, formats will be implemented using subclasses of {@link Format}
  55. * such as {@link DecimalFormat} and {@link java.text.SimpleDateFormat}. Therefore the
  56. * formats used by this class must obey the same pattern rules as these Format
  57. * subclasses. This means that only legal number pattern characters ("0", "#",
  58. * ".", "," etc.) may appear in number formats. Other characters can be
  59. * inserted <em>before</em> or <em> after</em> the number pattern to form a
  60. * prefix or suffix.
  61. * </p>
  62. * <p>
  63. * For example the Excel pattern <code>"$#,##0.00 "USD"_);($#,##0.00 "USD")"
  64. * </code> will be correctly formatted as "$1,000.00 USD" or "($1,000.00 USD)".
  65. * However the pattern <code>"00-00-00"</code> is incorrectly formatted by
  66. * DecimalFormat as "000000--". For Excel formats that are not compatible with
  67. * DecimalFormat, you can provide your own custom {@link Format} implementation
  68. * via <code>DataFormatter.addFormat(String,Format)</code>. The following
  69. * custom formats are already provided by this class:
  70. * </p>
  71. * <pre>
  72. * <ul><li>SSN "000-00-0000"</li>
  73. * <li>Phone Number "(###) ###-####"</li>
  74. * <li>Zip plus 4 "00000-0000"</li>
  75. * </ul>
  76. * </pre>
  77. * <p>
  78. * If the Excel format pattern cannot be parsed successfully, then a default
  79. * format will be used. The default number format will mimic the Excel General
  80. * format: "#" for whole numbers and "#.##########" for decimal numbers. You
  81. * can override the default format pattern with <code>
  82. * DataFormatter.setDefaultNumberFormat(Format)</code>. <b>Note:</b> the
  83. * default format will only be used when a Format cannot be created from the
  84. * cell's data format string.
  85. *
  86. * <p>
  87. * Note that by default formatted numeric values are trimmed.
  88. * Excel formats can contain spacers and padding and the default behavior is to strip them off.
  89. * </p>
  90. * <p>Example:</p>
  91. * <p>
  92. * Consider a numeric cell with a value <code>12.343</code> and format <code>"##.##_ "</code>.
  93. * The trailing underscore and space ("_ ") in the format adds a space to the end and Excel formats this cell as <code>"12.34 "</code>,
  94. * but <code>DataFormatter</code> trims the formatted value and returns <code>"12.34"</code>.
  95. * </p>
  96. * You can enable spaces by passing the <code>emulateCSV=true</code> flag in the <code>DateFormatter</code> cosntructor.
  97. * If set to true, then the output tries to conform to what you get when you take an xls or xlsx in Excel and Save As CSV file:
  98. * <ul>
  99. * <li>returned values are not trimmed</li>
  100. * <li>Invalid dates are formatted as 255 pound signs ("#")</li>
  101. * <li>simulate Excel's handling of a format string of all # when the value is 0.
  102. * Excel will output "", <code>DataFormatter</code> will output "0".
  103. * </ul>
  104. * <p>
  105. * Some formats are automatically "localized" by Excel, eg show as mm/dd/yyyy when
  106. * loaded in Excel in some Locales but as dd/mm/yyyy in others. These are always
  107. * returned in the "default" (US) format, as stored in the file.
  108. * Some format strings request an alternate locale, eg
  109. * <code>[$-809]d/m/yy h:mm AM/PM</code> which explicitly requests UK locale.
  110. * These locale directives are (currently) ignored.
  111. * You can use {@link DateFormatConverter} to do some of this localisation if
  112. * you need it.
  113. */
  114. public class DataFormatter implements Observer {
  115. private static final String defaultFractionWholePartFormat = "#";
  116. private static final String defaultFractionFractionPartFormat = "#/##";
  117. /** Pattern to find a number format: "0" or "#" */
  118. private static final Pattern numPattern = Pattern.compile("[0#]+");
  119. /** Pattern to find days of week as text "ddd...." */
  120. private static final Pattern daysAsText = Pattern.compile("([d]{3,})", Pattern.CASE_INSENSITIVE);
  121. /** Pattern to find "AM/PM" marker */
  122. private static final Pattern amPmPattern = Pattern.compile("((A|P)[M/P]*)", Pattern.CASE_INSENSITIVE);
  123. /** Pattern to find formats with condition ranges e.g. [>=100] */
  124. private static final Pattern rangeConditionalPattern = Pattern.compile(".*\\[\\s*(>|>=|<|<=|=)\\s*[0-9]*\\.*[0-9].*");
  125. /**
  126. * A regex to find locale patterns like [$$-1009] and [$?-452].
  127. * Note that we don't currently process these into locales
  128. */
  129. private static final Pattern localePatternGroup = Pattern.compile("(\\[\\$[^-\\]]*-[0-9A-Z]+\\])");
  130. /**
  131. * A regex to match the colour formattings rules.
  132. * Allowed colours are: Black, Blue, Cyan, Green,
  133. * Magenta, Red, White, Yellow, "Color n" (1<=n<=56)
  134. */
  135. private static final Pattern colorPattern =
  136. Pattern.compile("(\\[BLACK\\])|(\\[BLUE\\])|(\\[CYAN\\])|(\\[GREEN\\])|" +
  137. "(\\[MAGENTA\\])|(\\[RED\\])|(\\[WHITE\\])|(\\[YELLOW\\])|" +
  138. "(\\[COLOR\\s*\\d\\])|(\\[COLOR\\s*[0-5]\\d\\])", Pattern.CASE_INSENSITIVE);
  139. /**
  140. * A regex to identify a fraction pattern.
  141. * This requires that replaceAll("\\?", "#") has already been called
  142. */
  143. private static final Pattern fractionPattern = Pattern.compile("(?:([#\\d]+)\\s+)?(#+)\\s*\\/\\s*([#\\d]+)");
  144. /**
  145. * A regex to strip junk out of fraction formats
  146. */
  147. private static final Pattern fractionStripper = Pattern.compile("(\"[^\"]*\")|([^ \\?#\\d\\/]+)");
  148. /**
  149. * A regex to detect if an alternate grouping character is used
  150. * in a numeric format
  151. */
  152. private static final Pattern alternateGrouping = Pattern.compile("([#0]([^.#0])[#0]{3})");
  153. /**
  154. * Cells formatted with a date or time format and which contain invalid date or time values
  155. * show 255 pound signs ("#").
  156. */
  157. private static final String invalidDateTimeString;
  158. static {
  159. StringBuilder buf = new StringBuilder();
  160. for(int i = 0; i < 255; i++) buf.append('#');
  161. invalidDateTimeString = buf.toString();
  162. }
  163. /**
  164. * The decimal symbols of the locale used for formatting values.
  165. */
  166. private DecimalFormatSymbols decimalSymbols;
  167. /**
  168. * The date symbols of the locale used for formatting values.
  169. */
  170. private DateFormatSymbols dateSymbols;
  171. /**
  172. * A default date format, if no date format was given
  173. */
  174. private DateFormat defaultDateformat;
  175. /** <em>General</em> format for numbers. */
  176. private Format generalNumberFormat;
  177. /** A default format to use when a number pattern cannot be parsed. */
  178. private Format defaultNumFormat;
  179. /**
  180. * A map to cache formats.
  181. * Map<String,Format> formats
  182. */
  183. private final Map<String,Format> formats = new HashMap<String,Format>();
  184. private final boolean emulateCSV;
  185. /** stores the locale valid it the last formatting call */
  186. private Locale locale;
  187. /** stores if the locale should change according to {@link LocaleUtil#getUserLocale()} */
  188. private boolean localeIsAdapting;
  189. private class LocaleChangeObservable extends Observable {
  190. void checkForLocaleChange() {
  191. checkForLocaleChange(LocaleUtil.getUserLocale());
  192. }
  193. void checkForLocaleChange(Locale newLocale) {
  194. if (!localeIsAdapting) return;
  195. if (newLocale.equals(locale)) return;
  196. super.setChanged();
  197. notifyObservers(newLocale);
  198. }
  199. }
  200. /** the Observable to notify, when the locale has been changed */
  201. private final LocaleChangeObservable localeChangedObservable = new LocaleChangeObservable();
  202. /** For logging any problems we find */
  203. private static POILogger logger = POILogFactory.getLogger(DataFormatter.class);
  204. /**
  205. * Creates a formatter using the {@link Locale#getDefault() default locale}.
  206. */
  207. public DataFormatter() {
  208. this(false);
  209. }
  210. /**
  211. * Creates a formatter using the {@link Locale#getDefault() default locale}.
  212. *
  213. * @param emulateCSV whether to emulate CSV output.
  214. */
  215. public DataFormatter(boolean emulateCSV) {
  216. this(LocaleUtil.getUserLocale(), true, emulateCSV);
  217. }
  218. /**
  219. * Creates a formatter using the given locale.
  220. */
  221. public DataFormatter(Locale locale) {
  222. this(locale, false);
  223. }
  224. /**
  225. * Creates a formatter using the given locale.
  226. *
  227. * @param emulateCSV whether to emulate CSV output.
  228. */
  229. public DataFormatter(Locale locale, boolean emulateCSV) {
  230. this(locale, false, emulateCSV);
  231. }
  232. /**
  233. * Creates a formatter using the given locale.
  234. * @param localeIsAdapting (true only if locale is not user-specified)
  235. * @param emulateCSV whether to emulate CSV output.
  236. */
  237. private DataFormatter(Locale locale, boolean localeIsAdapting, boolean emulateCSV) {
  238. this.localeIsAdapting = true;
  239. localeChangedObservable.addObserver(this);
  240. // localeIsAdapting must be true prior to this first checkForLocaleChange call.
  241. localeChangedObservable.checkForLocaleChange(locale);
  242. // set localeIsAdapting so subsequent checks perform correctly
  243. // (whether a specific locale was provided to this DataFormatter or DataFormatter should
  244. // adapt to the current user locale as the locale changes)
  245. this.localeIsAdapting = localeIsAdapting;
  246. this.emulateCSV = emulateCSV;
  247. }
  248. /**
  249. * Return a Format for the given cell if one exists, otherwise try to
  250. * create one. This method will return <code>null</code> if the any of the
  251. * following is true:
  252. * <ul>
  253. * <li>the cell's style is null</li>
  254. * <li>the style's data format string is null or empty</li>
  255. * <li>the format string cannot be recognized as either a number or date</li>
  256. * </ul>
  257. *
  258. * @param cell The cell to retrieve a Format for
  259. * @return A Format for the format String
  260. */
  261. private Format getFormat(Cell cell) {
  262. return getFormat(cell, null);
  263. }
  264. private Format getFormat(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
  265. if (cell == null) return null;
  266. ExcelNumberFormat numFmt = ExcelNumberFormat.from(cell, cfEvaluator);
  267. if ( numFmt == null) {
  268. return null;
  269. }
  270. int formatIndex = numFmt.getIdx();
  271. String formatStr = numFmt.getFormat();
  272. if(formatStr == null || formatStr.trim().length() == 0) {
  273. return null;
  274. }
  275. return getFormat(cell.getNumericCellValue(), formatIndex, formatStr);
  276. }
  277. private Format getFormat(double cellValue, int formatIndex, String formatStrIn) {
  278. localeChangedObservable.checkForLocaleChange();
  279. // // Might be better to separate out the n p and z formats, falling back to p when n and z are not set.
  280. // // That however would require other code to be re factored.
  281. // String[] formatBits = formatStrIn.split(";");
  282. // int i = cellValue > 0.0 ? 0 : cellValue < 0.0 ? 1 : 2;
  283. // String formatStr = (i < formatBits.length) ? formatBits[i] : formatBits[0];
  284. String formatStr = formatStrIn;
  285. // Excel supports 2+ part conditional data formats, eg positive/negative/zero,
  286. // or (>1000),(>0),(0),(negative). As Java doesn't handle these kinds
  287. // of different formats for different ranges, just +ve/-ve, we need to
  288. // handle these ourselves in a special way.
  289. // For now, if we detect 2+ parts, we call out to CellFormat to handle it
  290. // TODO Going forward, we should really merge the logic between the two classes
  291. if (formatStr.contains(";") &&
  292. (formatStr.indexOf(';') != formatStr.lastIndexOf(';')
  293. || rangeConditionalPattern.matcher(formatStr).matches()
  294. ) ) {
  295. try {
  296. // Ask CellFormat to get a formatter for it
  297. CellFormat cfmt = CellFormat.getInstance(formatStr);
  298. // CellFormat requires callers to identify date vs not, so do so
  299. Object cellValueO = Double.valueOf(cellValue);
  300. if (DateUtil.isADateFormat(formatIndex, formatStr) &&
  301. // don't try to handle Date value 0, let a 3 or 4-part format take care of it
  302. ((Double)cellValueO).doubleValue() != 0.0) {
  303. cellValueO = DateUtil.getJavaDate(cellValue);
  304. }
  305. // Wrap and return (non-cachable - CellFormat does that)
  306. return new CellFormatResultWrapper( cfmt.apply(cellValueO) );
  307. } catch (Exception e) {
  308. logger.log(POILogger.WARN, "Formatting failed for format " + formatStr + ", falling back", e);
  309. }
  310. }
  311. // Excel's # with value 0 will output empty where Java will output 0. This hack removes the # from the format.
  312. if (emulateCSV && cellValue == 0.0 && formatStr.contains("#") && !formatStr.contains("0")) {
  313. formatStr = formatStr.replaceAll("#", "");
  314. }
  315. // See if we already have it cached
  316. Format format = formats.get(formatStr);
  317. if (format != null) {
  318. return format;
  319. }
  320. // Is it one of the special built in types, General or @?
  321. if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
  322. return generalNumberFormat;
  323. }
  324. // Build a formatter, and cache it
  325. format = createFormat(cellValue, formatIndex, formatStr);
  326. formats.put(formatStr, format);
  327. return format;
  328. }
  329. /**
  330. * Create and return a Format based on the format string from a cell's
  331. * style. If the pattern cannot be parsed, return a default pattern.
  332. *
  333. * @param cell The Excel cell
  334. * @return A Format representing the excel format. May return null.
  335. */
  336. public Format createFormat(Cell cell) {
  337. int formatIndex = cell.getCellStyle().getDataFormat();
  338. String formatStr = cell.getCellStyle().getDataFormatString();
  339. return createFormat(cell.getNumericCellValue(), formatIndex, formatStr);
  340. }
  341. private Format createFormat(double cellValue, int formatIndex, String sFormat) {
  342. localeChangedObservable.checkForLocaleChange();
  343. String formatStr = sFormat;
  344. // Remove colour formatting if present
  345. Matcher colourM = colorPattern.matcher(formatStr);
  346. while(colourM.find()) {
  347. String colour = colourM.group();
  348. // Paranoid replacement...
  349. int at = formatStr.indexOf(colour);
  350. if(at == -1) break;
  351. String nFormatStr = formatStr.substring(0,at) +
  352. formatStr.substring(at+colour.length());
  353. if(nFormatStr.equals(formatStr)) break;
  354. // Try again in case there's multiple
  355. formatStr = nFormatStr;
  356. colourM = colorPattern.matcher(formatStr);
  357. }
  358. // Strip off the locale information, we use an instance-wide locale for everything
  359. Matcher m = localePatternGroup.matcher(formatStr);
  360. while(m.find()) {
  361. String match = m.group();
  362. String symbol = match.substring(match.indexOf('$') + 1, match.indexOf('-'));
  363. if (symbol.indexOf('$') > -1) {
  364. symbol = symbol.substring(0, symbol.indexOf('$')) +
  365. '\\' +
  366. symbol.substring(symbol.indexOf('$'), symbol.length());
  367. }
  368. formatStr = m.replaceAll(symbol);
  369. m = localePatternGroup.matcher(formatStr);
  370. }
  371. // Check for special cases
  372. if(formatStr == null || formatStr.trim().length() == 0) {
  373. return getDefaultFormat(cellValue);
  374. }
  375. if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
  376. return generalNumberFormat;
  377. }
  378. if(DateUtil.isADateFormat(formatIndex,formatStr) &&
  379. DateUtil.isValidExcelDate(cellValue)) {
  380. return createDateFormat(formatStr, cellValue);
  381. }
  382. // Excel supports fractions in format strings, which Java doesn't
  383. if (formatStr.contains("#/") || formatStr.contains("?/")) {
  384. String[] chunks = formatStr.split(";");
  385. for (String chunk1 : chunks) {
  386. String chunk = chunk1.replaceAll("\\?", "#");
  387. Matcher matcher = fractionStripper.matcher(chunk);
  388. chunk = matcher.replaceAll(" ");
  389. chunk = chunk.replaceAll(" +", " ");
  390. Matcher fractionMatcher = fractionPattern.matcher(chunk);
  391. //take the first match
  392. if (fractionMatcher.find()) {
  393. String wholePart = (fractionMatcher.group(1) == null) ? "" : defaultFractionWholePartFormat;
  394. return new FractionFormat(wholePart, fractionMatcher.group(3));
  395. }
  396. }
  397. // Strip custom text in quotes and escaped characters for now as it can cause performance problems in fractions.
  398. //String strippedFormatStr = formatStr.replaceAll("\\\\ ", " ").replaceAll("\\\\.", "").replaceAll("\"[^\"]*\"", " ").replaceAll("\\?", "#");
  399. //System.out.println("formatStr: "+strippedFormatStr);
  400. return new FractionFormat(defaultFractionWholePartFormat, defaultFractionFractionPartFormat);
  401. }
  402. if (numPattern.matcher(formatStr).find()) {
  403. return createNumberFormat(formatStr, cellValue);
  404. }
  405. if (emulateCSV) {
  406. return new ConstantStringFormat(cleanFormatForNumber(formatStr));
  407. }
  408. // TODO - when does this occur?
  409. return null;
  410. }
  411. private Format createDateFormat(String pFormatStr, double cellValue) {
  412. String formatStr = pFormatStr;
  413. formatStr = formatStr.replaceAll("\\\\-","-");
  414. formatStr = formatStr.replaceAll("\\\\,",",");
  415. formatStr = formatStr.replaceAll("\\\\\\.","."); // . is a special regexp char
  416. formatStr = formatStr.replaceAll("\\\\ "," ");
  417. formatStr = formatStr.replaceAll("\\\\/","/"); // weird: m\\/d\\/yyyy
  418. formatStr = formatStr.replaceAll(";@", "");
  419. formatStr = formatStr.replaceAll("\"/\"", "/"); // "/" is escaped for no reason in: mm"/"dd"/"yyyy
  420. formatStr = formatStr.replace("\"\"", "'"); // replace Excel quoting with Java style quoting
  421. formatStr = formatStr.replaceAll("\\\\T","'T'"); // Quote the T is iso8601 style dates
  422. boolean hasAmPm = false;
  423. Matcher amPmMatcher = amPmPattern.matcher(formatStr);
  424. while (amPmMatcher.find()) {
  425. formatStr = amPmMatcher.replaceAll("@");
  426. hasAmPm = true;
  427. amPmMatcher = amPmPattern.matcher(formatStr);
  428. }
  429. formatStr = formatStr.replaceAll("@", "a");
  430. Matcher dateMatcher = daysAsText.matcher(formatStr);
  431. if (dateMatcher.find()) {
  432. String match = dateMatcher.group(0).toUpperCase(Locale.ROOT).replaceAll("D", "E");
  433. formatStr = dateMatcher.replaceAll(match);
  434. }
  435. // Convert excel date format to SimpleDateFormat.
  436. // Excel uses lower and upper case 'm' for both minutes and months.
  437. // From Excel help:
  438. /*
  439. The "m" or "mm" code must appear immediately after the "h" or"hh"
  440. code or immediately before the "ss" code; otherwise, Microsoft
  441. Excel displays the month instead of minutes."
  442. */
  443. StringBuilder sb = new StringBuilder();
  444. char[] chars = formatStr.toCharArray();
  445. boolean mIsMonth = true;
  446. List<Integer> ms = new ArrayList<Integer>();
  447. boolean isElapsed = false;
  448. for(int j=0; j<chars.length; j++) {
  449. char c = chars[j];
  450. if (c == '\'') {
  451. sb.append(c);
  452. j++;
  453. // skip until the next quote
  454. while(j<chars.length) {
  455. c = chars[j];
  456. sb.append(c);
  457. if(c == '\'') {
  458. break;
  459. }
  460. j++;
  461. }
  462. }
  463. else if (c == '[' && !isElapsed) {
  464. isElapsed = true;
  465. mIsMonth = false;
  466. sb.append(c);
  467. }
  468. else if (c == ']' && isElapsed) {
  469. isElapsed = false;
  470. sb.append(c);
  471. }
  472. else if (isElapsed) {
  473. if (c == 'h' || c == 'H') {
  474. sb.append('H');
  475. }
  476. else if (c == 'm' || c == 'M') {
  477. sb.append('m');
  478. }
  479. else if (c == 's' || c == 'S') {
  480. sb.append('s');
  481. }
  482. else {
  483. sb.append(c);
  484. }
  485. }
  486. else if (c == 'h' || c == 'H') {
  487. mIsMonth = false;
  488. if (hasAmPm) {
  489. sb.append('h');
  490. } else {
  491. sb.append('H');
  492. }
  493. }
  494. else if (c == 'm' || c == 'M') {
  495. if(mIsMonth) {
  496. sb.append('M');
  497. ms.add(
  498. Integer.valueOf(sb.length() -1)
  499. );
  500. } else {
  501. sb.append('m');
  502. }
  503. }
  504. else if (c == 's' || c == 'S') {
  505. sb.append('s');
  506. // if 'M' precedes 's' it should be minutes ('m')
  507. for (int index : ms) {
  508. if (sb.charAt(index) == 'M') {
  509. sb.replace(index, index + 1, "m");
  510. }
  511. }
  512. mIsMonth = true;
  513. ms.clear();
  514. }
  515. else if (Character.isLetter(c)) {
  516. mIsMonth = true;
  517. ms.clear();
  518. if (c == 'y' || c == 'Y') {
  519. sb.append('y');
  520. }
  521. else if (c == 'd' || c == 'D') {
  522. sb.append('d');
  523. }
  524. else {
  525. sb.append(c);
  526. }
  527. }
  528. else {
  529. if (Character.isWhitespace(c)){
  530. ms.clear();
  531. }
  532. sb.append(c);
  533. }
  534. }
  535. formatStr = sb.toString();
  536. try {
  537. return new ExcelStyleDateFormatter(formatStr, dateSymbols);
  538. } catch(IllegalArgumentException iae) {
  539. // the pattern could not be parsed correctly,
  540. // so fall back to the default number format
  541. return getDefaultFormat(cellValue);
  542. }
  543. }
  544. private String cleanFormatForNumber(String formatStr) {
  545. StringBuilder sb = new StringBuilder(formatStr);
  546. if (emulateCSV) {
  547. // Requested spacers with "_" are replaced by a single space.
  548. // Full-column-width padding "*" are removed.
  549. // Not processing fractions at this time. Replace ? with space.
  550. // This matches CSV output.
  551. for (int i = 0; i < sb.length(); i++) {
  552. char c = sb.charAt(i);
  553. if (c == '_' || c == '*' || c == '?') {
  554. if (i > 0 && sb.charAt((i - 1)) == '\\') {
  555. // It's escaped, don't worry
  556. continue;
  557. }
  558. if (c == '?') {
  559. sb.setCharAt(i, ' ');
  560. } else if (i < sb.length() - 1) {
  561. // Remove the character we're supposed
  562. // to match the space of / pad to the
  563. // column width with
  564. if (c == '_') {
  565. sb.setCharAt(i + 1, ' ');
  566. } else {
  567. sb.deleteCharAt(i + 1);
  568. }
  569. // Remove the character too
  570. sb.deleteCharAt(i);
  571. i--;
  572. }
  573. }
  574. }
  575. } else {
  576. // If they requested spacers, with "_",
  577. // remove those as we don't do spacing
  578. // If they requested full-column-width
  579. // padding, with "*", remove those too
  580. for (int i = 0; i < sb.length(); i++) {
  581. char c = sb.charAt(i);
  582. if (c == '_' || c == '*') {
  583. if (i > 0 && sb.charAt((i - 1)) == '\\') {
  584. // It's escaped, don't worry
  585. continue;
  586. }
  587. if (i < sb.length() - 1) {
  588. // Remove the character we're supposed
  589. // to match the space of / pad to the
  590. // column width with
  591. sb.deleteCharAt(i + 1);
  592. }
  593. // Remove the _ too
  594. sb.deleteCharAt(i);
  595. i--;
  596. }
  597. }
  598. }
  599. // Now, handle the other aspects like
  600. // quoting and scientific notation
  601. for(int i = 0; i < sb.length(); i++) {
  602. char c = sb.charAt(i);
  603. // remove quotes and back slashes
  604. if (c == '\\' || c == '"') {
  605. sb.deleteCharAt(i);
  606. i--;
  607. // for scientific/engineering notation
  608. } else if (c == '+' && i > 0 && sb.charAt(i - 1) == 'E') {
  609. sb.deleteCharAt(i);
  610. i--;
  611. }
  612. }
  613. return sb.toString();
  614. }
  615. private Format createNumberFormat(String formatStr, double cellValue) {
  616. String format = cleanFormatForNumber(formatStr);
  617. DecimalFormatSymbols symbols = decimalSymbols;
  618. // Do we need to change the grouping character?
  619. // eg for a format like #'##0 which wants 12'345 not 12,345
  620. Matcher agm = alternateGrouping.matcher(format);
  621. if (agm.find()) {
  622. char grouping = agm.group(2).charAt(0);
  623. // Only replace the grouping character if it is not the default
  624. // grouping character for the US locale (',') in order to enable
  625. // correct grouping for non-US locales.
  626. if (grouping!=',') {
  627. symbols = DecimalFormatSymbols.getInstance(locale);
  628. symbols.setGroupingSeparator(grouping);
  629. String oldPart = agm.group(1);
  630. String newPart = oldPart.replace(grouping, ',');
  631. format = format.replace(oldPart, newPart);
  632. }
  633. }
  634. try {
  635. DecimalFormat df = new DecimalFormat(format, symbols);
  636. setExcelStyleRoundingMode(df);
  637. return df;
  638. } catch(IllegalArgumentException iae) {
  639. // the pattern could not be parsed correctly,
  640. // so fall back to the default number format
  641. return getDefaultFormat(cellValue);
  642. }
  643. }
  644. /**
  645. * Returns a default format for a cell.
  646. * @param cell The cell
  647. * @return a default format
  648. */
  649. public Format getDefaultFormat(Cell cell) {
  650. return getDefaultFormat(cell.getNumericCellValue());
  651. }
  652. private Format getDefaultFormat(double cellValue) {
  653. localeChangedObservable.checkForLocaleChange();
  654. // for numeric cells try user supplied default
  655. if (defaultNumFormat != null) {
  656. return defaultNumFormat;
  657. // otherwise use general format
  658. }
  659. return generalNumberFormat;
  660. }
  661. /**
  662. * Performs Excel-style date formatting, using the
  663. * supplied Date and format
  664. */
  665. private String performDateFormatting(Date d, Format dateFormat) {
  666. return (dateFormat != null ? dateFormat : defaultDateformat).format(d);
  667. }
  668. /**
  669. * Returns the formatted value of an Excel date as a <tt>String</tt> based
  670. * on the cell's <code>DataFormat</code>. i.e. "Thursday, January 02, 2003"
  671. * , "01/02/2003" , "02-Jan" , etc.
  672. * <p/>
  673. * If any conditional format rules apply, the highest priority with a number format is used.
  674. * If no rules contain a number format, or no rules apply, the cell's style format is used.
  675. * If the style does not have a format, the default date format is applied.
  676. *
  677. * @param cell
  678. * @param cfEvaluator ConditionalFormattingEvaluator (if available)
  679. * @return
  680. */
  681. private String getFormattedDateString(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
  682. Format dateFormat = getFormat(cell, cfEvaluator);
  683. if(dateFormat instanceof ExcelStyleDateFormatter) {
  684. // Hint about the raw excel value
  685. ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(
  686. cell.getNumericCellValue()
  687. );
  688. }
  689. Date d = cell.getDateCellValue();
  690. return performDateFormatting(d, dateFormat);
  691. }
  692. /**
  693. * Returns the formatted value of an Excel number as a <tt>String</tt>
  694. * based on the cell's <code>DataFormat</code>. Supported formats include
  695. * currency, percents, decimals, phone number, SSN, etc.:
  696. * "61.54%", "$100.00", "(800) 555-1234".
  697. * <p/>
  698. * Format comes from either the highest priority conditional format rule with a
  699. * specified format, or from the cell style.
  700. *
  701. * @param cell The cell
  702. * @param cfEvaluator if available, or null
  703. * @return a formatted number string
  704. */
  705. private String getFormattedNumberString(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
  706. Format numberFormat = getFormat(cell, cfEvaluator);
  707. double d = cell.getNumericCellValue();
  708. if (numberFormat == null) {
  709. return String.valueOf(d);
  710. }
  711. String formatted = numberFormat.format(new Double(d));
  712. return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
  713. }
  714. /**
  715. * Formats the given raw cell value, based on the supplied
  716. * format index and string, according to excel style rules.
  717. * @see #formatCellValue(Cell)
  718. */
  719. public String formatRawCellContents(double value, int formatIndex, String formatString) {
  720. return formatRawCellContents(value, formatIndex, formatString, false);
  721. }
  722. /**
  723. * Formats the given raw cell value, based on the supplied
  724. * format index and string, according to excel style rules.
  725. * @see #formatCellValue(Cell)
  726. */
  727. public String formatRawCellContents(double value, int formatIndex, String formatString, boolean use1904Windowing) {
  728. localeChangedObservable.checkForLocaleChange();
  729. // Is it a date?
  730. if(DateUtil.isADateFormat(formatIndex,formatString)) {
  731. if(DateUtil.isValidExcelDate(value)) {
  732. Format dateFormat = getFormat(value, formatIndex, formatString);
  733. if(dateFormat instanceof ExcelStyleDateFormatter) {
  734. // Hint about the raw excel value
  735. ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(value);
  736. }
  737. Date d = DateUtil.getJavaDate(value, use1904Windowing);
  738. return performDateFormatting(d, dateFormat);
  739. }
  740. // RK: Invalid dates are 255 #s.
  741. if (emulateCSV) {
  742. return invalidDateTimeString;
  743. }
  744. }
  745. // else Number
  746. Format numberFormat = getFormat(value, formatIndex, formatString);
  747. if (numberFormat == null) {
  748. return String.valueOf(value);
  749. }
  750. // When formatting 'value', double to text to BigDecimal produces more
  751. // accurate results than double to Double in JDK8 (as compared to
  752. // previous versions). However, if the value contains E notation, this
  753. // would expand the values, which we do not want, so revert to
  754. // original method.
  755. String result;
  756. final String textValue = NumberToTextConverter.toText(value);
  757. if (textValue.indexOf('E') > -1) {
  758. result = numberFormat.format(new Double(value));
  759. }
  760. else {
  761. result = numberFormat.format(new BigDecimal(textValue));
  762. }
  763. // Complete scientific notation by adding the missing +.
  764. if (result.indexOf('E') > -1 && !result.contains("E-")) {
  765. result = result.replaceFirst("E", "E+");
  766. }
  767. return result;
  768. }
  769. /**
  770. * <p>
  771. * Returns the formatted value of a cell as a <tt>String</tt> regardless
  772. * of the cell type. If the Excel format pattern cannot be parsed then the
  773. * cell value will be formatted using a default format.
  774. * </p>
  775. * <p>When passed a null or blank cell, this method will return an empty
  776. * String (""). Formulas in formula type cells will not be evaluated.
  777. * </p>
  778. *
  779. * @param cell The cell
  780. * @return the formatted cell value as a String
  781. */
  782. public String formatCellValue(Cell cell) {
  783. return formatCellValue(cell, null);
  784. }
  785. /**
  786. * <p>
  787. * Returns the formatted value of a cell as a <tt>String</tt> regardless
  788. * of the cell type. If the Excel number format pattern cannot be parsed then the
  789. * cell value will be formatted using a default format.
  790. * </p>
  791. * <p>When passed a null or blank cell, this method will return an empty
  792. * String (""). Formula cells will be evaluated using the given
  793. * {@link FormulaEvaluator} if the evaluator is non-null. If the
  794. * evaluator is null, then the formula String will be returned. The caller
  795. * is responsible for setting the currentRow on the evaluator
  796. *</p>
  797. *
  798. * @param cell The cell (can be null)
  799. * @param evaluator The FormulaEvaluator (can be null)
  800. * @return a string value of the cell
  801. */
  802. public String formatCellValue(Cell cell, FormulaEvaluator evaluator) {
  803. return formatCellValue(cell, evaluator, null);
  804. }
  805. /**
  806. * <p>
  807. * Returns the formatted value of a cell as a <tt>String</tt> regardless
  808. * of the cell type. If the Excel number format pattern cannot be parsed then the
  809. * cell value will be formatted using a default format.
  810. * </p>
  811. * <p>When passed a null or blank cell, this method will return an empty
  812. * String (""). Formula cells will be evaluated using the given
  813. * {@link FormulaEvaluator} if the evaluator is non-null. If the
  814. * evaluator is null, then the formula String will be returned. The caller
  815. * is responsible for setting the currentRow on the evaluator
  816. *</p>
  817. * <p>
  818. * When a ConditionalFormattingEvaluator is present, it is checked first to see
  819. * if there is a number format to apply. If multiple rules apply, the last one is used.
  820. * If no ConditionalFormattingEvaluator is present, no rules apply, or the applied
  821. * rules do not define a format, the cell's style format is used.
  822. * </p>
  823. * <p>
  824. * The two evaluators should be from the same context, to avoid inconsistencies in cached values.
  825. *</p>
  826. *
  827. * @param cell The cell (can be null)
  828. * @param evaluator The FormulaEvaluator (can be null)
  829. * @param cfEvaluator ConditionalFormattingEvaluator (can be null)
  830. * @return a string value of the cell
  831. */
  832. public String formatCellValue(Cell cell, FormulaEvaluator evaluator, ConditionalFormattingEvaluator cfEvaluator) {
  833. localeChangedObservable.checkForLocaleChange();
  834. if (cell == null) {
  835. return "";
  836. }
  837. CellType cellType = cell.getCellTypeEnum();
  838. if (cellType == CellType.FORMULA) {
  839. if (evaluator == null) {
  840. return cell.getCellFormula();
  841. }
  842. cellType = evaluator.evaluateFormulaCellEnum(cell);
  843. }
  844. switch (cellType) {
  845. case NUMERIC :
  846. if (DateUtil.isCellDateFormatted(cell, cfEvaluator)) {
  847. return getFormattedDateString(cell, cfEvaluator);
  848. }
  849. return getFormattedNumberString(cell, cfEvaluator);
  850. case STRING :
  851. return cell.getRichStringCellValue().getString();
  852. case BOOLEAN :
  853. return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
  854. case BLANK :
  855. return "";
  856. case ERROR:
  857. return FormulaError.forInt(cell.getErrorCellValue()).getString();
  858. default:
  859. throw new RuntimeException("Unexpected celltype (" + cellType + ")");
  860. }
  861. }
  862. /**
  863. * <p>
  864. * Sets a default number format to be used when the Excel format cannot be
  865. * parsed successfully. <b>Note:</b> This is a fall back for when an error
  866. * occurs while parsing an Excel number format pattern. This will not
  867. * affect cells with the <em>General</em> format.
  868. * </p>
  869. * <p>
  870. * The value that will be passed to the Format's format method (specified
  871. * by <code>java.text.Format#format</code>) will be a double value from a
  872. * numeric cell. Therefore the code in the format method should expect a
  873. * <code>Number</code> value.
  874. * </p>
  875. *
  876. * @param format A Format instance to be used as a default
  877. * @see java.text.Format#format
  878. */
  879. public void setDefaultNumberFormat(Format format) {
  880. for (Map.Entry<String, Format> entry : formats.entrySet()) {
  881. if (entry.getValue() == generalNumberFormat) {
  882. entry.setValue(format);
  883. }
  884. }
  885. defaultNumFormat = format;
  886. }
  887. /**
  888. * Adds a new format to the available formats.
  889. * <p>
  890. * The value that will be passed to the Format's format method (specified
  891. * by <code>java.text.Format#format</code>) will be a double value from a
  892. * numeric cell. Therefore the code in the format method should expect a
  893. * <code>Number</code> value.
  894. * </p>
  895. * @param excelFormatStr The data format string
  896. * @param format A Format instance
  897. */
  898. public void addFormat(String excelFormatStr, Format format) {
  899. formats.put(excelFormatStr, format);
  900. }
  901. // Some custom formats
  902. /**
  903. * @return a <tt>DecimalFormat</tt> with parseIntegerOnly set <code>true</code>
  904. */
  905. private static DecimalFormat createIntegerOnlyFormat(String fmt) {
  906. DecimalFormatSymbols dsf = DecimalFormatSymbols.getInstance(Locale.ROOT);
  907. DecimalFormat result = new DecimalFormat(fmt, dsf);
  908. result.setParseIntegerOnly(true);
  909. return result;
  910. }
  911. /**
  912. * Enables excel style rounding mode (round half up) on the
  913. * Decimal Format given.
  914. */
  915. public static void setExcelStyleRoundingMode(DecimalFormat format) {
  916. setExcelStyleRoundingMode(format, RoundingMode.HALF_UP);
  917. }
  918. /**
  919. * Enables custom rounding mode on the given Decimal Format.
  920. * @param format DecimalFormat
  921. * @param roundingMode RoundingMode
  922. */
  923. public static void setExcelStyleRoundingMode(DecimalFormat format, RoundingMode roundingMode) {
  924. format.setRoundingMode(roundingMode);
  925. }
  926. /**
  927. * If the Locale has been changed via {@link LocaleUtil#setUserLocale(Locale)} the stored
  928. * formats need to be refreshed. All formats which aren't originated from DataFormatter
  929. * itself, i.e. all Formats added via {@link DataFormatter#addFormat(String, Format)} and
  930. * {@link DataFormatter#setDefaultNumberFormat(Format)}, need to be added again.
  931. * To notify callers, the returned {@link Observable} should be used.
  932. * The Object in {@link Observer#update(Observable, Object)} is the new Locale.
  933. *
  934. * @return the listener object, where callers can register themselves
  935. */
  936. public Observable getLocaleChangedObservable() {
  937. return localeChangedObservable;
  938. }
  939. /**
  940. * Update formats when locale has been changed
  941. *
  942. * @param observable usually this is our own Observable instance
  943. * @param localeObj only reacts on Locale objects
  944. */
  945. public void update(Observable observable, Object localeObj) {
  946. if (!(localeObj instanceof Locale)) return;
  947. Locale newLocale = (Locale)localeObj;
  948. if (!localeIsAdapting || newLocale.equals(locale)) return;
  949. locale = newLocale;
  950. dateSymbols = DateFormatSymbols.getInstance(locale);
  951. decimalSymbols = DecimalFormatSymbols.getInstance(locale);
  952. generalNumberFormat = new ExcelGeneralNumberFormat(locale);
  953. // taken from Date.toString()
  954. defaultDateformat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", dateSymbols);
  955. defaultDateformat.setTimeZone(LocaleUtil.getUserTimeZone());
  956. // init built-in formats
  957. formats.clear();
  958. Format zipFormat = ZipPlusFourFormat.instance;
  959. addFormat("00000\\-0000", zipFormat);
  960. addFormat("00000-0000", zipFormat);
  961. Format phoneFormat = PhoneFormat.instance;
  962. // allow for format string variations
  963. addFormat("[<=9999999]###\\-####;\\(###\\)\\ ###\\-####", phoneFormat);
  964. addFormat("[<=9999999]###-####;(###) ###-####", phoneFormat);
  965. addFormat("###\\-####;\\(###\\)\\ ###\\-####", phoneFormat);
  966. addFormat("###-####;(###) ###-####", phoneFormat);
  967. Format ssnFormat = SSNFormat.instance;
  968. addFormat("000\\-00\\-0000", ssnFormat);
  969. addFormat("000-00-0000", ssnFormat);
  970. }
  971. /**
  972. * Format class for Excel's SSN format. This class mimics Excel's built-in
  973. * SSN formatting.
  974. *
  975. * @author James May
  976. */
  977. @SuppressWarnings("serial")
  978. private static final class SSNFormat extends Format {
  979. public static final Format instance = new SSNFormat();
  980. private static final DecimalFormat df = createIntegerOnlyFormat("000000000");
  981. private SSNFormat() {
  982. // enforce singleton
  983. }
  984. /** Format a number as an SSN */
  985. public static String format(Number num) {
  986. String result = df.format(num);
  987. return result.substring(0, 3) + '-' +
  988. result.substring(3, 5) + '-' +
  989. result.substring(5, 9);
  990. }
  991. @Override
  992. public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
  993. return toAppendTo.append(format((Number)obj));
  994. }
  995. @Override
  996. public Object parseObject(String source, ParsePosition pos) {
  997. return df.parseObject(source, pos);
  998. }
  999. }
  1000. /**
  1001. * Format class for Excel Zip + 4 format. This class mimics Excel's
  1002. * built-in formatting for Zip + 4.
  1003. * @author James May
  1004. */
  1005. @SuppressWarnings("serial")
  1006. private static final class ZipPlusFourFormat extends Format {
  1007. public static final Format instance = new ZipPlusFourFormat();
  1008. private static final DecimalFormat df = createIntegerOnlyFormat("000000000");
  1009. private ZipPlusFourFormat() {
  1010. // enforce singleton
  1011. }
  1012. /** Format a number as Zip + 4 */
  1013. public static String format(Number num) {
  1014. String result = df.format(num);
  1015. return result.substring(0, 5) + '-' +
  1016. result.substring(5, 9);
  1017. }
  1018. @Override
  1019. public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
  1020. return toAppendTo.append(format((Number)obj));
  1021. }
  1022. @Override
  1023. public Object parseObject(String source, ParsePosition pos) {
  1024. return df.parseObject(source, pos);
  1025. }
  1026. }
  1027. /**
  1028. * Format class for Excel phone number format. This class mimics Excel's
  1029. * built-in phone number formatting.
  1030. * @author James May
  1031. */
  1032. @SuppressWarnings("serial")
  1033. private static final class PhoneFormat extends Format {
  1034. public static final Format instance = new PhoneFormat();
  1035. private static final DecimalFormat df = createIntegerOnlyFormat("##########");
  1036. private PhoneFormat() {
  1037. // enforce singleton
  1038. }
  1039. /** Format a number as a phone number */
  1040. public static String format(Number num) {
  1041. String result = df.format(num);
  1042. StringBuilder sb = new StringBuilder();
  1043. String seg1, seg2, seg3;
  1044. int len = result.length();
  1045. if (len <= 4) {
  1046. return result;
  1047. }
  1048. seg3 = result.substring(len - 4, len);
  1049. seg2 = result.substring(Math.max(0, len - 7), len - 4);
  1050. seg1 = result.substring(Math.max(0, len - 10), Math.max(0, len - 7));
  1051. if(seg1.trim().length() > 0) {
  1052. sb.append('(').append(seg1).append(") ");
  1053. }
  1054. if(seg2.trim().length() > 0) {
  1055. sb.append(seg2).append('-');
  1056. }
  1057. sb.append(seg3);
  1058. return sb.toString();
  1059. }
  1060. @Override
  1061. public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
  1062. return toAppendTo.append(format((Number)obj));
  1063. }
  1064. @Override
  1065. public Object parseObject(String source, ParsePosition pos) {
  1066. return df.parseObject(source, pos);
  1067. }
  1068. }
  1069. /**
  1070. * Format class that does nothing and always returns a constant string.
  1071. *
  1072. * This format is used to simulate Excel's handling of a format string
  1073. * of all # when the value is 0. Excel will output "", Java will output "0".
  1074. *
  1075. * @see DataFormatter#createFormat(double, int, String)
  1076. */
  1077. @SuppressWarnings("serial")
  1078. private static final class ConstantStringFormat extends Format {
  1079. private static final DecimalFormat df = createIntegerOnlyFormat("##########");
  1080. private final String str;
  1081. public ConstantStringFormat(String s) {
  1082. str = s;
  1083. }
  1084. @Override
  1085. public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
  1086. return toAppendTo.append(str);
  1087. }
  1088. @Override
  1089. public Object parseObject(String source, ParsePosition pos) {
  1090. return df.parseObject(source, pos);
  1091. }
  1092. }
  1093. /**
  1094. * Workaround until we merge {@link DataFormatter} with {@link CellFormat}.
  1095. * Constant, non-cachable wrapper around a {@link CellFormatResult}
  1096. */
  1097. @SuppressWarnings("serial")
  1098. private final class CellFormatResultWrapper extends Format {
  1099. private final CellFormatResult result;
  1100. private CellFormatResultWrapper(CellFormatResult result) {
  1101. this.result = result;
  1102. }
  1103. public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
  1104. if (emulateCSV) {
  1105. return toAppendTo.append(result.text);
  1106. } else {
  1107. return toAppendTo.append(result.text.trim());
  1108. }
  1109. }
  1110. public Object parseObject(String source, ParsePosition pos) {
  1111. return null; // Not supported
  1112. }
  1113. }
  1114. }