From 50107cae1611163a6e15b9753c01f1f0653779c5 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Tue, 8 Sep 2015 00:04:12 +0000 Subject: [PATCH] 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 --- .../poi/hssf/model/InternalWorkbook.java | 3 +- .../poi/hssf/record/FileSharingRecord.java | 19 ----- .../apache/poi/hssf/usermodel/HSSFSheet.java | 10 +-- src/java/org/apache/poi/util/DrawingDump.java | 18 +++-- .../dsig/services/TSPTimeStampService.java | 3 +- .../poi/poifs/crypt/TestEncryptionInfo.java | 4 + .../src/org/apache/poi/hdgf/HDGFDiagram.java | 9 ++- .../apache/poi/hdgf/chunks/ChunkFactory.java | 79 ++++++++++--------- .../apache/poi/hpbf/model/QuillContents.java | 7 +- .../org/apache/poi/hslf/dev/PPTXMLDump.java | 32 +++++--- .../poi/hslf/usermodel/HSLFTextParagraph.java | 2 +- .../hssf/converter/ExcelToFoConverter.java | 44 ++++------- .../hwpf/converter/AbstractWordConverter.java | 16 ++-- .../poi/hwpf/converter/WordToFoConverter.java | 34 +++----- .../hwpf/converter/WordToHtmlConverter.java | 33 +++----- .../hwpf/converter/WordToTextConverter.java | 37 +++------ .../apache/poi/hdgf/chunks/TestChunks.java | 27 ++++--- 17 files changed, 176 insertions(+), 201 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index 7994f2820..c92c89709 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -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); } diff --git a/src/java/org/apache/poi/hssf/record/FileSharingRecord.java b/src/java/org/apache/poi/hssf/record/FileSharingRecord.java index d53911dde..a82b4b65b 100644 --- a/src/java/org/apache/poi/hssf/record/FileSharingRecord.java +++ b/src/java/org/apache/poi/hssf/record/FileSharingRecord.java @@ -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 * diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 7d4ec2fb3..f1412af42 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -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 escherRecords = r.getEscherRecords(); - PrintWriter w = new PrintWriter(System.out); for (Iterator 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); diff --git a/src/java/org/apache/poi/util/DrawingDump.java b/src/java/org/apache/poi/util/DrawingDump.java index 4eb6a2aff..2f7e324d3 100644 --- a/src/java/org/apache/poi/util/DrawingDump.java +++ b/src/java/org/apache/poi/util/DrawingDump.java @@ -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(); } } } diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/services/TSPTimeStampService.java b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/services/TSPTimeStampService.java index 99a0e2998..06054c057 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/services/TSPTimeStampService.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/services/TSPTimeStampService.java @@ -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" diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptionInfo.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptionInfo.java index 698adb86d..322733442 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptionInfo.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptionInfo.java @@ -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(); } } diff --git a/src/scratchpad/src/org/apache/poi/hdgf/HDGFDiagram.java b/src/scratchpad/src/org/apache/poi/hdgf/HDGFDiagram.java index b8c918ed7..405bf69c4 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/HDGFDiagram.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/HDGFDiagram.java @@ -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(); } } diff --git a/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkFactory.java b/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkFactory.java index 3c490a24d..8bd8f5c20 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkFactory.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkFactory.java @@ -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 defsL = new ArrayList(); + + // 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 defsL = new ArrayList(); - - // 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; } diff --git a/src/scratchpad/src/org/apache/poi/hpbf/model/QuillContents.java b/src/scratchpad/src/org/apache/poi/hpbf/model/QuillContents.java index 87384fb1a..3494d9819 100644 --- a/src/scratchpad/src/org/apache/poi/hpbf/model/QuillContents.java +++ b/src/scratchpad/src/org/apache/poi/hpbf/model/QuillContents.java @@ -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); diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java b/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java index dad74ffa6..5d9c231f4 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java @@ -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 Writer 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, "" + 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 { diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java index 9165bc1f3..64308e808 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java @@ -1244,7 +1244,7 @@ public final class HSLFTextParagraph implements TextParagraph " ); + public static void main( String[] args ) throws Exception { + if ( args.length < 2 ) { + System.err.println( "Usage: ExcelToFoConverter " ); 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; diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java index 29875d67d..e8a3dfcf7 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java @@ -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 ) { diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java index 6da6d141a..3386f4e25 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java @@ -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 " ); + System.err.println( "Usage: WordToFoConverter " ); 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 diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java index 520d22004..582e4e064 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java @@ -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 diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java index 50d00b735..ba88a905a 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java @@ -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 " ); + System.err.println( "Usage: WordToTextConverter " ); 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 diff --git a/src/scratchpad/testcases/org/apache/poi/hdgf/chunks/TestChunks.java b/src/scratchpad/testcases/org/apache/poi/hdgf/chunks/TestChunks.java index f4107c8e3..2cbbbecbd 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdgf/chunks/TestChunks.java +++ b/src/scratchpad/testcases/org/apache/poi/hdgf/chunks/TestChunks.java @@ -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;