123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /*
- * ============================================================================
- * 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.
- */
-
-
- package org.apache.fop.render.rtf.rtflib.rtfdoc;
-
- import java.io.IOException;
- import java.io.Writer;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Iterator;
- import java.io.IOException;
- import org.apache.fop.render.rtf.rtflib.exceptions.RtfStructureException;
-
- /** Class which contains a linear text run. It has methods to add attributes, text, paragraph breaks....
- * @author Peter Herweg, pherweg@web.de
- */
-
- public class RtfTextrun extends RtfContainer {
-
- /** Class which represents the opening of a RTF group mark.*/
- private class RtfOpenGroupMark extends RtfElement
- {
- RtfOpenGroupMark(RtfContainer parent, Writer w, RtfAttributes attr)
- throws IOException {
- super(parent, w, attr);
- }
-
- /**
- * @return true if this element would generate no "useful" RTF content
- */
- public boolean isEmpty() {
- return false;
- }
-
- /**
- * write RTF code of all our children
- * @throws IOException for I/O problems
- */
- protected void writeRtfContent() throws IOException {
- writeGroupMark(true);
- writeAttributes(getRtfAttributes(), null);
- }
- }
-
- /** Class which represents the closing of a RTF group mark.*/
- private class RtfCloseGroupMark extends RtfElement
- {
- RtfCloseGroupMark(RtfContainer parent, Writer w)
- throws IOException {
- super(parent, w);
- }
-
- /**
- * @return true if this element would generate no "useful" RTF content
- */
- public boolean isEmpty() {
- return false;
- }
-
- /**
- * write RTF code of all our children
- * @throws IOException for I/O problems
- */
- protected void writeRtfContent() throws IOException {
- writeGroupMark(false);
- }
- }
-
- /** Class which represents a paragraph break.*/
- private class RtfParagraphBreak extends RtfElement
- {
- RtfParagraphBreak(RtfContainer parent, Writer w)
- throws IOException {
- super(parent, w);
- }
-
- /**
- * @return true if this element would generate no "useful" RTF content
- */
- public boolean isEmpty() {
- return false;
- }
-
- /**
- * write RTF code of all our children
- * @throws IOException for I/O problems
- */
- protected void writeRtfContent() throws IOException {
- writeControlWord("par");
- }
- }
-
- /** Create an RTF container as a child of given container */
- RtfTextrun(RtfContainer parent, Writer w, RtfAttributes attrs) throws IOException {
- super(parent, w, attrs);
- }
-
- public void pushAttributes(RtfAttributes attrs) throws IOException {
- RtfOpenGroupMark r=new RtfOpenGroupMark(this, writer, attrs);
- }
-
- public void popAttributes() throws IOException {
- RtfCloseGroupMark r=new RtfCloseGroupMark(this, writer);
- }
-
- public void addString(String s) throws IOException {
- RtfString r=new RtfString(this, writer, s);
- }
-
- public void addParagraphBreak() throws IOException {
- RtfParagraphBreak r=new RtfParagraphBreak(this, writer);
- }
-
- public void addPageNumber(RtfAttributes attr) throws IOException {
- RtfPageNumber r=new RtfPageNumber(this, writer, attr);
- }
-
- /**
- * Adds a new RtfTextrun to the given container if necessary, and returns it.
- * @param container RtfContainer, which is the parent of the returned RtfTextrun
- * @param writer Writer of the given RtfContainer
- * @param attrs RtfAttributes which are to write at the beginning of the RtfTextrun
- * @throws IOException for I/O problems
- */
- public static RtfTextrun getTextrun(RtfContainer container, Writer writer, RtfAttributes attrs)
- throws IOException {
- Object obj;
- List list=container.getChildren();
-
- if(list.size()==0) {
- //add a new RtfTextrun
- RtfTextrun textrun=new RtfTextrun(container, writer, attrs);
- list.add(textrun);
-
- return textrun;
- } else if ((obj=list.get(list.size()-1)) instanceof RtfTextrun ) {
- //if the last child is a RtfTextrun, return it
- return (RtfTextrun)obj;
- }
-
- //add a new RtfTextrun as the last child
- RtfTextrun textrun=new RtfTextrun(container, writer, attrs);
- list.add(textrun);
-
- return textrun;
- }
-
- /**
- * write RTF code of all our children
- * @throws IOException for I/O problems
- */
- protected void writeRtfContent() throws IOException {
- /**
- *TODO: The textrun's children are iterated threetimes:
- * 1. In WhitespaceCollapser
- * 2. To determine the last RtfParagraphBreak
- * 3. To write the children
- * Maybe this can be done more efficient.
- */
-
- if (attrib != null && attrib.isSet("WhiteSpaceFalse")) {
- attrib.unset("WhiteSpaceFalse");
- } else {
- new WhitespaceCollapser(this);
- }
-
- //determine, if this RtfTextrun is the last child of its parent
- boolean bLast=false;
- for (Iterator it = parent.getChildren().iterator(); it.hasNext();) {
- if(it.next() == this) {
- bLast=!it.hasNext();
- break;
- }
- }
-
- //get last RtfParagraphBreak, which is not followed by any visible child
- RtfParagraphBreak lastParagraphBreak=null;
- if(bLast) {
- for (Iterator it = getChildren().iterator(); it.hasNext();) {
- final RtfElement e = (RtfElement)it.next();
- if(e instanceof RtfParagraphBreak) {
- lastParagraphBreak=(RtfParagraphBreak)e;
- } else {
- if(!(e instanceof RtfOpenGroupMark)
- && !(e instanceof RtfCloseGroupMark)
- && e.isEmpty()) {
- lastParagraphBreak=null;
- }
- }
- }
- }
-
- //may contain for example \intbl
- writeAttributes(attrib, null);
-
- //write all children
- boolean bPrevPar = false;
- boolean bFirst = true;
- for (Iterator it = getChildren().iterator(); it.hasNext();) {
- final RtfElement e = (RtfElement)it.next();
- final boolean bRtfParagraphBreak = (e instanceof RtfParagraphBreak);
-
- /**
- * -Write RtfParagraphBreak only, if the previous visible child
- * was't also a RtfParagraphBreak.
- * -Write RtfParagraphBreak only, if it is not the first visible
- * child.
- * -If the RtfTextrun is the last child of its parent, write a
- * RtfParagraphBreak only, if it is not the last child.
- */
- boolean bHide=false;
- bHide=bRtfParagraphBreak;
- bHide=bHide &&
- (bPrevPar || bFirst || (bLast && lastParagraphBreak!=null && e==lastParagraphBreak) );
-
- if( !bHide) {
- e.writeRtf();
- }
-
- if(e instanceof RtfParagraphBreak) {
- bPrevPar=true;
- } else if(e instanceof RtfCloseGroupMark) {
- //do nothing
- } else if(e instanceof RtfOpenGroupMark) {
- //do nothing
- } else {
- bPrevPar=bPrevPar && e.isEmpty();
- bFirst=bFirst && e.isEmpty();
- }
- }
- }
- }
|