summaryrefslogtreecommitdiffstats
path: root/base/util/src/com/netscape/cmsutil/xml/XMLObject.java
blob: ed2fb67eed9665ba533dbefa970cf3f574ef4769 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmsutil.xml;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class XMLObject {
    private Document mDoc = null;

    public XMLObject() throws ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        mDoc = docBuilder.newDocument();
    }

    public XMLObject(InputStream s)
            throws SAXException, IOException, ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        mDoc = docBuilder.parse(s);
    }

    public XMLObject(File f)
            throws SAXException, IOException, ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        mDoc = docBuilder.parse(f);
    }

    public Document getDocument() {
        return mDoc;
    }

    /**
     * Each document should have 1 root only. This method should be called once.
     */
    public Node createRoot(String name) {
        Element root = mDoc.createElement(name);
        mDoc.appendChild(root);
        return (Node) root;
    }

    public Node getRoot() {
        return mDoc.getFirstChild();
    }

    /**
     * If you have duplicate containers, then this method will return the
     * first container in the list.
     */
    public Node getContainer(String tagname) {
        NodeList list = mDoc.getElementsByTagName(tagname);
        if (list.getLength() > 0)
            return list.item(0);
        return null;
    }

    public Node createContainer(Node containerParent, String containerName) {
        Element node = mDoc.createElement(containerName);
        containerParent.appendChild(node);
        return (Node) node;
    }

    public void addItemToContainer(Node container, String tagname, String value) {
        Element node = mDoc.createElement(tagname);
        Text text = mDoc.createTextNode(value);
        node.appendChild(text);
        container.appendChild(node);
    }

    public String getValue(String tagname) {
        Node n = getContainer(tagname);

        if (n != null) {
            NodeList c = n.getChildNodes();
            if (c.getLength() == 0)
                return null;
            Node item = c.item(0);
            return item.getNodeValue();
        }

        return null;
    }

    public Vector<String> getAllValues(String tagname) {
        Vector<String> v = new Vector<String>();
        NodeList nodes = mDoc.getElementsByTagName(tagname);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);
            NodeList c = n.getChildNodes();
            if (c.getLength() > 0) {
                Node nn = c.item(0);
                if (nn.getNodeType() == Node.TEXT_NODE)
                    v.addElement(nn.getNodeValue());
            }
        }
        return v;
    }

    public Vector<String> getValuesFromContainer(Node container, String tagname) {
        Vector<String> v = new Vector<String>();
        NodeList c = container.getChildNodes();
        int len = c.getLength();
        for (int i = 0; i < len; i++) {
            Node subchild = c.item(i);
            if (subchild.getNodeName().equals(tagname)) {
                NodeList grandchildren = subchild.getChildNodes();
                if (grandchildren.getLength() > 0) {
                    Node grandchild = grandchildren.item(0);
                    if (grandchild.getNodeType() == Node.TEXT_NODE)
                        v.addElement(grandchild.getNodeValue());
                }
            }
        }

        return v;
    }

    public byte[] toByteArray() throws TransformerConfigurationException, TransformerException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
        Source src = new DOMSource(mDoc);
        Result dest = new StreamResult(bos);
        aTransformer.transform(src, dest);
        return bos.toByteArray();
    }

    public void output(OutputStream os)
            throws TransformerConfigurationException, TransformerException {
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
        Source src = new DOMSource(mDoc);
        Result dest = new StreamResult(os);
        aTransformer.transform(src, dest);
    }

    public String toXMLString() throws TransformerConfigurationException, TransformerException {
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer transformer = tranFactory.newTransformer();
        Source src = new DOMSource(mDoc);
        StreamResult dest = new StreamResult(new StringWriter());
        transformer.transform(src, dest);
        String xmlString = dest.getWriter().toString();
        return xmlString;
    }
}