001: /*
002: * $Id: RtfParagraphStyle.java 2843 2007-06-19 11:40:33Z psoares33 $
003: * $Name$
004: *
005: * Copyright 2001, 2002, 2003, 2004 by Mark Hall
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050: package com.lowagie.text.rtf.style;
051:
052: import java.awt.Color;
053: import java.io.ByteArrayOutputStream;
054: import java.io.IOException;
055: import java.io.OutputStream;
056:
057: import com.lowagie.text.Element;
058: import com.lowagie.text.Font;
059: import com.lowagie.text.rtf.RtfBasicElement;
060: import com.lowagie.text.rtf.document.RtfDocument;
061: import com.lowagie.text.rtf.text.RtfParagraph;
062:
063: /**
064: * The RtfParagraphStyle stores all style/formatting attributes of a RtfParagraph.
065: * Additionally it also supports the style name system available in RTF. The RtfParagraphStyle
066: * is a Font and can thus be used as such. To use the stylesheet functionality
067: * it needs to be set as the font of a Paragraph. Otherwise it will work like a
068: * RtfFont. It also supports inheritance of styles.
069: *
070: * @version $Id: RtfParagraphStyle.java 2843 2007-06-19 11:40:33Z psoares33 $
071: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
072: * @author Thomas Bickel (tmb99@inode.at)
073: */
074: public class RtfParagraphStyle extends RtfFont {
075:
076: /**
077: * Constant for left alignment
078: */
079: public static final byte[] ALIGN_LEFT = "\\ql".getBytes();
080: /**
081: * Constant for right alignment
082: */
083: public static final byte[] ALIGN_RIGHT = "\\qr".getBytes();
084: /**
085: * Constant for center alignment
086: */
087: public static final byte[] ALIGN_CENTER = "\\qc".getBytes();
088: /**
089: * Constant for justified alignment
090: */
091: public static final byte[] ALIGN_JUSTIFY = "\\qj".getBytes();
092: /**
093: * Constant for the first line indentation
094: */
095: public static final byte[] FIRST_LINE_INDENT = "\\fi".getBytes();
096: /**
097: * Constant for left indentation
098: */
099: public static final byte[] INDENT_LEFT = "\\li".getBytes();
100: /**
101: * Constant for right indentation
102: */
103: public static final byte[] INDENT_RIGHT = "\\ri".getBytes();
104: /**
105: * Constant for keeping the paragraph together on one page
106: */
107: public static final byte[] KEEP_TOGETHER = "\\keep".getBytes();
108: /**
109: * Constant for keeping the paragraph toghether with the next one on one page
110: */
111: public static final byte[] KEEP_TOGETHER_WITH_NEXT = "\\keepn"
112: .getBytes();
113: /**
114: * Constant for the space after the paragraph.
115: */
116: public static final byte[] SPACING_AFTER = "\\sa".getBytes();
117: /**
118: * Constant for the space before the paragraph.
119: */
120: public static final byte[] SPACING_BEFORE = "\\sb".getBytes();
121:
122: /**
123: * The NORMAL/STANDARD style.
124: */
125: public static final RtfParagraphStyle STYLE_NORMAL = new RtfParagraphStyle(
126: "Normal", "Arial", 12, Font.NORMAL, Color.black);
127: /**
128: * The style for level 1 headings.
129: */
130: public static final RtfParagraphStyle STYLE_HEADING_1 = new RtfParagraphStyle(
131: "heading 1", "Normal");
132: /**
133: * The style for level 2 headings.
134: */
135: public static final RtfParagraphStyle STYLE_HEADING_2 = new RtfParagraphStyle(
136: "heading 2", "Normal");
137: /**
138: * The style for level 3 headings.
139: */
140: public static final RtfParagraphStyle STYLE_HEADING_3 = new RtfParagraphStyle(
141: "heading 3", "Normal");
142:
143: /**
144: * Initialises the properties of the styles.
145: */
146: static {
147: STYLE_HEADING_1.setSize(16);
148: STYLE_HEADING_1.setStyle(Font.BOLD);
149: STYLE_HEADING_2.setSize(14);
150: STYLE_HEADING_2.setStyle(Font.BOLDITALIC);
151: STYLE_HEADING_3.setSize(13);
152: STYLE_HEADING_3.setStyle(Font.BOLD);
153: }
154:
155: /**
156: * No modification has taken place when compared to the RtfParagraphStyle this RtfParagraphStyle
157: * is based on. These modification markers are used to determine what needs to be
158: * inherited and what not from the parent RtfParagraphStyle.
159: */
160: private static final int MODIFIED_NONE = 0;
161: /**
162: * The alignment has been modified.
163: */
164: private static final int MODIFIED_ALIGNMENT = 1;
165: /**
166: * The left indentation has been modified.
167: */
168: private static final int MODIFIED_INDENT_LEFT = 2;
169: /**
170: * The right indentation has been modified.
171: */
172: private static final int MODIFIED_INDENT_RIGHT = 4;
173: /**
174: * The spacing before a paragraph has been modified.
175: */
176: private static final int MODIFIED_SPACING_BEFORE = 8;
177: /**
178: * The spacing after a paragraph has been modified.
179: */
180: private static final int MODIFIED_SPACING_AFTER = 16;
181: /**
182: * The font name has been modified.
183: */
184: private static final int MODIFIED_FONT_NAME = 32;
185: /**
186: * The font style has been modified.
187: */
188: private static final int MODIFIED_FONT_SIZE = 64;
189: /**
190: * The font size has been modified.
191: */
192: private static final int MODIFIED_FONT_STYLE = 128;
193: /**
194: * The font colour has been modified.
195: */
196: private static final int MODIFIED_FONT_COLOR = 256;
197: /**
198: * The line leading has been modified.
199: */
200: private static final int MODIFIED_LINE_LEADING = 512;
201: /**
202: * The paragraph keep together setting has been modified.
203: */
204: private static final int MODIFIED_KEEP_TOGETHER = 1024;
205: /**
206: * The paragraph keep together with next setting has been modified.
207: */
208: private static final int MODIFIED_KEEP_TOGETHER_WITH_NEXT = 2048;
209:
210: /**
211: * The alignment of the paragraph.
212: */
213: private int alignment = Element.ALIGN_LEFT;
214: /**
215: * The indentation for the first line
216: */
217: private int firstLineIndent = 0;
218: /**
219: * The left indentation of the paragraph.
220: */
221: private int indentLeft = 0;
222: /**
223: * The right indentation of the paragraph.
224: */
225: private int indentRight = 0;
226: /**
227: * The spacing before a paragraph.
228: */
229: private int spacingBefore = 0;
230: /**
231: * The spacing after a paragraph.
232: */
233: private int spacingAfter = 0;
234: /**
235: * The line leading of the paragraph.
236: */
237: private int lineLeading = 0;
238: /**
239: * Whether this RtfParagraph must stay on one page.
240: */
241: private boolean keepTogether = false;
242: /**
243: * Whether this RtfParagraph must stay on the same page as the next paragraph.
244: */
245: private boolean keepTogetherWithNext = false;
246: /**
247: * The name of this RtfParagraphStyle.
248: */
249: private String styleName = "";
250: /**
251: * The name of the RtfParagraphStyle this RtfParagraphStyle is based on.
252: */
253: private String basedOnName = null;
254: /**
255: * The RtfParagraphStyle this RtfParagraphStyle is based on.
256: */
257: private RtfParagraphStyle baseStyle = null;
258: /**
259: * Which properties have been modified when compared to the base style.
260: */
261: private int modified = MODIFIED_NONE;
262: /**
263: * The number of this RtfParagraphStyle in the stylesheet list.
264: */
265: private int styleNumber = -1;
266:
267: /**
268: * Constructs a new RtfParagraphStyle with the given attributes.
269: *
270: * @param styleName The name of this RtfParagraphStyle.
271: * @param fontName The name of the font to use for this RtfParagraphStyle.
272: * @param fontSize The size of the font to use for this RtfParagraphStyle.
273: * @param fontStyle The style of the font to use for this RtfParagraphStyle.
274: * @param fontColor The colour of the font to use for this RtfParagraphStyle.
275: */
276: public RtfParagraphStyle(String styleName, String fontName,
277: int fontSize, int fontStyle, Color fontColor) {
278: super (null, new RtfFont(fontName, fontSize, fontStyle,
279: fontColor));
280: this .styleName = styleName;
281: }
282:
283: /**
284: * Constructs a new RtfParagraphStyle that is based on an existing RtfParagraphStyle.
285: *
286: * @param styleName The name of this RtfParagraphStyle.
287: * @param basedOnName The name of the RtfParagraphStyle this RtfParagraphStyle is based on.
288: */
289: public RtfParagraphStyle(String styleName, String basedOnName) {
290: super (null, new Font());
291: this .styleName = styleName;
292: this .basedOnName = basedOnName;
293: }
294:
295: /**
296: * Constructs a RtfParagraphStyle from another RtfParagraphStyle.
297: *
298: * INTERNAL USE ONLY
299: *
300: * @param doc The RtfDocument this RtfParagraphStyle belongs to.
301: * @param style The RtfParagraphStyle to copy settings from.
302: */
303: public RtfParagraphStyle(RtfDocument doc, RtfParagraphStyle style) {
304: super (doc, style);
305: this .document = doc;
306: this .styleName = style.getStyleName();
307: this .alignment = style.getAlignment();
308: this .firstLineIndent = (int) (style.getFirstLineIndent() * RtfBasicElement.TWIPS_FACTOR);
309: this .indentLeft = (int) (style.getIndentLeft() * RtfBasicElement.TWIPS_FACTOR);
310: this .indentRight = (int) (style.getIndentRight() * RtfBasicElement.TWIPS_FACTOR);
311: this .spacingBefore = (int) (style.getSpacingBefore() * RtfBasicElement.TWIPS_FACTOR);
312: this .spacingAfter = (int) (style.getSpacingAfter() * RtfBasicElement.TWIPS_FACTOR);
313: this .lineLeading = (int) (style.getLineLeading() * RtfBasicElement.TWIPS_FACTOR);
314: this .keepTogether = style.getKeepTogether();
315: this .keepTogetherWithNext = style.getKeepTogetherWithNext();
316: this .basedOnName = style.basedOnName;
317: this .modified = style.modified;
318: this .styleNumber = style.getStyleNumber();
319:
320: if (this .document != null) {
321: setRtfDocument(this .document);
322: }
323: }
324:
325: /**
326: * Gets the name of this RtfParagraphStyle.
327: *
328: * @return The name of this RtfParagraphStyle.
329: */
330: public String getStyleName() {
331: return this .styleName;
332: }
333:
334: /**
335: * Gets the name of the RtfParagraphStyle this RtfParagraphStyle is based on.
336: *
337: * @return The name of the base RtfParagraphStyle.
338: */
339: public String getBasedOnName() {
340: return this .basedOnName;
341: }
342:
343: /**
344: * Gets the alignment of this RtfParagraphStyle.
345: *
346: * @return The alignment of this RtfParagraphStyle.
347: */
348: public int getAlignment() {
349: return this .alignment;
350: }
351:
352: /**
353: * Sets the alignment of this RtfParagraphStyle.
354: *
355: * @param alignment The alignment to use.
356: */
357: public void setAlignment(int alignment) {
358: this .modified = this .modified | MODIFIED_ALIGNMENT;
359: this .alignment = alignment;
360: }
361:
362: /**
363: * Gets the first line indentation of this RtfParagraphStyle.
364: *
365: * @return The first line indentation of this RtfParagraphStyle.
366: */
367: public int getFirstLineIndent() {
368: return this .firstLineIndent;
369: }
370:
371: /**
372: * Sets the first line indententation of this RtfParagraphStyle. It
373: * is relative to the left indentation.
374: *
375: * @param firstLineIndent The first line indentation to use.
376: */
377: public void setFirstLineIndent(int firstLineIndent) {
378: this .firstLineIndent = firstLineIndent;
379: }
380:
381: /**
382: * Gets the left indentation of this RtfParagraphStyle.
383: *
384: * @return The left indentation of this RtfParagraphStyle.
385: */
386: public int getIndentLeft() {
387: return this .indentLeft;
388: }
389:
390: /**
391: * Sets the left indentation of this RtfParagraphStyle.
392: *
393: * @param indentLeft The left indentation to use.
394: */
395: public void setIndentLeft(int indentLeft) {
396: this .modified = this .modified | MODIFIED_INDENT_LEFT;
397: this .indentLeft = indentLeft;
398: }
399:
400: /**
401: * Gets the right indentation of this RtfParagraphStyle.
402: *
403: * @return The right indentation of this RtfParagraphStyle.
404: */
405: public int getIndentRight() {
406: return this .indentRight;
407: }
408:
409: /**
410: * Sets the right indentation of this RtfParagraphStyle.
411: *
412: * @param indentRight The right indentation to use.
413: */
414: public void setIndentRight(int indentRight) {
415: this .modified = this .modified | MODIFIED_INDENT_RIGHT;
416: this .indentRight = indentRight;
417: }
418:
419: /**
420: * Gets the space before the paragraph of this RtfParagraphStyle..
421: *
422: * @return The space before the paragraph.
423: */
424: public int getSpacingBefore() {
425: return this .spacingBefore;
426: }
427:
428: /**
429: * Sets the space before the paragraph of this RtfParagraphStyle.
430: *
431: * @param spacingBefore The space before to use.
432: */
433: public void setSpacingBefore(int spacingBefore) {
434: this .modified = this .modified | MODIFIED_SPACING_BEFORE;
435: this .spacingBefore = spacingBefore;
436: }
437:
438: /**
439: * Gets the space after the paragraph of this RtfParagraphStyle.
440: *
441: * @return The space after the paragraph.
442: */
443: public int getSpacingAfter() {
444: return this .spacingAfter;
445: }
446:
447: /**
448: * Sets the space after the paragraph of this RtfParagraphStyle.
449: *
450: * @param spacingAfter The space after to use.
451: */
452: public void setSpacingAfter(int spacingAfter) {
453: this .modified = this .modified | MODIFIED_SPACING_AFTER;
454: this .spacingAfter = spacingAfter;
455: }
456:
457: /**
458: * Sets the font name of this RtfParagraphStyle.
459: *
460: * @param fontName The font name to use
461: */
462: public void setFontName(String fontName) {
463: this .modified = this .modified | MODIFIED_FONT_NAME;
464: super .setFontName(fontName);
465: }
466:
467: /**
468: * Sets the font size of this RtfParagraphStyle.
469: *
470: * @param fontSize The font size to use.
471: */
472: public void setSize(float fontSize) {
473: this .modified = this .modified | MODIFIED_FONT_SIZE;
474: super .setSize(fontSize);
475: }
476:
477: /**
478: * Sets the font style of this RtfParagraphStyle.
479: *
480: * @param fontStyle The font style to use.
481: */
482: public void setStyle(int fontStyle) {
483: this .modified = this .modified | MODIFIED_FONT_STYLE;
484: super .setStyle(fontStyle);
485: }
486:
487: /**
488: * Sets the colour of this RtfParagraphStyle.
489: *
490: * @param color The Color to use.
491: */
492: public void setColor(Color color) {
493: this .modified = this .modified | MODIFIED_FONT_COLOR;
494: super .setColor(color);
495: }
496:
497: /**
498: * Gets the line leading of this RtfParagraphStyle.
499: *
500: * @return The line leading of this RtfParagraphStyle.
501: */
502: public int getLineLeading() {
503: return this .lineLeading;
504: }
505:
506: /**
507: * Sets the line leading of this RtfParagraphStyle.
508: *
509: * @param lineLeading The line leading to use.
510: */
511: public void setLineLeading(int lineLeading) {
512: this .lineLeading = lineLeading;
513: this .modified = this .modified | MODIFIED_LINE_LEADING;
514: }
515:
516: /**
517: * Gets whether the lines in the paragraph should be kept together in
518: * this RtfParagraphStyle.
519: *
520: * @return Whether the lines in the paragraph should be kept together.
521: */
522: public boolean getKeepTogether() {
523: return this .keepTogether;
524: }
525:
526: /**
527: * Sets whether the lines in the paragraph should be kept together in
528: * this RtfParagraphStyle.
529: *
530: * @param keepTogether Whether the lines in the paragraph should be kept together.
531: */
532: public void setKeepTogether(boolean keepTogether) {
533: this .keepTogether = keepTogether;
534: this .modified = this .modified | MODIFIED_KEEP_TOGETHER;
535: }
536:
537: /**
538: * Gets whether the paragraph should be kept toggether with the next in
539: * this RtfParagraphStyle.
540: *
541: * @return Whether the paragraph should be kept together with the next.
542: */
543: public boolean getKeepTogetherWithNext() {
544: return this .keepTogetherWithNext;
545: }
546:
547: /**
548: * Sets whether the paragraph should be kept together with the next in
549: * this RtfParagraphStyle.
550: *
551: * @param keepTogetherWithNext Whether the paragraph should be kept together with the next.
552: */
553: public void setKeepTogetherWithNext(boolean keepTogetherWithNext) {
554: this .keepTogetherWithNext = keepTogetherWithNext;
555: this .modified = this .modified
556: | MODIFIED_KEEP_TOGETHER_WITH_NEXT;
557: }
558:
559: /**
560: * Handles the inheritance of paragraph style settings. All settings that
561: * have not been modified will be inherited from the base RtfParagraphStyle.
562: * If this RtfParagraphStyle is not based on another one, then nothing happens.
563: */
564: public void handleInheritance() {
565: if (this .basedOnName != null
566: && this .document.getDocumentHeader()
567: .getRtfParagraphStyle(this .basedOnName) != null) {
568: this .baseStyle = this .document.getDocumentHeader()
569: .getRtfParagraphStyle(this .basedOnName);
570: this .baseStyle.handleInheritance();
571: if (!((this .modified & MODIFIED_ALIGNMENT) == MODIFIED_ALIGNMENT)) {
572: this .alignment = this .baseStyle.getAlignment();
573: }
574: if (!((this .modified & MODIFIED_INDENT_LEFT) == MODIFIED_INDENT_LEFT)) {
575: this .indentLeft = this .baseStyle.getIndentLeft();
576: }
577: if (!((this .modified & MODIFIED_INDENT_RIGHT) == MODIFIED_INDENT_RIGHT)) {
578: this .indentRight = this .baseStyle.getIndentRight();
579: }
580: if (!((this .modified & MODIFIED_SPACING_BEFORE) == MODIFIED_SPACING_BEFORE)) {
581: this .spacingBefore = this .baseStyle.getSpacingBefore();
582: }
583: if (!((this .modified & MODIFIED_SPACING_AFTER) == MODIFIED_SPACING_AFTER)) {
584: this .spacingAfter = this .baseStyle.getSpacingAfter();
585: }
586: if (!((this .modified & MODIFIED_FONT_NAME) == MODIFIED_FONT_NAME)) {
587: setFontName(this .baseStyle.getFontName());
588: }
589: if (!((this .modified & MODIFIED_FONT_SIZE) == MODIFIED_FONT_SIZE)) {
590: setSize(this .baseStyle.getFontSize());
591: }
592: if (!((this .modified & MODIFIED_FONT_STYLE) == MODIFIED_FONT_STYLE)) {
593: setStyle(this .baseStyle.getFontStyle());
594: }
595: if (!((this .modified & MODIFIED_FONT_COLOR) == MODIFIED_FONT_COLOR)) {
596: setColor(this .baseStyle.getColor());
597: }
598: if (!((this .modified & MODIFIED_LINE_LEADING) == MODIFIED_LINE_LEADING)) {
599: setLineLeading(this .baseStyle.getLineLeading());
600: }
601: if (!((this .modified & MODIFIED_KEEP_TOGETHER) == MODIFIED_KEEP_TOGETHER)) {
602: setKeepTogether(this .baseStyle.getKeepTogether());
603: }
604: if (!((this .modified & MODIFIED_KEEP_TOGETHER_WITH_NEXT) == MODIFIED_KEEP_TOGETHER_WITH_NEXT)) {
605: setKeepTogetherWithNext(this .baseStyle
606: .getKeepTogetherWithNext());
607: }
608: }
609: }
610:
611: /**
612: * Writes the settings of this RtfParagraphStyle.
613: *
614: * @return A byte array with the settings of this RtfParagraphStyle.
615: */
616: private byte[] writeParagraphSettings() {
617: ByteArrayOutputStream result = new ByteArrayOutputStream();
618: try {
619: if (this .keepTogether) {
620: result.write(RtfParagraphStyle.KEEP_TOGETHER);
621: }
622: if (this .keepTogetherWithNext) {
623: result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT);
624: }
625: switch (alignment) {
626: case Element.ALIGN_LEFT:
627: result.write(RtfParagraphStyle.ALIGN_LEFT);
628: break;
629: case Element.ALIGN_RIGHT:
630: result.write(RtfParagraphStyle.ALIGN_RIGHT);
631: break;
632: case Element.ALIGN_CENTER:
633: result.write(RtfParagraphStyle.ALIGN_CENTER);
634: break;
635: case Element.ALIGN_JUSTIFIED:
636: case Element.ALIGN_JUSTIFIED_ALL:
637: result.write(RtfParagraphStyle.ALIGN_JUSTIFY);
638: break;
639: }
640: result.write(FIRST_LINE_INDENT);
641: result.write(intToByteArray(this .firstLineIndent));
642: result.write(RtfParagraphStyle.INDENT_LEFT);
643: result.write(intToByteArray(indentLeft));
644: result.write(RtfParagraphStyle.INDENT_RIGHT);
645: result.write(intToByteArray(indentRight));
646: if (this .spacingBefore > 0) {
647: result.write(RtfParagraphStyle.SPACING_BEFORE);
648: result.write(intToByteArray(this .spacingBefore));
649: }
650: if (this .spacingAfter > 0) {
651: result.write(RtfParagraphStyle.SPACING_AFTER);
652: result.write(intToByteArray(this .spacingAfter));
653: }
654: if (this .lineLeading > 0) {
655: result.write(RtfParagraph.LINE_SPACING);
656: result.write(intToByteArray(this .lineLeading));
657: }
658: } catch (IOException ioe) {
659: ioe.printStackTrace();
660: }
661: return result.toByteArray();
662: }
663:
664: /**
665: * Writes the definition of this RtfParagraphStyle for the stylesheet list.
666: * @deprecated replaced by {@link #writeDefinition(OutputStream)}
667: */
668: public byte[] writeDefinition() {
669: ByteArrayOutputStream result = new ByteArrayOutputStream();
670: try {
671: writeDefinition(result);
672: } catch (IOException ioe) {
673: ioe.printStackTrace();
674: }
675: return result.toByteArray();
676: }
677:
678: /**
679: * Writes the definition of this RtfParagraphStyle for the stylesheet list.
680: */
681: public void writeDefinition(final OutputStream result)
682: throws IOException {
683: result.write("{".getBytes());
684: result.write("\\style".getBytes());
685: result.write("\\s".getBytes());
686: result.write(intToByteArray(this .styleNumber));
687: result.write(RtfBasicElement.DELIMITER);
688: result.write(writeParagraphSettings());
689: result.write(super .writeBegin());
690: result.write(RtfBasicElement.DELIMITER);
691: result.write(this .styleName.getBytes());
692: result.write(";".getBytes());
693: result.write("}".getBytes());
694: if (this .document.getDocumentSettings()
695: .isOutputDebugLineBreaks()) {
696: result.write('\n');
697: }
698: }
699:
700: /**
701: * Writes the start information of this RtfParagraphStyle.
702: */
703: public byte[] writeBegin() {
704: ByteArrayOutputStream result = new ByteArrayOutputStream();
705: try {
706: result.write("\\s".getBytes());
707: result.write(intToByteArray(this .styleNumber));
708: result.write(writeParagraphSettings());
709: } catch (IOException ioe) {
710: ioe.printStackTrace();
711: }
712: return result.toByteArray();
713: }
714:
715: /**
716: * Unused
717: * @return An empty byte array.
718: */
719: public byte[] writeEnd() {
720: return new byte[0];
721: }
722:
723: /**
724: * unused
725: * @deprecated replaced by {@link #writeContent(OutputStream)}
726: */
727: public byte[] write() {
728: return (new byte[0]);
729: }
730:
731: /**
732: * unused
733: */
734: public void writeContent(final OutputStream out) throws IOException {
735: }
736:
737: /**
738: * Tests whether two RtfParagraphStyles are equal. Equality
739: * is determined via the name.
740: */
741: public boolean equals(Object o) {
742: if (!(o instanceof RtfParagraphStyle)) {
743: return false;
744: }
745: RtfParagraphStyle paragraphStyle = (RtfParagraphStyle) o;
746: boolean result = this .getStyleName().equals(
747: paragraphStyle.getStyleName());
748: return result;
749: }
750:
751: /**
752: * Gets the hash code of this RtfParagraphStyle.
753: */
754: public int hashCode() {
755: return this .styleName.hashCode();
756: }
757:
758: /**
759: * Gets the number of this RtfParagraphStyle in the stylesheet list.
760: *
761: * @return The number of this RtfParagraphStyle in the stylesheet list.
762: */
763: private int getStyleNumber() {
764: return this .styleNumber;
765: }
766:
767: /**
768: * Sets the number of this RtfParagraphStyle in the stylesheet list.
769: *
770: * @param styleNumber The number to use.
771: */
772: protected void setStyleNumber(int styleNumber) {
773: this.styleNumber = styleNumber;
774: }
775: }
|