com.helger.commons.io.IHasInputStream Java Examples

The following examples show how to use com.helger.commons.io.IHasInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MicroReader.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public static IMicroDocument readMicroXML (@Nullable final IHasInputStream aISP,
                                           @Nullable final ISAXReaderSettings aSettings)
{
  if (aISP == null)
    return null;

  return readMicroXML (InputSourceFactory.create (aISP), aSettings);
}
 
Example #2
Source File: SimpleLSResourceResolverTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testOSGIBundle () throws BundleException
{
  LSInput aRes;

  // Initializing Apache Felix as OSGI container is required to get the
  // "bundle" URL protocol installed correctly
  // Otherwise the first call would end up as a "file" resource ;-)
  final Framework aOSGI = new FrameworkFactory ().newFramework (new HashMap <String, String> ());
  aOSGI.start ();
  try
  {
    // Bundle 0 is the org.apache.felix.framework bundle
    final Bundle b = aOSGI.getBundleContext ().getBundle (0);
    assertNotNull (b);
    assertEquals (Bundle.ACTIVE, b.getState ());

    // No leading slash is important as the ClassLoader is used!
    assertNotNull (b.getResource ("org/apache/felix/framework/util/Util.class"));

    final LSResourceResolver aRR = new SimpleLSResourceResolver ();

    // No class loader
    aRes = aRR.resolveResource (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                null,
                                null,
                                "../Felix.class",
                                "bundle://0.0:1/org/apache/felix/framework/util/Util.class");
    assertTrue (aRes instanceof ResourceLSInput);
    final IHasInputStream aISP = ((ResourceLSInput) aRes).getInputStreamProvider ();
    assertTrue (aISP instanceof URLResource);
    // Path maybe a "jar:file:" resource
    assertTrue (((URLResource) aISP).getPath ().endsWith ("org/apache/felix/framework/Felix.class"));
  }
  finally
  {
    aOSGI.stop ();
  }
}
 
Example #3
Source File: CachingTransformStreamSourceTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic ()
{
  final IReadableResource aRes = new ClassPathResource ("xml/test1.xslt");
  assertTrue (aRes.exists ());
  CachingTransformStreamSource src = new CachingTransformStreamSource (aRes);
  InputStream is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  assertEquals (aRes.getResourceID (), src.getSystemId ());
  assertNull (src.getPublicId ());

  src = new CachingTransformStreamSource ((IHasInputStream) aRes);
  is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  assertNull (src.getSystemId ());
  assertNull (src.getPublicId ());

  src = new CachingTransformStreamSource (aRes.getInputStream ());
  is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  assertNull (src.getSystemId ());
  assertNull (src.getPublicId ());

  CommonsTestHelper.testToStringImplementation (src);
}
 
Example #4
Source File: InputSourceFactory.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public static InputSource create (@Nonnull final IHasInputStream aISP)
{
  if (aISP instanceof IReadableResource)
    return create ((IReadableResource) aISP);
  return create (aISP.getInputStream ());
}
 
Example #5
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testNull ()
{
  assertNull (MicroReader.readMicroXML ((InputSource) null));
  assertNull (MicroReader.readMicroXML ((InputStream) null));
  assertNull (MicroReader.readMicroXML ((IReadableResource) null));
  assertNull (MicroReader.readMicroXML ((IHasInputStream) null));
  assertNull (MicroReader.readMicroXML ((Reader) null));
  assertNull (MicroReader.readMicroXML ((String) null));
  assertNull (MicroReader.readMicroXML ((CharSequence) null));
  assertNull (MicroReader.readMicroXML ((ByteBuffer) null));
  assertNull (MicroReader.readMicroXML ((byte []) null));
}
 
Example #6
Source File: CachingSAXInputSourceTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic ()
{
  final IReadableResource aRes = new ClassPathResource ("xml/list.xml");

  CachingSAXInputSource is = new CachingSAXInputSource (aRes);
  assertEquals (aRes.getResourceID (), is.getSystemId ());
  assertNotNull (StreamHelper.getAllBytes (is.getByteStream ()));

  is = new CachingSAXInputSource ((IHasInputStream) aRes);
  assertNull (is.getSystemId ());
  assertNotNull (StreamHelper.getAllBytes (is.getByteStream ()));

  is = new CachingSAXInputSource (aRes, "sysid");
  assertEquals ("sysid", is.getSystemId ());
  assertNotNull (StreamHelper.getAllBytes (is.getByteStream ()));

  is = new CachingSAXInputSource (aRes.getInputStream ());
  assertNull (is.getSystemId ());
  assertNotNull (StreamHelper.getAllBytes (is.getByteStream ()));

  is = new CachingSAXInputSource (aRes.getInputStream (), "sysid");
  assertEquals ("sysid", is.getSystemId ());
  assertNotNull (StreamHelper.getAllBytes (is.getByteStream ()));

  CommonsTestHelper.testToStringImplementation (is);
}
 
