protected static XMLStreamReader2 constructStreamReaderForFile(XMLInputFactory f, String filename) throws IOException, XMLStreamException { File inf = new File(filename); XMLStreamReader sr = f.createXMLStreamReader(inf.toURL().toString(), new FileReader(inf)); assertEquals(sr.getEventType(), START_DOCUMENT); return (XMLStreamReader2) sr; }
/** * Method that can be used to verify that the current element pointed to by the stream reader does * not belong to a namespace. */ protected static void assertElemNotInNamespace(XMLStreamReader sr) throws XMLStreamException { String uri = sr.getNamespaceURI(); if (uri == null) { fail("Excepted empty String to indicate \"no namespace\": got null"); } else if (uri.length() != 0) { fail("Excepted no (null) namespace URI: got '" + uri + "'"); } }
/** * Method that will iterate through contents of an XML document using specified stream reader; * will also access some of data to make sure reader reads most of lazy-loadable data. Method is * usually called to try to get an exception for invalid content. * * @return Dummy value calculated on contents; used to make sure no dead code is eliminated */ protected int streamThrough(XMLStreamReader sr) throws XMLStreamException { int result = 0; while (sr.hasNext()) { int type = sr.next(); result += type; if (sr.hasText()) { /* will also do basic verification for text content, to * see that all text accessor methods return same content */ result += getAndVerifyText(sr).hashCode(); } if (sr.hasName()) { result += sr.getName().hashCode(); } } return result; }
/** * Method that can be used to verify that the current element pointed to by the stream reader has * no prefix. */ protected static void assertNoElemPrefix(XMLStreamReader sr) throws XMLStreamException { String prefix = sr.getPrefix(); if (prefix != ELEM_NO_PREFIX) { fail( "Element that does not have a prefix should be indicated with <" + ELEM_NO_PREFIX + ">, not <" + prefix + ">"); } }
/** * Method that not only gets currently available text from the reader, but also checks that its * consistenly accessible using different (basic) StAX methods. */ protected static String getAndVerifyText(XMLStreamReader sr) throws XMLStreamException { /* 05-Apr-2006, TSa: Although getText() is available for DTD * and ENTITY_REFERENCE, getTextXxx() are not. Thus, can not * do more checks for those types. */ int type = sr.getEventType(); if (type == ENTITY_REFERENCE || type == DTD) { return sr.getText(); } int expLen = sr.getTextLength(); /* Hmmh. It's only ok to return empty text for DTD event... well, * maybe also for CDATA, since empty CDATA blocks are legal? */ /* !!! 01-Sep-2004, TSa: * note: theoretically, in coalescing mode, it could be possible * to have empty CDATA section(s) get converted to CHARACTERS, * which would be empty... may need to enhance this to check that * mode is not coalescing? Or something */ if (type == CHARACTERS) { assertTrue("Stream reader should never return empty Strings.", (expLen > 0)); } String text = sr.getText(); assertNotNull("getText() should never return null.", text); assertEquals( "Expected text length of " + expLen + ", got " + text.length(), expLen, text.length()); char[] textChars = sr.getTextCharacters(); int start = sr.getTextStart(); String text2 = new String(textChars, start, expLen); assertEquals(text, text2); return text; }