Java Code Examples for org.pentaho.reporting.libraries.resourceloader.ResourceManager#loadRawData()

The following examples show how to use org.pentaho.reporting.libraries.resourceloader.ResourceManager#loadRawData() . 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: ZipResourceBundleLoader.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Tries to load the bundle. If the key does not point to a usable resource-bundle, this method returns null. The
 * Exception is only thrown if the bundle is not readable because of IO-Errors.
 * <p/>
 * A resource-bundle loader should only load the bundle for the key itself, never for any of the derived subkeys. It
 * is the ResourceManager's responsibility to search the key's hierachy for the correct key.
 *
 * @param key the resource key pointing to the bundle.
 * @return the loaded bundle or null, if the resource was not understood.
 * @throws ResourceLoadingException if something goes wrong.
 */
public ResourceBundleData loadBundle( final ResourceManager resourceManager,
                                      final ResourceKey key ) throws ResourceLoadingException {
  if ( resourceManager == null ) {
    throw new NullPointerException();
  }
  if ( key == null ) {
    throw new NullPointerException();
  }

  try {
    final ResourceData rawData = resourceManager.loadRawData( key );
    // A zip bundle can be recognized by using simple finger-printing
    final byte[] buffer = new byte[ 2 ];
    rawData.getResource( resourceManager, buffer, 0, 2 );
    if ( buffer[ 0 ] != 'P' || buffer[ 1 ] != 'K' ) {
      return null;
    }

    final InputStream stream = rawData.getResourceAsStream( resourceManager );
    try {
      final ZipReadRepository zipReadRepository = new ZipReadRepository( stream );
      final String bundleType = BundleUtilities.getBundleType( zipReadRepository );
      final String bundleMapping = BundleUtilities.getBundleMapping( bundleType );

      final HashMap<FactoryParameterKey, Object> map = new HashMap<FactoryParameterKey, Object>();
      map.put( new FactoryParameterKey( "repository" ), zipReadRepository );
      map.put( new FactoryParameterKey( "repository-loader" ), this );

      final ResourceKey mainKey = new ResourceKey( key, ZipResourceBundleLoader.class.getName(), bundleMapping, map );
      return new RepositoryResourceBundleData( key, zipReadRepository, mainKey, false );
    } finally {
      stream.close();
    }
  } catch ( UnrecognizedLoaderException e ) {
    return null;
  } catch ( IOException ioe ) {
    throw new ResourceLoadingException( "IOError during load", ioe );
  }
}