Skip to content

Commit a481950

Browse files
committed
Nested html tags use styles of all elements in stack
1 parent 8699b55 commit a481950

File tree

20 files changed

+261
-447
lines changed

20 files changed

+261
-447
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
.classpath
55
/target
66
/.repository/
7+
.idea
8+
*.iml

document/fr.opensagres.xdocreport.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/textstyling/DocxDocumentHandler.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,13 @@
3434
import fr.opensagres.xdocreport.document.textstyling.AbstractDocumentHandler;
3535
import fr.opensagres.xdocreport.document.textstyling.properties.Color;
3636
import fr.opensagres.xdocreport.document.textstyling.properties.ContainerProperties;
37-
import fr.opensagres.xdocreport.document.textstyling.properties.HeaderProperties;
38-
import fr.opensagres.xdocreport.document.textstyling.properties.ListItemProperties;
39-
import fr.opensagres.xdocreport.document.textstyling.properties.ListProperties;
40-
import fr.opensagres.xdocreport.document.textstyling.properties.ParagraphProperties;
41-
import fr.opensagres.xdocreport.document.textstyling.properties.SpanProperties;
42-
import fr.opensagres.xdocreport.document.textstyling.properties.TableCellProperties;
4337
import fr.opensagres.xdocreport.document.textstyling.properties.TableProperties;
44-
import fr.opensagres.xdocreport.document.textstyling.properties.TableRowProperties;
4538
import fr.opensagres.xdocreport.document.textstyling.properties.TextAlignment;
4639
import fr.opensagres.xdocreport.template.IContext;
4740

4841
import java.io.IOException;
42+
import java.util.ArrayList;
43+
import java.util.List;
4944
import java.util.Stack;
5045

5146
/**
@@ -69,7 +64,7 @@ public class DocxDocumentHandler
6964

7065
private Stack<ContainerProperties> paragraphsStack;
7166

72-
private Stack<SpanProperties> spansStack;
67+
private Stack<ContainerProperties> spansStack;
7368

7469
private HyperlinkRegistry hyperlinkRegistry;
7570

@@ -108,7 +103,7 @@ public void startDocument()
108103
this.subscripting = false;
109104
this.superscripting = false;
110105
this.paragraphsStack = new Stack<ContainerProperties>();
111-
this.spansStack = new Stack<SpanProperties>();
106+
this.spansStack = new Stack<ContainerProperties>();
112107
this.addLineBreak = 0;
113108
}
114109

@@ -233,7 +228,7 @@ public void handleString( String content )
233228
boolean subscript = subscripting;
234229
boolean superscript = superscripting;
235230
Color color = null;
236-
SpanProperties properties = getCurrentSpanProperties();
231+
ContainerProperties properties = getCurrentProperties();
237232
if ( properties != null )
238233
{
239234
// override properties declared in the span.
@@ -359,7 +354,7 @@ private void internalStartParagraph( ContainerProperties properties )
359354
}
360355
}
361356

362-
public void startParagraph( ParagraphProperties properties )
357+
public void startParagraph( ContainerProperties properties )
363358
throws IOException
364359
{
365360
processPageBreakBefore( properties );
@@ -410,7 +405,7 @@ public void endParagraph()
410405
processPageBreakAfter( paragraphsStack.pop() );
411406
}
412407

413-
public void startListItem( ListItemProperties properties )
408+
public void startListItem( ContainerProperties properties )
414409
throws IOException
415410
{
416411
// Close current paragraph
@@ -517,7 +512,7 @@ public void endListItem()
517512
// endParagraph();
518513
}
519514

520-
public void startHeading( int level, HeaderProperties properties )
515+
public void startHeading( int level, ContainerProperties properties )
521516
throws IOException
522517
{
523518
// Close current paragraph
@@ -543,7 +538,7 @@ public void endHeading( int level )
543538
insideHeader = false;
544539
}
545540

546-
public void startSpan( SpanProperties properties )
541+
public void startSpan( ContainerProperties properties )
547542
throws IOException
548543
{
549544
spansStack.push( properties );
@@ -557,11 +552,26 @@ public void startSpan( SpanProperties properties )
557552
// super.write( "</w:r>" );
558553
}
559554

560-
private SpanProperties getCurrentSpanProperties()
555+
/**
556+
* Collects all active span properties
557+
* @return
558+
*/
559+
private ContainerProperties getCurrentProperties()
561560
{
562561
if ( !spansStack.isEmpty() )
563562
{
564-
return spansStack.peek();
563+
// block elements first, then inline elements
564+
List<ContainerProperties> propertiesList = new ArrayList<ContainerProperties>(paragraphsStack);
565+
propertiesList.addAll( spansStack );
566+
ContainerProperties result = null;
567+
for(ContainerProperties properties : propertiesList) {
568+
if (result == null) {
569+
result = properties;
570+
} else {
571+
result = ContainerProperties.combine( result, properties );
572+
}
573+
}
574+
return result;
565575
}
566576
return null;
567577
}
@@ -573,7 +583,7 @@ public void endSpan()
573583
}
574584

