summaryrefslogtreecommitdiff
path: root/java/src/IceGridGUI/XMLWriter.java
blob: a0c6e225f874fee6a82ed4370669c12171b09fdf (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
package IceGridGUI;

import java.io.*;

//
// Helper class to write XML files
//
public class XMLWriter
{
    public XMLWriter(File file) throws FileNotFoundException, IOException
    {
        try
        {
            _writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        }
        catch(UnsupportedEncodingException e)
        {
            assert false;
        }
        _writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + _newline);
        _writer.write("<!-- This file was written by IceGrid Admin -->" + _newline);
    }

    public void writeElement(String name, java.util.List attributes)
        throws IOException
    {
        _writer.write(_indent);
        _writer.write("<" + name);
        writeAttributes(attributes);
        _writer.write("/>" + _newline);
    }

    public void writeElement(String name) throws IOException
    {
        writeElement(name, (java.util.List)null);
    }

    public void writeElement(String name, String content) throws IOException
    {
        _writer.write(_indent);
        _writer.write("<" + name + ">" + escape(content) + 
                      "</" + name + ">" + _newline);
    }

    public void writeStartTag(String name, java.util.List attributes)
        throws IOException
    {
        _writer.write(_indent);
        _writer.write("<" + name);
        writeAttributes(attributes);
        _writer.write(">");
        _writer.write(_newline);
        increaseIndent();
    }

    public void writeStartTag(String name) throws IOException
    {
        writeStartTag(name, null);
    }

    public void writeEndTag(String name) throws IOException
    {
        decreaseIndent();
        _writer.write(_indent);
        _writer.write("</" + name + ">" + _newline);
    }

    public void close()  throws IOException
    {
        _writer.close();
    }

    public void flush() throws IOException
    {
        _writer.flush();
    }

    private void writeAttributes(java.util.List attributes)
        throws IOException
    {
        if(attributes != null)
        {
            java.util.Iterator p = attributes.iterator();
            while(p.hasNext())
            {
                String[] pair = (String[])p.next();
                _writer.write(" " + pair[0] + "=\"" + escape(pair[1]) + "\"");
            }
        }
    }

    private void increaseIndent()
    {
        _indent += "   ";
    }

    private void decreaseIndent()
    {
        if(_indent.length() > 0)
        {
            _indent = _indent.substring(3);
        }
    }

    private String
    escape(String input)
    {
        String v = input;

        //
        // Find out whether there is a reserved character to avoid
        // conversion if not necessary.
        //
        final String allReserved = "<>'\"&";
        boolean hasReserved = false;
        char[] arr = input.toCharArray();
        for(int i = 0; i < arr.length; i++)
        {
            if(allReserved.indexOf(arr[i]) != -1)
            {
                hasReserved = true;
                break;
            }
        }
        if(hasReserved)
        {
            //
            // First convert all & to &amp;
            //
            if(v.indexOf('&') != -1)
            {
                v = v.replaceAll("&", "&amp;");
            }

            //
            // Next convert remaining reserved characters.
            //
            if(v.indexOf('>') != -1)
            {
                v = v.replaceAll(">", "&gt;");
            }
            if(v.indexOf('<') != -1)
            {
                v = v.replaceAll("<", "&lt;");
            }
            if(v.indexOf('\'') != -1)
            {
                v = v.replaceAll("'", "&apos;");
            }
            if(v.indexOf('"') != -1)
            {
                v = v.replaceAll("\"", "&quot;");
            }
        }
        return v;
    }


    private Writer _writer;
    private String _indent = "";
    private static String _newline = System.getProperty("line.separator");
}