From 5595a24adf372acd4a2a3517cadee5f6ce32aac5 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 14 Aug 2008 20:50:06 +0000 Subject: [PATCH] Refactor header/footer stuff to remove duplication git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@686036 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFFooter.java | 179 +-------------- .../apache/poi/hssf/usermodel/HSSFHeader.java | 211 +----------------- .../poi/hssf/usermodel/HeaderFooter.java | 202 ++++++++++++++++- 3 files changed, 202 insertions(+), 390 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java index 2114ed5977..10c779ffab 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java @@ -32,64 +32,16 @@ import org.apache.poi.hssf.record.FooterRecord; *

* @author Shawn Laubach (slaubach at apache dot org) */ -public class HSSFFooter extends Object implements HeaderFooter { - - FooterRecord footerRecord; - String left; - String center; - String right; +public class HSSFFooter extends HeaderFooter { + private FooterRecord footerRecord; /** * Constructor. Creates a new footer interface from a footer record * @param footerRecord Footer record to create the footer with */ protected HSSFFooter(FooterRecord footerRecord) { - this.footerRecord = footerRecord; - String foot = footerRecord.getFooter(); - while (foot != null && foot.length() > 1) { - int pos = foot.length(); - switch (foot.substring(1, 2).charAt(0)) { - case 'L' : - if (foot.indexOf("&C") >= 0) { - pos = Math.min(pos, foot.indexOf("&C")); - } - if (foot.indexOf("&R") >= 0) { - pos = Math.min(pos, foot.indexOf("&R")); - } - left = foot.substring(2, pos); - foot = foot.substring(pos); - break; - case 'C' : - if (foot.indexOf("&L") >= 0) { - pos = Math.min(pos, foot.indexOf("&L")); - } - if (foot.indexOf("&R") >= 0) { - pos = Math.min(pos, foot.indexOf("&R")); - } - center = foot.substring(2, pos); - foot = foot.substring(pos); - break; - case 'R' : - if (foot.indexOf("&C") >= 0) { - pos = Math.min(pos, foot.indexOf("&C")); - } - if (foot.indexOf("&L") >= 0) { - pos = Math.min(pos, foot.indexOf("&L")); - } - right = foot.substring(2, pos); - foot = foot.substring(pos); - break; - default : foot = null; - } - } - } - - /** - * Get the left side of the footer. - * @return The string representing the left side. - */ - public String getLeft() { - return left; + super(footerRecord.getFooter()); + this.footerRecord = footerRecord; } /** @@ -101,14 +53,6 @@ public class HSSFFooter extends Object implements HeaderFooter { createFooterString(); } - /** - * Get the center of the footer. - * @return The string representing the center. - */ - public String getCenter() { - return center; - } - /** * Sets the center string. * @param newCenter The string to set as the center. @@ -118,14 +62,6 @@ public class HSSFFooter extends Object implements HeaderFooter { createFooterString(); } - /** - * Get the right side of the footer. - * @return The string representing the right side. - */ - public String getRight() { - return right; - } - /** * Sets the right string. * @param newRight The string to set as the right side. @@ -146,112 +82,5 @@ public class HSSFFooter extends Object implements HeaderFooter { "&R" + (right == null ? "" : right)); footerRecord.setFooterLength((byte)footerRecord.getFooter().length()); } - - /** - * Returns the string that represents the change in font size. - * @param size the new font size - * @return The special string to represent a new font size - */ - public static String fontSize(short size) { - return "&" + size; - } - - /** - * Returns the string that represents the change in font. - * @param font the new font - * @param style the fonts style - * @return The special string to represent a new font size - */ - public static String font(String font, String style) { - return "&\"" + font + "," + style + "\""; - } - - /** - * Returns the string representing the current page number - * @return The special string for page number - */ - public static String page() { - return "&P"; - } - - /** - * Returns the string representing the number of pages. - * @return The special string for the number of pages - */ - public static String numPages() { - return "&N"; - } - - /** - * Returns the string representing the current date - * @return The special string for the date - */ - public static String date() { - return "&D"; - } - - /** - * Returns the string representing the current time - * @return The special string for the time - */ - public static String time() { - return "&T"; - } - - /** - * Returns the string representing the current file name - * @return The special string for the file name - */ - public static String file() { - return "&F"; - } - - /** - * Returns the string representing the current tab (sheet) name - * @return The special string for tab name - */ - public static String tab() { - return "&A"; - } - - /** - * Returns the string representing the start underline - * - * @return The special string for start underline - */ - public static String startUnderline() - { - return "&U"; - } - - /** - * Returns the string representing the end underline - * - * @return The special string for end underline - */ - public static String endUnderline() - { - return "&U"; - } - - /** - * Returns the string representing the start double underline - * - * @return The special string for start double underline - */ - public static String startDoubleUnderline() - { - return "&E"; - } - - /** - * Returns the string representing the end double underline - * - * @return The special string for end double underline - */ - public static String endDoubleUnderline() - { - return "&E"; - } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java b/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java index d2c99f9888..7b932f6821 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java @@ -32,78 +32,17 @@ import org.apache.poi.hssf.record.HeaderRecord; * * @author Shawn Laubach (slaubach at apache dot org) */ -public class HSSFHeader implements HeaderFooter -{ - - HeaderRecord headerRecord; - String left; - String center; - String right; +public class HSSFHeader extends HeaderFooter { + private HeaderRecord headerRecord; /** * Constructor. Creates a new header interface from a header record * * @param headerRecord Header record to create the header with */ - protected HSSFHeader( HeaderRecord headerRecord ) - { + protected HSSFHeader( HeaderRecord headerRecord ) { + super(headerRecord.getHeader()); this.headerRecord = headerRecord; - String head = headerRecord.getHeader(); - while ( head != null && head.length() > 1 ) - { - int pos = head.length(); - switch ( head.substring( 1, 2 ).charAt( 0 ) ) - { - case 'L': - if ( head.indexOf( "&C" ) >= 0 ) - { - pos = Math.min( pos, head.indexOf( "&C" ) ); - } - if ( head.indexOf( "&R" ) >= 0 ) - { - pos = Math.min( pos, head.indexOf( "&R" ) ); - } - left = head.substring( 2, pos ); - head = head.substring( pos ); - break; - case 'C': - if ( head.indexOf( "&L" ) >= 0 ) - { - pos = Math.min( pos, head.indexOf( "&L" ) ); - } - if ( head.indexOf( "&R" ) >= 0 ) - { - pos = Math.min( pos, head.indexOf( "&R" ) ); - } - center = head.substring( 2, pos ); - head = head.substring( pos ); - break; - case 'R': - if ( head.indexOf( "&C" ) >= 0 ) - { - pos = Math.min( pos, head.indexOf( "&C" ) ); - } - if ( head.indexOf( "&L" ) >= 0 ) - { - pos = Math.min( pos, head.indexOf( "&L" ) ); - } - right = head.substring( 2, pos ); - head = head.substring( pos ); - break; - default : - head = null; - } - } - } - - /** - * Get the left side of the header. - * - * @return The string representing the left side. - */ - public String getLeft() - { - return left; } /** @@ -117,16 +56,6 @@ public class HSSFHeader implements HeaderFooter createHeaderString(); } - /** - * Get the center of the header. - * - * @return The string representing the center. - */ - public String getCenter() - { - return center; - } - /** * Sets the center string. * @@ -138,16 +67,6 @@ public class HSSFHeader implements HeaderFooter createHeaderString(); } - /** - * Get the right side of the header. - * - * @return The string representing the right side. - */ - public String getRight() - { - return right; - } - /** * Sets the right string. * @@ -171,127 +90,5 @@ public class HSSFHeader implements HeaderFooter headerRecord.setHeaderLength( (byte) headerRecord.getHeader().length() ); } - /** - * Returns the string that represents the change in font size. - * - * @param size the new font size - * @return The special string to represent a new font size - */ - public static String fontSize( short size ) - { - return "&" + size; - } - - /** - * Returns the string that represents the change in font. - * - * @param font the new font - * @param style the fonts style - * @return The special string to represent a new font size - */ - public static String font( String font, String style ) - { - return "&\"" + font + "," + style + "\""; - } - - /** - * Returns the string representing the current page number - * - * @return The special string for page number - */ - public static String page() - { - return "&P"; - } - - /** - * Returns the string representing the number of pages. - * - * @return The special string for the number of pages - */ - public static String numPages() - { - return "&N"; - } - - /** - * Returns the string representing the current date - * - * @return The special string for the date - */ - public static String date() - { - return "&D"; - } - - /** - * Returns the string representing the current time - * - * @return The special string for the time - */ - public static String time() - { - return "&T"; - } - - /** - * Returns the string representing the current file name - * - * @return The special string for the file name - */ - public static String file() - { - return "&F"; - } - - /** - * Returns the string representing the current tab (sheet) name - * - * @return The special string for tab name - */ - public static String tab() - { - return "&A"; - } - - /** - * Returns the string representing the start underline - * - * @return The special string for start underline - */ - public static String startUnderline() - { - return "&U"; - } - - /** - * Returns the string representing the end underline - * - * @return The special string for end underline - */ - public static String endUnderline() - { - return "&U"; - } - - /** - * Returns the string representing the start double underline - * - * @return The special string for start double underline - */ - public static String startDoubleUnderline() - { - return "&E"; - } - - /** - * Returns the string representing the end double underline - * - * @return The special string for end double underline - */ - public static String endDoubleUnderline() - { - return "&E"; - } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HeaderFooter.java b/src/java/org/apache/poi/hssf/usermodel/HeaderFooter.java index 8c5ef05f3e..6120e6d534 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HeaderFooter.java +++ b/src/java/org/apache/poi/hssf/usermodel/HeaderFooter.java @@ -17,17 +17,203 @@ package org.apache.poi.hssf.usermodel; /** - * Common interface for {@link HSSFHeader} and + * Common class for {@link HSSFHeader} and * {@link HSSFFooter}. */ -public interface HeaderFooter { - public String getLeft(); - public void setLeft( String newLeft ); +public abstract class HeaderFooter { + protected String left; + protected String center; + protected String right; + + protected HeaderFooter(String text) { + while (text != null && text.length() > 1) { + int pos = text.length(); + switch (text.substring(1, 2).charAt(0)) { + case 'L' : + if (text.indexOf("&C") >= 0) { + pos = Math.min(pos, text.indexOf("&C")); + } + if (text.indexOf("&R") >= 0) { + pos = Math.min(pos, text.indexOf("&R")); + } + left = text.substring(2, pos); + text = text.substring(pos); + break; + case 'C' : + if (text.indexOf("&L") >= 0) { + pos = Math.min(pos, text.indexOf("&L")); + } + if (text.indexOf("&R") >= 0) { + pos = Math.min(pos, text.indexOf("&R")); + } + center = text.substring(2, pos); + text = text.substring(pos); + break; + case 'R' : + if (text.indexOf("&C") >= 0) { + pos = Math.min(pos, text.indexOf("&C")); + } + if (text.indexOf("&L") >= 0) { + pos = Math.min(pos, text.indexOf("&L")); + } + right = text.substring(2, pos); + text = text.substring(pos); + break; + default: + text = null; + } + } + } + + /** + * Get the left side of the header or footer. + * @return The string representing the left side. + */ + public String getLeft() { + return left; + } + public abstract void setLeft( String newLeft ); - public String getCenter(); - public void setCenter( String newCenter ); + /** + * Get the center of the header or footer. + * @return The string representing the center. + */ + public String getCenter() { + return center; + } + public abstract void setCenter( String newCenter ); - public String getRight(); - public void setRight( String newRight ); + /** + * Get the right side of the header or footer. + * @return The string representing the right side. + */ + public String getRight() { + return right; + } + public abstract void setRight( String newRight ); + + + /** + * Returns the string that represents the change in font size. + * + * @param size the new font size + * @return The special string to represent a new font size + */ + public static String fontSize( short size ) + { + return "&" + size; + } + + /** + * Returns the string that represents the change in font. + * + * @param font the new font + * @param style the fonts style + * @return The special string to represent a new font size + */ + public static String font( String font, String style ) + { + return "&\"" + font + "," + style + "\""; + } + + /** + * Returns the string representing the current page number + * + * @return The special string for page number + */ + public static String page() + { + return "&P"; + } + + /** + * Returns the string representing the number of pages. + * + * @return The special string for the number of pages + */ + public static String numPages() + { + return "&N"; + } + + /** + * Returns the string representing the current date + * + * @return The special string for the date + */ + public static String date() + { + return "&D"; + } + + /** + * Returns the string representing the current time + * + * @return The special string for the time + */ + public static String time() + { + return "&T"; + } + + /** + * Returns the string representing the current file name + * + * @return The special string for the file name + */ + public static String file() + { + return "&F"; + } + + /** + * Returns the string representing the current tab (sheet) name + * + * @return The special string for tab name + */ + public static String tab() + { + return "&A"; + } + + /** + * Returns the string representing the start underline + * + * @return The special string for start underline + */ + public static String startUnderline() + { + return "&U"; + } + + /** + * Returns the string representing the end underline + * + * @return The special string for end underline + */ + public static String endUnderline() + { + return "&U"; + } + + /** + * Returns the string representing the start double underline + * + * @return The special string for start double underline + */ + public static String startDoubleUnderline() + { + return "&E"; + } + + /** + * Returns the string representing the end double underline + * + * @return The special string for end double underline + */ + public static String endDoubleUnderline() + { + return "&E"; + } } -- 2.39.5