Line 0
Link Here
|
|
|
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 |
package org.apache.fop.render.odf.odt.tags; |
19 |
|
20 |
import java.util.Iterator; |
21 |
|
22 |
import org.odftoolkit.odfdom.dom.element.style.StyleParagraphPropertiesElement; |
23 |
import org.odftoolkit.odfdom.dom.element.style.StyleStyleElement; |
24 |
import org.odftoolkit.odfdom.dom.element.style.StyleTextPropertiesElement; |
25 |
import org.odftoolkit.odfdom.dom.element.table.TableTableCellElement; |
26 |
import org.odftoolkit.odfdom.dom.element.text.TextBookmarkElement; |
27 |
import org.odftoolkit.odfdom.dom.element.text.TextLineBreakElement; |
28 |
import org.odftoolkit.odfdom.dom.element.text.TextListItemElement; |
29 |
import org.odftoolkit.odfdom.dom.element.text.TextPElement; |
30 |
import org.odftoolkit.odfdom.pkg.OdfElement; |
31 |
import org.odftoolkit.odfdom.pkg.OdfXMLFactory; |
32 |
|
33 |
import org.apache.fop.fo.FONode; |
34 |
import org.apache.fop.fo.FOText; |
35 |
import org.apache.fop.fo.flow.Block; |
36 |
import org.apache.fop.fo.flow.ListBlock; |
37 |
import org.apache.fop.fo.flow.table.Table; |
38 |
import org.apache.fop.layoutmgr.BlockLayoutManager; |
39 |
import org.apache.fop.render.odf.OdfException; |
40 |
import org.apache.fop.render.odf.odt.Namespace; |
41 |
import org.apache.fop.render.odf.odt.Style; |
42 |
import org.apache.fop.render.odf.odt.Style.Params; |
43 |
/** |
44 |
* Block converter |
45 |
*/ |
46 |
public class BlockTag extends Tag { |
47 |
|
48 |
private Block bl = null; |
49 |
|
50 |
private TextPElement paragraph = null; |
51 |
|
52 |
private OdfElement paragraphContainer = null; |
53 |
|
54 |
@Override |
55 |
protected OdfElement getTextContainer() { |
56 |
if (this.paragraph != null) { |
57 |
return this.paragraph; |
58 |
} else { |
59 |
return parent.getTextContainer(); |
60 |
} |
61 |
} |
62 |
|
63 |
/** |
64 |
* {@inheritDoc} |
65 |
* @throws OdfException |
66 |
*/ |
67 |
@Override |
68 |
protected TextPElement getParagraph() throws OdfException { |
69 |
if (this.paragraph != null) { |
70 |
return this.paragraph; |
71 |
} else { |
72 |
return parent.getParagraph(); |
73 |
} |
74 |
} |
75 |
|
76 |
/** |
77 |
* {@inheritDoc} |
78 |
*/ |
79 |
@Override |
80 |
protected OdfElement getParagraphContainer() { |
81 |
if (paragraphContainer != null) { |
82 |
return this.paragraphContainer; |
83 |
} else { |
84 |
return parent.getParagraphContainer(); |
85 |
} |
86 |
} |
87 |
|
88 |
/** |
89 |
* Constructor |
90 |
* @throws OdfException |
91 |
*/ |
92 |
BlockTag(Tag parent, Block bl) throws OdfException { |
93 |
super(parent); |
94 |
this.bl = bl; |
95 |
this.currentStyle.getParameters().put(Params.FONT_NAME, bl.getCommonFont().getFirstFontFamily().split(",")[0]); |
96 |
this.currentStyle.getParameters().put(Params.FONT_STYLE, Style.fopFontToOdfFontStyle( bl.getCommonFont())); |
97 |
this.currentStyle.getParameters().put(Params.FONT_WEIGHT, Style.fopFontToOdfFontWeight( bl.getCommonFont())); |
98 |
this.currentStyle.getParameters().put(Params.FONT_SIZE, Style.fopLengthToPt( bl.getCommonFont().getFontSize())); |
99 |
this.currentStyle.getParameters().put(Params.COLOR, Style.awtColorToOdfColor( bl.getColor())); |
100 |
this.currentStyle.getParameters().put(Params.TEXT_ALIGN, Style.fopAlignToOdfAlign( bl.getTextAlign())); |
101 |
this.currentStyle.getParameters().put(Params.SPACE_BEFORE, Style.fopSpaceBeforeToPt( bl.getCommonMarginBlock().spaceBefore, new BlockLayoutManager(bl))); |
102 |
this.currentStyle.getParameters().put(Params.SPACE_AFTER, Style.fopSpaceAfterToPt( bl.getCommonMarginBlock().spaceAfter, new BlockLayoutManager(bl))); |
103 |
this.currentStyle.getParameters().put(Params.MARGIN_LEFT, Style.fopLengthToPt( bl.getCommonMarginBlock().marginLeft)); |
104 |
this.currentStyle.getParameters().put(Params.MARGIN_RIGHT, Style.fopLengthToPt( bl.getCommonMarginBlock().marginRight)); |
105 |
this.currentStyle.getParameters().put(Params.BACKGROUND_COLOR, Style.awtColorToString( bl.getCommonBorderPaddingBackground().backgroundColor)); |
106 |
|
107 |
registerFont(this.currentStyle.getParameters().get(Params.FONT_NAME)); |
108 |
} |
109 |
|
110 |
/** |
111 |
* {@inheritDoc} |
112 |
* @throws OdfException |
113 |
*/ |
114 |
@Override |
115 |
public void execute() throws OdfException { |
116 |
parent.executeFromParent(this); |
117 |
} |
118 |
|
119 |
/** |
120 |
* {@inheritDoc} |
121 |
* @throws OdfException |
122 |
*/ |
123 |
@Override |
124 |
public void execute(Tag tag) throws OdfException { |
125 |
|
126 |
if (!bl.hasChildren()) { |
127 |
addLineBreak(); |
128 |
} else { |
129 |
if (!childrenAreOnlyBlocks(this.bl)) { |
130 |
this.paragraph = this.newParagraph(parent.getParagraphContainer()); |
131 |
|
132 |
if (bl.getId() != null) { |
133 |
insertBookmark(bl.getId()); |
134 |
} |
135 |
|
136 |
StyleStyleElement sse = null; |
137 |
try { |
138 |
sse = (StyleStyleElement) OdfXMLFactory.newOdfElement(this.getOdt().getContentDom(), StyleStyleElement.ELEMENT_NAME); |
139 |
} catch (Exception e) { |
140 |
throw new OdfException("Can't create new pragraph.", e); |
141 |
} |
142 |
|
143 |
sse.setAttributeNS(Namespace.STYLE, "style:family", "paragraph"); |
144 |
|
145 |
StyleParagraphPropertiesElement sppe = null; |
146 |
try { |
147 |
sppe = (StyleParagraphPropertiesElement) OdfXMLFactory.newOdfElement(this.getOdt().getContentDom(), StyleParagraphPropertiesElement.ELEMENT_NAME); |
148 |
} catch (Exception e) { |
149 |
throw new OdfException("Can't create new pragraph.", e); |
150 |
} |
151 |
|
152 |
sppe.setAttributeNS(Namespace.FO, "fo:margin-top", this.currentStyle.get(Params.SPACE_BEFORE)); |
153 |
sppe.setAttributeNS(Namespace.FO, "fo:margin-bottom", this.currentStyle.get(Params.SPACE_AFTER)); |
154 |
sppe.setAttributeNS(Namespace.FO, "fo:margin-left", this.currentStyle.get(Params.MARGIN_LEFT)); |
155 |
sppe.setAttributeNS(Namespace.FO, "fo:margin-right", this.currentStyle.get(Params.MARGIN_RIGHT)); |
156 |
sppe.setAttributeNS(Namespace.FO, "fo:background-color", this.currentStyle.get(Params.BACKGROUND_COLOR)); |
157 |
sppe.setAttributeNS(Namespace.FO, "fo:text-align", this.currentStyle.get(Params.TEXT_ALIGN)); |
158 |
sse.appendChild(sppe); |
159 |
|
160 |
StyleTextPropertiesElement stpe = null; |
161 |
|
162 |
try { |
163 |
stpe = (StyleTextPropertiesElement) OdfXMLFactory.newOdfElement(this.getOdt().getContentDom(), StyleTextPropertiesElement.ELEMENT_NAME); |
164 |
} catch (Exception e) { |
165 |
throw new OdfException("Can't create new pragraph.", e); |
166 |
} |
167 |
|
168 |
stpe.setAttributeNS(Namespace.STYLE, "style:font-name", this.currentStyle.get(Params.FONT_NAME)); |
169 |
stpe.setAttributeNS(Namespace.FO, "fo:color", this.currentStyle.get(Params.COLOR)); |
170 |
stpe.setAttributeNS(Namespace.FO, "fo:font-size", this.currentStyle.get(Params.FONT_SIZE)); |
171 |
stpe.setAttributeNS(Namespace.FO, "fo:font-style", this.currentStyle.get(Params.FONT_STYLE)); |
172 |
stpe.setAttributeNS(Namespace.FO, "fo:font-weight", this.currentStyle.get(Params.FONT_WEIGHT)); |
173 |
|
174 |
sse.appendChild(stpe); |
175 |
|
176 |
this.paragraph.setStyleName(this.appendNewStyle(sse)); |
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
private void insertBookmark(String id) throws OdfException { |
182 |
TextBookmarkElement tbe = null; |
183 |
try { |
184 |
tbe = (TextBookmarkElement) OdfXMLFactory.newOdfElement(this.getOdt().getContentDom(), TextBookmarkElement.ELEMENT_NAME); |
185 |
} catch (Exception e) { |
186 |
throw new OdfException("Can't create link intern.", e); |
187 |
} |
188 |
tbe.setAttributeNS(Namespace.TEXT, "text:name", id); |
189 |
this.paragraph.appendChild(tbe); |
190 |
} |
191 |
|
192 |
/** |
193 |
* {@inheritDoc} |
194 |
* @throws OdfException |
195 |
*/ |
196 |
@Override |
197 |
public void execute(TableCellTag tag) throws OdfException { |
198 |
TableTableCellElement ttce = tag.getTableCellElement(); |
199 |
|
200 |
this.paragraphContainer = ttce; |
201 |
|
202 |
if (!childrenAreOnlyBlocks(this.bl)) { |
203 |
this.paragraph = this.newParagraph(this.paragraphContainer); |
204 |
} |
205 |
} |
206 |
|
207 |
/** |
208 |
* {@inheritDoc} |
209 |
* @throws OdfException |
210 |
*/ |
211 |
@Override |
212 |
public void execute(ListItemTag tag) throws OdfException { |
213 |
TextListItemElement tlie = tag.getTextListItemElement(); |
214 |
|
215 |
this.paragraphContainer = tlie; |
216 |
|
217 |
if (!childrenAreOnlyBlocks(this.bl)) { |
218 |
this.paragraph = this.newParagraph(this.paragraphContainer); |
219 |
} |
220 |
} |
221 |
|
222 |
private void addLineBreak() throws OdfException { |
223 |
TextLineBreakElement tlb = null; |
224 |
try { |
225 |
tlb = (TextLineBreakElement) OdfXMLFactory.newOdfElement(this.getOdt().getContentDom(), TextLineBreakElement.ELEMENT_NAME); |
226 |
} catch (Exception e) { |
227 |
e.printStackTrace(); |
228 |
} |
229 |
|
230 |
this.getParagraph().appendChild(tlb); |
231 |
} |
232 |
|
233 |
private boolean childrenAreOnlyBlocks(Block bl) { |
234 |
|
235 |
if (bl.hasChildren()) { |
236 |
for (Iterator<?> it = bl.getChildNodes(); it.hasNext();) { |
237 |
FONode next = (FONode) it.next(); |
238 |
if (!(next instanceof Block || next instanceof ListBlock || next instanceof Table) ) { |
239 |
if (next instanceof FOText) { |
240 |
if ( ((FOText) next).length() != 0) { |
241 |
return false; |
242 |
} |
243 |
} else { |
244 |
return false; |
245 |
} |
246 |
} |
247 |
} |
248 |
} else { |
249 |
return false; |
250 |
} |
251 |
|
252 |
return true; |
253 |
} |
254 |
|
255 |
/** |
256 |
* {@inheritDoc} |
257 |
* @throws OdfException |
258 |
*/ |
259 |
public void executeFromParent(TagExecutable child) throws OdfException { |
260 |
child.execute(this); |
261 |
} |
262 |
} |