1 17 18 19 20 package org.apache.fop.layoutengine; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.apache.fop.layoutmgr.KnuthBox; 26 import org.apache.fop.layoutmgr.KnuthElement; 27 import org.apache.fop.layoutmgr.KnuthGlue; 28 import org.apache.fop.layoutmgr.KnuthPenalty; 29 import org.w3c.dom.CDATASection ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 import org.w3c.dom.Text ; 34 35 38 public class ElementListCheck implements LayoutEngineCheck { 39 40 private String category; 41 private String id; 42 private int index = -1; 43 private Element checkElement; 44 45 49 public ElementListCheck(Node node) { 50 this.category = node.getAttributes().getNamedItem("category").getNodeValue(); 51 if (node.getAttributes().getNamedItem("id") != null) { 52 this.id = node.getAttributes().getNamedItem("id").getNodeValue(); 53 } 54 if (!haveID()) { 55 if (node.getAttributes().getNamedItem("index") != null) { 56 String s = node.getAttributes().getNamedItem("index").getNodeValue(); 57 this.index = Integer.parseInt(s); 58 } 59 } 60 this.checkElement = (Element)node; 61 } 62 63 66 public void check(LayoutResult result) { 67 ElementListCollector.ElementList elementList = findElementList(result); 68 NodeList children = checkElement.getChildNodes(); 69 int pos = -1; 70 for (int i = 0; i < children.getLength(); i++) { 71 Node node = children.item(i); 72 if (node instanceof Element) { 73 pos++; 74 Element domEl = (Element)node; 75 KnuthElement knuthEl = (KnuthElement)elementList.getElementList().get(pos); 76 if ("skip".equals(domEl.getLocalName())) { 77 pos += Integer.parseInt(getElementText(domEl)) - 1; 78 } else if ("box".equals(domEl.getLocalName())) { 79 if (!(knuthEl instanceof KnuthBox)) { 80 fail("Expected KnuthBox" 81 + " at position " + pos 82 + " but got: " + knuthEl.getClass().getName()); 83 } 84 if (domEl.getAttribute("w").length() > 0) { 85 int w = Integer.parseInt(domEl.getAttribute("w")); 86 if (w != knuthEl.getW()) { 87 fail("Expected w=" + w 88 + " at position " + pos 89 + " but got: " + knuthEl.getW()); 90 } 91 } 92 if ("true".equals(domEl.getAttribute("aux"))) { 93 if (!knuthEl.isAuxiliary()) { 94 fail("Expected auxiliary box" 95 + " at position " + pos); 96 } 97 } 98 if ("false".equals(domEl.getAttribute("aux"))) { 99 if (knuthEl.isAuxiliary()) { 100 fail("Expected a normal, not an auxiliary box" 101 + " at position " + pos); 102 } 103 } 104 } else if ("penalty".equals(domEl.getLocalName())) { 105 if (!(knuthEl instanceof KnuthPenalty)) { 106 fail("Expected KnuthPenalty " 107 + " at position " + pos 108 + " but got: " + knuthEl.getClass().getName()); 109 } 110 KnuthPenalty pen = (KnuthPenalty)knuthEl; 111 if (domEl.getAttribute("w").length() > 0) { 112 int w = Integer.parseInt(domEl.getAttribute("w")); 113 if (w != knuthEl.getW()) { 114 fail("Expected w=" + w 115 + " at position " + pos 116 + " but got: " + knuthEl.getW()); 117 } 118 } 119 if (domEl.getAttribute("p").length() > 0) { 120 if ("<0".equals(domEl.getAttribute("p"))) { 121 if (knuthEl.getP() >= 0) { 122 fail("Expected p<0" 123 + " at position " + pos 124 + " but got: " + knuthEl.getP()); 125 } 126 } else if (">0".equals(domEl.getAttribute("p"))) { 127 if (knuthEl.getP() <= 0) { 128 fail("Expected p>0" 129 + " at position " + pos 130 + " but got: " + knuthEl.getP()); 131 } 132 } else { 133 int p; 134 if ("INF".equalsIgnoreCase(domEl.getAttribute("p"))) { 135 p = KnuthPenalty.INFINITE; 136 } else if ("INFINITE".equalsIgnoreCase(domEl.getAttribute("p"))) { 137 p = KnuthPenalty.INFINITE; 138 } else if ("-INF".equalsIgnoreCase(domEl.getAttribute("p"))) { 139 p = -KnuthPenalty.INFINITE; 140 } else if ("-INFINITE".equalsIgnoreCase(domEl.getAttribute("p"))) { 141 p = -KnuthPenalty.INFINITE; 142 } else { 143 p = Integer.parseInt(domEl.getAttribute("p")); 144 } 145 if (p != knuthEl.getP()) { 146 fail("Expected p=" + p 147 + " at position " + pos 148 + " but got: " + knuthEl.getP()); 149 } 150 } 151 } 152 if ("true".equals(domEl.getAttribute("flagged"))) { 153 if (!pen.isFlagged()) { 154 fail("Expected flagged penalty" 155 + " at position " + pos); 156 } 157 } else if ("false".equals(domEl.getAttribute("flagged"))) { 158 if (pen.isFlagged()) { 159 fail("Expected non-flagged penalty" 160 + " at position " + pos); 161 } 162 } 163 if ("true".equals(domEl.getAttribute("aux"))) { 164 if (!pen.isAuxiliary()) { 165 fail("Expected auxiliary penalty" 166 + " at position " + pos); 167 } 168 } else if ("false".equals(domEl.getAttribute("aux"))) { 169 if (pen.isAuxiliary()) { 170 fail("Expected non-auxiliary penalty" 171 + " at position " + pos); 172 } 173 } 174 } else if ("glue".equals(domEl.getLocalName())) { 175 if (!(knuthEl instanceof KnuthGlue)) { 176 fail("Expected KnuthGlue" 177 + " at position " + pos 178 + " but got: " + knuthEl.getClass().getName()); 179 } 180 KnuthGlue glue = (KnuthGlue)knuthEl; 181 if (domEl.getAttribute("w").length() > 0) { 182 int w = Integer.parseInt(domEl.getAttribute("w")); 183 if (w != knuthEl.getW()) { 184 fail("Expected w=" + w 185 + " at position " + pos 186 + " but got: " + knuthEl.getW()); 187 } 188 } 189 if (domEl.getAttribute("y").length() > 0) { 190 int stretch = Integer.parseInt(domEl.getAttribute("y")); 191 if (stretch != knuthEl.getY()) { 192 fail("Expected y=" + stretch 193 + " (stretch) at position " + pos 194 + " but got: " + knuthEl.getY()); 195 } 196 } 197 if (domEl.getAttribute("z").length() > 0) { 198 int shrink = Integer.parseInt(domEl.getAttribute("z")); 199 if (shrink != knuthEl.getZ()) { 200 fail("Expected z=" + shrink 201 + " (shrink) at position " + pos 202 + " but got: " + knuthEl.getZ()); 203 } 204 } 205 } else { 206 throw new IllegalArgumentException ("Invalid child node for 'element-list': " 207 + domEl.getLocalName() 208 + " at position " + pos + " (" + this + ")"); 209 } 210 211 } 212 } 213 pos++; 214 if (elementList.getElementList().size() > pos) { 215 fail("There are " 216 + (elementList.getElementList().size() - pos) 217 + " unchecked elements at the end of the list"); 218 } 219 } 220 221 private void fail(String msg) { 222 throw new RuntimeException (msg + " (" + this + ")"); 223 } 224 225 private boolean haveID() { 226 return (this.id != null && this.id.length() > 0); 227 } 228 229 private ElementListCollector.ElementList findElementList(LayoutResult result) { 230 List candidates = new java.util.ArrayList (); 231 Iterator iter = result.getElementListCollector().getElementLists().iterator(); 232 while (iter.hasNext()) { 233 ElementListCollector.ElementList el = (ElementListCollector.ElementList)iter.next(); 234 if (el.getCategory().equals(category)) { 235 if (haveID() && this.id.equals(el.getID())) { 236 candidates.add(el); 237 break; 238 } else if (!haveID()) { 239 candidates.add(el); 240 } 241 } 242 } 243 if (candidates.size() == 0) { 244 throw new ArrayIndexOutOfBoundsException ("Requested element list not found"); 245 } else if (index >= 0) { 246 return (ElementListCollector.ElementList)candidates.get(index); 247 } else { 248 return (ElementListCollector.ElementList)candidates.get(0); 249 } 250 } 251 252 private static String getElementText(Element el) { 253 StringBuffer sb = new StringBuffer (); 254 NodeList children = el.getChildNodes(); 255 for (int i = 0; i < children.getLength(); i++) { 256 Node node = children.item(i); 257 if (node instanceof Text ) { 258 sb.append(((Text )node).getData()); 259 } else if (node instanceof CDATASection ) { 260 sb.append(((CDATASection )node).getData()); 261 } 262 } 263 return sb.toString(); 264 } 265 266 267 public String toString() { 268 StringBuffer sb = new StringBuffer ("element-list"); 269 sb.append(" category=").append(category); 270 if (haveID()) { 271 sb.append(" id=").append(id); 272 } else if (index >= 0) { 273 sb.append(" index=").append(index); 274 } 275 return sb.toString(); 276 } 277 } 278 | Popular Tags |