PageRenderTime 36ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/usermodel/HSSFHeader.java

http://github.com/openmicroscopy/bioformats
Java | 319 lines | 141 code | 25 blank | 153 comment | 13 complexity | a40350ae7145c843f3d207491b4edb7d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.usermodel;
  38. import loci.poi.hssf.record.HeaderRecord;
  39. /**
  40. * Class to read and manipulate the header.
  41. * <P>
  42. * The header works by having a left, center, and right side. The total cannot
  43. * be more that 255 bytes long. One uses this class by getting the HSSFHeader
  44. * from HSSFSheet and then getting or setting the left, center, and right side.
  45. * For special things (such as page numbers and date), one can use a the methods
  46. * that return the characters used to represent these. One can also change the
  47. * fonts by using similar methods.
  48. * <P>
  49. *
  50. * @author Shawn Laubach (slaubach at apache dot org)
  51. */
  52. public class HSSFHeader
  53. {
  54. HeaderRecord headerRecord;
  55. String left;
  56. String center;
  57. String right;
  58. /**
  59. * Constructor. Creates a new header interface from a header record
  60. *
  61. * @param headerRecord Header record to create the header with
  62. */
  63. protected HSSFHeader( HeaderRecord headerRecord )
  64. {
  65. this.headerRecord = headerRecord;
  66. String head = headerRecord.getHeader();
  67. while ( head != null && head.length() > 1 )
  68. {
  69. int pos = head.length();
  70. switch ( head.substring( 1, 2 ).charAt( 0 ) )
  71. {
  72. case 'L':
  73. if ( head.indexOf( "&C" ) >= 0 )
  74. {
  75. pos = Math.min( pos, head.indexOf( "&C" ) );
  76. }
  77. if ( head.indexOf( "&R" ) >= 0 )
  78. {
  79. pos = Math.min( pos, head.indexOf( "&R" ) );
  80. }
  81. left = head.substring( 2, pos );
  82. head = head.substring( pos );
  83. break;
  84. case 'C':
  85. if ( head.indexOf( "&L" ) >= 0 )
  86. {
  87. pos = Math.min( pos, head.indexOf( "&L" ) );
  88. }
  89. if ( head.indexOf( "&R" ) >= 0 )
  90. {
  91. pos = Math.min( pos, head.indexOf( "&R" ) );
  92. }
  93. center = head.substring( 2, pos );
  94. head = head.substring( pos );
  95. break;
  96. case 'R':
  97. if ( head.indexOf( "&C" ) >= 0 )
  98. {
  99. pos = Math.min( pos, head.indexOf( "&C" ) );
  100. }
  101. if ( head.indexOf( "&L" ) >= 0 )
  102. {
  103. pos = Math.min( pos, head.indexOf( "&L" ) );
  104. }
  105. right = head.substring( 2, pos );
  106. head = head.substring( pos );
  107. break;
  108. default :
  109. head = null;
  110. }
  111. }
  112. }
  113. /**
  114. * Get the left side of the header.
  115. *
  116. * @return The string representing the left side.
  117. */
  118. public String getLeft()
  119. {
  120. return left;
  121. }
  122. /**
  123. * Sets the left string.
  124. *
  125. * @param newLeft The string to set as the left side.
  126. */
  127. public void setLeft( String newLeft )
  128. {
  129. left = newLeft;
  130. createHeaderString();
  131. }
  132. /**
  133. * Get the center of the header.
  134. *
  135. * @return The string representing the center.
  136. */
  137. public String getCenter()
  138. {
  139. return center;
  140. }
  141. /**
  142. * Sets the center string.
  143. *
  144. * @param newCenter The string to set as the center.
  145. */
  146. public void setCenter( String newCenter )
  147. {
  148. center = newCenter;
  149. createHeaderString();
  150. }
  151. /**
  152. * Get the right side of the header.
  153. *
  154. * @return The string representing the right side.
  155. */
  156. public String getRight()
  157. {
  158. return right;
  159. }
  160. /**
  161. * Sets the right string.
  162. *
  163. * @param newRight The string to set as the right side.
  164. */
  165. public void setRight( String newRight )
  166. {
  167. right = newRight;
  168. createHeaderString();
  169. }
  170. /**
  171. * Creates the complete header string based on the left, center, and middle
  172. * strings.
  173. */
  174. private void createHeaderString()
  175. {
  176. headerRecord.setHeader( "&C" + ( center == null ? "" : center ) +
  177. "&L" + ( left == null ? "" : left ) +
  178. "&R" + ( right == null ? "" : right ) );
  179. headerRecord.setHeaderLength( (byte) headerRecord.getHeader().length() );
  180. }
  181. /**
  182. * Returns the string that represents the change in font size.
  183. *
  184. * @param size the new font size
  185. * @return The special string to represent a new font size
  186. */
  187. public static String fontSize( short size )
  188. {
  189. return "&" + size;
  190. }
  191. /**
  192. * Returns the string that represents the change in font.
  193. *
  194. * @param font the new font
  195. * @param style the fonts style
  196. * @return The special string to represent a new font size
  197. */
  198. public static String font( String font, String style )
  199. {
  200. return "&\"" + font + "," + style + "\"";
  201. }
  202. /**
  203. * Returns the string representing the current page number
  204. *
  205. * @return The special string for page number
  206. */
  207. public static String page()
  208. {
  209. return "&P";
  210. }
  211. /**
  212. * Returns the string representing the number of pages.
  213. *
  214. * @return The special string for the number of pages
  215. */
  216. public static String numPages()
  217. {
  218. return "&N";
  219. }
  220. /**
  221. * Returns the string representing the current date
  222. *
  223. * @return The special string for the date
  224. */
  225. public static String date()
  226. {
  227. return "&D";
  228. }
  229. /**
  230. * Returns the string representing the current time
  231. *
  232. * @return The special string for the time
  233. */
  234. public static String time()
  235. {
  236. return "&T";
  237. }
  238. /**
  239. * Returns the string representing the current file name
  240. *
  241. * @return The special string for the file name
  242. */
  243. public static String file()
  244. {
  245. return "&F";
  246. }
  247. /**
  248. * Returns the string representing the current tab (sheet) name
  249. *
  250. * @return The special string for tab name
  251. */
  252. public static String tab()
  253. {
  254. return "&A";
  255. }
  256. /**
  257. * Returns the string representing the start underline
  258. *
  259. * @return The special string for start underline
  260. */
  261. public static String startUnderline()
  262. {
  263. return "&U";
  264. }
  265. /**
  266. * Returns the string representing the end underline
  267. *
  268. * @return The special string for end underline
  269. */
  270. public static String endUnderline()
  271. {
  272. return "&U";
  273. }
  274. /**
  275. * Returns the string representing the start double underline
  276. *
  277. * @return The special string for start double underline
  278. */
  279. public static String startDoubleUnderline()
  280. {
  281. return "&E";
  282. }
  283. /**
  284. * Returns the string representing the end double underline
  285. *
  286. * @return The special string for end double underline
  287. */
  288. public static String endDoubleUnderline()
  289. {
  290. return "&E";
  291. }
  292. }