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.

HeaderFooter.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.hssf.usermodel;
  16. /**
  17. * Common class for {@link HSSFHeader} and {@link HSSFFooter}.
  18. */
  19. public abstract class HeaderFooter implements org.apache.poi.ss.usermodel.HeaderFooter {
  20. protected HeaderFooter() {
  21. //
  22. }
  23. /**
  24. * @return the internal text representation (combining center, left and right parts).
  25. * Possibly empty string if no header or footer is set. Never <code>null</code>.
  26. */
  27. protected abstract String getRawText();
  28. private String[] splitParts() {
  29. String text = getRawText();
  30. // default values
  31. String _left = "";
  32. String _center = "";
  33. String _right = "";
  34. // FIXME: replace outer goto. just eww.
  35. // no unit tests test the goto loop but poi-integration tests hangs if you remove it
  36. // (some file has an edge case header or footer)
  37. outer:
  38. while (text.length() > 1) {
  39. if (text.charAt(0) != '&') {
  40. // Mimics the behaviour of Excel, which would put it in the center.
  41. _center = text;
  42. break;
  43. }
  44. int pos = text.length();
  45. switch (text.charAt(1)) {
  46. case 'L':
  47. if (text.contains("&C")) {
  48. pos = Math.min(pos, text.indexOf("&C"));
  49. }
  50. if (text.contains("&R")) {
  51. pos = Math.min(pos, text.indexOf("&R"));
  52. }
  53. _left = text.substring(2, pos);
  54. text = text.substring(pos);
  55. break;
  56. case 'C':
  57. if (text.contains("&L")) {
  58. pos = Math.min(pos, text.indexOf("&L"));
  59. }
  60. if (text.contains("&R")) {
  61. pos = Math.min(pos, text.indexOf("&R"));
  62. }
  63. _center = text.substring(2, pos);
  64. text = text.substring(pos);
  65. break;
  66. case 'R':
  67. if (text.contains("&C")) {
  68. pos = Math.min(pos, text.indexOf("&C"));
  69. }
  70. if (text.contains("&L")) {
  71. pos = Math.min(pos, text.indexOf("&L"));
  72. }
  73. _right = text.substring(2, pos);
  74. text = text.substring(pos);
  75. break;
  76. default:
  77. // Mimics the behaviour of Excel, which would put it in the center.
  78. _center = text;
  79. break outer;
  80. }
  81. }
  82. return new String[] { _left, _center, _right, };
  83. }
  84. /**
  85. * @return the left side of the header or footer.
  86. */
  87. public final String getLeft() {
  88. return splitParts()[0];
  89. }
  90. /**
  91. * @param newLeft The string to set as the left side.
  92. */
  93. public final void setLeft(String newLeft) {
  94. updatePart(0, newLeft);
  95. }
  96. /**
  97. * @return the center of the header or footer.
  98. */
  99. public final String getCenter() {
  100. return splitParts()[1];
  101. }
  102. /**
  103. * @param newCenter The string to set as the center.
  104. */
  105. public final void setCenter(String newCenter) {
  106. updatePart(1, newCenter);
  107. }
  108. /**
  109. * @return The right side of the header or footer.
  110. */
  111. public final String getRight() {
  112. return splitParts()[2];
  113. }
  114. /**
  115. * @param newRight The string to set as the right side.
  116. */
  117. public final void setRight(String newRight) {
  118. updatePart(2, newRight);
  119. }
  120. private void updatePart(int partIndex, String newValue) {
  121. String[] parts = splitParts();
  122. parts[partIndex] = newValue == null ? "" : newValue;
  123. updateHeaderFooterText(parts);
  124. }
  125. /**
  126. * Creates the complete footer string based on the left, center, and middle
  127. * strings.
  128. */
  129. private void updateHeaderFooterText(String[] parts) {
  130. String _left = parts[0];
  131. String _center = parts[1];
  132. String _right = parts[2];
  133. if (_center.length() < 1 && _left.length() < 1 && _right.length() < 1) {
  134. setHeaderFooterText("");
  135. return;
  136. }
  137. StringBuilder sb = new StringBuilder(64);
  138. sb.append("&C");
  139. sb.append(_center);
  140. sb.append("&L");
  141. sb.append(_left);
  142. sb.append("&R");
  143. sb.append(_right);
  144. String text = sb.toString();
  145. setHeaderFooterText(text);
  146. }
  147. /**
  148. * @param text the new header footer text (contains mark-up tags). Possibly
  149. * empty string never <code>null</code>
  150. */
  151. protected abstract void setHeaderFooterText(String text);
  152. /**
  153. * @param size
  154. * the new font size
  155. * @return The mark-up tag representing a new font size
  156. */
  157. public static String fontSize(short size) {
  158. return "&" + size;
  159. }
  160. /**
  161. * @param font
  162. * the new font
  163. * @param style
  164. * the fonts style, one of regular, italic, bold, italic bold or
  165. * bold italic
  166. * @return The mark-up tag representing a new font size
  167. */
  168. public static String font(String font, String style) {
  169. return "&\"" + font + "," + style + "\"";
  170. }
  171. /**
  172. * @return The mark-up tag representing the current page number
  173. */
  174. public static String page() {
  175. return MarkupTag.PAGE_FIELD.getRepresentation();
  176. }
  177. /**
  178. * @return The mark-up tag representing the number of pages
  179. */
  180. public static String numPages() {
  181. return MarkupTag.NUM_PAGES_FIELD.getRepresentation();
  182. }
  183. /**
  184. * @return The mark-up tag representing the current date date
  185. */
  186. public static String date() {
  187. return MarkupTag.DATE_FIELD.getRepresentation();
  188. }
  189. /**
  190. * @return The mark-up tag representing current time
  191. */
  192. public static String time() {
  193. return MarkupTag.TIME_FIELD.getRepresentation();
  194. }
  195. /**
  196. * @return The mark-up tag representing the current file name
  197. */
  198. public static String file() {
  199. return MarkupTag.FILE_FIELD.getRepresentation();
  200. }
  201. /**
  202. * @return The mark-up tag representing the current tab (sheet) name
  203. */
  204. public static String tab() {
  205. return MarkupTag.SHEET_NAME_FIELD.getRepresentation();
  206. }
  207. /**
  208. * @return The mark-up tag for start bold
  209. */
  210. public static String startBold() {
  211. return MarkupTag.BOLD_FIELD.getRepresentation();
  212. }
  213. /**
  214. * @return The mark-up tag for end bold
  215. */
  216. public static String endBold() {
  217. return MarkupTag.BOLD_FIELD.getRepresentation();
  218. }
  219. /**
  220. * @return The mark-up tag for start underline
  221. */
  222. public static String startUnderline() {
  223. return MarkupTag.UNDERLINE_FIELD.getRepresentation();
  224. }
  225. /**
  226. * @return The mark-up tag for end underline
  227. */
  228. public static String endUnderline() {
  229. return MarkupTag.UNDERLINE_FIELD.getRepresentation();
  230. }
  231. /**
  232. * @return The mark-up tag for start double underline
  233. */
  234. public static String startDoubleUnderline() {
  235. return MarkupTag.DOUBLE_UNDERLINE_FIELD.getRepresentation();
  236. }
  237. /**
  238. * @return The mark-up tag for end double underline
  239. */
  240. public static String endDoubleUnderline() {
  241. return MarkupTag.DOUBLE_UNDERLINE_FIELD.getRepresentation();
  242. }
  243. /**
  244. * Removes any fields (eg macros, page markers etc) from the string.
  245. * Normally used to make some text suitable for showing to humans, and the
  246. * resultant text should not normally be saved back into the document!
  247. */
  248. public static String stripFields(String pText) {
  249. int pos;
  250. // Check we really got something to work on
  251. if (pText == null || pText.length() == 0) {
  252. return pText;
  253. }
  254. String text = pText;
  255. // Firstly, do the easy ones which are static
  256. for (MarkupTag mt : MarkupTag.values()) {
  257. String seq = mt.getRepresentation();
  258. while ((pos = text.indexOf(seq)) >= 0) {
  259. text = text.substring(0, pos) + text.substring(pos + seq.length());
  260. }
  261. }
  262. // Now do the tricky, dynamic ones
  263. // These are things like font sizes, font names and colours
  264. text = text.replaceAll("&\\d+", "");
  265. text = text.replaceAll("&\".*?,.*?\"", "");
  266. text = text.replaceAll("&K[\\dA-F]{6}", "");
  267. text = text.replaceAll("&K[\\d]{2}[+][\\d]{3}", "");
  268. text = text.replaceAll("&&", "&");
  269. // All done
  270. return text;
  271. }
  272. private enum MarkupTag {
  273. SHEET_NAME_FIELD ("&A", false),
  274. DATE_FIELD ("&D", false),
  275. FILE_FIELD ("&F", false),
  276. FULL_FILE_FIELD ("&Z", false),
  277. PAGE_FIELD ("&P", false),
  278. TIME_FIELD ("&T", false),
  279. NUM_PAGES_FIELD ("&N", false),
  280. PICTURE_FIELD ("&G", false),
  281. BOLD_FIELD ("&B", true),
  282. ITALIC_FIELD ("&I", true),
  283. STRIKETHROUGH_FIELD ("&S", true),
  284. SUBSCRIPT_FIELD ("&Y", true),
  285. SUPERSCRIPT_FIELD ("&X", true),
  286. UNDERLINE_FIELD ("&U", true),
  287. DOUBLE_UNDERLINE_FIELD ("&E", true),
  288. ;
  289. private final String _representation;
  290. private final boolean _occursInPairs;
  291. private MarkupTag(String sequence, boolean occursInPairs) {
  292. _representation = sequence;
  293. _occursInPairs = occursInPairs;
  294. }
  295. /**
  296. * @return The character sequence that marks this field
  297. */
  298. public String getRepresentation() {
  299. return _representation;
  300. }
  301. /**
  302. * @return true if this markup tag normally comes in a pair, eg turn on
  303. * underline / turn off underline
  304. */
  305. public boolean occursPairs() {
  306. return _occursInPairs;
  307. }
  308. }
  309. }