KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > mif > MIFElement


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: MIFElement.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.render.mif;
21
22 // Java
23
import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * The is the basis for MIF document elements.
30  * This enables the creation of the element and to write it
31  * to an output stream including sub-elements or a single value.
32  */

33 public class MIFElement {
34     protected String JavaDoc name;
35     protected String JavaDoc valueStr = null;
36     protected List JavaDoc valueElements = null;
37
38     protected boolean started = false;
39     protected boolean finish = false;
40     protected boolean finished = false;
41
42     /**
43      */

44     public MIFElement(String JavaDoc n) {
45         name = n;
46     }
47
48     public void setValue(String JavaDoc str) {
49         valueStr = str;
50     }
51
52     public void addElement(MIFElement el) {
53         if (valueElements == null) {
54             valueElements = new java.util.ArrayList JavaDoc();
55         }
56         valueElements.add(el);
57     }
58
59     /**
60      * Output this element to an output stream.
61      * This will output only so far as the fisrt unfinished child element.
62      * This method can be called again to continue from the previous point.
63      * An element that contains child elements will only be finished when
64      * the finish method is called.
65      */

66     public boolean output(OutputStream JavaDoc os, int indent) throws IOException JavaDoc {
67         if (finished) {
68             return true;
69         }
70         if (valueElements == null && valueStr == null) {
71             return false;
72         }
73
74         String JavaDoc indentStr = "";
75         for (int c = 0; c < indent; c++) {
76             indentStr += " ";
77         }
78         if (!started) {
79             os.write((indentStr + "<" + name).getBytes());
80             if (valueElements != null) {
81                 os.write(("\n").getBytes());
82             }
83             started = true;
84         }
85         if (valueElements != null) {
86             boolean done = true;
87             for (Iterator JavaDoc iter = valueElements.iterator(); iter.hasNext();) {
88                 MIFElement el = (MIFElement)iter.next();
89                 boolean d = el.output(os, indent + 1);
90                 if (d) {
91                     iter.remove();
92                 } else {
93                     done = false;
94                     break;
95                 }
96             }
97             if (!finish || !done) {
98                 return false;
99             }
100             os.write((indentStr + "> # end of " + name + "\n").getBytes());
101         } else {
102             os.write((" " + valueStr + ">\n").getBytes());
103         }
104         finished = true;
105         return true;
106     }
107
108     public void finish(boolean deep) {
109         finish = true;
110         if (deep && valueElements != null) {
111             for (Iterator JavaDoc iter = valueElements.iterator(); iter.hasNext();) {
112                 MIFElement el = (MIFElement)iter.next();
113                 el.finish(deep);
114             }
115         }
116     }
117 }
118
119
Popular Tags