2002-12-01 02:21:00 +00:00
|
|
|
/*
|
|
|
|
* AppleCommander - An Apple ][ image utility.
|
|
|
|
* Copyright (C) 2002 by Robert Greene
|
|
|
|
* robgreene at users.sourceforge.net
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2004-06-09 03:58:00 +00:00
|
|
|
package com.webcodepro.applecommander.storage.filters;
|
2003-02-08 16:42:07 +00:00
|
|
|
|
2002-12-01 02:21:00 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
|
2004-06-09 03:58:00 +00:00
|
|
|
import com.webcodepro.applecommander.storage.FileEntry;
|
|
|
|
import com.webcodepro.applecommander.storage.FileFilter;
|
|
|
|
import com.webcodepro.applecommander.ui.AppleCommander;
|
|
|
|
import com.webcodepro.applecommander.util.AppleUtil;
|
|
|
|
|
2002-12-01 02:21:00 +00:00
|
|
|
/**
|
|
|
|
* Extract the contents of an AWP (AppleWorks word processor) document and
|
2003-02-08 16:42:07 +00:00
|
|
|
* convert to a text format. Currently supported formats are plain text,
|
|
|
|
* HTML, or RTF. These are not exact duplicates, but they are close
|
|
|
|
* approximations. RTF format is suitable for conversion to other word
|
|
|
|
* processors.
|
|
|
|
* <p>
|
|
|
|
* To choose export format, use the appropriately named select method.
|
|
|
|
* <p>
|
|
|
|
* See AWP format documentation at:
|
2002-12-01 02:21:00 +00:00
|
|
|
* http://www.gno.org/pub/apple2/doc/apple/filetypes/ftn.1a.xxxx
|
|
|
|
* <p>
|
|
|
|
* Date created: Nov 15, 2002 3:55:21 PM
|
2004-06-18 05:15:33 +00:00
|
|
|
* @author Rob Greene
|
2002-12-01 02:21:00 +00:00
|
|
|
*/
|
|
|
|
public class AppleWorksWordProcessorFileFilter implements FileFilter {
|
2003-02-08 16:42:07 +00:00
|
|
|
/*
|
|
|
|
* This list identifies the various rendering options.
|
|
|
|
* As the internal format may change in the future,
|
|
|
|
* the internal representation is hidden and the developer
|
|
|
|
* should use the appropriate select method.
|
|
|
|
*/
|
|
|
|
private static final int RENDER_AS_TEXT = 0;
|
|
|
|
private static final int RENDER_AS_HTML = 1;
|
|
|
|
private static final int RENDER_AS_RTF = 2;
|
2002-12-01 02:21:00 +00:00
|
|
|
private int rendering = RENDER_AS_TEXT;
|
2003-02-11 04:33:55 +00:00
|
|
|
/**
|
|
|
|
* Indicates if we are in a header or footer. This is somewhat of a
|
|
|
|
* hack to generate a RTF file correctly. If a header is found (and
|
|
|
|
* presumably a footer), there isn't necessarily a header end code
|
|
|
|
* included.
|
|
|
|
*/
|
|
|
|
private boolean inHeaderOrFooter = false;
|
2003-02-17 04:37:38 +00:00
|
|
|
/**
|
|
|
|
* This constant indicates how many TWIPS there are per inch.
|
|
|
|
* I'm fairly certain of this number, but it may need tweaking.
|
|
|
|
*/
|
|
|
|
private static final int TWIPS_PER_INCH = 1440;
|
2003-02-08 16:42:07 +00:00
|
|
|
/*
|
|
|
|
* Identifies the codes embedded in the AppleWorks file.
|
|
|
|
*/
|
|
|
|
private static final int CODE_BOLD_ON = 0x01;
|
|
|
|
private static final int CODE_BOLD_OFF = 0x02;
|
|
|
|
private static final int CODE_SUPERSCRIPT_ON = 0x03;
|
|
|
|
private static final int CODE_SUPERSCRIPT_OFF = 0x04;
|
|
|
|
private static final int CODE_SUBSCRIPT_ON = 0x05;
|
|
|
|
private static final int CODE_SUBSCRIPT_OFF = 0x06;
|
|
|
|
private static final int CODE_UNDERLINE_ON = 0x07;
|
|
|
|
private static final int CODE_UNDERLINE_OFF = 0x08;
|
|
|
|
private static final int CODE_PAGE_NUMBER = 0x09;
|
2004-07-11 15:07:56 +00:00
|
|
|
// private static final int CODE_ENTER_KEYBOARD = 0x0a;
|
2003-02-11 04:33:55 +00:00
|
|
|
private static final int CODE_STICKY_SPACE = 0x0b;
|
2004-07-11 15:07:56 +00:00
|
|
|
// private static final int CODE_MAILMERGE_BEGIN = 0x0c;
|
|
|
|
// private static final int CODE_RESERVED1 = 0x0d;
|
2003-02-08 16:42:07 +00:00
|
|
|
private static final int CODE_DATE = 0x0e;
|
|
|
|
private static final int CODE_TIME = 0x0f;
|
2004-07-11 15:07:56 +00:00
|
|
|
// private static final int CODE_SPECIAL_1 = 0x10;
|
|
|
|
// private static final int CODE_SPECIAL_2 = 0x11;
|
|
|
|
// private static final int CODE_SPECIAL_3 = 0x12;
|
|
|
|
// private static final int CODE_SPECIAL_4 = 0x13;
|
|
|
|
// private static final int CODE_SPECIAL_5 = 0x14;
|
|
|
|
// private static final int CODE_SPECIAL_6 = 0x15;
|
|
|
|
// private static final int CODE_TAB = 0x16;
|
|
|
|
// private static final int CODE_TAB_FILL = 0x17;
|
|
|
|
// private static final int CODE_RESERVED2 = 0x18;
|
2003-02-08 16:42:07 +00:00
|
|
|
/*
|
|
|
|
* Identifies the commands embedded in the AppleWorks file.
|
|
|
|
*/
|
2004-07-11 15:07:56 +00:00
|
|
|
// private static final int COMMAND_RESERVED = 0xd4;
|
2003-02-11 04:33:55 +00:00
|
|
|
private static final int COMMAND_PAGEHEADER_END = 0xd5;
|
|
|
|
private static final int COMMAND_PAGEFOOTER_END = 0xd6;
|
2003-02-08 16:42:07 +00:00
|
|
|
private static final int COMMAND_RIGHT = 0xd7;
|
2003-02-11 04:33:55 +00:00
|
|
|
private static final int COMMAND_PLATEN_WIDTH = 0xd8; // 10ths of an inch
|
|
|
|
private static final int COMMAND_MARGIN_LEFT = 0xd9; // 10ths of an inch
|
|
|
|
private static final int COMMAND_MARGIN_RIGHT = 0xda; // 10ths of an inch
|
2004-07-11 15:07:56 +00:00
|
|
|
// private static final int COMMAND_CHARS_PER_INCH = 0xdb;
|
|
|
|
// private static final int COMMAND_PROPORTIONAL_1 = 0xdc;
|
|
|
|
// private static final int COMMAND_PROPORTIONAL_2 = 0xdd;
|
|
|
|
// private static final int COMMAND_INDENT = 0xde; // in characters
|
2003-02-11 04:33:55 +00:00
|
|
|
private static final int COMMAND_JUSTIFY = 0xdf;
|
|
|
|
private static final int COMMAND_LEFT = 0xe0;
|
2003-02-08 16:42:07 +00:00
|
|
|
private static final int COMMAND_CENTER = 0xe1;
|
2003-02-11 04:33:55 +00:00
|
|
|
private static final int COMMAND_PAPER_LENGTH = 0xe2; // 10ths of an inch
|
|
|
|
private static final int COMMAND_MARGIN_TOP = 0xe3; // 10ths of an inch
|
2003-02-17 04:37:38 +00:00
|
|
|
private static final int COMMAND_MARGIN_BOTTOM = 0xe4; // 10ths of an inch
|
2004-07-11 15:07:56 +00:00
|
|
|
// private static final int COMMAND_LINES_PER_INCH = 0xe5;
|
|
|
|
// private static final int COMMAND_SINGLE_SPACE = 0xe6;
|
|
|
|
// private static final int COMMAND_DOUBLE_SPACE = 0xe7;
|
|
|
|
// private static final int COMMAND_TRIPLE_SPACE = 0xe8;
|
2003-02-11 04:33:55 +00:00
|
|
|
private static final int COMMAND_NEW_PAGE = 0xe9;
|
2004-07-11 15:07:56 +00:00
|
|
|
// private static final int COMMAND_GROUP_BEGIN = 0xea;
|
|
|
|
// private static final int COMMAND_GROUP_END = 0xeb;
|
2003-02-11 04:33:55 +00:00
|
|
|
private static final int COMMAND_PAGEHEADER = 0xed; // may be mixed up
|
|
|
|
private static final int COMMAND_PAGEFOOTER = 0xec; // with this...
|
|
|
|
private static final int COMMAND_SKIP_LINES = 0xee;
|
2004-07-11 15:07:56 +00:00
|
|
|
// private static final int COMMAND_PAGE_NUMBER = 0xef;
|
|
|
|
// private static final int COMMAND_PAUSE_EACH_PAGE = 0xf0;
|
|
|
|
// private static final int COMMAND_PAUSE_HERE = 0xf1;
|
|
|
|
// private static final int COMMAND_SET_MARKER = 0xf2;
|
|
|
|
// private static final int COMMAND_PAGE_NUMBER_256 = 0xf3; // add 256
|
2003-02-11 04:33:55 +00:00
|
|
|
private static final int COMMAND_PAGE_BREAK = 0xf4; // byte page#
|
|
|
|
private static final int COMMAND_PAGE_BREAK_256 = 0xf5; // byte page# + 256
|
2004-07-11 15:07:56 +00:00
|
|
|
// private static final int COMMAND_PP_PAGE_BREAK = 0xf6; // break in midl/par.
|
|
|
|
// private static final int COMMAND_PP_PAGE_BREAK_256 = 0xf7; // +256 ??
|
2003-02-11 04:33:55 +00:00
|
|
|
private static final int COMMAND_EOF = 0xff; // END OF FILE
|
2003-02-08 16:42:07 +00:00
|
|
|
|
2002-12-01 02:21:00 +00:00
|
|
|
/**
|
|
|
|
* Constructor for AppleWorksWordProcessorFileFilter.
|
|
|
|
*/
|
|
|
|
public AppleWorksWordProcessorFileFilter() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Process the given FileEntry and return a byte array with filtered data.
|
|
|
|
* @see com.webcodepro.applecommander.storage.FileFilter#filter(FileEntry)
|
|
|
|
*/
|
|
|
|
public byte[] filter(FileEntry fileEntry) {
|
|
|
|
byte[] fileData = fileEntry.getFileData();
|
|
|
|
if (fileData[4] != 0x4f) return null; // not an AWP file!
|
|
|
|
ByteArrayOutputStream byteArray = new ByteArrayOutputStream(fileData.length);
|
|
|
|
PrintWriter printWriter = new PrintWriter(byteArray, true);
|
|
|
|
if (isHtmlRendering()) {
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println("<html><style>BODY { font-family: monospace; }</style><body>"); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
} else if (isRtfRendering()) {
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("{\\rtf1"); //$NON-NLS-1$
|
|
|
|
printWriter.print("{\\fonttbl{\\f0\\fmodern\\fprq1\\fcharset0 Courier New;}}"); //$NON-NLS-1$
|
|
|
|
printWriter.print("{\\*\\generator AppleCommander "); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
printWriter.print(AppleCommander.VERSION);
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println(";}"); //$NON-NLS-1$
|
|
|
|
printWriter.print("\\f0 "); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
}
|
|
|
|
boolean version3 = (fileData[183] != 0);
|
|
|
|
int offset = 300 + (version3 ? 2 : 0); // version 3.0's first line record is invalid
|
|
|
|
while (offset < fileData.length) {
|
|
|
|
int byte0 = AppleUtil.getUnsignedByte(fileData[offset++]);
|
|
|
|
int byte1 = AppleUtil.getUnsignedByte(fileData[offset++]);
|
|
|
|
|
2003-02-11 04:33:55 +00:00
|
|
|
if (byte0 == COMMAND_EOF && byte1 == COMMAND_EOF) {
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
|
|
|
} else if (byte1 == 0xd0) { // Carriage return line records
|
|
|
|
handleReturn(printWriter);
|
|
|
|
} else if (byte1 > 0xd0) { // Command line records
|
|
|
|
if (isHtmlRendering()) {
|
|
|
|
offset = handleCommandRecordAsHtml(byte0, byte1, printWriter, offset);
|
2003-02-08 16:42:07 +00:00
|
|
|
} else if (isRtfRendering()) {
|
|
|
|
offset = handleCommandRecordAsRtf(byte0, byte1, printWriter, offset);
|
|
|
|
} else {
|
|
|
|
offset = handleCommandRecordAsText(byte0, byte1, printWriter, offset);
|
2002-12-01 02:21:00 +00:00
|
|
|
}
|
|
|
|
} else { // Text records (assumed)
|
|
|
|
offset = handleTextRecord(fileData, printWriter, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isHtmlRendering()) {
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println("</body></html>"); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
} else if (isRtfRendering()) {
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println("}"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
}
|
|
|
|
return byteArray.toByteArray();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Deal with an individual text record.
|
|
|
|
*/
|
|
|
|
protected int handleTextRecord(byte[] fileData, PrintWriter printWriter, int offset) {
|
2003-02-08 16:42:07 +00:00
|
|
|
/* byte2 */ AppleUtil.getUnsignedByte(fileData[offset++]);
|
2002-12-01 02:21:00 +00:00
|
|
|
int byte3 = AppleUtil.getUnsignedByte(fileData[offset++]);
|
|
|
|
boolean addReturn = (byte3 >= 0x80);
|
|
|
|
int length = (byte3 & 0x7f);
|
|
|
|
while (length > 0) {
|
|
|
|
byte ch = fileData[offset++];
|
|
|
|
length--;
|
|
|
|
if (ch < 0x20) { // special formatting character
|
|
|
|
if (isHtmlRendering()) handleSpecialCodesAsHtml(printWriter, ch);
|
2003-02-08 16:42:07 +00:00
|
|
|
else if (isRtfRendering()) handleSpecialCodesAsRtf(printWriter, ch);
|
|
|
|
else handleSpecialCodesAsText(printWriter, ch);
|
2002-12-01 02:21:00 +00:00
|
|
|
} else {
|
|
|
|
if (isHtmlRendering() && ch == ' ') {
|
|
|
|
int extraSpaces = 0;
|
|
|
|
while (fileData[offset+extraSpaces] == ' ') {
|
|
|
|
extraSpaces++;
|
|
|
|
}
|
|
|
|
if (extraSpaces > 0) {
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print(" "); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
while (fileData[offset] == ' ') {
|
|
|
|
offset++;
|
|
|
|
length--;
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print(" "); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printWriter.print((char)ch);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printWriter.print((char)ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (addReturn) handleReturn(printWriter);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Deal with carriage-return.
|
|
|
|
*/
|
|
|
|
protected void handleReturn(PrintWriter printWriter) {
|
2004-07-11 15:07:56 +00:00
|
|
|
if (isHtmlRendering()) printWriter.println("<br>"); //$NON-NLS-1$
|
|
|
|
else if (isRtfRendering()) printWriter.println("\\par"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
else printWriter.println();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Process special coding of a text record.
|
|
|
|
*/
|
|
|
|
protected void handleSpecialCodesAsHtml(PrintWriter printWriter, byte ch) {
|
|
|
|
switch (ch) {
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_BOLD_ON:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("<b>"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_BOLD_OFF:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("</b>"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_SUPERSCRIPT_ON:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("<sup>"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_SUPERSCRIPT_OFF:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("</sup>"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_SUBSCRIPT_ON:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("<sub>"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_SUBSCRIPT_OFF:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("</sub>"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_UNDERLINE_ON:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("<u>"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_UNDERLINE_OFF:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("</u>"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-11 04:33:55 +00:00
|
|
|
case CODE_STICKY_SPACE:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print(" "); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
break;
|
|
|
|
default: handleSpecialCodesAsText(printWriter, ch);
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Process special coding of a text record.
|
|
|
|
*/
|
|
|
|
protected void handleSpecialCodesAsRtf(PrintWriter printWriter, byte ch) {
|
|
|
|
switch (ch) {
|
2003-02-11 04:33:55 +00:00
|
|
|
case CODE_PAGE_NUMBER:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("{\\chpgn}"); //$NON-NLS-1$
|
2003-02-11 04:33:55 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_BOLD_ON:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\b "); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_BOLD_OFF:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\b0 "); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
break;
|
|
|
|
case CODE_UNDERLINE_ON:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\ul "); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
break;
|
|
|
|
case CODE_UNDERLINE_OFF:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\ulnone"); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
break;
|
2003-02-11 04:33:55 +00:00
|
|
|
case CODE_SUPERSCRIPT_ON:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\super "); //$NON-NLS-1$
|
2003-02-11 04:33:55 +00:00
|
|
|
break;
|
|
|
|
case CODE_SUBSCRIPT_ON:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\sub "); //$NON-NLS-1$
|
2003-02-11 04:33:55 +00:00
|
|
|
break;
|
|
|
|
case CODE_SUPERSCRIPT_OFF:
|
|
|
|
case CODE_SUBSCRIPT_OFF:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\nosupersub "); //$NON-NLS-1$
|
2003-02-11 04:33:55 +00:00
|
|
|
break;
|
|
|
|
case CODE_STICKY_SPACE:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print(" "); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
break;
|
|
|
|
default: handleSpecialCodesAsText(printWriter, ch);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Process special coding of a text record.
|
|
|
|
*/
|
|
|
|
protected void handleSpecialCodesAsText(PrintWriter printWriter, byte ch) {
|
|
|
|
switch (ch) {
|
|
|
|
case CODE_PAGE_NUMBER:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("[Page#]"); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
break;
|
|
|
|
case CODE_DATE:
|
2004-07-11 15:07:56 +00:00
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
printWriter.print(dateFormat.format(new Date()));
|
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case CODE_TIME:
|
2004-07-11 15:07:56 +00:00
|
|
|
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
printWriter.print(timeFormat.format(new Date()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Deal with an individual command line record.
|
|
|
|
*/
|
|
|
|
protected int handleCommandRecordAsHtml(int byte0, int byte1,
|
|
|
|
PrintWriter printWriter, int offset) {
|
|
|
|
|
|
|
|
switch (byte1) {
|
2003-02-08 16:42:07 +00:00
|
|
|
case COMMAND_RIGHT:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println("<style>BODY: text-align: right;</style>"); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
break;
|
|
|
|
case COMMAND_JUSTIFY:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println("<style>BODY: text-align: justify;</style>"); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
break;
|
|
|
|
case COMMAND_LEFT:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println("<style>BODY: text-align: left;</style>"); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
break;
|
|
|
|
case COMMAND_CENTER:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println("<style>BODY: text-align: center;</style>"); //$NON-NLS-1$
|
2003-02-08 16:42:07 +00:00
|
|
|
break;
|
|
|
|
default: offset = handleCommandRecordAsText(byte0, byte1,
|
|
|
|
printWriter, offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Deal with an individual command line record.
|
|
|
|
*/
|
|
|
|
protected int handleCommandRecordAsRtf(int byte0, int byte1,
|
|
|
|
PrintWriter printWriter, int offset) {
|
|
|
|
|
2003-02-11 04:33:55 +00:00
|
|
|
if (inHeaderOrFooter) {
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("}\\f0 "); //$NON-NLS-1$
|
2003-02-11 04:33:55 +00:00
|
|
|
inHeaderOrFooter = false;
|
|
|
|
}
|
2003-02-17 04:37:38 +00:00
|
|
|
int twipDistance = byte0 * TWIPS_PER_INCH / 10;
|
2003-02-08 16:42:07 +00:00
|
|
|
switch (byte1) {
|
2003-02-11 04:33:55 +00:00
|
|
|
case COMMAND_PAGEHEADER:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("{\\header "); //$NON-NLS-1$
|
2003-02-11 04:33:55 +00:00
|
|
|
inHeaderOrFooter = true;
|
|
|
|
break;
|
|
|
|
case COMMAND_PAGEFOOTER:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("{\\footer "); //$NON-NLS-1$
|
2003-02-11 04:33:55 +00:00
|
|
|
inHeaderOrFooter = true;
|
|
|
|
break;
|
|
|
|
case COMMAND_PAGEHEADER_END:
|
|
|
|
case COMMAND_PAGEFOOTER_END:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("}"); //$NON-NLS-1$
|
2003-02-11 04:33:55 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case COMMAND_RIGHT:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println("\\pard\\qr "); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case COMMAND_LEFT:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println("\\pard "); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
case COMMAND_CENTER:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.println("\\pard\\qc "); //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-11 04:33:55 +00:00
|
|
|
case COMMAND_JUSTIFY:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\qj "); //$NON-NLS-1$
|
2003-02-11 04:33:55 +00:00
|
|
|
break;
|
|
|
|
case COMMAND_PAGE_BREAK:
|
|
|
|
case COMMAND_PAGE_BREAK_256:
|
|
|
|
case COMMAND_NEW_PAGE:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\page "); //$NON-NLS-1$
|
2003-02-11 04:33:55 +00:00
|
|
|
break;
|
2003-02-17 04:37:38 +00:00
|
|
|
case COMMAND_PLATEN_WIDTH:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\paperw"); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
printWriter.print(twipDistance);
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print(" "); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
break;
|
|
|
|
case COMMAND_PAPER_LENGTH:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\paperl"); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
printWriter.print(twipDistance);
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print(" "); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
break;
|
|
|
|
case COMMAND_MARGIN_LEFT:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\margl"); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
printWriter.print(twipDistance);
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print(" "); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
break;
|
|
|
|
case COMMAND_MARGIN_RIGHT:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\margr"); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
printWriter.print(twipDistance);
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print(" "); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
break;
|
|
|
|
case COMMAND_MARGIN_TOP:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\margt"); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
printWriter.print(twipDistance);
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print(" "); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
break;
|
|
|
|
case COMMAND_MARGIN_BOTTOM:
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print("\\margb"); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
printWriter.print(twipDistance);
|
2004-07-11 15:07:56 +00:00
|
|
|
printWriter.print(" "); //$NON-NLS-1$
|
2003-02-17 04:37:38 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
default: offset = handleCommandRecordAsText(byte0, byte1,
|
|
|
|
printWriter, offset);
|
2002-12-01 02:21:00 +00:00
|
|
|
break;
|
2003-02-08 16:42:07 +00:00
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Deal with an individual command line record.
|
|
|
|
*/
|
|
|
|
protected int handleCommandRecordAsText(int byte0, int byte1,
|
|
|
|
PrintWriter printWriter, int offset) {
|
|
|
|
|
|
|
|
switch (byte1) {
|
2003-02-11 04:33:55 +00:00
|
|
|
case COMMAND_SKIP_LINES:
|
2003-02-08 16:42:07 +00:00
|
|
|
for (int i=0; i<byte0; i++) {
|
|
|
|
handleReturn(printWriter);
|
2002-12-01 02:21:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Give suggested file name.
|
|
|
|
* @see com.webcodepro.applecommander.storage.FileFilter#getSuggestedFileName(FileEntry)
|
|
|
|
*/
|
|
|
|
public String getSuggestedFileName(FileEntry fileEntry) {
|
|
|
|
String fileName = fileEntry.getFilename().trim();
|
2004-07-11 15:07:56 +00:00
|
|
|
String extension = ".txt"; //$NON-NLS-1$
|
|
|
|
if (isHtmlRendering()) extension = ".html"; //$NON-NLS-1$
|
|
|
|
else if (isRtfRendering()) extension = ".rtf"; //$NON-NLS-1$
|
2002-12-01 02:21:00 +00:00
|
|
|
|
|
|
|
if (!fileName.toLowerCase().endsWith(extension)) {
|
|
|
|
fileName = fileName + extension;
|
|
|
|
}
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Set the rendering method.
|
|
|
|
*/
|
2003-02-08 16:42:07 +00:00
|
|
|
protected void setRendering(int rendering) {
|
2002-12-01 02:21:00 +00:00
|
|
|
this.rendering = rendering;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Indicates if this is a text rendering.
|
|
|
|
*/
|
|
|
|
public boolean isTextRendering() {
|
|
|
|
return rendering == RENDER_AS_TEXT;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Indicates if this is an HTML rendering.
|
|
|
|
*/
|
|
|
|
public boolean isHtmlRendering() {
|
|
|
|
return rendering == RENDER_AS_HTML;
|
|
|
|
}
|
2003-02-08 16:42:07 +00:00
|
|
|
/**
|
|
|
|
* Indicates if this is an RTF rendering.
|
|
|
|
*/
|
|
|
|
public boolean isRtfRendering() {
|
|
|
|
return rendering == RENDER_AS_RTF;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Selects the text rendering engine.
|
|
|
|
*/
|
|
|
|
public void selectTextRendering() {
|
|
|
|
rendering = RENDER_AS_TEXT;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Selects the HTML rendering engine.
|
|
|
|
*/
|
|
|
|
public void selectHtmlRendering() {
|
|
|
|
rendering = RENDER_AS_HTML;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Selects the RTF rendering engine.
|
|
|
|
*/
|
|
|
|
public void selectRtfRendering() {
|
|
|
|
rendering = RENDER_AS_RTF;
|
|
|
|
}
|
2002-12-01 02:21:00 +00:00
|
|
|
}
|