* @author Shawn Laubach (slaubach at apache dot org)
*/
-public class HSSFFooter implements Footer, HeaderFooter {
- FooterRecord footerRecord;
- String left;
- String center;
- String right;
+public class HSSFFooter extends HeaderFooter implements Footer {
+ 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 +54,6 @@ public class HSSFFooter implements Footer, 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 +63,6 @@ public class HSSFFooter implements Footer, 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 +83,5 @@ public class HSSFFooter implements Footer, 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 535b70efa..76bfdbd86 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java
@@ -33,78 +33,17 @@ import org.apache.poi.ss.usermodel.Header;
*
* @author Shawn Laubach (slaubach at apache dot org)
*/
-public class HSSFHeader implements Header, HeaderFooter
-{
-
- HeaderRecord headerRecord;
- String left;
- String center;
- String right;
+public class HSSFHeader extends HeaderFooter implements Header {
+ 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;
}
/**
@@ -118,16 +57,6 @@ public class HSSFHeader implements Header, HeaderFooter
createHeaderString();
}
- /**
- * Get the center of the header.
- *
- * @return The string representing the center.
- */
- public String getCenter()
- {
- return center;
- }
-
/**
* Sets the center string.
*
@@ -139,16 +68,6 @@ public class HSSFHeader implements Header, 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.
*
@@ -172,127 +91,5 @@ public class HSSFHeader implements Header, 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 44dfd7956..499cda792 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HeaderFooter.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HeaderFooter.java
@@ -16,9 +16,310 @@
==================================================================== */
package org.apache.poi.hssf.usermodel;
+import java.util.ArrayList;
+
/**
- * Common interface for {@link HSSFHeader} and
+ * Common class for {@link HSSFHeader} and
* {@link HSSFFooter}.
*/
-public interface HeaderFooter extends org.apache.poi.ss.usermodel.HeaderFooter {
+public abstract class HeaderFooter implements org.apache.poi.ss.usermodel.HeaderFooter {
+ protected String left;
+ protected String center;
+ protected String right;
+
+ private boolean stripFields = false;
+
+ 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() {
+ if(stripFields)
+ return stripFields(left);
+ return left;
+ }
+ public abstract void setLeft( String newLeft );
+
+ /**
+ * Get the center of the header or footer.
+ * @return The string representing the center.
+ */
+ public String getCenter() {
+ if(stripFields)
+ return stripFields(center);
+ return center;
+ }
+ public abstract void setCenter( String newCenter );
+
+ /**
+ * Get the right side of the header or footer.
+ * @return The string representing the right side.
+ */
+ public String getRight() {
+ if(stripFields)
+ return stripFields(right);
+ 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, one of regular, italic, bold, italic bold or bold italic
+ * @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 PAGE_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the number of pages.
+ *
+ * @return The special string for the number of pages
+ */
+ public static String numPages() {
+ return NUM_PAGES_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the current date
+ *
+ * @return The special string for the date
+ */
+ public static String date() {
+ return DATE_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the current time
+ *
+ * @return The special string for the time
+ */
+ public static String time() {
+ return TIME_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the current file name
+ *
+ * @return The special string for the file name
+ */
+ public static String file() {
+ return FILE_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the current tab (sheet) name
+ *
+ * @return The special string for tab name
+ */
+ public static String tab() {
+ return SHEET_NAME_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the start bold
+ *
+ * @return The special string for start bold
+ */
+ public static String startBold() {
+ return BOLD_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the end bold
+ *
+ * @return The special string for end bold
+ */
+ public static String endBold() {
+ return BOLD_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the start underline
+ *
+ * @return The special string for start underline
+ */
+ public static String startUnderline() {
+ return UNDERLINE_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the end underline
+ *
+ * @return The special string for end underline
+ */
+ public static String endUnderline() {
+ return UNDERLINE_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the start double underline
+ *
+ * @return The special string for start double underline
+ */
+ public static String startDoubleUnderline() {
+ return DOUBLE_UNDERLINE_FIELD.sequence;
+ }
+
+ /**
+ * Returns the string representing the end double underline
+ *
+ * @return The special string for end double underline
+ */
+ public static String endDoubleUnderline() {
+ return DOUBLE_UNDERLINE_FIELD.sequence;
+ }
+
+
+ /**
+ * Removes any fields (eg macros, page markers etc)
+ * from the string.
+ * Normally used to make some text suitable for showing
+ * to humans, and the resultant text should not normally
+ * be saved back into the document!
+ */
+ public static String stripFields(String text) {
+ int pos;
+
+ // Firstly, do the easy ones which are static
+ for(int i=0; i