@Test public void testEncodingAttributeXML() throws Exception { final InputStream is = new ByteArrayInputStream(ENCODING_ATTRIBUTE_XML .getBytes("UTF-8")); final XmlStreamReader xmlReader = new XmlStreamReader(is, "", true); assertEquals(xmlReader.getEncoding(), "UTF-8"); xmlReader.close(); }
@Test public void testRawContent() throws Exception { final String encoding = "UTF-8"; final String xml = getXML("no-bom", XML3, encoding, encoding); final ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes(encoding)); final XmlStreamReader xmlReader = new XmlStreamReader(is); assertEquals("Check encoding", xmlReader.getEncoding(), encoding); assertEquals("Check content", xml, IOUtils.toString(xmlReader)); }
@Test public void testHttpContent() throws Exception { final String encoding = "UTF-8"; final String xml = getXML("no-bom", XML3, encoding, encoding); final ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes(encoding)); final XmlStreamReader xmlReader = new XmlStreamReader(is, encoding); assertEquals("Check encoding", xmlReader.getEncoding(), encoding); assertEquals("Check content", xml, IOUtils.toString(xmlReader)); }
protected void _testRawNoBomInvalid(final String encoding) throws Exception { final InputStream is = getXmlStream("no-bom", XML3, encoding, encoding); try { (new XmlStreamReader(is, false)).close();; fail("It should have failed"); } catch (final IOException ex) { assertTrue(ex.getMessage().contains("Invalid encoding,")); } }
protected void _testHttpInvalid(final String cT, final String bomEnc, final String streamEnc, final String prologEnc) throws Exception { final InputStream is = getXmlStream(bomEnc, prologEnc == null ? XML2 : XML3, streamEnc, prologEnc); try { (new XmlStreamReader(is, cT, false)).close();; fail("It should have failed for HTTP Content-type " + cT + ", BOM " + bomEnc + ", streamEnc " + streamEnc + " and prologEnc " + prologEnc); } catch (final IOException ex) { assertTrue(ex.getMessage().contains("Invalid encoding,")); } }
protected void _testRawBomValid(final String encoding) throws Exception { final InputStream is = getXmlStream(encoding + "-bom", XML3, encoding, encoding); final XmlStreamReader xmlReader = new XmlStreamReader(is, false); if (!encoding.equals("UTF-16") && !encoding.equals("UTF-32")) { assertEquals(xmlReader.getEncoding(), encoding); } else { assertEquals(xmlReader.getEncoding() .substring(0, encoding.length()), encoding); } xmlReader.close(); }
public void _testHttpValid(final String cT, final String bomEnc, final String streamEnc, final String prologEnc) throws Exception { final InputStream is = getXmlStream(bomEnc, prologEnc == null ? XML1 : XML3, streamEnc, prologEnc); final XmlStreamReader xmlReader = new XmlStreamReader(is, cT, false); if (!streamEnc.equals("UTF-16")) { // we can not assert things here because UTF-8, US-ASCII and // ISO-8859-1 look alike for the chars used for detection // (niallp 2010-10-06 - I re-instated the check below and removed the 2 tests that failed) assertEquals(xmlReader.getEncoding(), streamEnc); } else { assertEquals(xmlReader.getEncoding().substring(0, streamEnc.length()), streamEnc); } xmlReader.close(); }
protected void _testRawBomInvalid(final String bomEnc, final String streamEnc, final String prologEnc) throws Exception { final InputStream is = getXmlStream(bomEnc, XML3, streamEnc, prologEnc); XmlStreamReader xmlReader = null; try { xmlReader = new XmlStreamReader(is, false); final String foundEnc = xmlReader.getEncoding(); fail("Expected IOException for BOM " + bomEnc + ", streamEnc " + streamEnc + " and prologEnc " + prologEnc + ": found " + foundEnc); } catch (final IOException ex) { assertTrue(ex.getMessage().contains("Invalid encoding,")); } if (xmlReader != null) { xmlReader.close(); } }
public void _testAlternateDefaultEncoding(final String cT, final String bomEnc, final String streamEnc, final String prologEnc, final String alternateEnc) throws Exception { final InputStream is = getXmlStream(bomEnc, prologEnc == null ? XML1 : XML3, streamEnc, prologEnc); final XmlStreamReader xmlReader = new XmlStreamReader(is, cT, false, alternateEnc); if (!streamEnc.equals("UTF-16")) { // we can not assert things here because UTF-8, US-ASCII and // ISO-8859-1 look alike for the chars used for detection // (niallp 2010-10-06 - I re-instated the check below - the tests(6) passed) final String enc = alternateEnc != null ? alternateEnc : streamEnc; assertEquals(xmlReader.getEncoding(), enc); } else { //String enc = (alternateEnc != null) ? alternateEnc : streamEnc; assertEquals(xmlReader.getEncoding().substring(0, streamEnc.length()), streamEnc); } xmlReader.close(); }
protected void _testHttpLenient(final String cT, final String bomEnc, final String streamEnc, final String prologEnc, final String shouldbe) throws Exception { final InputStream is = getXmlStream(bomEnc, prologEnc == null ? XML2 : XML3, streamEnc, prologEnc); final XmlStreamReader xmlReader = new XmlStreamReader(is, cT, true); assertEquals(xmlReader.getEncoding(), shouldbe); xmlReader.close(); }
protected void _testRawNoBomValid(final String encoding) throws Exception { InputStream is = getXmlStream("no-bom", XML1, encoding, encoding); XmlStreamReader xmlReader = new XmlStreamReader(is, false); assertEquals(xmlReader.getEncoding(), "UTF-8"); xmlReader.close(); is = getXmlStream("no-bom", XML2, encoding, encoding); xmlReader = new XmlStreamReader(is); assertEquals(xmlReader.getEncoding(), "UTF-8"); xmlReader.close(); is = getXmlStream("no-bom", XML3, encoding, encoding); xmlReader = new XmlStreamReader(is); assertEquals(xmlReader.getEncoding(), encoding); xmlReader.close(); is = getXmlStream("no-bom", XML4, encoding, encoding); xmlReader = new XmlStreamReader(is); assertEquals(xmlReader.getEncoding(), encoding); xmlReader.close(); is = getXmlStream("no-bom", XML5, encoding, encoding); xmlReader = new XmlStreamReader(is); assertEquals(xmlReader.getEncoding(), encoding); xmlReader.close(); }
@Test public void testReadXmlWithBOMUtf32Be() throws Exception { Assume.assumeTrue("JVM and SAX need to support UTF_32BE for this", jvmAndSaxBothSupportCharset("UTF_32BE")); final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32BE\"?><X/>".getBytes("UTF_32BE"); parseXml(new BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE)); // XML parser does not know what to do with UTF-32, so we warp the input stream with a XmlStreamReader parseXml(new XmlStreamReader(createUtf32BeDataStream(data, true))); }
@Test public void testReadXmlWithBOMUtf32Le() throws Exception { Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE")); final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32LE\"?><X/>".getBytes("UTF_32LE"); parseXml(new BOMInputStream(createUtf32LeDataStream(data, true), ByteOrderMark.UTF_32LE)); // XML parser does not know what to do with UTF-32, so we warp the input stream with a XmlStreamReader parseXml(new XmlStreamReader(createUtf32LeDataStream(data, true))); }
protected void setUp() throws Exception { assertTrue( "Maven 3.2.4 or better is required", MAVEN_VERSION == null || new DefaultArtifactVersion( "3.2.3" ).compareTo( MAVEN_VERSION ) < 0 ); configurator = getContainer().lookup( ComponentConfigurator.class, "basic" ); InputStream is = getClass().getResourceAsStream( "/" + getPluginDescriptorLocation() ); XmlStreamReader reader = new XmlStreamReader( is ); InterpolationFilterReader interpolationFilterReader = new InterpolationFilterReader( new BufferedReader( reader ), container.getContext().getContextData() ); PluginDescriptor pluginDescriptor = new PluginDescriptorBuilder().build( interpolationFilterReader ); Artifact artifact = lookup( RepositorySystem.class ).createArtifact( pluginDescriptor.getGroupId(), pluginDescriptor.getArtifactId(), pluginDescriptor.getVersion(), ".jar" ); artifact.setFile( getPluginArtifactFile() ); pluginDescriptor.setPluginArtifact( artifact ); pluginDescriptor.setArtifacts( Arrays.asList( artifact ) ); for ( ComponentDescriptor<?> desc : pluginDescriptor.getComponents() ) { getContainer().addComponentDescriptor( desc ); } mojoDescriptors = new HashMap<String, MojoDescriptor>(); for ( MojoDescriptor mojoDescriptor : pluginDescriptor.getMojos() ) { mojoDescriptors.put( mojoDescriptor.getGoal(), mojoDescriptor ); } }
/** * @param is {@link InputStream} * @param lenient yes/no * @throws IOException in case of an error. * @throws XmlStreamReaderException in case of an error. */ public XmlStreamReader( InputStream is, boolean lenient ) throws IOException, XmlStreamReaderException { reader = new org.apache.commons.io.input.XmlStreamReader( is, lenient, staticDefaultEncoding ); }
/** * @param conn The URL connection {@link URLConnection}. * @throws IOException in case of error. */ public XmlStreamReader( URLConnection conn ) throws IOException { reader = new org.apache.commons.io.input.XmlStreamReader( conn, staticDefaultEncoding ); }
/** * Create a new Reader with XML encoding detection rules. * * @param in not null input stream. * @return an XML reader instance for the input stream. * @throws IOException if any. * @see XmlStreamReader */ public static Reader newXmlReader( @Nonnull InputStream in ) throws IOException { return new XmlStreamReader( in ); }
private String convertToString(final InputStream inputStream) { try { XmlStreamReader reader = new XmlStreamReader(inputStream); return IOUtils.toString(reader); } catch (IOException e) { throw new IllegalArgumentException("Incorrect stream. Required XML stream", e); } } }
public ModifyingReader createXmlVersionModifyingReader(InputStream xmlStream, String newXmlVersion) throws IOException { // buffer stream // (is this really necessary to get optimal performance?) if (!(xmlStream instanceof BufferedInputStream)) { xmlStream = new BufferedInputStream(xmlStream); } XmlStreamReader xmlReader = new XmlStreamReader(xmlStream); XmlVersionReader xmlVersionReader = new XmlVersionReader(xmlReader); // create the reader that replaces the XML version in prolog Modifier modifier = new XmlVersionModifier(newXmlVersion, 8192); return new ModifyingReader(xmlVersionReader, modifier); }
private void assertXmlVersionInProlog(byte[] input, String newXmlVersion, String expectedProlog) throws Exception { XmlVersionReader xmlVersionReader = new XmlVersionReader(new XmlStreamReader(new ByteArrayInputStream(input))); // create the reader that modifies the XML version ModifyingReader reader = new ModifyingReader(xmlVersionReader, createModifier(newXmlVersion, 5)); String actualProlog = IOUtils.toString(reader); assertEquals(expectedProlog, actualProlog); }