FileDocCategorySizeDatePackage
SimpleParserTest.javaAPI DocAndroid 1.5 API9148Wed May 06 22:41:06 BST 2009tests.xml

SimpleParserTest

public class SimpleParserTest extends TestCase implements ContentHandler

Fields Summary
private SAXParser
parser
private StringBuffer
instructions
private Map
namespaces1
private Map
namespaces2
private StringBuffer
elements1
private StringBuffer
elements2
private Map
attributes1
private Map
attributes2
private StringBuffer
text
Constructors Summary
Methods Summary
public voidcharacters(char[] ch, int start, int length)


        String s = new String(ch, start, length).trim();
        if (s.length() != 0) {
            if (text.length() != 0) {
                text.append(",");
            }

            text.append(s);
        }
    
public voidendDocument()

    
public voidendElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)

    
public voidendPrefixMapping(java.lang.String prefix)

    
public voidignorableWhitespace(char[] ch, int start, int length)

    
public voidprocessingInstruction(java.lang.String target, java.lang.String data)

        String s = target + ":" + data;

        if (instructions.length() != 0) {
            instructions.append(",");
        }

        instructions.append(s);
    
public voidsetDocumentLocator(org.xml.sax.Locator locator)

    
protected voidsetUp()

        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);
        factory.setNamespaceAware(true);

        parser = factory.newSAXParser();
        parser.getXMLReader().setContentHandler(this);

        instructions = new StringBuffer();
        namespaces1 = new HashMap<String, String>();
        namespaces2 = new HashMap<String, String>();
        elements1 = new StringBuffer();
        elements2 = new StringBuffer();
        attributes1 = new HashMap<String, String>();
        attributes2 = new HashMap<String, String>();
        text = new StringBuffer();
    
public voidskippedEntity(java.lang.String name)

    
public voidstartDocument()

    
public voidstartElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)


        if (elements1.length() != 0) {
            elements1.append(",");
        }

        elements1.append(localName);

        if (!"".equals(uri)) {
            namespaces1.put(localName, uri);
        }

        for (int i = 0; i < atts.getLength(); i++) {
            attributes1.put(atts.getLocalName(i), atts.getValue(i));
        }

        if (elements2.length() != 0) {
            elements2.append(",");
        }

        elements2.append(qName);

        if (!"".equals(uri)) {
            namespaces2.put(qName, uri);
        }

        for (int i = 0; i < atts.getLength(); i++) {
            attributes2.put(atts.getQName(i), atts.getValue(i));
        }
    
public voidstartPrefixMapping(java.lang.String prefix, java.lang.String uri)

    
protected voidtearDown()

        instructions = null;
        parser = null;
        namespaces1 = null;
        namespaces2 = null;
        attributes1 = null;
        attributes2 = null;
        elements1 = null;
        elements2 = null;
        text = null;
    
public voidtestEntityResolver()

        final StringBuilder text = new StringBuilder();
        DefaultHandler handler = new DefaultHandler() {
            public void characters(char[] ch, int start, int length) {
                String s = new String(ch, start, length).trim();
                if (s.length() != 0) {
                    if (text.length() != 0) {
                        text.append(",");
                    }
                    text.append(s);
                }
            }

            public InputSource resolveEntity(String publicId, String systemId)
                    throws IOException, SAXException {
                return new InputSource(new InputStreamReader(
                        new ByteArrayInputStream("test".getBytes())));
            }
        };

        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setValidating(false);
        parser = spf.newSAXParser();
        parser.parse(this.getClass().getResourceAsStream("/staffEntRes.xml"),
                handler);
        assertTrue(
                "resolved external entity must be in parser character stream",
                text.toString().contains("test"));
    
public voidtestGetValue()

        parser.parse(getClass().getResourceAsStream("/staffNS.xml"), 
                new DefaultHandler() {
            boolean firstAddressElem = true;
            @Override
            public void startElement (String uri, String localName,
                    String qName, Attributes attributes) {
                if(firstAddressElem && localName.equals("address")) {
                    firstAddressElem = false;
                    assertNotNull(attributes.getValue("http://www.usa.com",
                            "domestic"));
                }
            }
        });
    
public voidtestWorkingFile1()

        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);
        factory.setNamespaceAware(true);

        SAXParser parser = factory.newSAXParser();
        parser.getXMLReader().setContentHandler(this);

        parser.parse(getClass().getResourceAsStream("/SimpleParserTest.xml"),
                (DefaultHandler) null);

        assertEquals("The:quick,brown:fox", instructions.toString());

        assertEquals("stuff,nestedStuff,nestedStuff,nestedStuff", elements1
                .toString());

        assertEquals("Some text here,some more here...", text.toString());

        assertEquals("eins", attributes1.get("one"));
        assertEquals("zwei", attributes1.get("two"));
        assertEquals("drei", attributes1.get("three"));

        assertEquals("http://www.foobar.org", namespaces1.get("stuff"));
    
public voidtestWorkingFile2()

        SAXParserFactory factory = SAXParserFactory.newInstance();

        factory.setValidating(false);
        factory.setNamespaceAware(false);
        factory.setFeature("http://xml.org/sax/features/namespace-prefixes",
                true);

        SAXParser parser = factory.newSAXParser();
        parser.getXMLReader().setContentHandler(this);
        parser.parse(getClass().getResourceAsStream("/SimpleParserTest.xml"),
                (DefaultHandler) null);

        assertFalse(parser.isNamespaceAware());

        assertEquals("The:quick,brown:fox", instructions.toString());

        assertEquals("t:stuff,nestedStuff,nestedStuff,nestedStuff", elements2
                .toString());

        assertEquals("Some text here,some more here...", text.toString());

        assertEquals("eins", attributes2.get("one"));
        assertEquals("zwei", attributes2.get("two"));
        assertEquals("drei", attributes2.get("three"));

        assertEquals(0, namespaces2.size());