File: FastDisplayModelBuilder.java

package info (click to toggle)
liblayout 0.2.10-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,332 kB
  • sloc: java: 51,125; xml: 138; makefile: 17
file content (861 lines) | stat: -rw-r--r-- 29,353 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
/**
 * ===========================================
 * LibLayout : a free Java layouting library
 * ===========================================
 *
 * Project Info:  http://reporting.pentaho.org/liblayout/
 *
 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ------------
 * $Id: FastDisplayModelBuilder.java 6489 2008-11-28 14:53:40Z tmorgner $
 * ------------
 * (C) Copyright 2006-2007, by Pentaho Corporation.
 */

package org.jfree.layouting.normalizer.displaymodel;

import java.io.IOException;

import org.jfree.layouting.LayoutProcess;
import org.jfree.layouting.State;
import org.jfree.layouting.StateException;
import org.jfree.layouting.StatefullComponent;
import org.jfree.layouting.input.style.keys.box.BoxStyleKeys;
import org.jfree.layouting.input.style.keys.box.DisplayModel;
import org.jfree.layouting.input.style.keys.box.DisplayRole;
import org.jfree.layouting.input.style.values.CSSValue;
import org.jfree.layouting.layouter.content.ContentToken;
import org.jfree.layouting.layouter.context.ContextId;
import org.jfree.layouting.layouter.context.LayoutContext;
import org.jfree.layouting.layouter.context.PageContext;
import org.jfree.layouting.layouter.style.resolver.StyleResolver;
import org.jfree.layouting.normalizer.content.NormalizationException;
import org.jfree.layouting.normalizer.generator.ContentGenerator;
import org.jfree.layouting.renderer.Renderer;
import org.jfree.layouting.util.IntList;
import org.pentaho.reporting.libraries.base.util.FastStack;

/**
 * Creation-Date: Jan 5, 2007, 3:01:09 PM
 *
 * @author Thomas Morgner
 */
public class FastDisplayModelBuilder implements ModelBuilder
{
  private static class FastDisplayModelBuilderState implements State
  {
    private FlowContext[] contexts;
    private int suspendCounter;
    private State contentGeneratorState;

    private FastDisplayModelBuilderState(final FastDisplayModelBuilder builder)
        throws StateException
    {
      this.contentGeneratorState = builder.contentGenerator.saveState();
      this.suspendCounter = builder.suspendCounter;
      this.contexts = new FlowContext[builder.flowContexts.size()];
      try
      {
        for (int i = 0; i < contexts.length; i++)
        {
          final FlowContext flowContext = (FlowContext) builder.flowContexts.get(i);
          contexts[i] = (FlowContext) flowContext.clone();

        }
      }
      catch (CloneNotSupportedException cne)
      {
        throw new StateException("Cloning failed.", cne);
      }
    }

    /**
     * Creates a restored instance of the saved component.
     * <p/>
     * By using this factory-like approach, we gain independence from having to
     * know the actual implementation. This makes things a lot easier.
     *
     * @param layoutProcess the layout process that controls it all
     * @return the saved state
     * @throws org.jfree.layouting.StateException
     *
     */
    public StatefullComponent restore(final LayoutProcess layoutProcess)
        throws StateException
    {
      try
      {
        final FastDisplayModelBuilder fmb = new FastDisplayModelBuilder();
        fmb.layoutProcess = layoutProcess;
        fmb.flowContexts = new FastStack();
        fmb.suspendCounter = suspendCounter;
        for (int i = 0; i < contexts.length; i++)
        {
          final FlowContext context = contexts[i];
          fmb.flowContexts.push(context.clone());
        }
        fmb.contentGenerator = (ContentGenerator) contentGeneratorState.restore(layoutProcess);
        return fmb;
      }
      catch (CloneNotSupportedException e)
      {
        throw new StateException("Clone failed", e);
      }
    }
  }