575585
@Override
576-
protected void doStartOrderedList( ListProperties properties )
586+
protected void doStartOrderedList( ContainerProperties properties )
577587
throws IOException
578588
{
579589
if(this.addLineBreak>0)
@@ -593,7 +603,7 @@ protected void doStartOrderedList( ListProperties properties )
593603
}
594604

595605
@Override
596-
protected void doStartUnorderedList( ListProperties properties )
606+
protected void doStartUnorderedList( ContainerProperties properties )
597607
throws IOException
598608
{
599609
if(this.addLineBreak>0)
@@ -767,7 +777,7 @@ public void doEndTable( TableProperties properties )
767777
super.write( "<w:p/>" );
768778
}
769779

770-
public void doStartTableRow( TableRowProperties properties )
780+
public void doStartTableRow( ContainerProperties properties )
771781
throws IOException
772782
{
773783
super.write( "<w:tr>" );
@@ -779,7 +789,7 @@ public void doEndTableRow()
779789
super.write( "</w:tr>" );
780790
}
781791

782-
public void doStartTableCell( TableCellProperties properties )
792+
public void doStartTableCell( ContainerProperties properties )
783793
throws IOException
784794
{
785795
super.write( "<w:tc>" );

document/fr.opensagres.xdocreport.document.docx/src/test/java/fr/opensagres/xdocreport/document/docx/textstyling/DocxDocumentHandlerTestCase.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,22 @@ public void testStrikeWithStrike()
244244
Assert.assertEquals( "", handler.getTextEnd() );
245245
}
246246

247+
@Test
248+
public void testBoldAndItalicSpan()
249+
throws Exception
250+
{
251+
IContext context = new MockContext();
252+
253+
ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE;
254+
IDocumentHandler handler = new DocxDocumentHandler( null, context, "word/document.xml" );
255+
formatter.transform( "<span style=\"font-weight:bold;\"><span style=\"font-style: italic;\">text</span></span>", handler );
256+
257+
Assert.assertEquals( "", handler.getTextBefore() );
258+
Assert.assertEquals( "<w:r><w:rPr><w:b /><w:i /></w:rPr><w:t xml:space=\"preserve\" >text</w:t></w:r>",
259+
handler.getTextBody() );
260+
Assert.assertEquals( "", handler.getTextEnd() );
261+
}
262+
247263
@Test
248264
public void testHyperlinkByUsingXDocReport_HyperlinkStyle()
249265
throws Exception

document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/textstyling/ODTDefaultStylesGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import java.util.regex.Pattern;
3131

3232
import fr.opensagres.xdocreport.document.textstyling.properties.ContainerProperties;
33-
import fr.opensagres.xdocreport.document.textstyling.properties.ContainerProperties.ContainerType;
33+
import fr.opensagres.xdocreport.document.textstyling.properties.ContainerType;
3434
import fr.opensagres.xdocreport.document.textstyling.properties.TextAlignment;
3535

