add page breaks support; fix paragraphs-in-bookmarks handling

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1160288 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-08-22 15:01:49 +00:00
parent ca2e6c13df
commit 39d5fc7ceb
7 changed files with 99 additions and 16 deletions

View File

@ -342,8 +342,8 @@ public abstract class AbstractWordConverter
bookmarkStack.addAll( bookmarks ); bookmarkStack.addAll( bookmarks );
try try
{ {
Range subrange = new Range( structure.start, structure.end, int end = Math.min( range.getEndOffset(), structure.end );
range ) Range subrange = new Range( structure.start, end, range )
{ {
@Override @Override
public String toString() public String toString()
@ -372,7 +372,7 @@ public abstract class AbstractWordConverter
+ structure.structure.getClass() ); + structure.structure.getClass() );
} }
previous = structure.end; previous = Math.min( range.getEndOffset(), structure.end );
} }
if ( previous != range.getStartOffset() ) if ( previous != range.getStartOffset() )
@ -865,6 +865,9 @@ public abstract class AbstractWordConverter
return false; return false;
} }
protected abstract void processPageBreak( HWPFDocumentCore wordDocument,
Element flow );
protected abstract void processPageref( HWPFDocumentCore wordDocument, protected abstract void processPageref( HWPFDocumentCore wordDocument,
Element currentBlock, Range textRange, int currentTableLevel, Element currentBlock, Range textRange, int currentTableLevel,
String pageref ); String pageref );
@ -903,6 +906,11 @@ public abstract class AbstractWordConverter
continue; continue;
} }
if ( paragraph.text().equals( "\u000c" ) )
{
processPageBreak( wordDocument, flow );
}
if ( paragraph.getIlfo() != currentListInfo ) if ( paragraph.getIlfo() != currentListInfo )
{ {
currentListInfo = paragraph.getIlfo(); currentListInfo = paragraph.getIlfo();

View File

@ -130,7 +130,7 @@ public class AbstractWordUtils
return true; return true;
} }
static void compactChildNodes( Element parentElement, String childTagName ) static void compactChildNodesR( Element parentElement, String childTagName )
{ {
NodeList childNodes = parentElement.getChildNodes(); NodeList childNodes = parentElement.getChildNodes();
for ( int i = 0; i < childNodes.getLength() - 1; i++ ) for ( int i = 0; i < childNodes.getLength() - 1; i++ )
@ -146,6 +146,16 @@ public class AbstractWordUtils
child2.getParentNode().removeChild( child2 ); child2.getParentNode().removeChild( child2 );
i--; i--;
} }
childNodes = parentElement.getChildNodes();
for ( int i = 0; i < childNodes.getLength() - 1; i++ )
{
Node child = childNodes.item( i );
if ( child instanceof Element )
{
compactChildNodesR( (Element) child, childTagName );
}
}
} }
static boolean equals( String str1, String str2 ) static boolean equals( String str1, String str2 )
@ -320,10 +330,12 @@ public class AbstractWordUtils
if ( argbValue == -1 ) if ( argbValue == -1 )
throw new IllegalArgumentException( "This colorref is empty" ); throw new IllegalArgumentException( "This colorref is empty" );
int value = argbValue & 0x00FFFFFF; int bgrValue = argbValue & 0x00FFFFFF;
int rgbValue = ( bgrValue & 0x0000FF ) << 16 | ( bgrValue & 0x00FF00 )
| ( bgrValue & 0xFF0000 ) >> 16;
// http://www.w3.org/TR/REC-html40/types.html#h-6.5 // http://www.w3.org/TR/REC-html40/types.html#h-6.5
switch ( value ) switch ( rgbValue )
{ {
case 0xFFFFFF: case 0xFFFFFF:
return "white"; return "white";
@ -360,7 +372,7 @@ public class AbstractWordUtils
} }
StringBuilder result = new StringBuilder( "#" ); StringBuilder result = new StringBuilder( "#" );
String hex = Integer.toHexString( value ); String hex = Integer.toHexString( rgbValue );
for ( int i = hex.length(); i < 6; i++ ) for ( int i = hex.length(); i < 6; i++ )
{ {
result.append( '0' ); result.append( '0' );

View File

@ -50,6 +50,8 @@ import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text; import org.w3c.dom.Text;
/** /**
@ -119,6 +121,8 @@ public class WordToFoConverter extends AbstractWordConverter
private AtomicInteger internalLinkCounter = new AtomicInteger( 0 ); private AtomicInteger internalLinkCounter = new AtomicInteger( 0 );
private boolean outputCharactersLanguage = false;
private Set<String> usedIds = new LinkedHashSet<String>(); private Set<String> usedIds = new LinkedHashSet<String>();
/** /**
@ -202,6 +206,11 @@ public class WordToFoConverter extends AbstractWordConverter
return foDocumentFacade.getDocument(); return foDocumentFacade.getDocument();
} }
public boolean isOutputCharactersLanguage()
{
return outputCharactersLanguage;
}
@Override @Override
protected void outputCharacters( Element block, CharacterRun characterRun, protected void outputCharacters( Element block, CharacterRun characterRun,
String text ) String text )
@ -211,11 +220,15 @@ public class WordToFoConverter extends AbstractWordConverter
Triplet triplet = getCharacterRunTriplet( characterRun ); Triplet triplet = getCharacterRunTriplet( characterRun );
if ( WordToFoUtils.isNotEmpty( triplet.fontName ) ) if ( WordToFoUtils.isNotEmpty( triplet.fontName ) )
WordToFoUtils.setFontFamily( inline, characterRun.getFontName() ); WordToFoUtils.setFontFamily( inline, triplet.fontName );
WordToFoUtils.setBold( inline, triplet.bold ); WordToFoUtils.setBold( inline, triplet.bold );
WordToFoUtils.setItalic( inline, triplet.italic ); WordToFoUtils.setItalic( inline, triplet.italic );
WordToFoUtils.setFontSize( inline, characterRun.getFontSize() / 2 ); WordToFoUtils.setFontSize( inline, characterRun.getFontSize() / 2 );
WordToFoUtils.setCharactersProperties( characterRun, inline ); WordToFoUtils.setCharactersProperties( characterRun, inline );
if ( isOutputCharactersLanguage() )
WordToFoUtils.setLanguage( characterRun, inline );
block.appendChild( inline ); block.appendChild( inline );
Text textNode = foDocumentFacade.createText( text ); Text textNode = foDocumentFacade.createText( text );
@ -411,6 +424,32 @@ public class WordToFoConverter extends AbstractWordConverter
block.appendChild( foDocumentFacade.createBlock() ); block.appendChild( foDocumentFacade.createBlock() );
} }
@Override
protected void processPageBreak( HWPFDocumentCore wordDocument, Element flow )
{
Element block = null;
NodeList childNodes = flow.getChildNodes();
if ( childNodes.getLength() > 0 )
{
Node lastChild = childNodes.item( childNodes.getLength() - 1 );
if ( lastChild instanceof Element )
{
Element lastElement = (Element) lastChild;
if ( !lastElement.hasAttribute( "break-after" ) )
{
block = lastElement;
}
}
}
if ( block == null )
{
block = foDocumentFacade.createBlock();
flow.appendChild( block );
}
block.setAttribute( "break-after", "page" );
}
protected void processPageref( HWPFDocumentCore hwpfDocument, protected void processPageref( HWPFDocumentCore hwpfDocument,
Element currentBlock, Range textRange, int currentTableLevel, Element currentBlock, Range textRange, int currentTableLevel,
String pageref ) String pageref )
@ -606,4 +645,9 @@ public class WordToFoConverter extends AbstractWordConverter
return true; return true;
} }
public void setOutputCharactersLanguage( boolean outputCharactersLanguage )
{
this.outputCharactersLanguage = outputCharactersLanguage;
}
} }

View File

@ -30,7 +30,7 @@ public class WordToFoUtils extends AbstractWordUtils
{ {
static void compactInlines( Element blockElement ) static void compactInlines( Element blockElement )
{ {
compactChildNodes( blockElement, "fo:inline" ); compactChildNodesR( blockElement, "fo:inline" );
} }
public static void setBold( final Element element, final boolean bold ) public static void setBold( final Element element, final boolean bold )
@ -82,12 +82,6 @@ public class WordToFoUtils extends AbstractWordUtils
inline.setAttribute( "opacity", inline.setAttribute( "opacity",
getOpacity( characterRun.getIco24() ) ); getOpacity( characterRun.getIco24() ) );
} }
if ( characterRun.getLanguageCode() != 0 )
{
final String language = getLanguage( characterRun.getLanguageCode() );
if ( isNotEmpty( language ) )
inline.setAttribute( "language", language );
}
if ( characterRun.isCapitalized() ) if ( characterRun.isCapitalized() )
{ {
inline.setAttribute( "text-transform", "uppercase" ); inline.setAttribute( "text-transform", "uppercase" );
@ -206,6 +200,17 @@ public class WordToFoUtils extends AbstractWordUtils
element.setAttribute( "text-align", justification ); element.setAttribute( "text-align", justification );
} }
public static void setLanguage( final CharacterRun characterRun,
final Element inline )
{
if ( characterRun.getLanguageCode() != 0 )
{
final String language = getLanguage( characterRun.getLanguageCode() );
if ( isNotEmpty( language ) )
inline.setAttribute( "language", language );
}
}
public static void setParagraphProperties( Paragraph paragraph, public static void setParagraphProperties( Paragraph paragraph,
Element block ) Element block )
{ {

View File

@ -495,6 +495,12 @@ public class WordToHtmlConverter extends AbstractWordConverter
} }
} }
@Override
protected void processPageBreak( HWPFDocumentCore wordDocument, Element flow )
{
flow.appendChild( htmlDocumentFacade.createLineBreak() );
}
protected void processPageref( HWPFDocumentCore hwpfDocument, protected void processPageref( HWPFDocumentCore hwpfDocument,
Element currentBlock, Range textRange, int currentTableLevel, Element currentBlock, Range textRange, int currentTableLevel,
String pageref ) String pageref )

View File

@ -233,7 +233,7 @@ public class WordToHtmlUtils extends AbstractWordUtils
static void compactSpans( Element pElement ) static void compactSpans( Element pElement )
{ {
compactChildNodes( pElement, "span" ); compactChildNodesR( pElement, "span" );
} }
} }

View File

@ -380,6 +380,14 @@ public class WordToTextConverter extends AbstractWordConverter
} }
} }
@Override
protected void processPageBreak( HWPFDocumentCore wordDocument, Element flow )
{
Element block = textDocumentFacade.createBlock();
block.appendChild( textDocumentFacade.createText( "\n" ) );
flow.appendChild( block );
}
@Override @Override
protected void processPageref( HWPFDocumentCore wordDocument, protected void processPageref( HWPFDocumentCore wordDocument,
Element currentBlock, Range textRange, int currentTableLevel, Element currentBlock, Range textRange, int currentTableLevel,