Example #7
Source File: IJAXBReader.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Read a document from the specified input stream provider. The secure
 * reading feature has affect when using this method.
 *
 * @param aISP
 *        The input stream provider to read. May not be <code>null</code>.
 * @return <code>null</code> in case reading fails.
 */
@Nullable
default JAXBTYPE read (@Nonnull final IHasInputStream aISP)
{
  ValueEnforcer.notNull (aISP, "Resource");

  // Ensure to use InputStream for BOM handling
  return read (aISP.getInputStream ());
}
 
Example #8
Source File: XMLListHandler.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess readList (@Nonnull final IHasInputStream aISP, @Nonnull final Collection <String> aTargetList)
{
  ValueEnforcer.notNull (aISP, "InputStreamProvider");

  return readList (aISP.getInputStream (), aTargetList);
}
 
Example #9
Source File: XMLListHandler.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
@ReturnsMutableCopy
public static ICommonsList <String> readList (@Nonnull final IHasInputStream aISP)
{
  ValueEnforcer.notNull (aISP, "InputStreamProvider");

  return readList (aISP.getInputStream ());
}
 
Example #10
Source File: XMLMapHandler.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess readMap (@Nonnull final IHasInputStream aISP, @Nonnull final Map <String, String> aTargetMap)
{
  ValueEnforcer.notNull (aISP, "InputStreamProvider");

  return readMap (aISP.getInputStream (), aTargetMap);
}
 
Example #11
Source File: XMLMapHandler.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
@ReturnsMutableCopy
public static ICommonsMap <String, String> readMap (@Nonnull final IHasInputStream aISP)
{
  ValueEnforcer.notNull (aISP, "InputStreamProvider");

  return readMap (aISP.getInputStream ());
}
 
Example #12
Source File: JsonReader.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Use an InputStream provider as JSON source with a custom fallback
 * charset.
 *
 * @param aISP
 *        The InputStream provider to be used. May not be <code>null</code>.
 * @param aFallbackCharset
 *        The fallback charset to be used. May not be <code>null</code>.
 * @return this for chaining
 */
@Nonnull
public Builder setSource (@Nonnull final IHasInputStream aISP, @Nonnull final Charset aFallbackCharset)
{
  ValueEnforcer.notNull (aISP, "InputStreamProvider");
  ValueEnforcer.notNull (aFallbackCharset, "FallbackCharset");

  final InputStream aIS = aISP.getInputStream ();
  if (aIS != null)
    setSource (aIS, aFallbackCharset);
  return this;
}
 
Example #13
Source File: StreamHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Read all bytes from the passed input stream into a byte array.
 *
 * @param aISP
 *        The input stream provider to read from. May be <code>null</code> .
 * @return The byte array or <code>null</code> if the parameter or the
 *         resolved input stream is <code>null</code>.
 */
@Nullable
public static byte [] getAllBytes (@Nullable final IHasInputStream aISP)
{
  if (aISP == null)
    return null;

  return getAllBytes (aISP.getInputStream ());
}
 
Example #14
Source File: StreamHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressFBWarnings (value = "NP_NONNULL_PARAM_VIOLATION")
public void testGetAllBytesCharset ()
{
  final String sInput = "Hallo";
  final byte [] aInput = sInput.getBytes (StandardCharsets.ISO_8859_1);
  assertArrayEquals (aInput, StreamHelper.getAllBytes (new ByteArrayInputStreamProvider (aInput)));
  assertArrayEquals (aInput, StreamHelper.getAllBytes (new NonBlockingByteArrayInputStream (aInput)));
  assertNull (StreamHelper.getAllBytes ((IHasInputStream) null));
  assertNull (StreamHelper.getAllBytes ((InputStream) null));

  assertEquals (sInput,
                StreamHelper.getAllBytesAsString (new ByteArrayInputStreamProvider (aInput),
                                                  StandardCharsets.ISO_8859_1));
  assertEquals (sInput,
                StreamHelper.getAllBytesAsString (new NonBlockingByteArrayInputStream (aInput),
                                                  StandardCharsets.ISO_8859_1));
  assertNull (StreamHelper.getAllBytesAsString ((IHasInputStream) null, StandardCharsets.ISO_8859_1));
  assertNull (StreamHelper.getAllBytesAsString ((InputStream) null, StandardCharsets.ISO_8859_1));
  try
  {
    StreamHelper.getAllBytesAsString (new NonBlockingByteArrayInputStream (aInput), (Charset) null);
    fail ();
  }
  catch (final NullPointerException ex)
  {}
}
 
