blob: cac1f95df2c2b8224db0418a82d98b91188fa1e9 [file] [log] [blame]
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed 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.
*/
package com.android.layoutlib.bridge;
import org.kxml2.io.KXmlParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import junit.framework.TestCase;
public class BridgeXmlBlockParserTest extends TestCase {
private String mXmlPath;
private Document mDoc;
@Override
protected void setUp() throws Exception {
super.setUp();
URL url = this.getClass().getClassLoader().getResource("data/layout1.xml");
mXmlPath = url.getFile();
mDoc = getXmlDocument(mXmlPath);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testXmlBlockParser() throws Exception {
XmlPullParser parser = new KXmlParser();
parser = new BridgeXmlBlockParser(parser, null, false /* platformResourceFlag */);
parser.setInput(new FileReader(new File(mXmlPath)));
assertEquals(XmlPullParser.START_DOCUMENT, parser.next());
assertEquals(XmlPullParser.START_TAG, parser.next());
assertEquals("LinearLayout", parser.getName());
assertEquals(XmlPullParser.TEXT, parser.next());
assertEquals(XmlPullParser.START_TAG, parser.next());
assertEquals("Button", parser.getName());
assertEquals(XmlPullParser.TEXT, parser.next());
assertEquals(XmlPullParser.END_TAG, parser.next());
assertEquals(XmlPullParser.TEXT, parser.next());
assertEquals(XmlPullParser.START_TAG, parser.next());
assertEquals("View", parser.getName());
assertEquals(XmlPullParser.END_TAG, parser.next());
assertEquals(XmlPullParser.TEXT, parser.next());
assertEquals(XmlPullParser.START_TAG, parser.next());
assertEquals("TextView", parser.getName());
assertEquals(XmlPullParser.END_TAG, parser.next());
assertEquals(XmlPullParser.TEXT, parser.next());
assertEquals(XmlPullParser.END_TAG, parser.next());
assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
}
//------------
private Document getXmlDocument(String xmlFilePath)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// keep comments
factory.setIgnoringComments(false);
// don't validate our bogus DTD
factory.setValidating(false);
// we want namespaces
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new File(xmlFilePath));
}
/**
* Quick'n'dirty debug helper that dumps an XML structure to stdout.
*/
@SuppressWarnings("unused")
private void dump(Node node, String prefix) {
Node n;
String[] types = {
"unknown",
"ELEMENT_NODE",
"ATTRIBUTE_NODE",
"TEXT_NODE",
"CDATA_SECTION_NODE",
"ENTITY_REFERENCE_NODE",
"ENTITY_NODE",
"PROCESSING_INSTRUCTION_NODE",
"COMMENT_NODE",
"DOCUMENT_NODE",
"DOCUMENT_TYPE_NODE",
"DOCUMENT_FRAGMENT_NODE",
"NOTATION_NODE"
};
String s = String.format("%s<%s> %s %s",
prefix,
types[node.getNodeType()],
node.getNodeName(),
node.getNodeValue() == null ? "" : node.getNodeValue().trim());
System.out.println(s);
n = node.getFirstChild();
if (n != null) {
dump(n, prefix + "- ");
}
n = node.getNextSibling();
if (n != null) {
dump(n, prefix);
}
}
}