123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- /*
- * $Id$
- * ============================================================================
- * The Apache Software License, Version 1.1
- * ============================================================================
- *
- * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modifica-
- * tion, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * 3. The end-user documentation included with the redistribution, if any, must
- * include the following acknowledgment: "This product includes software
- * developed by the Apache Software Foundation (http://www.apache.org/)."
- * Alternately, this acknowledgment may appear in the software itself, if
- * and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "FOP" and "Apache Software Foundation" must not be used to
- * endorse or promote products derived from this software without prior
- * written permission. For written permission, please contact
- * apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache", nor may
- * "Apache" appear in their name, without prior written permission of the
- * Apache Software Foundation.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
- * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- * ============================================================================
- *
- * This software consists of voluntary contributions made by many individuals
- * on behalf of the Apache Software Foundation and was originally created by
- * James Tauber <jtauber@jtauber.com>. For more information on the Apache
- * Software Foundation, please see <http://www.apache.org/>.
- */
-
- /*
- * This file is part of the RTF library of the FOP project, which was originally
- * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
- * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
- * the FOP project.
- */
-
- package org.apache.fop.render.rtf.rtflib.rtfdoc;
-
- import java.io.Writer;
- import java.io.IOException;
- import java.util.Iterator;
- //import org.apache.fop.render.rtf.rtflib.jfor.main.JForVersionInfo;
-
- /** Base class for all elements of an RTF file.
- * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
- * @author Andreas Putz a.putz@skynamics.com
- */
- public abstract class RtfElement {
- /** Writer to be used */
- protected final Writer writer;
- /** parent element */
- protected final RtfContainer parent;
- /** attributes of the element */
- protected final RtfAttributes attrib;
- private boolean written;
- private boolean closed;
- private final int id;
- private static int idCounter;
-
- /** Create an RTF element as a child of given container */
- RtfElement(RtfContainer parent, Writer w) throws IOException {
- this(parent, w, null);
- }
-
- /** Create an RTF element as a child of given container with given attributes */
- RtfElement(RtfContainer parent, Writer w, RtfAttributes attr) throws IOException {
-
- id = idCounter++;
- this.parent = parent;
- attrib = (attr != null ? attr : new RtfAttributes());
- if (this.parent != null) {
- this.parent.addChild(this);
- }
- writer = w;
- written = false;
- }
-
- /**
- * Does nothing, meant to allow elements to write themselves without waiting
- * for write(), but not implemented yet
- * @throws IOException for I/O problems
- */
- public final void close() throws IOException {
- closed = true;
- }
-
- /**
- * Write the RTF code of this element to our Writer
- * @throws IOException for I/O problems
- */
- public final void writeRtf() throws IOException {
- if (!written) {
- written = true;
- if (okToWriteRtf()) {
- writeRtfPrefix();
- writeRtfContent();
- writeRtfSuffix();
- }
- }
- }
-
- /**
- * Write an RTF control word to our Writer
- * @param word RTF control word to write
- * @throws IOException for I/O problems
- */
- protected final void writeControlWord(String word)
- throws IOException {
- writer.write('\\');
- writer.write(word);
- writer.write(' ');
- }
-
- /**
- * Write an RTF control word to our Writer, preceeded by a star '*'
- * meaning "ignore this if you don't know what it means"
- * @param word RTF control word to write
- * @throws IOException for I/O problems
- */
- protected final void writeStarControlWord(String word)
- throws IOException {
- writer.write("\\*\\");
- writer.write(word);
- writer.write(' ');
- }
-
- /**
- * Same as writeStarControlWord(String word), except with no space behind it
- * @param word RTF control word to write
- * @throws IOException for I/O problems
- */
- protected final void writeStarControlWordNS(String word)
- throws IOException {
- writer.write("\\*\\");
- writer.write(word);
- }
-
- /**
- * Write rtf control word without the space behind it
- * @param word RTF control word to write
- * @throws IOException for I/O problems
- */
- protected final void writeControlWordNS(String word)
- throws IOException {
- writer.write('\\');
- writer.write(word);
- }
-
- /**
- * Called before writeRtfContent()
- * @throws IOException for I/O problems
- */
- protected void writeRtfPrefix() throws IOException {
- }
-
- /**
- * Must be implemented to write RTF content to m_writer
- * @throws IOException for I/O problems
- */
- protected abstract void writeRtfContent() throws IOException;
-
- /**
- * Called after writeRtfContent()
- * @throws IOException for I/O problems
- */
- protected void writeRtfSuffix() throws IOException {
- }
-
- /**
- * Write a start or end group mark
- * @param isStart set to true if this is a start mark
- * @throws IOException for I/O problems
- */
- protected final void writeGroupMark(boolean isStart)
- throws IOException {
- writer.write(isStart ? "{" : "}");
- }
-
- /**
- * Write given attribute values to our Writer
- * @param attr RtfAttributes to be written
- * @param nameList if given, only attribute names from this list are considered
- * @throws IOException for I/O problems
- */
- protected void writeAttributes(RtfAttributes attr, String [] nameList)
- throws IOException {
- if (attr == null) {
- return;
- }
-
- if (nameList != null) {
- // process only given attribute names
- for (int i = 0; i < nameList.length; i++) {
- final String name = nameList[i];
- if (attr.isSet(name)) {
- writeOneAttribute(name, attr.getValue(name));
- }
- }
- } else {
- // process all defined attributes
- for (Iterator it = attr.nameIterator(); it.hasNext();) {
- final String name = (String)it.next();
- if (attr.isSet(name)) {
- writeOneAttribute(name, attr.getValue(name));
- }
- }
- }
- }
-
- /**
- * Write one attribute to our Writer
- * @param name name of attribute to write
- * @param value value of attribute to be written
- * @throws IOException for I/O problems
- */
- protected void writeOneAttribute(String name, Object value)
- throws IOException {
- String cw = name;
- if (value instanceof Integer) {
- // attribute has integer value, must write control word + value
- cw += value;
- } else if (value instanceof String) {
- cw += value;
- }
- writeControlWord(cw);
- }
-
- /**
- * Write one attribute to our Writer without a space
- * @param name name of attribute to write
- * @param value value of attribute to be written
- * @throws IOException for I/O problems
- */
- protected void writeOneAttributeNS(String name, Object value)
- throws IOException {
- String cw = name;
- if (value instanceof Integer) {
- // attribute has integer value, must write control word + value
- cw += value;
- } else if (value instanceof String) {
- cw += value;
- }
- writeControlWordNS(cw);
- }
-
- /**
- * can be overridden to suppress all RTF output
- * @return true if this object can be written into the RTF
- */
- protected boolean okToWriteRtf() {
- return true;
- }
-
- /** debugging to given PrintWriter */
- void dump(Writer w, int indent)
- throws IOException {
- for (int i = 0; i < indent; i++) {
- w.write(' ');
- }
- w.write(this.toString());
- w.write('\n');
- w.flush();
- }
-
- /**
- * minimal debugging display
- * @return String representation of object
- */
- public String toString() {
- return (this == null) ? "null" : (this.getClass().getName() + " #" + id);
- }
-
- /** true if close() has been called */
- boolean isClosed() {
- return closed;
- }
-
- /** access our RtfFile, which is always the topmost parent */
- RtfFile getRtfFile() {
- // go up the chain of parents until we find the topmost one
- RtfElement result = this;
- while (result.parent != null) {
- result = result.parent;
- }
-
- // topmost parent must be an RtfFile
- // a ClassCastException here would mean that the parent-child structure is not as expected
- return (RtfFile)result;
- }
-
- /** find the first parent where c.isAssignableFrom(parent.getClass()) is true
- * @return null if not found
- */
- RtfElement getParentOfClass(Class c) {
- RtfElement result = null;
- RtfElement current = this;
- while (current.parent != null) {
- current = current.parent;
- if (c.isAssignableFrom(current.getClass())) {
- result = current;
- break;
- }
- }
- return result;
- }
-
- /**
- * @return true if this element would generate no "useful" RTF content
- */
- public abstract boolean isEmpty();
-
- /**
- * Make a visible entry in the RTF for an exception
- * @param ie Exception to flag
- * @throws IOException for I/O problems
- */
- protected void writeExceptionInRtf(Exception ie)
- throws IOException {
- writeGroupMark(true);
- writeControlWord("par");
-
- // make the exception message stand out so that the problem is visible
- writeControlWord("fs48");
- // RtfStringConverter.getInstance().writeRtfString(m_writer,
- // JForVersionInfo.getShortVersionInfo() + ": ");
- RtfStringConverter.getInstance().writeRtfString(writer, ie.getClass().getName());
-
- writeControlWord("fs20");
- RtfStringConverter.getInstance().writeRtfString(writer, " " + ie.toString());
-
- writeControlWord("par");
- writeGroupMark(false);
- }
-
- /**
- * Added by Normand Masse
- * Used for attribute inheritance
- * @return RtfAttributes
- */
- public RtfAttributes getRtfAttributes() {
- return attrib;
- }
- }
|