Example #15
Source File: AbstractSchematronResource.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nullable
protected NodeAndBaseURI getAsNode (@Nonnull final IHasInputStream aXMLResource) throws Exception
{
  final StreamSource aStreamSrc = TransformSourceFactory.create (aXMLResource);
  InputStream aIS = null;
  try
  {
    aIS = aStreamSrc.getInputStream ();
  }
  catch (final IllegalStateException ex)
  {
    // Fall through
    // Happens e.g. for ResourceStreamSource with non-existing resources
  }
  if (aIS == null)
  {
    // Resource not found
    LOGGER.warn ("XML resource " + aXMLResource + " does not exist!");
    return null;
  }
  final Document aDoc = DOMReader.readXMLDOM (aIS, internalCreateDOMReaderSettings ());
  if (aDoc == null)
    throw new IllegalArgumentException ("Failed to read resource " + aXMLResource + " as XML");

  LOGGER.info ("Read XML resource " + aXMLResource);
  return new NodeAndBaseURI (aDoc, aStreamSrc.getSystemId ());
}
 
Example #16
Source File: AbstractSchematronResource.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nonnull
public EValidity getSchematronValidity (@Nonnull final IHasInputStream aXMLResource) throws Exception
{
  if (!isValidSchematron ())
    return EValidity.INVALID;

  final NodeAndBaseURI aXMLNode = getAsNode (aXMLResource);
  if (aXMLNode == null)
    return EValidity.INVALID;

  return getSchematronValidity (aXMLNode.m_aDoc, aXMLNode.m_sBaseURI);
}
 
Example #17
Source File: AbstractSchematronResource.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nullable
public Document applySchematronValidation (@Nonnull final IHasInputStream aXMLResource) throws Exception
{
  if (!isValidSchematron ())
    return null;

  final NodeAndBaseURI aXMLNode = getAsNode (aXMLResource);
  if (aXMLNode == null)
    return null;

  return applySchematronValidation (aXMLNode.m_aDoc, aXMLNode.m_sBaseURI);
}
 
Example #18
Source File: AbstractSchematronResource.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nullable
public SchematronOutputType applySchematronValidationToSVRL (@Nonnull final IHasInputStream aXMLResource) throws Exception
{
  if (!isValidSchematron ())
    return null;

  final NodeAndBaseURI aXMLNode = getAsNode (aXMLResource);
  if (aXMLNode == null)
    return null;

  return applySchematronValidationToSVRL (aXMLNode.m_aDoc, aXMLNode.m_sBaseURI);
}
 
Example #19
Source File: TransformSourceFactory.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static StreamSource create (@Nonnull final IHasInputStream aISP)
{
  if (aISP instanceof IReadableResource)
    return create ((IReadableResource) aISP);
  return create (aISP.getInputStream ());
}
 
Example #20
Source File: ISettingsPersistence.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Read settings from an InputStream provider and convert it to an
 * {@link ISettings} object.
 *
 * @param aISP
 *        The InputStream provider to read from. May not be <code>null</code>.
 * @return <code>null</code> if reading failed, a non-<code>null</code>
 *         settings object otherwise.
 */
@Nonnull
default ISettings readSettings (@Nonnull final IHasInputStream aISP)
{
  ValueEnforcer.notNull (aISP, "InputStreamProvider");

  final InputStream aIS = aISP.getInputStream ();
  if (aIS == null)
    throw new IllegalArgumentException ("Failed to open the provided input stream for the settings: " + aISP);
  return readSettings (aIS);
}
 
