org.apache.commons.io.input.XmlStreamReader.<init>()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(96)

本文整理了Java中org.apache.commons.io.input.XmlStreamReader.<init>()方法的一些代码示例,展示了XmlStreamReader.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XmlStreamReader.<init>()方法的具体详情如下:
包路径:org.apache.commons.io.input.XmlStreamReader
类名称:XmlStreamReader
方法名:<init>

XmlStreamReader.<init>介绍

[英]Creates a Reader for a File.

It looks for the UTF-8 BOM first, if none sniffs the XML prolog charset, if this is also missing defaults to UTF-8.

It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
[中]为文件创建读取器。
它首先查找UTF-8 BOM,如果没有嗅探XML prolog字符集,如果也缺少,则默认为UTF-8。
它执行宽松的字符集编码检测,请使用lenient参数检查构造函数以了解详细信息。

代码示例

代码示例来源:origin: commons-io/commons-io

@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();
}

代码示例来源:origin: commons-io/commons-io

@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));
}

代码示例来源:origin: commons-io/commons-io

@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));
}

代码示例来源:origin: commons-io/commons-io

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,"));
  }
}

代码示例来源:origin: commons-io/commons-io

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,"));
  }
}

代码示例来源:origin: commons-io/commons-io

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();
}

代码示例来源:origin: commons-io/commons-io

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();
}

代码示例来源:origin: commons-io/commons-io

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();
  }
}

代码示例来源:origin: commons-io/commons-io

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();
}

代码示例来源:origin: commons-io/commons-io

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();
}

代码示例来源:origin: commons-io/commons-io

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();
}

代码示例来源:origin: commons-io/commons-io

@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)));
}

代码示例来源:origin: commons-io/commons-io

@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)));
}

代码示例来源:origin: org.apache.maven.plugin-testing/maven-plugin-testing-harness

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 );
  }
}

代码示例来源:origin: org.apache.maven.shared/maven-shared-utils

/**
 * @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 );
}

代码示例来源:origin: org.apache.maven.shared/maven-shared-utils

/**
 * @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 );
}

代码示例来源:origin: org.apache.maven.shared/maven-shared-utils

/**
 * 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 );
}

代码示例来源:origin: pl.edu.icm.synat/synat-core-services-impl

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);
    }
  }
}

代码示例来源:origin: com.github.rwitzel.streamflyer/streamflyer-core

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);
}

代码示例来源:origin: com.github.rwitzel.streamflyer/streamflyer-core

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);
}

相关文章