Bug 58326 - Forbidden APIs patches - second set of changes for charset settings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1701713 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2015-09-08 00:04:12 +00:00
parent 91e6a64627
commit 50107cae16
17 changed files with 176 additions and 201 deletions

View File

@ -82,6 +82,7 @@ import org.apache.poi.hssf.record.WriteAccessRecord;
import org.apache.poi.hssf.record.WriteProtectRecord;
import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalName;
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheetRange;
@ -2375,7 +2376,7 @@ public final class InternalWorkbook {
WriteAccessRecord waccess = getWriteAccess();
/* WriteProtectRecord wprotect =*/ getWriteProtect();
frec.setReadOnly((short)1);
frec.setPassword(FileSharingRecord.hashPassword(password));
frec.setPassword((short)CryptoFunctions.createXorVerifier1(password));
frec.setUsername(username);
waccess.setUsername(username);
}

View File

@ -52,25 +52,6 @@ public final class FileSharingRecord extends StandardRecord {
}
}
//this is the world's lamest "security". thanks to Wouter van Vugt for making me
//not have to try real hard. -ACO
public static short hashPassword(String password) {
byte[] passwordCharacters = password.getBytes();
int hash = 0;
if (passwordCharacters.length > 0) {
int charIndex = passwordCharacters.length;
while (charIndex-- > 0) {
hash = ((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff);
hash ^= passwordCharacters[charIndex];
}
// also hash with charcount
hash = ((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff);
hash ^= passwordCharacters.length;
hash ^= (0x8000 | ('N' << 8) | 'K');
}
return (short)hash;
}
/**
* set the readonly flag
*

View File

@ -1819,21 +1819,20 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* Aggregates the drawing records and dumps the escher record hierarchy
* to the standard output.
*/
public void dumpDrawingRecords(boolean fat) {
public void dumpDrawingRecords(boolean fat, PrintWriter pw) {
_sheet.aggregateDrawingRecords(_book.getDrawingManager(), false);
EscherAggregate r = (EscherAggregate) getSheet().findFirstRecordBySid(EscherAggregate.sid);
List<EscherRecord> escherRecords = r.getEscherRecords();
PrintWriter w = new PrintWriter(System.out);
for (Iterator<EscherRecord> iterator = escherRecords.iterator(); iterator.hasNext(); ) {
EscherRecord escherRecord = iterator.next();
if (fat) {
System.out.println(escherRecord.toString());
pw.println(escherRecord.toString());
} else {
escherRecord.display(w, 0);
escherRecord.display(pw, 0);
}
}
w.flush();
pw.flush();
}
/**
@ -2053,6 +2052,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
*
* @return the name of this sheet
*/
@SuppressWarnings("resource")
public String getSheetName() {
HSSFWorkbook wb = getWorkbook();
int idx = wb.getSheetIndex(this);

View File

@ -20,6 +20,9 @@ package org.apache.poi.util;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -30,23 +33,24 @@ import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
*/
public class DrawingDump
{
public static void main( String[] args ) throws IOException
{
NPOIFSFileSystem fs =
new NPOIFSFileSystem(new File(args[0]));
public static void main( String[] args ) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(System.out, Charset.defaultCharset());
PrintWriter pw = new PrintWriter(osw);
NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(args[0]));
HSSFWorkbook wb = new HSSFWorkbook(fs);
try {
System.out.println( "Drawing group:" );
pw.println( "Drawing group:" );
wb.dumpDrawingGroupRecords(true);
for (int sheetNum = 1; sheetNum <= wb.getNumberOfSheets(); sheetNum++)
{
System.out.println( "Sheet " + sheetNum + ":" );
pw.println( "Sheet " + sheetNum + ":" );
HSSFSheet sheet = wb.getSheetAt(sheetNum - 1);
sheet.dumpDrawingRecords(true);
sheet.dumpDrawingRecords(true, pw);
}
} finally {
wb.close();
fs.close();
}
}
}

View File

@ -47,6 +47,7 @@ import javax.xml.bind.DatatypeConverter;
import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.poifs.crypt.dsig.SignatureConfig;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@ -160,7 +161,7 @@ public class TSPTimeStampService implements TimeStampService {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOUtils.copy(huc.getInputStream(), bos);
LOG.log(POILogger.DEBUG, "response content: ", bos.toString());
LOG.log(POILogger.DEBUG, "response content: ", HexDump.dump(bos.toByteArray(), 0, 0));
if (!contentType.startsWith(signatureConfig.isTspOldProtocol()
? "application/timestamp-response"

View File

@ -40,6 +40,8 @@ public class TestEncryptionInfo {
assertEquals(32, info.getVerifier().getEncryptedVerifierHash().length);
assertEquals(CipherProvider.aes, info.getHeader().getCipherProvider());
assertEquals("Microsoft Enhanced RSA and AES Cryptographic Provider", info.getHeader().getCspName());
fs.close();
}
@Test
@ -57,5 +59,7 @@ public class TestEncryptionInfo {
assertEquals(64, info.getVerifier().getEncryptedVerifierHash().length);
assertEquals(CipherProvider.aes, info.getHeader().getCipherProvider());
// assertEquals("Microsoft Enhanced RSA and AES Cryptographic Provider", info.getHeader().getCspName());
fs.close();
}
}

View File

@ -17,7 +17,7 @@
package org.apache.poi.hdgf;
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
@ -34,6 +34,7 @@ import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LocaleUtil;
/**
* See
@ -80,7 +81,7 @@ public final class HDGFDiagram extends POIDocument {
dir.createDocumentInputStream("VisioDocument").read(_docstream);
// Check it's really visio
String typeString = new String(_docstream, 0, 20);
String typeString = new String(_docstream, 0, 20, LocaleUtil.CHARSET_1252 );
if(! typeString.equals(VISIO_HEADER)) {
throw new IllegalArgumentException("Wasn't a valid visio document, started with " + typeString);
}
@ -171,7 +172,9 @@ public final class HDGFDiagram extends POIDocument {
* For testing only
*/
public static void main(String args[]) throws Exception {
HDGFDiagram hdgf = new HDGFDiagram(new POIFSFileSystem(new FileInputStream(args[0])));
NPOIFSFileSystem pfs = new NPOIFSFileSystem(new File(args[0]));
HDGFDiagram hdgf = new HDGFDiagram(pfs);
hdgf.debug();
pfs.close();
}
}

View File

@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@ -65,43 +66,49 @@ public final class ChunkFactory {
*/
private void processChunkParseCommands() throws IOException {
String line;
InputStream cpd = ChunkFactory.class.getResourceAsStream(chunkTableName);
if(cpd == null) {
throw new IllegalStateException("Unable to find HDGF chunk definition on the classpath - " + chunkTableName);
InputStream cpd = null;
BufferedReader inp = null;
try {
cpd = ChunkFactory.class.getResourceAsStream(chunkTableName);
if(cpd == null) {
throw new IllegalStateException("Unable to find HDGF chunk definition on the classpath - " + chunkTableName);
}
inp = new BufferedReader(new InputStreamReader(cpd, LocaleUtil.CHARSET_1252));
while( (line = inp.readLine()) != null ) {
if(line.startsWith("#")) continue;
if(line.startsWith(" ")) continue;
if(line.startsWith("\t")) continue;
if(line.length() == 0) continue;
// Start xxx
if(!line.startsWith("start")) {
throw new IllegalStateException("Expecting start xxx, found " + line);
}
int chunkType = Integer.parseInt(line.substring(6));
ArrayList<CommandDefinition> defsL = new ArrayList<CommandDefinition>();
// Data entries
while( ! (line = inp.readLine()).startsWith("end") ) {
StringTokenizer st = new StringTokenizer(line, " ");
int defType = Integer.parseInt(st.nextToken());
int offset = Integer.parseInt(st.nextToken());
String name = st.nextToken("\uffff").substring(1);
CommandDefinition def = new CommandDefinition(defType,offset,name);
defsL.add(def);
}
CommandDefinition[] defs = defsL.toArray(new CommandDefinition[defsL.size()]);
// Add to the hashtable
chunkCommandDefinitions.put(Integer.valueOf(chunkType), defs);
}
} finally {
if (inp != null) inp.close();
if (cpd != null) cpd.close();
}
BufferedReader inp = new BufferedReader(new InputStreamReader(cpd));
while( (line = inp.readLine()) != null ) {
if(line.startsWith("#")) continue;
if(line.startsWith(" ")) continue;
if(line.startsWith("\t")) continue;
if(line.length() == 0) continue;
// Start xxx
if(!line.startsWith("start")) {
throw new IllegalStateException("Expecting start xxx, found " + line);
}
int chunkType = Integer.parseInt(line.substring(6));
ArrayList<CommandDefinition> defsL = new ArrayList<CommandDefinition>();
// Data entries
while( ! (line = inp.readLine()).startsWith("end") ) {
StringTokenizer st = new StringTokenizer(line, " ");
int defType = Integer.parseInt(st.nextToken());
int offset = Integer.parseInt(st.nextToken());
String name = st.nextToken("\uffff").substring(1);
CommandDefinition def = new CommandDefinition(defType,offset,name);
defsL.add(def);
}
CommandDefinition[] defs = defsL.toArray(new CommandDefinition[defsL.size()]);
// Add to the hashtable
chunkCommandDefinitions.put(Integer.valueOf(chunkType), defs);
}
inp.close();
cpd.close();
}
public int getVersion() { return version; }

View File

@ -25,6 +25,7 @@ import org.apache.poi.hpbf.model.qcbits.QCTextBit;
import org.apache.poi.hpbf.model.qcbits.UnknownQCBit;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LocaleUtil;
/**
* Quill -> QuillSub -> CONTENTS
@ -40,7 +41,7 @@ public final class QuillContents extends HPBFPart {
// all our bits
// Check first 8 bytes
String f8 = new String(data, 0, 8);
String f8 = new String(data, 0, 8, LocaleUtil.CHARSET_1252);
if(! f8.equals("CHNKINK ")) {
throw new IllegalArgumentException("Expecting 'CHNKINK ' but was '"+f8+"'");
}
@ -52,11 +53,11 @@ public final class QuillContents extends HPBFPart {
int offset = 0x20 + i*24;
if(data[offset] == 0x18 && data[offset+1] == 0x00) {
// Has some data
String thingType = new String(data, offset+2, 4);
String thingType = new String(data, offset+2, 4, LocaleUtil.CHARSET_1252);
int optA = LittleEndian.getUShort(data, offset+6);
int optB = LittleEndian.getUShort(data, offset+8);
int optC = LittleEndian.getUShort(data, offset+10);
String bitType = new String(data, offset+12, 4);
String bitType = new String(data, offset+12, 4, LocaleUtil.CHARSET_1252);
int from = (int)LittleEndian.getUInt(data, offset+16);
int len = (int)LittleEndian.getUInt(data, offset+20);

View File

@ -19,10 +19,12 @@ package org.apache.poi.hslf.dev;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import org.apache.poi.hslf.record.RecordTypes;
import org.apache.poi.poifs.filesystem.DocumentEntry;
@ -49,21 +51,26 @@ public final class PPTXMLDump {
protected boolean hexHeader = true;
public PPTXMLDump(File ppt) throws IOException {
NPOIFSFileSystem fs = new NPOIFSFileSystem(ppt);
//read the document entry from OLE file system
DocumentEntry entry = (DocumentEntry)fs.getRoot().getEntry(PPDOC_ENTRY);
docstream = new byte[entry.getSize()];
DocumentInputStream is = fs.createDocumentInputStream(PPDOC_ENTRY);
is.read(docstream);
NPOIFSFileSystem fs = new NPOIFSFileSystem(ppt, true);
DocumentInputStream is = null;
try {
//read the document entry from OLE file system
DocumentEntry entry = (DocumentEntry)fs.getRoot().getEntry(PPDOC_ENTRY);
docstream = new byte[entry.getSize()];
is = fs.createDocumentInputStream(PPDOC_ENTRY);
is.read(docstream);
is.close();
entry = (DocumentEntry)fs.getRoot().getEntry(PICTURES_ENTRY);
pictstream = new byte[entry.getSize()];
is = fs.createDocumentInputStream(PICTURES_ENTRY);
is.read(pictstream);
} catch(FileNotFoundException e){
//silently catch errors if the presentation does not contain pictures
} finally {
if (is != null) is.close();
fs.close();
}
}
@ -72,8 +79,8 @@ public final class PPTXMLDump {
* @param out <code>Writer</code> to write out
* @throws java.io.IOException
*/
public void dump(Writer out) throws IOException {
this.out = out;
public void dump(Writer outWriter) throws IOException {
this.out = outWriter;
int padding = 0;
write(out, "<Presentation>" + CR, padding);
@ -197,7 +204,8 @@ public final class PPTXMLDump {
System.out.println("Dumping " + args[i]);
if (outFile){
FileWriter out = new FileWriter(ppt.getName() + ".xml");
FileOutputStream fos = new FileOutputStream(ppt.getName() + ".xml");
OutputStreamWriter out = new OutputStreamWriter(fos, Charset.forName("UTF8"));
dump.dump(out);
out.close();
} else {

View File

@ -1244,7 +1244,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFShape,HSLFText
wrapper.appendChildRecord(tha);
TextBytesAtom tba = new TextBytesAtom();
tba.setText("".getBytes());
tba.setText("".getBytes(LocaleUtil.CHARSET_1252));
wrapper.appendChildRecord(tba);
StyleTextPropAtom sta = new StyleTextPropAtom(1);

View File

@ -17,7 +17,6 @@
package org.apache.poi.hssf.converter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
@ -77,38 +76,27 @@ public class ExcelToFoConverter extends AbstractExcelConverter
* Where infile is an input .xls file ( Word 97-2007) which will be rendered
* as XSL FO into outfile
*/
public static void main( String[] args )
{
if ( args.length < 2 )
{
System.err
.println( "Usage: ExcelToFoConverter <inputFile.xls> <saveTo.xml>" );
public static void main( String[] args ) throws Exception {
if ( args.length < 2 ) {
System.err.println( "Usage: ExcelToFoConverter <inputFile.xls> <saveTo.xml>" );
return;
}
System.out.println( "Converting " + args[0] );
System.out.println( "Saving output to " + args[1] );
try
{
Document doc = ExcelToHtmlConverter.process( new File( args[0] ) );
FileWriter out = new FileWriter( args[1] );
DOMSource domSource = new DOMSource( doc );
StreamResult streamResult = new StreamResult( out );
Document doc = ExcelToHtmlConverter.process( new File( args[0] ) );
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
// TODO set encoding from a command argument
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
serializer.setOutputProperty( OutputKeys.INDENT, "no" );
serializer.setOutputProperty( OutputKeys.METHOD, "xml" );
serializer.transform( domSource, streamResult );
out.close();
}
catch ( Exception e )
{
e.printStackTrace();
}
DOMSource domSource = new DOMSource( doc );
StreamResult streamResult = new StreamResult( new File(args[1]) );
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
// TODO set encoding from a command argument
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
serializer.setOutputProperty( OutputKeys.INDENT, "no" );
serializer.setOutputProperty( OutputKeys.METHOD, "xml" );
serializer.transform( domSource, streamResult );
}
/**
@ -125,7 +113,9 @@ public class ExcelToFoConverter extends AbstractExcelConverter
XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
.newDocument() );
excelToHtmlConverter.processWorkbook( workbook );
return excelToHtmlConverter.getDocument();
Document doc = excelToHtmlConverter.getDocument();
workbook.close();
return doc;
}
private final FoDocumentFacade foDocumentFacade;

View File

@ -495,8 +495,7 @@ public abstract class AbstractWordConverter
}
String text = characterRun.text();
if ( text.getBytes().length == 0 )
continue;
if ( text.isEmpty() ) continue;
if ( characterRun.isSpecialCharacter() )
{
@ -530,7 +529,7 @@ public abstract class AbstractWordConverter
}
}
if ( text.getBytes()[0] == FIELD_BEGIN_MARK )
if ( text.charAt(0) == FIELD_BEGIN_MARK )
{
if ( wordDocument instanceof HWPFDocument )
{
@ -566,12 +565,12 @@ public abstract class AbstractWordConverter
continue;
}
if ( text.getBytes()[0] == FIELD_SEPARATOR_MARK )
if ( text.charAt(0) == FIELD_SEPARATOR_MARK )
{
// shall not appear without FIELD_BEGIN_MARK
continue;
}
if ( text.getBytes()[0] == FIELD_END_MARK )
if ( text.charAt(0) == FIELD_END_MARK )
{
// shall not appear without FIELD_BEGIN_MARK
continue;
@ -1168,10 +1167,9 @@ public abstract class AbstractWordConverter
CharacterRun characterRun = range.getCharacterRun( c );
String text = characterRun.text();
if ( text.getBytes().length == 0 )
continue;
if ( text.isEmpty() ) continue;
final byte firstByte = text.getBytes()[0];
final char firstByte = text.charAt(0);
if ( firstByte == FIELD_BEGIN_MARK )
{
int[] nested = tryDeadField_lookupFieldSeparatorEnd(
@ -1195,7 +1193,7 @@ public abstract class AbstractWordConverter
continue;
}
if ( text.getBytes()[0] == FIELD_END_MARK )
if ( firstByte == FIELD_END_MARK )
{
if ( endMark != -1 )
{

View File

@ -17,7 +17,6 @@
package org.apache.poi.hwpf.converter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
@ -73,36 +72,25 @@ public class WordToFoConverter extends AbstractWordConverter
* Where infile is an input .doc file ( Word 97-2007) which will be rendered
* as XSL-FO into outfile
*/
public static void main( String[] args )
{
public static void main( String[] args ) throws Exception {
if ( args.length < 2 )
{
System.err
.println( "Usage: WordToFoConverter <inputFile.doc> <saveTo.fo>" );
System.err.println( "Usage: WordToFoConverter <inputFile.doc> <saveTo.fo>" );
return;
}
System.out.println( "Converting " + args[0] );
System.out.println( "Saving output to " + args[1] );
try
{
Document doc = WordToFoConverter.process( new File( args[0] ) );
Document doc = WordToFoConverter.process( new File( args[0] ) );
FileWriter out = new FileWriter( args[1] );
DOMSource domSource = new DOMSource( doc );
StreamResult streamResult = new StreamResult( out );
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
// TODO set encoding from a command argument
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
serializer.setOutputProperty( OutputKeys.INDENT, "yes" );
serializer.transform( domSource, streamResult );
out.close();
}
catch ( Exception e )
{
e.printStackTrace();
}
DOMSource domSource = new DOMSource( doc );
StreamResult streamResult = new StreamResult( new File( args[1] ) );
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
// TODO set encoding from a command argument
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
serializer.setOutputProperty( OutputKeys.INDENT, "yes" );
serializer.transform( domSource, streamResult );
}
static Document process( File docFile ) throws Exception

View File

@ -19,7 +19,6 @@ package org.apache.poi.hwpf.converter;
import static org.apache.poi.hwpf.converter.AbstractWordUtils.TWIPS_PER_INCH;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.Stack;
@ -119,7 +118,7 @@ public class WordToHtmlConverter extends AbstractWordConverter
* Where infile is an input .doc file ( Word 95-2007) which will be rendered
* as HTML into outfile
*/
public static void main( String[] args )
public static void main( String[] args ) throws Exception
{
if ( args.length < 2 )
{
@ -130,27 +129,19 @@ public class WordToHtmlConverter extends AbstractWordConverter
System.out.println( "Converting " + args[0] );
System.out.println( "Saving output to " + args[1] );
try
{
Document doc = WordToHtmlConverter.process( new File( args[0] ) );
FileWriter out = new FileWriter( args[1] );
DOMSource domSource = new DOMSource( doc );
StreamResult streamResult = new StreamResult( out );
Document doc = WordToHtmlConverter.process( new File( args[0] ) );
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
// TODO set encoding from a command argument
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
serializer.setOutputProperty( OutputKeys.INDENT, "yes" );
serializer.setOutputProperty( OutputKeys.METHOD, "html" );
serializer.transform( domSource, streamResult );
out.close();
}
catch ( Exception e )
{
e.printStackTrace();
}
DOMSource domSource = new DOMSource( doc );
StreamResult streamResult = new StreamResult( new File(args[1]) );
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
// TODO set encoding from a command argument
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
serializer.setOutputProperty( OutputKeys.INDENT, "yes" );
serializer.setOutputProperty( OutputKeys.METHOD, "html" );
serializer.transform( domSource, streamResult );
}
static Document process( File docFile ) throws Exception

View File

@ -17,7 +17,6 @@
package org.apache.poi.hwpf.converter;
import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.List;
@ -91,38 +90,28 @@ public class WordToTextConverter extends AbstractWordConverter
* Where infile is an input .doc file ( Word 95-2007) which will be rendered
* as plain text into outfile
*/
public static void main( String[] args )
{
public static void main( String[] args ) throws Exception {
if ( args.length < 2 )
{
System.err
.println( "Usage: WordToTextConverter <inputFile.doc> <saveTo.txt>" );
System.err.println( "Usage: WordToTextConverter <inputFile.doc> <saveTo.txt>" );
return;
}
System.out.println( "Converting " + args[0] );
System.out.println( "Saving output to " + args[1] );
try
{
Document doc = WordToTextConverter.process( new File( args[0] ) );
FileWriter out = new FileWriter( args[1] );
DOMSource domSource = new DOMSource( doc );
StreamResult streamResult = new StreamResult( out );
Document doc = WordToTextConverter.process( new File( args[0] ) );
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
// TODO set encoding from a command argument
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
serializer.setOutputProperty( OutputKeys.INDENT, "no" );
serializer.setOutputProperty( OutputKeys.METHOD, "text" );
serializer.transform( domSource, streamResult );
out.close();
}
catch ( Exception e )
{
e.printStackTrace();
}
DOMSource domSource = new DOMSource( doc );
StreamResult streamResult = new StreamResult( new File( args[1] ) );
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
// TODO set encoding from a command argument
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
serializer.setOutputProperty( OutputKeys.INDENT, "no" );
serializer.setOutputProperty( OutputKeys.METHOD, "text" );
serializer.transform( domSource, streamResult );
}
static Document process( File docFile ) throws Exception

View File

@ -17,9 +17,14 @@
package org.apache.poi.hdgf.chunks;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public final class TestChunks extends TestCase {
import org.junit.Test;
public final class TestChunks {
public static final byte[] data_a = new byte[] { 70, 0, 0, 0,
-1, -1, -1, -1, 2, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0,
0, 0, 2, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@ -82,9 +87,9 @@ public static final byte[] data_b = new byte[] { 70, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0
};
@Test
public void testChunkHeaderA() throws Exception {
ChunkFactory cf = new ChunkFactory(11);
ChunkHeader h =
ChunkHeader.createChunkHeader(11, data_a, 0);
@ -101,8 +106,9 @@ public static final byte[] data_b = new byte[] { 70, 0, 0, 0,
assertTrue(header.hasTrailer());
assertTrue(header.hasSeparator());
}
public void testChunkHeaderB() throws Exception {
ChunkFactory cf = new ChunkFactory(11);
@Test
public void testChunkHeaderB() throws Exception {
ChunkHeader h =
ChunkHeader.createChunkHeader(11, data_b, 0);
@ -120,7 +126,8 @@ public static final byte[] data_b = new byte[] { 70, 0, 0, 0,
assertTrue(header.hasSeparator());
}
public void testOneChunk() throws Exception {
@Test
public void testOneChunk() throws Exception {
ChunkFactory cf = new ChunkFactory(11);
cf.createChunk(data_a, 0);
cf.createChunk(data_b, 0);
@ -152,7 +159,8 @@ public static final byte[] data_b = new byte[] { 70, 0, 0, 0,
assertEquals("0", chunk.commandDefinitions[1].getName());
}
public void testAnotherChunk() throws Exception {
@Test
public void testAnotherChunk() throws Exception {
ChunkFactory cf = new ChunkFactory(11);
// Go for the 2nd chunk in the stream
@ -187,7 +195,8 @@ public static final byte[] data_b = new byte[] { 70, 0, 0, 0,
assertEquals("0", chunk.commandDefinitions[1].getName());
}
public void testManyChunks() throws Exception {
@Test
public void testManyChunks() throws Exception {
ChunkFactory cf = new ChunkFactory(11);
Chunk chunk;
int offset = 0;