]> source.dussan.org Git - poi.git/commitdiff
Bug 58326 - Forbidden APIs patches - second set of changes for charset settings
authorAndreas Beeker <kiwiwings@apache.org>
Tue, 8 Sep 2015 00:04:12 +0000 (00:04 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Tue, 8 Sep 2015 00:04:12 +0000 (00:04 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1701713 13f79535-47bb-0310-9956-ffa450edef68

17 files changed:
src/java/org/apache/poi/hssf/model/InternalWorkbook.java
src/java/org/apache/poi/hssf/record/FileSharingRecord.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/java/org/apache/poi/util/DrawingDump.java
src/ooxml/java/org/apache/poi/poifs/crypt/dsig/services/TSPTimeStampService.java
src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptionInfo.java
src/scratchpad/src/org/apache/poi/hdgf/HDGFDiagram.java
src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkFactory.java
src/scratchpad/src/org/apache/poi/hpbf/model/QuillContents.java
src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java
src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java
src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java
src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java
src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java
src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java
src/scratchpad/testcases/org/apache/poi/hdgf/chunks/TestChunks.java

index 7994f2820218cc5650060ed1199bf6faf268a5ab..c92c897094a6bb3d4686571bd13e4b27ab1aa61a 100644 (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);
     }
index d53911dde5c5784bde1ee62f7de55e39deefed46..a82b4b65bfd55f904b83854b62f84f87aee629cb 100644 (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
      *
index 7d4ec2fb3ebb5d471f5d68c80c4c708698258568..f1412af42b646cd22e101c5189d1f55c48ecefd3 100644 (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);
index 4eb6a2aff2ec8a730b8d915b609225c9bf20a44e..2f7e324d3b0ad1d1d7c755bbc10ca9cd63665e4c 100644 (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();
         }
     }
 }
index 99a0e29982b835ebdd61ec6d584f0d83311e9f13..06054c0576857b4172c3f8b4e675af19b56a6588 100644 (file)
@@ -47,6 +47,7 @@ import javax.xml.bind.DatatypeConverter;
 import org.apache.poi.poifs.crypt.CryptoFunctions;\r
 import org.apache.poi.poifs.crypt.HashAlgorithm;\r
 import org.apache.poi.poifs.crypt.dsig.SignatureConfig;\r
+import org.apache.poi.util.HexDump;\r
 import org.apache.poi.util.IOUtils;\r
 import org.apache.poi.util.POILogFactory;\r
 import org.apache.poi.util.POILogger;\r
@@ -160,7 +161,7 @@ public class TSPTimeStampService implements TimeStampService {
         \r
         ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
         IOUtils.copy(huc.getInputStream(), bos);\r
-        LOG.log(POILogger.DEBUG, "response content: ", bos.toString());\r
+        LOG.log(POILogger.DEBUG, "response content: ", HexDump.dump(bos.toByteArray(), 0, 0));\r
         \r
         if (!contentType.startsWith(signatureConfig.isTspOldProtocol() \r
             ? "application/timestamp-response"\r
index 698adb86d93a47b312c0ddafb13e822acced544d..322733442936d5b2ae5d0e1ae7ef2295fe8cd81d 100644 (file)
@@ -40,6 +40,8 @@ public class TestEncryptionInfo {
         assertEquals(32, info.getVerifier().getEncryptedVerifierHash().length);\r
         assertEquals(CipherProvider.aes, info.getHeader().getCipherProvider());                \r
         assertEquals("Microsoft Enhanced RSA and AES Cryptographic Provider", info.getHeader().getCspName());\r
+        \r
+        fs.close();\r
     }\r
     \r
     @Test\r
@@ -57,5 +59,7 @@ public class TestEncryptionInfo {
         assertEquals(64, info.getVerifier().getEncryptedVerifierHash().length);\r
         assertEquals(CipherProvider.aes, info.getHeader().getCipherProvider());                \r
 //        assertEquals("Microsoft Enhanced RSA and AES Cryptographic Provider", info.getHeader().getCspName());\r
+        \r
+        fs.close();\r
     }\r
 }\r
index b8c918ed7908838e622a93c2ab1f4d7cb5871c3d..405bf69c4bb027603603ea14dffc92fe3e631a1d 100644 (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();
        }
 }
index 3c490a24d02cb67298351863ae7d872f6918df59..8bd8f5c20afad265a213d3d4d8fdad92d117a936 100644 (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; }
index 87384fb1a2e94e405cb05ad04c9de986c26de281..3494d9819d55cffcdc8e29d456cb88ea0ba6b6df 100644 (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);
 
index dad74ffa6e076ebe8294e2f477b684120629ba7c..5d9c231f4950e936f1fa0872eaf010de6750af1d 100644 (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 {
index 9165bc1f3cf65a9d7c90879f531c53ea4885c402..64308e8086331669715c5d0c5697dc4d466330d3 100644 (file)
@@ -1244,7 +1244,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFShape,HSLFText
         wrapper.appendChildRecord(tha);\r
 \r
         TextBytesAtom tba = new TextBytesAtom();\r
-        tba.setText("".getBytes());\r
+        tba.setText("".getBytes(LocaleUtil.CHARSET_1252));\r
         wrapper.appendChildRecord(tba);\r
 \r
         StyleTextPropAtom sta = new StyleTextPropAtom(1);\r
index ac2f7316ba880a240820a7259dcb8c92a336c92f..df1922e0c0f79c0e6ce67b0f3a5f1cae93d08a3b 100644 (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 );
-
-            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();
-        }
+
+        Document doc = ExcelToHtmlConverter.process( new File( args[0] ) );
+
+        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;
index 29875d67d7f4751d21c32ed65e700bc07a424904..e8a3dfcf7a1a4bfe9fe3e2228fee62cbd0176791 100644 (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 )
                 {
index 6da6d141a062859dabaa5bbd0444d3ff25ed3a23..3386f4e25becedf205827b891359bba2ebdb4884 100644 (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] ) );
-
-            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();
-        }
+        Document doc = WordToFoConverter.process( new File( args[0] ) );
+
+        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
index 520d22004e3eb22ee317c8d0264e0a11f3fec745..582e4e0643fdb8710f17fc014b1c41e357102bf2 100644 (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 );
-
-            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();
-        }
+
+        Document doc = WordToHtmlConverter.process( new File( args[0] ) );
+
+        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
index 50d00b735bb3fa5e04858dead20f2a6261caaf9c..ba88a905a84578296508a4feb06c20b2c1b7f1c6 100644 (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 );
-
-            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();
-        }
+
+        Document doc = WordToTextConverter.process( new File( args[0] ) );
+
+        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
index f4107c8e3a5d3105bd348cd01b1ffdeb61220716..2cbbbecbdf8032a6268ddd4a8aeecda4a3bfffd3 100644 (file)
 
 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;