Refactor header/footer stuff to remove duplication

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@686036 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-08-14 20:50:06 +00:00
parent e334fea0c3
commit 074a69b42a
3 changed files with 202 additions and 390 deletions

View File

@ -32,64 +32,16 @@ import org.apache.poi.hssf.record.FooterRecord;
* <P> * <P>
* @author Shawn Laubach (slaubach at apache dot org) * @author Shawn Laubach (slaubach at apache dot org)
*/ */
public class HSSFFooter extends Object implements HeaderFooter { public class HSSFFooter extends HeaderFooter {
private FooterRecord footerRecord;
FooterRecord footerRecord;
String left;
String center;
String right;
/** /**
* Constructor. Creates a new footer interface from a footer record * Constructor. Creates a new footer interface from a footer record
* @param footerRecord Footer record to create the footer with * @param footerRecord Footer record to create the footer with
*/ */
protected HSSFFooter(FooterRecord footerRecord) { protected HSSFFooter(FooterRecord footerRecord) {
this.footerRecord = footerRecord; super(footerRecord.getFooter());
String foot = footerRecord.getFooter(); this.footerRecord = footerRecord;
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;
} }
/** /**
@ -101,14 +53,6 @@ public class HSSFFooter extends Object implements HeaderFooter {
createFooterString(); createFooterString();
} }
/**
* Get the center of the footer.
* @return The string representing the center.
*/
public String getCenter() {
return center;
}
/** /**
* Sets the center string. * Sets the center string.
* @param newCenter The string to set as the center. * @param newCenter The string to set as the center.
@ -118,14 +62,6 @@ public class HSSFFooter extends Object implements HeaderFooter {
createFooterString(); createFooterString();
} }
/**
* Get the right side of the footer.
* @return The string representing the right side.
*/
public String getRight() {
return right;
}
/** /**
* Sets the right string. * Sets the right string.
* @param newRight The string to set as the right side. * @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)); "&R" + (right == null ? "" : right));
footerRecord.setFooterLength((byte)footerRecord.getFooter().length()); 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";
}
} }

View File

@ -32,78 +32,17 @@ import org.apache.poi.hssf.record.HeaderRecord;
* *
* @author Shawn Laubach (slaubach at apache dot org) * @author Shawn Laubach (slaubach at apache dot org)
*/ */
public class HSSFHeader implements HeaderFooter public class HSSFHeader extends HeaderFooter {
{ private HeaderRecord headerRecord;
HeaderRecord headerRecord;
String left;
String center;
String right;
/** /**
* Constructor. Creates a new header interface from a header record * Constructor. Creates a new header interface from a header record
* *
* @param headerRecord Header record to create the header with * @param headerRecord Header record to create the header with
*/ */
protected HSSFHeader( HeaderRecord headerRecord ) protected HSSFHeader( HeaderRecord headerRecord ) {
{ super(headerRecord.getHeader());
this.headerRecord = headerRecord; 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(); createHeaderString();
} }
/**
* Get the center of the header.
*
* @return The string representing the center.
*/
public String getCenter()
{
return center;
}
/** /**
* Sets the center string. * Sets the center string.
* *
@ -138,16 +67,6 @@ public class HSSFHeader implements HeaderFooter
createHeaderString(); createHeaderString();
} }
/**
* Get the right side of the header.
*
* @return The string representing the right side.
*/
public String getRight()
{
return right;
}
/** /**
* Sets the right string. * Sets the right string.
* *
@ -171,127 +90,5 @@ public class HSSFHeader implements HeaderFooter
headerRecord.setHeaderLength( (byte) headerRecord.getHeader().length() ); 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";
}
} }

View File

@ -17,17 +17,203 @@
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
/** /**
* Common interface for {@link HSSFHeader} and * Common class for {@link HSSFHeader} and
* {@link HSSFFooter}. * {@link HSSFFooter}.
*/ */
public interface HeaderFooter { public abstract class HeaderFooter {
public String getLeft(); protected String left;
public void setLeft( String newLeft ); 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";
}
} }