1 17 18 19 20 package org.apache.fop.render.rtf.rtflib.rtfdoc; 21 22 28 29 import java.io.Writer ; 30 import java.io.IOException ; 31 import java.util.List ; 32 33 38 39 public class RtfParagraph extends RtfBookmarkContainerImpl 40 implements IRtfTextContainer, IRtfPageBreakContainer, IRtfHyperLinkContainer, 41 IRtfExternalGraphicContainer, IRtfPageNumberContainer, 42 IRtfPageNumberCitationContainer { 43 private RtfText text; 44 private RtfHyperLink hyperlink; 45 private RtfExternalGraphic externalGraphic; 46 private RtfPageNumber pageNumber; 47 private RtfPageNumberCitation pageNumberCitation; 48 private boolean keepn = false; 50 private boolean resetProperties = false; 51 52 58 private boolean writeForBreak = false; 59 60 61 private static final String [] PARA_ATTRIBUTES = {"intbl"}; 62 63 64 RtfParagraph(IRtfParagraphContainer parent, Writer w) throws IOException { 65 super((RtfContainer)parent, w); 66 } 67 68 69 RtfParagraph(IRtfParagraphContainer parent, Writer w, RtfAttributes attr) throws IOException { 70 super((RtfContainer)parent, w, attr); 71 } 72 73 77 public String getText() { 78 return (text.getText()); 79 } 80 81 82 public void setKeepn() { 83 this.keepn = true; 84 } 85 86 87 public void setResetProperties() { 88 this.resetProperties = true; 89 } 90 91 95 public RtfAttributes getTextContainerAttributes() { 96 if (attrib == null) { 97 return null; 98 } 99 return (RtfAttributes)this.attrib.clone(); 100 } 101 102 106 protected void writeRtfPrefix() throws IOException { 107 108 if (resetProperties) { 110 writeControlWord("pard"); 111 } 112 113 118 writeAttributes(attrib, RtfText.ATTR_NAMES); 119 writeAttributes(attrib, PARA_ATTRIBUTES); 120 if (attrib.isSet("intbl") && mustWriteAttributes()) { 123 writeAttributes(attrib, RtfText.ALIGNMENT); 124 } 125 126 if (keepn) { 128 writeControlWord("keepn"); 129 } 130 131 if (mustWriteGroupMark()) { 133 writeGroupMark(true); 134 } 135 136 137 if (mustWriteAttributes()) { 138 if (!attrib.isSet("intbl")) { 142 writeAttributes(attrib, RtfText.ALIGNMENT); 143 } 144 writeAttributes(attrib, RtfText.BORDER); 146 writeAttributes(attrib, RtfText.INDENT); 147 writeAttributes(attrib, RtfText.TABS); 148 if (writeForBreak) { 149 writeControlWord("pard\\par"); 150 } 151 } 152 153 } 154 155 159 protected void writeRtfSuffix() throws IOException { 160 boolean writeMark = true; 162 if (parent instanceof RtfTableCell) { 163 writeMark = ((RtfTableCell)parent).paragraphNeedsPar(this); 164 } 165 if (writeMark) { 166 writeControlWord("par"); 167 } 168 169 if (mustWriteGroupMark()) { 170 writeGroupMark(false); 171 } 172 173 174 } 175 176 182 public RtfText newText(String str) throws IOException { 183 return newText(str, null); 184 } 185 186 193 public RtfText newText(String str, RtfAttributes attr) throws IOException { 194 closeAll(); 195 text = new RtfText(this, writer, str, attr); 196 return text; 197 } 198 199 203 public void newPageBreak() throws IOException { 204 writeForBreak = true; 205 new RtfPageBreak(this, writer); 206 } 207 208 212 public void newLineBreak() throws IOException { 213 new RtfLineBreak(this, writer); 214 } 215 216 221 public RtfPageNumber newPageNumber()throws IOException { 222 pageNumber = new RtfPageNumber(this, writer); 223 return pageNumber; 224 } 225 226 232 public RtfPageNumberCitation newPageNumberCitation(String id) throws IOException { 233 pageNumberCitation = new RtfPageNumberCitation(this, writer, id); 234 return pageNumberCitation; 235 } 236 237 244 public RtfHyperLink newHyperLink(String str, RtfAttributes attr) throws IOException { 245 hyperlink = new RtfHyperLink(this, writer, str, attr); 246 return hyperlink; 247 } 248 249 254 public RtfExternalGraphic newImage() throws IOException { 255 closeAll(); 256 externalGraphic = new RtfExternalGraphic(this, writer); 257 return externalGraphic; 258 } 259 260 private void closeCurrentText() throws IOException { 261 if (text != null) { 262 text.close(); 263 } 264 } 265 266 private void closeCurrentHyperLink() throws IOException { 267 if (hyperlink != null) { 268 hyperlink.close(); 269 } 270 } 271 272 private void closeAll() throws IOException { 273 closeCurrentText(); 274 closeCurrentHyperLink(); 275 } 276 277 281 protected boolean okToWriteRtf() { 282 boolean result = super.okToWriteRtf(); 283 284 if (parent.getOptions().ignoreEmptyParagraphs() && getChildCount() == 0) { 285 result = false; 288 } 289 290 return result; 291 } 292 293 294 private boolean mustWriteAttributes() { 295 boolean writeAttributes = false; 296 final int children = getChildCount(); 297 if (children > 0) { 298 final List childList = getChildren(); 299 for (int i = 0; i < children; i++) { 300 final RtfElement el = (RtfElement) childList.get(i); 301 if (!el.isEmpty()) { 302 if (el.getClass() == RtfText.class) { 303 boolean tmp = ((RtfText) el).isNbsp(); 304 if (!tmp) { 305 writeAttributes = true; 306 break; 307 } 308 } else { 309 writeAttributes = true; 310 break; 311 } 312 } 313 } 314 } 315 return writeAttributes; 316 } 317 318 324 private boolean mustWriteGroupMark() { 325 return getChildCount() > 0; 326 } 327 328 332 public RtfAttributes getTextAttributes() { 333 if (text == null) { 334 return null; 335 } 336 return text.getTextAttributes(); 337 } 338 } 339 | Popular Tags |