KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > rtfdoc > RtfSpaceManager


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: RtfSpaceManager.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24
25 /**
26  * This class is responsible for saving space-before/space-after attributes
27  * history and adding spacing to established candidates (i.e. attributes) or
28  * accumulation spacing in case of candidate absence.
29  */

30 public class RtfSpaceManager {
31     /** Stack for saving rtf block-level attributes. */
32     private LinkedList JavaDoc blockAttributes = new LinkedList JavaDoc();
33
34     /** Stack for saving rtf inline-level attributes. */
35     private LinkedList JavaDoc inlineAttributes = new LinkedList JavaDoc();
36
37     /**
38      * Keeps value of accumulated space in twips. For example if block has
39      * nonzero space-before or space-after properties and has no plain text
40      * inside, then the next block should has increased value of space-before
41      * property.
42      */

43     private int accumulatedSpace = 0;
44     
45     /**
46      * Construct a newly allocated <code>RtfSpaceManager</code> object.
47      */

48     public RtfSpaceManager() {
49     }
50
51     /**
52      * Iterates block-level stack (i.e. all open blocks) and stops updating
53      * candidate for adding space-before/space-after attribute in case of
54      * candidate presence.
55      */

56     public void stopUpdatingSpaceBefore() {
57         for (Iterator JavaDoc it = blockAttributes.iterator(); it.hasNext();) {
58             RtfSpaceSplitter splitter = (RtfSpaceSplitter) it.next();
59             if (splitter.isBeforeCadidateSet()) {
60                 splitter.stopUpdatingSpaceBefore();
61             }
62         }
63     }
64     
65     /**
66      * Set attributes as candidate for space attributes inheritance.
67      *
68      * @param attrs attributes to set
69      */

70     public void setCandidate(RtfAttributes attrs) {
71         for (Iterator JavaDoc it = blockAttributes.iterator(); it.hasNext();) {
72             RtfSpaceSplitter splitter = (RtfSpaceSplitter) it.next();
73             splitter.setSpaceBeforeCandidate(attrs);
74             splitter.setSpaceAfterCandidate(attrs);
75         }
76     }
77     
78     /**
79      * Builds RtfSpaceSplitter on <code>attrs</code> and adds it to the
80      * block-level stack.
81      *
82      * @param attrs RtfAttribute to add
83      * @return instance of RtfSpaceSplitter
84      */

85     public RtfSpaceSplitter pushRtfSpaceSplitter(RtfAttributes attrs) {
86         RtfSpaceSplitter splitter;
87         splitter = new RtfSpaceSplitter(attrs, accumulatedSpace);
88         // set accumulatedSpace to 0, because now accumulatedSpace used
89
// in splitter
90
accumulatedSpace = 0;
91         blockAttributes.addLast(splitter);
92         return splitter;
93     }
94     
95     /**
96      * Removes RtfSpaceSplitter from top of block-level stack.
97      */

98     public void popRtfSpaceSplitter() {
99         if (!blockAttributes.isEmpty()) {
100             RtfSpaceSplitter splitter;
101             splitter = (RtfSpaceSplitter) blockAttributes.removeLast();
102             accumulatedSpace += splitter.flush();
103         }
104     }
105
106     /**
107      * Pushes inline attributes to inline-level stack.
108      *
109      * @param attrs attributes to add
110      */

111     public void pushInlineAttributes(RtfAttributes attrs) {
112         inlineAttributes.addLast(attrs);
113     }
114
115     /**
116      * Pops inline attributes from inline-level stack.
117      */

118     public void popInlineAttributes() {
119         if (!inlineAttributes.isEmpty()) {
120             inlineAttributes.removeLast();
121         }
122     }
123
124     /**
125      * Peeks at inline-level attribute stack.
126      *
127      * @return RtfAttributes from top of inline-level stack
128      */

129     public RtfAttributes getLastInlineAttribute() {
130         return (RtfAttributes) inlineAttributes.getLast();
131     }
132 }
133
Popular Tags