1 17 18 19 20 package org.apache.fop.render.rtf.rtflib.rtfdoc; 21 22 28 29 import java.io.Writer ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Iterator ; 33 import java.io.IOException ; 34 import org.apache.fop.render.rtf.rtflib.exceptions.RtfStructureException; 35 36 39 40 public class RtfContainer extends RtfElement { 41 private LinkedList children; private RtfOptions options = new RtfOptions(); 43 private RtfElement lastChild; 44 45 46 RtfContainer(RtfContainer parent, Writer w) throws IOException { 47 this(parent, w, null); 48 } 49 50 51 RtfContainer(RtfContainer parent, Writer w, RtfAttributes attr) throws IOException { 52 super(parent, w, attr); 53 children = new LinkedList (); 54 } 55 56 60 public void setOptions(RtfOptions opt) { 61 options = opt; 62 } 63 64 69 protected void addChild(RtfElement e) 70 throws RtfStructureException { 71 if (isClosed()) { 72 final StringBuffer sb = new StringBuffer (); 74 sb.append("addChild: container already closed (parent="); 75 sb.append(this.getClass().getName()); 76 sb.append(" child="); 77 sb.append(e.getClass().getName()); 78 sb.append(")"); 79 final String msg = sb.toString(); 80 81 final RtfFile rf = getRtfFile(); 83 87 92 } 93 94 children.add(e); 95 lastChild = e; 96 } 97 98 101 public List getChildren() { 102 return (List )children.clone(); 103 } 104 105 108 public int getChildCount() { 109 return children.size(); 110 } 111 112 118 public boolean setChildren (List list) { 119 if (list instanceof LinkedList ) { 120 this.children = (LinkedList ) list; 121 return true; 122 } 123 124 return false; 125 } 126 127 131 protected void writeRtfContent() 132 throws IOException { 133 for (Iterator it = children.iterator(); it.hasNext();) { 134 final RtfElement e = (RtfElement)it.next(); 135 e.writeRtf(); 136 } 137 } 138 139 140 RtfOptions getOptions() { 141 return options; 142 } 143 144 145 boolean containsText() { 146 boolean result = false; 147 for (Iterator it = children.iterator(); it.hasNext();) { 148 final RtfElement e = (RtfElement)it.next(); 149 if (e instanceof RtfText) { 150 result = !e.isEmpty(); 151 } else if (e instanceof RtfContainer) { 152 if (((RtfContainer)e).containsText()) { 153 result = true; 154 } 155 } 156 if (result) { 157 break; 158 } 159 } 160 return result; 161 } 162 163 164 void dump(Writer w, int indent) 165 throws IOException { 166 super.dump(w, indent); 167 for (Iterator it = children.iterator(); it.hasNext();) { 168 final RtfElement e = (RtfElement)it.next(); 169 e.dump(w, indent + 1); 170 } 171 } 172 173 177 public String toString() { 178 return super.toString() + " (" + getChildCount() + " children)"; 179 } 180 181 184 protected boolean okToWriteRtf() { 185 boolean result = super.okToWriteRtf() && !isEmpty(); 186 if (result && !options.renderContainer(this)) { 187 result = false; 188 } 189 return result; 190 } 191 192 196 public boolean isEmpty() { 197 boolean result = true; 198 for (Iterator it = children.iterator(); it.hasNext();) { 199 final RtfElement e = (RtfElement)it.next(); 200 if (!e.isEmpty()) { 201 result = false; 202 break; 203 } 204 } 205 return result; 206 } 207 } | Popular Tags |