  public static final int TYPE_BLOCK = 1;
  public static final int TYPE_INLINE = 2;
  public static final int TYPE_MARKER = 3;
  public static final int TYPE_RUBY = 4;
  public static final int TYPE_TABLE = 5;
  public static final int TYPE_TABLE_CELL = 6;
  public static final int TYPE_TABLE_ROW = 7;
  public static final int TYPE_TABLE_SECTION = 8;
  public static final int TYPE_TABLE_COLGROUP = 9;
  public static final int TYPE_TABLE_COL = 10;
  public static final int TYPE_TABLE_CAPTION = 11;
  public static final int TYPE_IGNORED = 12;
  public static final int TYPE_PARAGRAPH = 13;
  public static final int TYPE_FLOW = 14;
  public static final int FLAG_AUTOGENERATED = 0x01000000;

  public static final int MODEL_BLOCK_INSIDE = 1;
  public static final int MODEL_INLINE_INSIDE = 2;
  public static final int MODEL_TABLE = 3;
  public static final int MODEL_TABLE_SECTION = 4;
  public static final int MODEL_TABLE_ROW = 5;
  public static final int MODEL_TABLE_COLGROUP = 6;
  public static final int MODEL_TABLE_COL = 7;

  private LayoutProcess layoutProcess;
  private FastStack flowContexts;
  private ContentGenerator contentGenerator;
  private int suspendCounter;

  public FastDisplayModelBuilder()
  {
  }

  public FastDisplayModelBuilder(final ContentGenerator contentGenerator,
                                 final LayoutProcess layoutProcess)
  {
    if (layoutProcess == null)
    {
      throw new NullPointerException();
    }
    if (contentGenerator == null)
    {
      throw new NullPointerException();
    }
    this.contentGenerator = contentGenerator;
    this.layoutProcess = layoutProcess;
    this.flowContexts = new FastStack();
  }

  public void startDocument(final PageContext pageContext)
      throws NormalizationException
  {
    contentGenerator.startedDocument(pageContext);
  }


  public void startElement(final LayoutContext layoutContext)
      throws NormalizationException, IOException
  {
    // Make sure that we have a root-context
    if (flowContexts.isEmpty())
    {
      // open up a new flow-context ..
      final FlowContext fc = new FlowContext();
      flowContexts.push(fc);
      fc.addElement(MODEL_BLOCK_INSIDE, FLAG_AUTOGENERATED | TYPE_FLOW, layoutContext);
      contentGenerator.startedFlow(layoutContext);
    }

    final CSSValue displayRole = layoutContext.getValue(BoxStyleKeys.DISPLAY_ROLE);
    if (displayRole == DisplayRole.NONE || suspendCounter > 0)
    {
      // do something with that.
      suspendCounter += 1;
      contentGenerator.startedPassThrough(layoutContext);
      return;
    }

    if (displayRole == DisplayRole.TABLE_CAPTION)
    {
      addTableCaption(layoutContext);
      return;
    }
    else if (displayRole == DisplayRole.TABLE_COLUMN)
    {
      addTableColumn(layoutContext);
      return;
    }
    else if (displayRole == DisplayRole.TABLE_COLUMN_GROUP)
    {
      addTableColumnGroup(layoutContext);
      return;
    }
    else if (displayRole == DisplayRole.TABLE_FOOTER_GROUP ||
        displayRole == DisplayRole.TABLE_HEADER_GROUP ||
        displayRole == DisplayRole.TABLE_ROW_GROUP)
    {
      addTableSection(layoutContext);
      return;
    }
    else if (displayRole == DisplayRole.TABLE_ROW)
    {
      addTableRow(layoutContext);
      return;
    }
    else if (displayRole == DisplayRole.TABLE_CELL)
    {
      addTableCell(layoutContext);
      return;
    }

    final FlowContext fc = (FlowContext) flowContexts.peek();
    final int currentDisplayModel = fc.getActiveDisplayModel();
    switch (currentDisplayModel)
    {
      case MODEL_BLOCK_INSIDE:
      {
        //closeSuspended();
        addToBlockLevelBox(layoutContext);
        return;
      }
      case MODEL_INLINE_INSIDE:
      {
        addToInlineLevelBox(layoutContext);
        return;
      }
      case MODEL_TABLE:
      {
        addToTableLevelBox(layoutContext);
        return;
      }
      case MODEL_TABLE_SECTION:
      {
        // Currently, we're in a table-body element, and expect rows to follow.
        // Stupid user gave us something else instead, so we have to autogenerate
        // a row and a cell..
        // Autogenerate the table-row ..
        final StyleResolver styleResolver = layoutProcess.getStyleResolver();
        final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);
        final LayoutContext rowContext =
            styleResolver.createAnonymousContext(id, layoutContext);
        fc.addElement(MODEL_TABLE_ROW, FLAG_AUTOGENERATED | TYPE_TABLE_ROW, rowContext);
        contentGenerator.startedTableRow(rowContext);

        final LayoutContext cellContext =
            styleResolver.createAnonymousContext(id, rowContext);
        fc.addElement(MODEL_BLOCK_INSIDE, FLAG_AUTOGENERATED | TYPE_TABLE_CELL, cellContext);
        contentGenerator.startedTableCell(cellContext);
        addToBlockLevelBox(layoutContext);
        return;
      }
      case MODEL_TABLE_ROW:
      {
        final StyleResolver styleResolver = layoutProcess.getStyleResolver();
        final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);

        final LayoutContext cellContext =
            styleResolver.createAnonymousContext(id, layoutContext);
        fc.addElement(MODEL_BLOCK_INSIDE, FLAG_AUTOGENERATED | TYPE_TABLE_CELL, cellContext);
        contentGenerator.startedTableCell(cellContext);
        addToBlockLevelBox(layoutContext);
        return;
      }
      case MODEL_TABLE_COLGROUP:
      case MODEL_TABLE_COL:
      {
        // This is clearly stupid. We handle that as pass-through content as
        // columns and colgroups are not displayable at all.
        suspendCounter += 1;
        contentGenerator.startedPassThrough(layoutContext);
        return;
      }

