package jxl.biff; import common.Logger; public abstract class HeaderFooter { private static Logger logger = Logger.getLogger(HeaderFooter.class); private static final String BOLD_TOGGLE = "&B"; private static final String UNDERLINE_TOGGLE = "&U"; private static final String ITALICS_TOGGLE = "&I"; private static final String STRIKETHROUGH_TOGGLE = "&S"; private static final String DOUBLE_UNDERLINE_TOGGLE = "&E"; private static final String SUPERSCRIPT_TOGGLE = "&X"; private static final String SUBSCRIPT_TOGGLE = "&Y"; private static final String OUTLINE_TOGGLE = "&O"; private static final String SHADOW_TOGGLE = "&H"; private static final String LEFT_ALIGN = "&L"; private static final String CENTRE = "&C"; private static final String RIGHT_ALIGN = "&R"; private static final String PAGENUM = "&P"; private static final String TOTAL_PAGENUM = "&N"; private static final String DATE = "&D"; private static final String TIME = "&T"; private static final String WORKBOOK_NAME = "&F"; private static final String WORKSHEET_NAME = "&A"; private Contents left; private Contents right; private Contents centre; protected static class Contents { private StringBuffer contents; protected Contents() { this.contents = new StringBuffer(); } protected Contents(String s) { this.contents = new StringBuffer(s); } protected Contents(Contents copy) { this.contents = new StringBuffer(copy.getContents()); } protected String getContents() { return (this.contents != null) ? this.contents.toString() : ""; } private void appendInternal(String txt) { if (this.contents == null) this.contents = new StringBuffer(); this.contents.append(txt); } private void appendInternal(char ch) { if (this.contents == null) this.contents = new StringBuffer(); this.contents.append(ch); } protected void append(String txt) { appendInternal(txt); } protected void toggleBold() { appendInternal("&B"); } protected void toggleUnderline() { appendInternal("&U"); } protected void toggleItalics() { appendInternal("&I"); } protected void toggleStrikethrough() { appendInternal("&S"); } protected void toggleDoubleUnderline() { appendInternal("&E"); } protected void toggleSuperScript() { appendInternal("&X"); } protected void toggleSubScript() { appendInternal("&Y"); } protected void toggleOutline() { appendInternal("&O"); } protected void toggleShadow() { appendInternal("&H"); } protected void setFontName(String fontName) { appendInternal("&\""); appendInternal(fontName); appendInternal('"'); } protected boolean setFontSize(int size) { String fontSize; if (size < 1 || size > 99) return false; if (size < 10) { fontSize = "0" + size; } else { fontSize = Integer.toString(size); } appendInternal('&'); appendInternal(fontSize); return true; } protected void appendPageNumber() { appendInternal("&P"); } protected void appendTotalPages() { appendInternal("&N"); } protected void appendDate() { appendInternal("&D"); } protected void appendTime() { appendInternal("&T"); } protected void appendWorkbookName() { appendInternal("&F"); } protected void appendWorkSheetName() { appendInternal("&A"); } protected void clear() { this.contents = null; } protected boolean empty() { if (this.contents == null || this.contents.length() == 0) return true; return false; } } protected HeaderFooter() { this.left = createContents(); this.right = createContents(); this.centre = createContents(); } protected HeaderFooter(HeaderFooter hf) { this.left = createContents(hf.left); this.right = createContents(hf.right); this.centre = createContents(hf.centre); } protected HeaderFooter(String s) { if (s == null || s.length() == 0) { this.left = createContents(); this.right = createContents(); this.centre = createContents(); return; } int pos = 0; int leftPos = s.indexOf("&L"); int rightPos = s.indexOf("&R"); int centrePos = s.indexOf("&C"); if (pos == leftPos) if (centrePos != -1) { this.left = createContents(s.substring(pos + 2, centrePos)); pos = centrePos; } else if (rightPos != -1) { this.left = createContents(s.substring(pos + 2, rightPos)); pos = rightPos; } else { this.left = createContents(s.substring(pos + 2)); pos = s.length(); } if (pos == centrePos || (leftPos == -1 && rightPos == -1 && centrePos == -1)) if (rightPos != -1) { this.centre = createContents(s.substring(pos + 2, rightPos)); pos = rightPos; } else { this.centre = createContents(s.substring(pos + 2)); pos = s.length(); } if (pos == rightPos) { this.right = createContents(s.substring(pos + 2)); pos = s.length(); } if (this.left == null) this.left = createContents(); if (this.centre == null) this.centre = createContents(); if (this.right == null) this.right = createContents(); } public String toString() { StringBuffer hf = new StringBuffer(); if (!this.left.empty()) { hf.append("&L"); hf.append(this.left.getContents()); } if (!this.centre.empty()) { hf.append("&C"); hf.append(this.centre.getContents()); } if (!this.right.empty()) { hf.append("&R"); hf.append(this.right.getContents()); } return hf.toString(); } protected Contents getRightText() { return this.right; } protected Contents getCentreText() { return this.centre; } protected Contents getLeftText() { return this.left; } protected void clear() { this.left.clear(); this.right.clear(); this.centre.clear(); } protected abstract Contents createContents(); protected abstract Contents createContents(String paramString); protected abstract Contents createContents(Contents paramContents); }