From 9e530bcf9668784e6856352ea0b34b69587cf4f3 Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Wed, 26 Oct 2011 13:50:07 +0000 Subject: [PATCH] add TOC support git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1189217 13f79535-47bb-0310-9956-ffa450edef68 --- .../hwpf/converter/AbstractWordConverter.java | 63 ++++++++++--------- 1 file changed, 34 insertions(+), 29 deletions(-) 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 af4c0be1f..d8331eb82 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java @@ -58,6 +58,21 @@ import org.w3c.dom.Element; @Beta public abstract class AbstractWordConverter { + private static class DeadFieldBoundaries + { + final int beginMark; + final int endMark; + final int separatorMark; + + public DeadFieldBoundaries( int beginMark, int separatorMark, + int endMark ) + { + this.beginMark = beginMark; + this.separatorMark = separatorMark; + this.endMark = endMark; + } + } + private static final class Structure implements Comparable { final int end; @@ -71,6 +86,13 @@ public abstract class AbstractWordConverter this.structure = bookmark; } + Structure( DeadFieldBoundaries deadFieldBoundaries, int start, int end ) + { + this.start = start; + this.end = end; + this.structure = deadFieldBoundaries; + } + Structure( Field field ) { this.start = field.getFieldStartOffset(); @@ -78,13 +100,6 @@ public abstract class AbstractWordConverter this.structure = field; } - Structure( int start, int end ) - { - this.start = start; - this.end = end; - this.structure = null; - } - public int compareTo( Structure o ) { return start < o.start ? -1 : start == o.start ? 0 : 1; @@ -334,9 +349,11 @@ public abstract class AbstractWordConverter wordDocument, range, c ); if ( separatorEnd != null ) { - addToStructures( structures, new Structure( - characterRun.getStartOffset(), - separatorEnd[1] + 1 ) ); + addToStructures( structures, + new Structure( new DeadFieldBoundaries( c, + separatorEnd[0], separatorEnd[1] ), + characterRun.getStartOffset(), + separatorEnd[1] + 1 ) ); c = separatorEnd[1]; } } @@ -407,6 +424,13 @@ public abstract class AbstractWordConverter processField( (HWPFDocument) wordDocument, range, currentTableLevel, field, block ); } + else if ( structure.structure instanceof DeadFieldBoundaries ) + { + DeadFieldBoundaries boundaries = (DeadFieldBoundaries) structure.structure; + processDeadField( wordDocument, block, range, + currentTableLevel, boundaries.beginMark, + boundaries.separatorMark, boundaries.endMark ); + } else { throw new UnsupportedOperationException( "NYI: " @@ -660,25 +684,6 @@ public abstract class AbstractWordConverter return; } - protected Field processDeadField( HWPFDocumentCore wordDocument, - Range charactersRange, int currentTableLevel, int startOffset, - Element currentBlock ) - { - if ( !( wordDocument instanceof HWPFDocument ) ) - return null; - - HWPFDocument hwpfDocument = (HWPFDocument) wordDocument; - Field field = hwpfDocument.getFields().getFieldByStartOffset( - FieldsDocumentPart.MAIN, startOffset ); - if ( field == null ) - return null; - - processField( hwpfDocument, charactersRange, currentTableLevel, field, - currentBlock ); - - return field; - } - public void processDocument( HWPFDocumentCore wordDocument ) { try