From c447e9e00d29c84f9c39966159d8c4b0744375f0 Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Tue, 5 Jul 2011 00:48:53 +0000 Subject: [PATCH] add additional options and features to HWPFLister, enabling PAPX and paragraphs (including SPRMs) information dump git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1142861 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hwpf/dev/HWPFLister.java | 141 ++++++++++++++---- 1 file changed, 115 insertions(+), 26 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java b/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java index d435ebff08..fddecf0b7d 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java @@ -18,36 +18,125 @@ package org.apache.poi.hwpf.dev; import java.io.FileInputStream; +import java.util.Arrays; import org.apache.poi.hwpf.HWPFDocument; +import org.apache.poi.hwpf.HWPFDocumentCore; import org.apache.poi.hwpf.model.FileInformationBlock; +import org.apache.poi.hwpf.model.PAPX; +import org.apache.poi.hwpf.sprm.SprmIterator; +import org.apache.poi.hwpf.sprm.SprmOperation; +import org.apache.poi.hwpf.usermodel.Paragraph; +import org.apache.poi.hwpf.usermodel.Range; /** - * Used by developers to list out key information on a - * HWPF file. End users will probably never need to - * use this program. + * Used by developers to list out key information on a HWPF file. End users will + * probably never need to use this program. + * + * @author Nick Burch (nick at torchbox dot com) + * @author Sergey Vladimirov (vlsergey at gmail dot com) */ -public final class HWPFLister { - private final HWPFDocument _doc; - public HWPFLister(HWPFDocument doc) { - _doc = doc; - } - - public static void main(String[] args) throws Exception { - if(args.length == 0) { - System.err.println("Use:"); - System.err.println(" HWPFLister "); - System.exit(1); - } - - HWPFLister l = new HWPFLister( - new HWPFDocument(new FileInputStream(args[0])) - ); - l.dumpFIB(); - } - - public void dumpFIB() { - FileInformationBlock fib = _doc.getFileInformationBlock(); - System.out.println(fib.toString()); - } +public final class HWPFLister +{ + public static void main( String[] args ) throws Exception + { + if ( args.length == 0 ) + { + System.err.println( "Use:" ); + System.err.println( " HWPFLister " + + "[--papx] [--papxProperties] " + + "[--paragraphs] [--paragraphsSprms] [--paragraphsText]" ); + System.exit( 1 ); + } + + boolean outputParagraphs = false; + boolean outputParagraphsSprms = false; + boolean outputParagraphsText = false; + + boolean outputPapx = false; + boolean outputPapxProperties = false; + + for ( String arg : Arrays.asList( args ).subList( 1, args.length ) ) + { + if ( "--paragraphs".equals( arg ) ) + outputParagraphs = true; + if ( "--paragraphsSprms".equals( arg ) ) + outputParagraphsSprms = true; + if ( "--paragraphsText".equals( arg ) ) + outputParagraphsText = true; + + if ( "--papx".equals( arg ) ) + outputPapx = true; + if ( "--papxProperties".equals( arg ) ) + outputPapxProperties = true; + } + + HWPFLister lister = new HWPFLister( new HWPFDocument( + new FileInputStream( args[0] ) ) ); + lister.dumpFIB(); + + if ( outputParagraphs ) + { + System.out.println( "== Paragraphs ==" ); + lister.dumpParagraphs( outputParagraphsSprms, outputPapx, + outputParagraphsText ); + } + + if ( !outputParagraphs && outputPapx ) + { + System.out.println( "== PAPX ==" ); + lister.dumpPapx( outputPapxProperties ); + } + } + + private final HWPFDocumentCore _doc; + + public HWPFLister( HWPFDocumentCore doc ) + { + _doc = doc; + } + + public void dumpFIB() + { + FileInformationBlock fib = _doc.getFileInformationBlock(); + System.out.println( fib ); + } + + public void dumpPapx( boolean withProperties ) + { + for ( PAPX papx : _doc.getParagraphTable().getParagraphs() ) + { + System.out.println( papx ); + + if ( withProperties ) + System.out.println( papx.getParagraphProperties( _doc + .getStyleSheet() ) ); + } + } + + public void dumpParagraphs( boolean withSprms, boolean withPapx, + boolean withText ) + { + Range range = _doc.getRange(); + for ( int p = 0; p < range.numParagraphs(); p++ ) + { + Paragraph paragraph = range.getParagraph( p ); + System.out.println( paragraph.toString( withPapx ) ); + + if ( withSprms ) + { + PAPX papx = _doc.getParagraphTable().getParagraphs().get( p ); + + SprmIterator sprmIt = new SprmIterator( papx.getGrpprl(), 2 ); + while ( sprmIt.hasNext() ) + { + SprmOperation sprm = sprmIt.next(); + System.out.println( "\t" + sprm.toString() ); + } + } + + if ( withText ) + System.out.println( paragraph.text() ); + } + } } -- 2.39.5