KickJava   Java API By Example, From Geeks To Geeks.

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


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: RtfSpaceSplitter.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21
22 /**
23  * This class splits block attributes into space-before attribute, space-after
24  * attribute and common attributes.
25  */

26 public class RtfSpaceSplitter {
27
28     /** Common attributes for all text. */
29     private RtfAttributes commonAttributes;
30
31     /** Space-before attributes of a block. */
32     private int spaceBefore;
33
34     /** Space-after attributes of a block. */
35     private int spaceAfter;
36
37     /** Indicate that we can update candidate for space-before. */
38     private boolean updatingSpaceBefore;
39
40     /** Candidate for adding space-before. */
41     private RtfAttributes spaceBeforeCandidate;
42
43     /** Candidate for adding space-before. */
44     private RtfAttributes spaceAfterCandidate;
45
46     /**
47      * Create RtfSpaceSplitter with given RtfAttributes.
48      *
49      * @param attrs RtfAttributes for splitting
50      * @param previousSpace integer, representing accumulated spacing
51      */

52     public RtfSpaceSplitter(RtfAttributes attrs, int previousSpace) {
53         commonAttributes = attrs;
54         updatingSpaceBefore = true;
55         spaceBeforeCandidate = null;
56         spaceAfterCandidate = null;
57
58         spaceBefore = split(RtfText.SPACE_BEFORE) + previousSpace;
59         spaceAfter = split(RtfText.SPACE_AFTER);
60     }
61
62     /**
63      * Remove attributes with name <code>key</code> from
64      * <code>commonAttributes</code> and return it as int.
65      *
66      * @param key attributes name to extract
67      * @return integer, representing value of extracted attributes
68      */

69     public int split(String JavaDoc key) {
70         Integer JavaDoc i = (Integer JavaDoc) commonAttributes.getValue(key);
71         if (i == null) {
72             i = new Integer JavaDoc(0);
73         }
74
75         commonAttributes.unset(key);
76         return i.intValue();
77     }
78
79     /** @return attributes, applicable to whole block. */
80     public RtfAttributes getCommonAttributes() {
81         return commonAttributes;
82     }
83
84     /** @return space-before value. */
85     public int getSpaceBefore() {
86         return spaceBefore;
87     }
88
89     /**
90      * Sets a candidate for space-before property.
91      *
92      * @param candidate instance of <code>RtfAttributes</code>, considered as
93      * a candidate for space-before adding
94      */

95     public void setSpaceBeforeCandidate(RtfAttributes candidate) {
96         if (updatingSpaceBefore) {
97             this.spaceBeforeCandidate = candidate;
98         }
99     }
100
101     /**
102      * Sets a candidate for space-after property.
103      *
104      * @param candidate instance of <code>RtfAttributes</code>, considered as
105      * a candidate for space-after adding
106      */

107     public void setSpaceAfterCandidate(RtfAttributes candidate) {
108         this.spaceAfterCandidate = candidate;
109     }
110
111     /** @return true, if candidate for space-before is set. */
112     public boolean isBeforeCadidateSet() {
113         return spaceBeforeCandidate != null;
114     }
115
116     /** @return true, if candidate for space-after is set. */
117     public boolean isAfterCadidateSet() {
118         return spaceAfterCandidate != null;
119     }
120     
121     /**
122      * Stops updating candidates for space-before attribute.
123      */

124     public void stopUpdatingSpaceBefore() {
125         updatingSpaceBefore = false;
126     }
127
128     /**
129      * Adds corresponding attributes to their candidates.
130      *
131      * @return integer, representing value of space-before/space-after
132      * attributes, that can't be added anywhere (i.e. these attributes
133      * hasn't their candidates)
134      */

135     public int flush() {
136         int accumulatingSpace = 0;
137         if (!isBeforeCadidateSet()) {
138             accumulatingSpace += spaceBefore;
139         } else {
140             spaceBeforeCandidate.addIntegerValue(spaceBefore,
141                     RtfText.SPACE_BEFORE);
142         }
143
144         if (!isAfterCadidateSet()) {
145             accumulatingSpace += spaceAfter;
146         } else {
147             spaceAfterCandidate.addIntegerValue(spaceAfter,
148                             RtfText.SPACE_AFTER);
149         }
150
151         return accumulatingSpace;
152     }
153 }
154
Popular Tags