1 /* ==================================================================== 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2002 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "Apache" and "Apache Software Foundation" and 27 * "Apache POI" must not be used to endorse or promote products 28 * derived from this software without prior written permission. For 29 * written permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache", 32 * "Apache POI", nor may "Apache" appear in their name, without 33 * prior written permission of the Apache Software Foundation. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>. 53 */ 54 55 package org.apache.poi.hssf.usermodel; 56 57 import org.apache.poi.hssf.record.HeaderRecord; 58 59 /** 60 * Class to read and manipulate the header. 61 * <P> 62 * The header works by having a left, center, and right side. The total cannot 63 * be more that 255 bytes long. One uses this class by getting the HSSFHeader 64 * from HSSFSheet and then getting or setting the left, center, and right side. 65 * For special things (such as page numbers and date), one can use a the methods 66 * that return the characters used to represent these. One can also change the 67 * fonts by using similar methods. 68 * <P> 69 * @author Shawn Laubach (slaubach at apache dot org) 70 */ 71 public class HSSFHeader extends Object { 72 73 HeaderRecord headerRecord; 74 String left; 75 String center; 76 String right; 77 78 /** 79 * Constructor. Creates a new header interface from a header record 80 * @param headerRecord Header record to create the header with 81 */ 82 protected HSSFHeader(HeaderRecord headerRecord) { 83 this.headerRecord = headerRecord; 84 String head = headerRecord.getHeader(); 85 while (head != null && head.length() > 1) { 86 int pos = head.length(); 87 switch (head.substring(1, 2).charAt(0)) { 88 case 'L' : 89 if (head.indexOf("&C") >= 0) { 90 pos = Math.min(pos, head.indexOf("&C")); 91 } 92 if (head.indexOf("&R") >= 0) { 93 pos = Math.min(pos, head.indexOf("&R")); 94 } 95 left = head.substring(2, pos); 96 head = head.substring(pos); 97 break; 98 case 'C' : 99 if (head.indexOf("&L") >= 0) { 100 pos = Math.min(pos, head.indexOf("&L")); 101 } 102 if (head.indexOf("&R") >= 0) { 103 pos = Math.min(pos, head.indexOf("&R")); 104 } 105 center = head.substring(2, pos); 106 head = head.substring(pos); 107 break; 108 case 'R' : 109 if (head.indexOf("&C") >= 0) { 110 pos = Math.min(pos, head.indexOf("&C")); 111 } 112 if (head.indexOf("&L") >= 0) { 113 pos = Math.min(pos, head.indexOf("&L")); 114 } 115 right = head.substring(2, pos); 116 head = head.substring(pos); 117 break; 118 default : head = null; 119 } 120 } 121 } 122 123 /** 124 * Get the left side of the header. 125 * @return The string representing the left side. 126 */ 127 public String getLeft() { 128 return left; 129 } 130 131 /** 132 * Sets the left string. 133 * @newLeft The string to set as the left side. 134 */ 135 public void setLeft(String newLeft) { 136 left = newLeft; 137 createHeaderString(); 138 } 139 140 /** 141 * Get the center of the header. 142 * @return The string representing the center. 143 */ 144 public String getCenter() { 145 return center; 146 } 147 148 /** 149 * Sets the center string. 150 * @newLeft The string to set as the center. 151 */ 152 public void setCenter(String newCenter) { 153 center = newCenter; 154 createHeaderString(); 155 } 156 157 /** 158 * Get the right side of the header. 159 * @return The string representing the right side. 160 */ 161 public String getRight() { 162 return right; 163 } 164 165 /** 166 * Sets the right string. 167 * @newLeft The string to set as the right side. 168 */ 169 public void setRight(String newRight) { 170 right = newRight; 171 createHeaderString(); 172 } 173 174 /** 175 * Creates the complete header string based on the left, center, and middle 176 * strings. 177 */ 178 private void createHeaderString() { 179 headerRecord.setHeader( 180 "&C" + (center == null ? "" : center) + 181 "&L" + (left == null ? "" : left) + 182 "&R" + (right == null ? "" : right)); 183 headerRecord.setHeaderLength((byte)headerRecord.getHeader().length()); 184 } 185 186 /** 187 * Returns the string that represents the change in font size. 188 * @param size the new font size 189 * @return The special string to represent a new font size 190 */ 191 public static String fontSize(short size) { 192 return "&" + size; 193 } 194 195 /** 196 * Returns the string that represents the change in font. 197 * @param font the new font 198 * @param style the fonts style 199 * @return The special string to represent a new font size 200 */ 201 public static String font(String font, String style) { 202 return "&\"" + font + "," + style + "\""; 203 } 204 205 /** 206 * Returns the string representing the current page number 207 * @return The special string for page number 208 */ 209 public static String page() { 210 return "&P"; 211 } 212 213 /** 214 * Returns the string representing the number of pages. 215 * @return The special string for the number of pages 216 */ 217 public static String numPages() { 218 return "&N"; 219 } 220 221 /** 222 * Returns the string representing the current date 223 * @return The special string for the date 224 */ 225 public static String date() { 226 return "&D"; 227 } 228 229 /** 230 * Returns the string representing the current time 231 * @return The special string for the time 232 */ 233 public static String time() { 234 return "&T"; 235 } 236 237 /** 238 * Returns the string representing the current file name 239 * @return The special string for the file name 240 */ 241 public static String file() { 242 return "&F"; 243 } 244 245 /** 246 * Returns the string representing the current tab (sheet) name 247 * @return The special string for tab name 248 */ 249 public static String tab() { 250 return "&A"; 251 } 252 } 253 254