Example #21
Source File: JsonReader.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Check if the passed input stream can be resembled to valid Json content.
 * This is accomplished by fully parsing the Json file each time the method is
 * called. This consumes <b>less memory</b> than calling any of the
 * <code>read...</code> methods and checking for a non-<code>null</code>
 * result.
 *
 * @param aISP
 *        The resource to be parsed. May not be <code>null</code>.
 * @param aFallbackCharset
 *        The charset to be used for reading the Json file in case no BOM is
 *        present. May not be <code>null</code>.
 * @return <code>true</code> if the file can be parsed without error,
 *         <code>false</code> if not
 * @deprecated Since v9.3.3 - use {@link #builder()} instead.
 */
@Deprecated
public static boolean isValidJson (@Nonnull final IHasInputStream aISP, @Nonnull final Charset aFallbackCharset)
{
  ValueEnforcer.notNull (aISP, "InputStreamProvider");
  ValueEnforcer.notNull (aFallbackCharset, "FallbackCharset");

  final InputStream aIS = aISP.getInputStream ();
  if (aIS == null)
  {
    if (LOGGER.isWarnEnabled ())
      LOGGER.warn ("Failed to open Json InputStream from " + aISP);
    return false;
  }
  return isValidJson (aIS, aFallbackCharset);
}
 
Example #22
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testReadNonExistingResource ()
{
  assertNull (MicroReader.readMicroXML (new ClassPathResource ("does-not-exist.xml")));
  assertNull (MicroReader.readMicroXML ((IHasInputStream) new ClassPathResource ("does-not-exist.xml")));
}
 
Example #23
Source File: CSSReaderDeclarationList.java    From ph-css with Apache License 2.0 4 votes vote down vote up
/**
 * Read the CSS from the passed {@link IHasInputStream}.
 *
 * @param aISP
 *        The input stream provider to use. May not be <code>null</code>.
 * @param aCharset
 *        The charset to be used. May not be <code>null</code>.
 * @param eVersion
 *        The CSS version to use. May not be <code>null</code>.
 * @param aCustomErrorHandler
 *        An optional custom error handler that can be used to collect the
 *        recoverable parsing errors. May be <code>null</code>.
 * @param aCustomExceptionHandler
 *        An optional custom exception handler that can be used to collect the
 *        unrecoverable parsing errors. May be <code>null</code>.
 * @return <code>null</code> if reading failed, the CSS declarations
 *         otherwise.
 */
@Nullable
public static CSSDeclarationList readFromStream (@Nonnull final IHasInputStream aISP,
                                                 @Nonnull final Charset aCharset,
                                                 @Nonnull final ECSSVersion eVersion,
                                                 @Nullable final ICSSParseErrorHandler aCustomErrorHandler,
                                                 @Nullable final ICSSParseExceptionCallback aCustomExceptionHandler)
{
  ValueEnforcer.notNull (aISP, "InputStreamProvider");

  final InputStream aIS = aISP.getInputStream ();
  if (aIS == null)
    return null;
  return readFromReader (StreamHelper.createReader (aIS, aCharset),
                         new CSSReaderSettings ().setCSSVersion (eVersion)
                                                 .setCustomErrorHandler (aCustomErrorHandler)
                                                 .setCustomExceptionHandler (aCustomExceptionHandler));
}
 
Example #24
Source File: ReadableResourceSAXInputSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public ReadableResourceSAXInputSource (@Nonnull final IHasInputStream aISP, @Nullable final String sSystemID)
{
  m_aISP = ValueEnforcer.notNull (aISP, "InputStreamProvider");
  setSystemId (sSystemID);
}
 
Example #25
Source File: ResourceStreamSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public ResourceStreamSource (@Nonnull final IHasInputStream aISP, @Nullable final String sSystemID)
{
  m_aISP = ValueEnforcer.notNull (aISP, "InputStreamProvider");
  setSystemId (sSystemID);
}
 
Example #26
Source File: ResourceStreamSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
public IHasInputStream getInputStreamProvider ()
{
  return m_aISP;
}
 
Example #27
Source File: CachingTransformStreamSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public CachingTransformStreamSource (@Nonnull final IHasInputStream aIIS)
{
  this (aIIS, null);
}
 
Example #28
Source File: CachingTransformStreamSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public CachingTransformStreamSource (@Nonnull final IHasInputStream aIIS, @Nullable final String sSystemID)
{
  this (aIIS.getInputStream (), sSystemID);
}
 
Example #29
Source File: SAXReader.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static ESuccess readXMLSAX (@Nonnull final IHasInputStream aISP, @Nonnull final ISAXReaderSettings aSettings)
{
  return readXMLSAX (InputSourceFactory.create (aISP), aSettings);
}
 
Example #30
Source File: ResourceLSInput.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public ResourceLSInput (@Nonnull final IHasInputStream aISP, @Nullable final String sSystemID)
{
  m_aISP = ValueEnforcer.notNull (aISP, "InputStreamProvider");
  m_sSystemId = sSystemID;
}