1ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes/*
2ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes * Copyright (C) 2009 The Android Open Source Project
3f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
4ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes * you may not use this file except in compliance with the License.
6ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes * You may obtain a copy of the License at
7f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
8ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
10ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes * See the License for the specific language governing permissions and
14ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes * limitations under the License.
15ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes */
16ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes
17ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughespackage tests.support;
18ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes
19ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughesimport java.io.ByteArrayInputStream;
20ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughesimport javax.xml.parsers.DocumentBuilder;
21ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughesimport javax.xml.parsers.DocumentBuilderFactory;
22ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughesimport org.w3c.dom.Document;
23ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughesimport org.w3c.dom.Element;
24ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughesimport org.w3c.dom.Node;
25ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughesimport org.w3c.dom.NodeList;
26ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughesimport static junit.framework.Assert.assertEquals;
27ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes
28ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughespublic class Support_Xml {
29ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes    public static Document domOf(String xml) throws Exception {
30ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        // DocumentBuilderTest assumes we're using DocumentBuilder to do this parsing!
31ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
32ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        dbf.setCoalescing(true);
33ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        dbf.setExpandEntityReferences(true);
34f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes
35ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes());
36ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        DocumentBuilder builder = dbf.newDocumentBuilder();
37f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes
38ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        return builder.parse(stream);
39ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes    }
40f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes
41ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes    public static String firstChildTextOf(Document doc) throws Exception {
42ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        NodeList children = doc.getFirstChild().getChildNodes();
43ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        assertEquals(1, children.getLength());
44ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        return children.item(0).getNodeValue();
45ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes    }
46f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes
47ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes    public static Element firstElementOf(Document doc) throws Exception {
48ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        return (Element) doc.getFirstChild();
49ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes    }
50f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes
51ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes    public static String attrOf(Element e) throws Exception {
52ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes        return e.getAttribute("attr");
53ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes    }
54ff42219e3ea3d712f931ae7f26af236339b5cf23Elliott Hughes}
55