blob: b0aa7d5f589fca55c6bab5d8b3eb15623f5dbc5c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.render.mif;
// Java
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
/**
* The is the basis for MIF document elements.
* This enables the creation of the element and to write it
* to an output stream including sub-elements or a single value.
*/
public class MIFElement {
/** name */
protected String name;
/** value string */
protected String valueStr = null;
/** value elements */
protected List valueElements = null;
/** true if started */
protected boolean started = false;
/** true if finishing */
protected boolean finish = false;
/** true if finished */
protected boolean finished = false;
/**
* @param name a name
*/
public MIFElement(String name) {
this.name = name;
}
/** @param str a string value */
public void setValue(String str) {
valueStr = str;
}
/** @param el an MIF element */
public void addElement(MIFElement el) {
if (valueElements == null) {
valueElements = new java.util.ArrayList();
}
valueElements.add(el);
}
/**
* Output this element to an output stream.
* This will output only so far as the fisrt unfinished child element.
* This method can be called again to continue from the previous point.
* An element that contains child elements will only be finished when
* the finish method is called.
* @param os output stream
* @param indent indentation
* @return true if finished
* @throws IOException if not caught
*/
public boolean output(OutputStream os, int indent) throws IOException {
if (finished) {
return true;
}
if (valueElements == null && valueStr == null) {
return false;
}
String indentStr = "";
for (int c = 0; c < indent; c++) {
indentStr += " ";
}
if (!started) {
os.write((indentStr + "<" + name).getBytes());
if (valueElements != null) {
os.write(("\n").getBytes());
}
started = true;
}
if (valueElements != null) {
boolean done = true;
for (Iterator iter = valueElements.iterator(); iter.hasNext();) {
MIFElement el = (MIFElement)iter.next();
boolean d = el.output(os, indent + 1);
if (d) {
iter.remove();
} else {
done = false;
break;
}
}
if (!finish || !done) {
return false;
}
os.write((indentStr + "> # end of " + name + "\n").getBytes());
} else {
os.write((" " + valueStr + ">\n").getBytes());
}
finished = true;
return true;
}
/** @param deep if true, also perform finish over value elements */
public void finish(boolean deep) {
finish = true;
if (deep && valueElements != null) {
for (Iterator iter = valueElements.iterator(); iter.hasNext();) {
MIFElement el = (MIFElement)iter.next();
el.finish(deep);
}
}
}
}
|