public URL toURL( final ResourceKey key ) { return (URL) key.getIdentifier(); }
public String getFileName() { final Object identifier = source.getIdentifier(); if ( identifier instanceof File ) { final File f = (File) identifier; return f.getPath(); } return null; }
public ClassloaderResourceData( final ResourceKey key ) { if ( key == null ) { throw new NullPointerException(); } this.key = key; final String rawPath = (String) key.getIdentifier(); this.resourcePath = rawPath.substring( 6 ); }
public URL toURL( final ResourceKey key ) { if ( key == null ) { throw new NullPointerException(); } final File file = (File) key.getIdentifier(); try { return file.toURI().toURL(); } catch ( MalformedURLException e ) { return null; } }
public RawResourceData( final ResourceKey rawKey ) { if ( rawKey == null ) { throw new NullPointerException(); } this.rawKey = rawKey; final byte[] data = (byte[]) rawKey.getIdentifier(); this.data = (byte[]) data.clone(); }
public Object getAttribute( final String key ) { if ( key.equals( ResourceData.FILENAME ) ) { return LoaderUtils.getFileName( (String) this.key.getIdentifier() ); } return null; }
public static File getContextAsFile( ResourceKey key ) { while ( key != null ) { final Object identifier = key.getIdentifier(); if ( identifier instanceof File ) { return (File) identifier; } key = key.getParent(); } return null; }
public URL toURL( final ResourceKey key ) { if ( key == null ) { throw new NullPointerException(); } if ( isSupportedKey( key ) == false ) { throw new IllegalArgumentException( "Key format is not recognized." ); } try { final File file = (File) key.getIdentifier(); return file.toURI().toURL(); } catch ( MalformedURLException e ) { return null; } }
public URLResourceData( final ResourceKey key ) { if ( key == null ) { throw new NullPointerException(); } this.modificationDate = -1; this.key = key; this.url = (URL) key.getIdentifier(); // for the ease of implementation, we take the file name from the URL. // Feel free to add a 'Content-Disposition' parser with all details :) this.filename = IOUtils.getInstance().getFileName( url ); }
/** * Serializes the resource key to a String representation which can be recreated using the * <code>deserialize(ResourceKey)<code> method. * * @param bundleKey * @param key * @return a <code>String<code> which is a serialized version of the <code>ResourceKey</code> * @throws ResourceException indicates an error serializing the resource key */ public String serialize( final ResourceKey bundleKey, final ResourceKey key ) throws ResourceException { // Validate the parameter if ( key == null ) { throw new NullPointerException( "The ResourceKey can not be null" ); } if ( isSupportedKey( key ) == false ) { throw new IllegalArgumentException( "Key format is not recognized." ); } if ( !( key.getIdentifier() instanceof String ) ) { throw new IllegalArgumentException( "ResourceKey is invalid - identifier is not a byte[] object" ); } final String data = (String) key.getIdentifier(); return ResourceKeyUtils.createStringResourceKey ( String.valueOf( key.getSchema() ), data, key.getFactoryParameters() ); }
public FileResourceData( final ResourceKey key ) throws ResourceLoadingException { if ( key == null ) { throw new NullPointerException(); } final File file = (File) key.getIdentifier(); if ( file.exists() == false ) { throw new ResourceLoadingException ( "File-handle given does not point to an existing file." ); } if ( file.isFile() == false ) { throw new ResourceLoadingException ( "File-handle given does not point to a regular file." ); } if ( file.canRead() == false ) { throw new ResourceLoadingException ( "File '" + file + "' is not readable." ); } this.key = key; this.file = file; }
/** * Creates a String version of the resource key that can be used to generate a new ResourceKey object via * deserialization * * @param bundleKey * @param key */ public String serialize( final ResourceKey bundleKey, final ResourceKey key ) throws ResourceException { // Validate the parameter if ( key == null ) { throw new NullPointerException( "The ResourceKey can not be null" ); } if ( isSupportedKey( key ) == false ) { throw new IllegalArgumentException( "Key format is not recognized." ); } if ( !( key.getIdentifier() instanceof byte[] ) ) { throw new IllegalArgumentException( "ResourceKey is invalid - identifier is not a byte[] object" ); } final byte[] data = (byte[]) key.getIdentifier(); final char[] cdata = new char[ data.length ]; for ( int i = 0; i < data.length; i++ ) { cdata[ i ] = (char) ( data[ i ] & 0xFF ); } return ResourceKeyUtils.createStringResourceKey ( String.valueOf( key.getSchema() ), new String( cdata ), key.getFactoryParameters() ); }
public String serialize( final ResourceKey bundleKey, final ResourceKey key ) throws ResourceException { // Validate the parameter if ( key == null ) { throw new NullPointerException( "The ResourceKey can not be null" ); } if ( isSupportedKey( key ) == false ) { throw new IllegalArgumentException( "Key format is not recognized." ); } if ( !( key.getIdentifier() instanceof File ) ) { throw new IllegalArgumentException( "ResourceKey is invalid - identifier is not a File object" ); } // Log information logger.debug( "Serializing a File Resource Key..." ); if ( key.getParent() != null ) { throw new ResourceException ( "Unable to serialize a File-ResourceKey with a parent. This type is not expected to have a parent." ); } // Create a string version of the identifier try { final File file = (File) key.getIdentifier(); final String strIdentifier = file.getCanonicalPath(); final String result = ResourceKeyUtils.createStringResourceKey ( key.getSchema().toString(), strIdentifier, key.getFactoryParameters() ); logger.debug( "Serialized File Resource Key: [" + result + "]" ); return result; } catch ( IOException ioe ) { throw new IllegalArgumentException( "Could not determine cononical path to file specified in ResourceKey: " + ioe.getMessage() ); } }
throw new IllegalArgumentException( "Key format is not recognized." ); if ( !( key.getIdentifier() instanceof URL ) ) { throw new IllegalArgumentException( "ResourceKey is invalid - identifier is not a URL object" ); final URL url = (URL) key.getIdentifier(); final String result = ResourceKeyUtils.createStringResourceKey ( key.getSchema().toString(), url.toExternalForm(), key.getFactoryParameters() );
/** * Creates a String version of the resource key that can be used to generate a new ResourceKey object via * deserialization * * @param bundleKey * @param key */ public String serialize( final ResourceKey bundleKey, final ResourceKey key ) throws ResourceException { // Validate the parameter if ( key == null ) { throw new NullPointerException( "The ResourceKey can not be null" ); } if ( isSupportedKey( key ) == false ) { throw new IllegalArgumentException( "Key format is not recognized." ); } if ( !( key.getIdentifier() instanceof String ) ) { throw new IllegalArgumentException( "ResourceKey is invalid - identifier is not a String object" ); } // Log information logger.debug( "Serializing a Classloader Resource Key..." ); if ( key.getParent() != null ) { throw new ResourceException ( "Cannot serialize this key, it contains a parent, but should not contain one at all." ); } // Serialize the key final String result = ResourceKeyUtils.createStringResourceKey( key.getSchema().toString(), (String) key.getIdentifier(), key.getFactoryParameters() ); logger.debug( "Serialized Classloader Resource Key: [" + result + "]" ); return result; }
public InputStream getResourceAsStream( final ResourceManager caller ) throws ResourceLoadingException { // again, this is going to hurt the performance. final ResourceKey parentKey = key.getParent(); final ResourceData data = caller.load( parentKey ); final ZipInputStream zin = new ZipInputStream( data.getResourceAsStream( caller ) ); try { try { ZipEntry zipEntry = zin.getNextEntry(); while ( zipEntry != null ) { if ( zipEntry.getName().equals( key.getIdentifier() ) == false ) { zipEntry = zin.getNextEntry(); continue; } // read from here .. return zin; } } finally { zin.close(); } } catch ( IOException e ) { throw new ResourceLoadingException ( "Reading the zip-file failed.", e ); } throw new ResourceLoadingException ( "The zip-file did not contain the specified entry" ); }
public String serialize( ResourceKey bundleKey, final ResourceKey key ) throws ResourceException { if ( isSupportedKey( key ) == false ) { throw new IllegalArgumentException( "Not supported" ); } final ZipEntryKey entryKey = (ZipEntryKey) key.getIdentifier(); final String name = entryKey.getEntryName(); final ResourceKey zipKey = entryKey.getZipFile().getKey(); // For now, we are just going to have to pass on this one throw new ResourceException( "Can not serialize a ZipResourceKey" ); }
/** * Parses the report using the provided resource manager. * * @param manager * the resource manager (can be null). * @param input * the resource key pointing to the report definition. * @param contextKey * the report's context (used to load content that has been referenced with relative URLs). * @return the parsed report. * @throws ResourceException * if parsing or loading failed for some reason. */ public MasterReport parseReport( ResourceManager manager, final ResourceKey input, final ResourceKey contextKey ) throws ResourceException { if ( manager == null ) { manager = new ResourceManager(); } final HashMap map = new HashMap( input.getFactoryParameters() ); final Iterator it = this.helperObjects.keySet().iterator(); while ( it.hasNext() ) { final String name = (String) it.next(); map.put( new FactoryParameterKey( name ), helperObjects.get( name ) ); } final ResourceKey key = new ResourceKey( input.getParent(), input.getSchema(), input.getIdentifier(), input.getFactoryParameters() ); final Resource resource = manager.create( key, contextKey, MasterReport.class ); return (MasterReport) resource.getResource(); }
/** * Derives a new resource key from the given key. If neither a path nor new factory-keys are given, the parent key is * returned. * * @param parent the parent * @param path the derived path (can be null). * @param factoryKeys the optional factory keys (can be null). * @return the derived key. * @throws org.pentaho.reporting.libraries.resourceloader.ResourceKeyCreationException if the key cannot be derived * for any reason. */ public ResourceKey deriveKey( final ResourceKey parent, final String path, final Map factoryKeys ) throws ResourceKeyCreationException { if ( path != null ) { throw new ResourceKeyCreationException( "Unable to derive key for new path." ); } if ( isSupportedKey( parent ) == false ) { throw new ResourceKeyCreationException( "Assertation: Unsupported parent key type" ); } if ( factoryKeys == null ) { return parent; } final HashMap map = new HashMap(); map.putAll( parent.getFactoryParameters() ); map.putAll( factoryKeys ); return new ResourceKey( parent.getSchema(), parent.getIdentifier(), map ); }
public DefaultImageReference( final Resource imageResource ) throws ResourceException { if ( imageResource == null ) { throw new NullPointerException(); } final Object o = imageResource.getResource(); if ( o instanceof Image == false ) { throw new ResourceException( "ImageResource does not contain a java.awt.Image object." ); } final ResourceKey resKey = imageResource.getSource(); final Object identifier = resKey.getIdentifier(); if ( identifier instanceof URL ) { this.url = (URL) identifier; } this.resourceKey = resKey; this.image = (Image) o; final WaitingImageObserver obs = new WaitingImageObserver( image ); obs.waitImageLoaded(); if ( obs.isError() ) { throw new ResourceException( "Failed to load the image. ImageObserver signaled an error." ); } this.width = image.getWidth( null ); this.height = image.getHeight( null ); }