      default:
      {
        throw new NormalizationException("Unexpected type: " +
            currentDisplayModel + " for display-role " + displayRole);
      }
    }
  }
//
//  private void closeSuspended() throws NormalizationException
//  {
//    final FlowContext fc = (FlowContext) flowContexts.peek();
//    int state = fc.getCurrentState();
//    while (state == FlowContext.STATE_SUSPEND)
//    {
//      callFinish(fc.getCurrentDisplayRole());
//      fc.close();
//      state = fc.getCurrentState();
//    }
//  }

  private void addTableCaption(final LayoutContext context)
      throws NormalizationException
  {
    final FlowContext fc = (FlowContext) flowContexts.peek();
    // check if the parent is a table.
    final int currentDisplayModel = fc.getCurrentDisplayModel();
    if (currentDisplayModel != MODEL_TABLE)
    {
      // Autogenerate the table ..
      final StyleResolver styleResolver = layoutProcess.getStyleResolver();
      final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);

      final LayoutContext tableContext =
          styleResolver.createAnonymousContext(id, context);
      fc.addElement(MODEL_TABLE, FLAG_AUTOGENERATED | TYPE_TABLE, tableContext);
      contentGenerator.startedTable(tableContext);
    }

    fc.addElement(MODEL_BLOCK_INSIDE, TYPE_TABLE_CAPTION, context);
    contentGenerator.startedTableCaption(context);
  }

  private void addTableColumnGroup(final LayoutContext context)
      throws NormalizationException
  {
    final FlowContext fc = (FlowContext) flowContexts.peek();
    // check if the parent is a table.
    final int currentDisplayModel = fc.getCurrentDisplayModel();
    if (currentDisplayModel != MODEL_TABLE)
    {
      // Autogenerate the table ..
      final StyleResolver styleResolver = layoutProcess.getStyleResolver();
      final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);

      final LayoutContext tableContext =
          styleResolver.createAnonymousContext(id, context);
      fc.addElement(MODEL_TABLE, FLAG_AUTOGENERATED | TYPE_TABLE, tableContext);
      contentGenerator.startedTable(tableContext);
    }

    fc.addElement(MODEL_TABLE_COLGROUP, TYPE_TABLE_COLGROUP, context);
    contentGenerator.startedTableColumnGroup(context);
  }

  private void addTableColumn(final LayoutContext context)
      throws NormalizationException
  {
    final FlowContext fc = (FlowContext) flowContexts.peek();
    // check if the parent is a table.
    final int currentDisplayModel = fc.getCurrentDisplayModel();
    if (currentDisplayModel != MODEL_TABLE &&
        currentDisplayModel != MODEL_TABLE_COLGROUP)
    {
      // Autogenerate the table ..
      final StyleResolver styleResolver = layoutProcess.getStyleResolver();
      final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);

      suspendNonBlockElements();
      final LayoutContext tableContext =
          styleResolver.createAnonymousContext(id, context);
      fc.addElement(MODEL_TABLE, FLAG_AUTOGENERATED | TYPE_TABLE, tableContext);
      contentGenerator.startedTable(tableContext);
    }

    fc.addElement(MODEL_TABLE_COL, TYPE_TABLE_COL, context);
    contentGenerator.startedTableColumn(context);
  }

  private void addTableSection(final LayoutContext context)
      throws NormalizationException
  {
    final FlowContext fc = (FlowContext) flowContexts.peek();
    // check if the parent is a table.
    final int currentDisplayModel = fc.getCurrentDisplayModel();
    if (currentDisplayModel != MODEL_TABLE)
    {
      // Autogenerate the table ..
      final StyleResolver styleResolver = layoutProcess.getStyleResolver();
      final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);

      // the table is a block level element here, so we have to close any open
      // paragraphs before proceeding ..
      suspendNonBlockElements();

      final LayoutContext tableContext =
          styleResolver.createAnonymousContext(id, context);
      fc.addElement(MODEL_TABLE, FLAG_AUTOGENERATED | TYPE_TABLE, tableContext);
      contentGenerator.startedTable(tableContext);
    }

    fc.addElement(MODEL_TABLE_SECTION, TYPE_TABLE_SECTION, context);
    contentGenerator.startedTableSection(context);
  }

  private void addTableRow(final LayoutContext context)
      throws NormalizationException
  {
    final FlowContext fc = (FlowContext) flowContexts.peek();
    // check if the parent is a table.
    final int currentDisplayModel = fc.getCurrentDisplayModel();
    if (currentDisplayModel != MODEL_TABLE_SECTION)
    {
      final StyleResolver styleResolver = layoutProcess.getStyleResolver();
      final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);

      if (currentDisplayModel != MODEL_TABLE)
      {
        // there is not even a table .. generate it
        // Autogenerate the table ..
        final LayoutContext tableContext =
            styleResolver.createAnonymousContext(id, context);

        suspendNonBlockElements();
        fc.addElement(MODEL_TABLE, FLAG_AUTOGENERATED | TYPE_TABLE, tableContext);
        contentGenerator.startedTable(tableContext);
      }

      // OK, finally add the table section ..
      final LayoutContext sectionContext =
          styleResolver.createAnonymousContext(id, context);
      fc.addElement(MODEL_TABLE_SECTION, FLAG_AUTOGENERATED | TYPE_TABLE_SECTION, sectionContext);
      contentGenerator.startedTableSection(sectionContext);
    }

    fc.addElement(MODEL_TABLE_ROW, TYPE_TABLE_ROW, context);
    contentGenerator.startedTableRow(context);
  }

  private void addTableCell(final LayoutContext context)
      throws NormalizationException
  {
    final FlowContext fc = (FlowContext) flowContexts.peek();
    // check if the parent is a table.
    int currentDisplayModel = fc.getCurrentDisplayModel();
    if (currentDisplayModel != MODEL_TABLE_ROW)
    {
      final StyleResolver styleResolver = layoutProcess.getStyleResolver();
      final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);

      if (currentDisplayModel != MODEL_TABLE &&
          currentDisplayModel != MODEL_TABLE_SECTION)
      {
        // generate the table ..
        suspendNonBlockElements();

        final LayoutContext tableContext =
            styleResolver.createAnonymousContext(id, context);
        fc.addElement(MODEL_TABLE, FLAG_AUTOGENERATED | TYPE_TABLE, tableContext);
        contentGenerator.startedTable(tableContext);
        currentDisplayModel = MODEL_TABLE;
      }

      if (currentDisplayModel != MODEL_TABLE_SECTION)
      {
        // OK, generate the table section ..
        final LayoutContext sectionContext =
            styleResolver.createAnonymousContext(id, context);
        fc.addElement(MODEL_TABLE_SECTION, FLAG_AUTOGENERATED | TYPE_TABLE_SECTION, sectionContext);
        contentGenerator.startedTableSection(sectionContext);
      }

      // Autogenerate the table-row ..
      final LayoutContext rowContext =
          styleResolver.createAnonymousContext(id, context);
      fc.addElement(MODEL_TABLE_ROW, FLAG_AUTOGENERATED | TYPE_TABLE_ROW, rowContext);
      contentGenerator.startedTableRow(rowContext);
    }

    fc.addElement(MODEL_BLOCK_INSIDE, TYPE_TABLE_CELL, context);
    contentGenerator.startedTableCell(context);
  }

  private void addToBlockLevelBox(final LayoutContext context)
      throws NormalizationException
  {
    final FlowContext fc = (FlowContext) flowContexts.peek();
    final CSSValue displayRole = context.getValue(BoxStyleKeys.DISPLAY_ROLE);
    if (DisplayRole.INLINE.equals(displayRole))
    {
      // special treatment for a inline box that is added to a block-parent
      // Generate the paragraph box ..
      if (reactivateSuspendedInlines(fc) == false)
      {
        // todo: check for possibly suspended inlines and reactivate them

        final StyleResolver styleResolver = layoutProcess.getStyleResolver();
        final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);
        final LayoutContext paragraphContext =
            styleResolver.createAnonymousContext(id, context);
        fc.addElement(MODEL_INLINE_INSIDE, FLAG_AUTOGENERATED | TYPE_PARAGRAPH, paragraphContext);
        contentGenerator.startedRootInline(paragraphContext);
      }

      // now add the element ..
      final CSSValue displayModel = context.getValue(BoxStyleKeys.DISPLAY_MODEL);
      if (DisplayModel.TABLE.equals(displayModel))
      {
        fc.addElement(MODEL_TABLE, TYPE_INLINE, context);
        contentGenerator.startedTable(context);
      }
      else if (DisplayModel.INLINE_INSIDE.equals(displayModel))
      {
        fc.addElement(MODEL_INLINE_INSIDE, TYPE_INLINE, context);
        contentGenerator.startedInline(context);
      }
      else
      {
        fc.addElement(MODEL_BLOCK_INSIDE, TYPE_BLOCK, context);
        contentGenerator.startedBlock(context);
      }
      return;
    }

    // Todo: need to check for run-in and compact as well..
    // Everything else is considered a block-level element and treated as such..
    final CSSValue displayModel = context.getValue(BoxStyleKeys.DISPLAY_MODEL);
    if (DisplayModel.TABLE.equals(displayModel))
    {
      fc.addElement(MODEL_TABLE, TYPE_TABLE, context);
      contentGenerator.startedTable(context);
    }
    else if (DisplayModel.INLINE_INSIDE.equals(displayModel))
    {
      // A block level element that declares that all its direct childs should
      // be treated as inline-level elements. We create a paragraph box for that
      // one.
      fc.addElement(MODEL_INLINE_INSIDE, TYPE_BLOCK, context);
      contentGenerator.startedBlock(context);

      final StyleResolver styleResolver = layoutProcess.getStyleResolver();
      final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);
      final LayoutContext paragraphContext =
          styleResolver.createAnonymousContext(id, context);
      fc.addElement(MODEL_INLINE_INSIDE,
          FLAG_AUTOGENERATED | TYPE_PARAGRAPH, paragraphContext);
      contentGenerator.startedRootInline(paragraphContext);
    }
    else
    {
      fc.addElement(MODEL_BLOCK_INSIDE, TYPE_BLOCK, context);
      contentGenerator.startedBlock(context);
    }
  }

  private void addToInlineLevelBox(final LayoutContext context)
      throws NormalizationException
  {
    final FlowContext fc = (FlowContext) flowContexts.peek();
    final CSSValue displayRole = context.getValue(BoxStyleKeys.DISPLAY_ROLE);
    if (DisplayRole.INLINE.equals(displayRole))
    {
      // The current linebox is suspended and the boxes are added as if they
      // were direct childs of the current block-context.
      // now add the element ..
      final CSSValue displayModel = context.getValue(BoxStyleKeys.DISPLAY_MODEL);
      if (DisplayModel.TABLE.equals(displayModel))
      {
        fc.addElement(MODEL_TABLE, TYPE_TABLE, context);
        contentGenerator.startedTable(context);
      }
      else if (DisplayModel.INLINE_INSIDE.equals(displayModel))
      {
        fc.addElement(MODEL_INLINE_INSIDE, TYPE_INLINE, context);
        contentGenerator.startedInline(context);
      }
      else
      {
        fc.addElement(MODEL_BLOCK_INSIDE, TYPE_BLOCK, context);
        contentGenerator.startedBlock(context);
      }
      return;
    }

    // we have to suspend the lineboxes to get access to the block-level context.
    // then follow the ordinary block-level procedure ..
    suspendNonBlockElements();
    addToBlockLevelBox(context);
  }

  private void suspendNonBlockElements()
      throws NormalizationException
  {
    final FlowContext fc = (FlowContext) flowContexts.peek();
    int model = fc.getCurrentDisplayModel();
    while (model != MODEL_BLOCK_INSIDE)
    {
      final int currentDisplayRole = fc.getCurrentDisplayRole();
      callFinish(currentDisplayRole);
      fc.suspend();
      model = fc.getCurrentDisplayModel();
    }
  }

  private void addToTableLevelBox(final LayoutContext context)
      throws NormalizationException
  {
    final StyleResolver styleResolver = layoutProcess.getStyleResolver();
    final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);

    // This is some non-table content that has been added to a table.
    // This generally means that someone messed up the layout, we have to
    // generate a default table ...
    final FlowContext fc = (FlowContext) flowContexts.peek();

    final LayoutContext sectionContext =
        styleResolver.createAnonymousContext(id, context);
    fc.addElement(MODEL_TABLE_SECTION, FLAG_AUTOGENERATED | TYPE_TABLE_SECTION, sectionContext);
    contentGenerator.startedTableSection(sectionContext);

    final LayoutContext rowContext =
        styleResolver.createAnonymousContext(id, sectionContext);
    fc.addElement(MODEL_TABLE_ROW, FLAG_AUTOGENERATED | TYPE_TABLE_ROW, rowContext);
    contentGenerator.startedTableRow(rowContext);

    final LayoutContext cellContext =
        styleResolver.createAnonymousContext(id, rowContext);
    fc.addElement(MODEL_BLOCK_INSIDE, FLAG_AUTOGENERATED | TYPE_TABLE_CELL, cellContext);
    contentGenerator.startedTableCell(cellContext);

    addToBlockLevelBox(context);
  }

  public void addContent(final ContentToken content)
      throws NormalizationException
  {
    final FlowContext fc = (FlowContext) flowContexts.peek();
    final LayoutContext layoutContext = fc.getCurrentLayoutContext();

    if (suspendCounter > 0)
    {
      contentGenerator.addPassThroughContent(layoutContext, content);
      return;
    }

    final int currentDisplayModel = fc.getActiveDisplayModel();
    if (currentDisplayModel == MODEL_BLOCK_INSIDE)
    {
      // todo: check for suspended inlines and reactivate them if needed.
      if (reactivateSuspendedInlines(fc) == false)
      {
        // generate a inline box ..
        final StyleResolver styleResolver = layoutProcess.getStyleResolver();
        final ContextId id = new ContextId(ContextId.SOURCE_DISPLAY_MODEL, -1, 0);
        final LayoutContext paragraphContext =
            styleResolver.createAnonymousContext(id, layoutContext);
        fc.addElement(MODEL_INLINE_INSIDE, FLAG_AUTOGENERATED | TYPE_PARAGRAPH, paragraphContext);
        contentGenerator.startedRootInline(paragraphContext);
        contentGenerator.addContent(paragraphContext, content);
      }
      else
      {
        contentGenerator.addContent(layoutContext, content);
      }
      return;
    }

    if (currentDisplayModel == MODEL_INLINE_INSIDE)
    {
      reactivateSuspendedInlines(fc);
      contentGenerator.addContent(layoutContext, content);
    }
    else
    {
      // content that has been added inside a table or any of the table
      // elements is ignored - ok, we add it anyway, but whether it is
      // rendered is not up to us.
      // (Maybe we should not forward the content?)
      contentGenerator.addContent(layoutContext, content);
    }
  }

  private boolean reactivateSuspendedInlines(final FlowContext fc)
      throws NormalizationException
  {
    // If there are suspended inlines, reactivate them ..
    if (fc.getCurrentState() != FlowContext.STATE_SUSPEND)
    {
      return false;
    }

    final IntList roles = new IntList(5);
    final IntList models = new IntList(5);
    final FastStack contexts = new FastStack();
    while (fc.getCurrentState() == FlowContext.STATE_SUSPEND)
    {
      contexts.push(fc.getCurrentLayoutContext());
      roles.push(fc.getCurrentDisplayRole());
      models.push(fc.getCurrentDisplayModel());
      fc.close();
    }

    while (contexts.isEmpty() == false)
    {
      final LayoutContext lc = (LayoutContext) contexts.pop();
      final int role = roles.pop();
      final int model = models.pop();
      final int cleanRole = role & 0xFFFFFF;
      if (cleanRole == TYPE_INLINE)
      {
        contentGenerator.startedInline(lc);
        fc.addElement(model, role, lc);
      }
      else if (cleanRole == TYPE_PARAGRAPH)
      {
        contentGenerator.startedRootInline(lc);
        fc.addElement(model, role, lc);
      }
      else
      {
        throw new IllegalStateException("Unexpected role: " + cleanRole);
      }
    }

    return true;
  }

  public void endElement() throws NormalizationException
  {
    if (suspendCounter > 0)
    {
      contentGenerator.finishedPassThrough();
      suspendCounter -= 1;
      return;
    }

    // close all suspended elements and the first non suspendend element
    FlowContext context = (FlowContext) flowContexts.peek();

    int role;
    do
    {
      role = context.getCurrentDisplayRole();
      if (context.getCurrentState() == FlowContext.STATE_OPEN)
      {
        callFinish(role);
      }
      else
      {
        final LayoutContext lc = context.getCurrentLayoutContext();
      }
      context.close();
      if (role == TYPE_FLOW)
      {
        context = (FlowContext) flowContexts.peek();
      }
    }
    while ((role & FLAG_AUTOGENERATED) == FLAG_AUTOGENERATED &&
        context.isEmpty() == false);

  }

  public void endDocument() throws NormalizationException
  {
    endElement();
    final FlowContext context = (FlowContext) flowContexts.pop();
    contentGenerator.finishedDocument();
  }


  private void callFinish(final int type) throws NormalizationException
  {
    switch ((type & 0xffffff))
    {
      case TYPE_BLOCK:
      {
        contentGenerator.finishedBlock();
        break;
      }
      case TYPE_FLOW:
      {
        contentGenerator.finishedFlow();
        break;
      }
      case TYPE_TABLE:
      {
        contentGenerator.finishedTable();
        break;
      }
      case TYPE_TABLE_CAPTION:
      {
        contentGenerator.finishedTableCaption();
        break;
      }
      case TYPE_TABLE_CELL:
      {
        contentGenerator.finishedTableCell();
        break;
      }
      case TYPE_TABLE_COL:
      {
        contentGenerator.finishedTableColumn();
        break;
      }
      case TYPE_TABLE_COLGROUP:
      {
        contentGenerator.finishedTableColumnGroup();
        break;
      }
      case TYPE_TABLE_ROW:
      {
        contentGenerator.finishedTableRow();
        break;
      }
      case TYPE_TABLE_SECTION:
      {
        contentGenerator.finishedTableSection();
        break;
      }
      case TYPE_INLINE:
      {
        contentGenerator.finishedInline();
        break;
      }
      case TYPE_MARKER:
      {
        contentGenerator.finishedMarker();
        break;
      }
      case TYPE_PARAGRAPH:
      {
        contentGenerator.finishedRootInline();
        break;
      }
      default:
      {
        throw new IllegalStateException();
      }
    }
  }

  public void handlePageBreak(final PageContext pageContext)
      throws NormalizationException
  {
    contentGenerator.handlePageBreak(pageContext);
  }

  public Renderer getRenderer()
  {
    return contentGenerator.getRenderer();
  }

  public State saveState() throws StateException
  {
    return new FastDisplayModelBuilderState(this);
  }
}