3636
/**

document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/textstyling/ODTDocumentHandler.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,8 @@
3333
import fr.opensagres.xdocreport.document.odt.template.ODTContextHelper;
3434
import fr.opensagres.xdocreport.document.preprocessor.sax.BufferedElement;
3535
import fr.opensagres.xdocreport.document.textstyling.AbstractDocumentHandler;
36-
import fr.opensagres.xdocreport.document.textstyling.IDocumentHandler.TextLocation;
37-
import fr.opensagres.xdocreport.document.textstyling.properties.HeaderProperties;
38-
import fr.opensagres.xdocreport.document.textstyling.properties.ListItemProperties;
39-
import fr.opensagres.xdocreport.document.textstyling.properties.ListProperties;
40-
import fr.opensagres.xdocreport.document.textstyling.properties.ParagraphProperties;
41-
import fr.opensagres.xdocreport.document.textstyling.properties.SpanProperties;
42-
import fr.opensagres.xdocreport.document.textstyling.properties.TableCellProperties;
36+
import fr.opensagres.xdocreport.document.textstyling.properties.ContainerProperties;
4337
import fr.opensagres.xdocreport.document.textstyling.properties.TableProperties;
44-
import fr.opensagres.xdocreport.document.textstyling.properties.TableRowProperties;
4538
import fr.opensagres.xdocreport.template.IContext;
4639

4740
public class ODTDocumentHandler
@@ -263,7 +256,7 @@ private void startParagraphIfNeeded()
263256
}
264257
}
265258

266-
public void startParagraph( ParagraphProperties properties )
259+
public void startParagraph( ContainerProperties properties )
267260
throws IOException
268261
{
269262
if ( paragraphsStack.isEmpty() || !paragraphsStack.peek() )
@@ -283,7 +276,7 @@ public void endParagraph()
283276
// }
284277
}
285278

286-
private void internalStartParagraph( boolean containerIsList, ParagraphProperties properties )
279+
private void internalStartParagraph( boolean containerIsList, ContainerProperties properties )
287280
throws IOException
288281
{
289282
String styleName = null;
@@ -350,7 +343,7 @@ private void internalEndParagraph()
350343
}
351344
}
352345

353-
public void startHeading( int level, HeaderProperties properties )
346+
public void startHeading( int level, ContainerProperties properties )
354347
throws IOException
355348
{
356349
endParagraphIfNeeded();
@@ -371,7 +364,7 @@ public void endHeading( int level )
371364
}
372365

373366
@Override
374-
protected void doStartOrderedList( ListProperties properties )
367+
protected void doStartOrderedList( ContainerProperties properties )
375368
throws IOException
376369
{
377370
internalStartList( styleGen.getOLStyleName() );
@@ -385,7 +378,7 @@ protected void doEndOrderedList()
385378
}
386379

387380
@Override
388-
protected void doStartUnorderedList( ListProperties properties )
381+
protected void doStartUnorderedList( ContainerProperties properties )
389382
throws IOException
390383
{
391384
internalStartList( styleGen.getULStyleName() );
@@ -440,7 +433,7 @@ protected void internalEndList()
440433
}
441434
}
442435

443-
public void startListItem( ListItemProperties properties )
436+
public void startListItem( ContainerProperties properties )
444437
throws IOException
445438
{
446439
if ( itemStyle != null )
@@ -470,7 +463,7 @@ public void endListItem()
470463
super.write( "</text:list-item>" );
471464
}
472465

473-
public void startSpan( SpanProperties properties )
466+
public void startSpan( ContainerProperties properties )
474467
throws IOException
475468
{
476469
internalStartSpan( styleGen.getTextStyleName( properties ), true );
@@ -532,7 +525,7 @@ public void doEndTable( TableProperties properties )
532525
super.write( "</table:table>" );
533526
}
534527

535-
protected void doStartTableRow( TableRowProperties properties )
528+
protected void doStartTableRow( ContainerProperties properties )
536529
throws IOException
537530
{
538531
super.write( "<table:table-row>" );
@@ -544,7 +537,7 @@ protected void doEndTableRow()
544537
super.write( "</table:table-row>" );
545538
}
546539

547-
protected void doStartTableCell( TableCellProperties properties )
540+
protected void doStartTableCell( ContainerProperties properties )
548541
throws IOException
549542
{
550543
super.write( "<table:table-cell>" );

document/fr.opensagres.xdocreport.document.textstyling.wiki/src/main/java/fr/opensagres/xdocreport/document/textstyling/wiki/WemListenerAdapter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929
import java.util.logging.Logger;
3030

31+
import fr.opensagres.xdocreport.document.textstyling.properties.ContainerProperties;
3132
import org.wikimodel.wem.EmptyWemListener;
3233
import org.wikimodel.wem.IWemConstants;
3334
import org.wikimodel.wem.WikiFormat;
@@ -37,9 +38,6 @@
3738

3839
import fr.opensagres.xdocreport.core.logging.LogUtils;
3940
import fr.opensagres.xdocreport.document.textstyling.IDocumentHandler;
40-
import fr.opensagres.xdocreport.document.textstyling.properties.ListItemProperties;
41-
import fr.opensagres.xdocreport.document.textstyling.properties.ListProperties;
42-
import fr.opensagres.xdocreport.document.textstyling.properties.ParagraphProperties;
4341

4442
/**
4543
* Wiki Event Model Adaptor to call methods of {@link IDocumentHandler}.
@@ -136,7 +134,7 @@ public void beginList( WikiParameters params, boolean ordered )
136134
{
137135
try
138136
{
139-
ListProperties properties = null;
137+
ContainerProperties properties = null;
140138
if ( ordered )
141139
{
142140
documentHandler.startOrderedList( properties );
@@ -157,7 +155,7 @@ public void beginListItem()
157155
{
158156
try
159157
{
160-
ListItemProperties properties = null;
158+
ContainerProperties properties = null;
161159
documentHandler.startListItem( properties );
162160
}
163161
catch ( IOException e )
@@ -230,7 +228,7 @@ public void beginParagraph( WikiParameters params )
230228
{
231229
try
232230
{
233-
ParagraphProperties properties = null;
231+
ContainerProperties properties = null;
234232
documentHandler.startParagraph( properties );
235233
}
236234
catch ( IOException e )

0 commit comments

Comments
 (0)