KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutengine > ElementListCheck


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: ElementListCheck.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutengine;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
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 JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33 import org.w3c.dom.Text JavaDoc;
34
35 /**
36  * Check implementation that checks a Knuth element list.
37  */

38 public class ElementListCheck implements LayoutEngineCheck {
39
40     private String JavaDoc category;
41     private String JavaDoc id;
42     private int index = -1;
43     private Element checkElement;
44     
45     /**
46      * Creates a new instance from a DOM node.
47      * @param node DOM node that defines this check
48      */

49     public ElementListCheck(Node JavaDoc 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 JavaDoc s = node.getAttributes().getNamedItem("index").getNodeValue();
57                 this.index = Integer.parseInt(s);
58             }
59         }
60         this.checkElement = (Element)node;
61     }
62
63     /**
64      * @see org.apache.fop.layoutengine.LayoutEngineCheck
65      */

66     public void check(LayoutResult result) {
67         ElementListCollector.ElementList elementList = findElementList(result);
68         NodeList JavaDoc children = checkElement.getChildNodes();
69         int pos = -1;
70         for (int i = 0; i < children.getLength(); i++) {
71             Node JavaDoc 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 JavaDoc("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 JavaDoc msg) {
222         throw new RuntimeException JavaDoc(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 JavaDoc candidates = new java.util.ArrayList JavaDoc();
231         Iterator JavaDoc 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 JavaDoc("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 JavaDoc getElementText(Element el) {
253         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
254         NodeList JavaDoc children = el.getChildNodes();
255         for (int i = 0; i < children.getLength(); i++) {
256             Node JavaDoc node = children.item(i);
257             if (node instanceof Text JavaDoc) {
258                 sb.append(((Text JavaDoc)node).getData());
259             } else if (node instanceof CDATASection JavaDoc) {
260                 sb.append(((CDATASection JavaDoc)node).getData());
261             }
262         }
263         return sb.toString();
264     }
265     
266     /** @see java.lang.Object#toString() */
267     public String JavaDoc toString() {
268         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("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