aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/mif/MIFElement.java
blob: bff204717fb58806506000f17edcc2d3d162c7d9 (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
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.mif;

// Java
import java.io.*;
import java.util.*;

/**
 * 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 {
    protected String name;
    protected String valueStr = null;
    protected ArrayList valueElements = null;

    protected boolean started = false;
    protected boolean finish = false;
    protected boolean finished = false;

    /**
     */
    public MIFElement(String n) {
        name = n;
    }

    public void setValue(String str) {
        valueStr = str;
    }

    public void addElement(MIFElement el) {
        if(valueElements == null) {
            valueElements = new 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.
     */
    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;
    }

    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);
            }
        }
    }
}