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

The following examples show how to use org.pentaho.reporting.libraries.resourceloader.ResourceManager#deriveKey() . 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: FunctionUtilities.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static ResourceKey createURI( final String uri, final DocumentContext layoutProcess ) {
  try {
    final ResourceKey base = layoutProcess.getContextKey();
    final ResourceManager resourceManager =
      layoutProcess.getResourceManager();

    if ( base != null ) {
      try {
        return resourceManager.deriveKey( base, uri );
      } catch ( ResourceKeyCreationException ex ) {
        // ignore ..
      }
    }
    return resourceManager.createKey( uri );
  } catch ( Exception e ) {
    return null;
  }
}
 
Example 2
Source File: AbstractXmlReadHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Parses an external file using LibLoader and returns the parsed result as an object of type
 * <code>targetClass</code>. The file is given as relative pathname (relative to the current source file). The current
 * helper-methods are used as parse-parameters for the external parsing.
 *
 * @param file        the file to be parsed.
 * @param targetClass the target type of the parse operation.
 * @param map         the map of parse parameters.
 * @return the result, never null.
 * @throws ParseException           if parsing the result failed for some reason.
 * @throws ResourceLoadingException if there was an IO error loading the resource.
 * @see #deriveParseParameters()
 */
protected Object performExternalParsing( final String file, final Class targetClass, final Map map )
  throws ParseException, ResourceLoadingException {
  try {
    final ResourceManager resourceManager = rootHandler.getResourceManager();
    final ResourceKey source = rootHandler.getSource();

    final ResourceKey target = resourceManager.deriveKey( source, file, map );
    final DependencyCollector dc = rootHandler.getDependencyCollector();

    final Resource resource = resourceManager.create( target, rootHandler.getContext(), targetClass );
    dc.add( resource );
    return resource.getResource();
  } catch ( ResourceLoadingException rle ) {
    throw rle;
  } catch ( ResourceException e ) {
    throw new ParseException( "Failure while loading data: " + file, e, getLocator() );
  }

}
 
Example 3
Source File: StaticDocumentMetaData.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public StaticDocumentMetaData( final ResourceManager resourceManager,
                               final ResourceKey bundleKey ) throws ResourceException {
  if ( resourceManager == null ) {
    throw new NullPointerException();
  }
  if ( bundleKey == null ) {
    throw new NullPointerException();
  }


  this.resourceManager = resourceManager;
  this.bundleKey = bundleKey;

  // A bundle without a manifest is not valid.
  final ResourceKey manifestDataKey = resourceManager.deriveKey( bundleKey, "/META-INF/manifest.xml" );
  final Resource manifestDataResource = resourceManager.create( manifestDataKey, null, BundleManifest.class );
  manifest = (BundleManifest) manifestDataResource.getResource();

  metaData = createMetaData( resourceManager, bundleKey );

  bundleType = readBundleType();
  if ( bundleType == null ) {
    bundleType = manifest.getMimeType( "/" );
  }
  // bundle type can still be null.
}
 
Example 4
Source File: ReportReadHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Starts parsing.
 *
 * @param attrs
 *          the attributes.
 * @throws org.xml.sax.SAXException
 *           if there is a parsing error.
 */
protected void startParsing( final Attributes attrs ) throws SAXException {
  targetType = attrs.getValue( getUri(), "target-type" );
  if ( targetType == null ) {
    throw new ParseException( "Mandatory attribute 'target-type' is missing.", getLocator() );
  }
  final String href = attrs.getValue( getUri(), "href" );
  if ( href == null ) {
    throw new ParseException( "Mandatory attribute 'href' is missing.", getLocator() );
  }

  try {
    final ResourceManager resourceManager = getRootHandler().getResourceManager();
    final ResourceKey key = resourceManager.deriveKey( getRootHandler().getSource(), href );
    final Resource resource = resourceManager.create( key, null, MasterReport.class );
    report = (MasterReport) resource.getResource();
  } catch ( ResourceException re ) {
    throw new ParseException( "Mandatory attribute 'href' is not pointing to a valid report.", re, getLocator() );
  }
}
 
Example 5
Source File: MetaDataReadHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Starts parsing.
 *
 * @param attrs
 *          the attributes.
 * @throws SAXException
 *           if there is a parsing error.
 */
protected void startParsing( final Attributes attrs ) throws SAXException {
  super.startParsing( attrs );

  final ResourceManager resourceManager = getRootHandler().getResourceManager();
  final ResourceKey context = getRootHandler().getContext();

  final Configuration configuration = ClassicEngineBoot.getInstance().getGlobalConfig();
  final Iterator keys = configuration.findPropertyKeys( ElementMetaDataParser.GLOBAL_INCLUDES_PREFIX );
  while ( keys.hasNext() ) {
    final String key = (String) keys.next();
    final String href = configuration.getConfigProperty( key );
    if ( StringUtils.isEmpty( href, true ) ) {
      continue;
    }

    try {
      final ResourceKey resourceKey = resourceManager.deriveKey( context, href );
      final Resource resource = resourceManager.create( resourceKey, null, GlobalMetaDefinition.class );
      globalMetaDefinition.merge( (GlobalMetaDefinition) resource.getResource() );
    } catch ( ResourceException e ) {
      logger.warn( "Failed to parse included global definitions: " + getLocator(), e );
    }
  }

}
 
Example 6
Source File: IncludeGlobalMetaDataReadHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Starts parsing.
 *
 * @param attrs
 *          the attributes.
 * @throws SAXException
 *           if there is a parsing error.
 */
protected void startParsing( final Attributes attrs ) throws SAXException {
  final String href = attrs.getValue( getUri(), "src" );
  if ( href == null ) {
    throw new ParseException( "Required attribute 'src' is missing", getLocator() );
  }

  final ResourceManager resourceManager = getRootHandler().getResourceManager();
  final ResourceKey context = getRootHandler().getContext();
  try {
    final ResourceKey resourceKey = resourceManager.deriveKey( context, href );
    final Resource resource = resourceManager.create( resourceKey, null, GlobalMetaDefinition.class );
    result = (GlobalMetaDefinition) resource.getResource();
  } catch ( ResourceException e ) {
    throw new ParseException( "Failed to parse included global definitions", e, getLocator() );
  }

  if ( globalMetaDefinition != null ) {
    globalMetaDefinition.merge( result );
  }
}
 
Example 7
Source File: StyleSheetHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Receive notification of a import statement in the style sheet.
 *
 * @param uri                 The URI of the imported style sheet.
 * @param media               The intended destination media for style information.
 * @param defaultNamespaceURI The default namespace URI for the imported style sheet.
 * @throws CSSException Any CSS exception, possibly wrapping another exception.
 */
public void importStyle( String uri,
                         SACMediaList media,
                         String defaultNamespaceURI )
  throws CSSException {
  //  instantiate a new parser and parse the stylesheet.
  final ResourceManager manager = getResourceManager();
  if ( manager == null ) {
    // there is no source set, so we have no resource manager, and thus
    // we do no parsing.
    //
    // This should only be the case if we parse style-values; in that case
    // include-statement are not supported anyway.
    return;
  }
  try {
    CSSParserContext.getContext().setDefaultNamespace( defaultNamespaceURI );
    final ResourceKey key;
    if ( source == null ) {
      key = manager.createKey( uri );
    } else {
      key = manager.deriveKey( source, uri );
    }

    final Resource res = manager.create( key, source, StyleSheet.class );
    if ( res == null ) {
      return;
    }
    final StyleSheet styleSheet = (StyleSheet) res.getResource();
    this.styleSheet.addStyleSheet( styleSheet );
  } catch ( ResourceException e ) {
    // ignore ..
  } finally {
    CSSParserContext.getContext().setStyleKeyRegistry( styleKeyRegistry );
    CSSParserContext.getContext().setSource( getSource() );
    CSSParserContext.getContext().setNamespaces( namespaces );
    CSSParserContext.getContext().setDefaultNamespace( defaultNamespace );
  }
}
 
Example 8
Source File: FunctionUtilities.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static CSSResourceValue loadResource( final DocumentContext process,
                                             final Object value,
                                             final Class[] type )
  throws FunctionEvaluationException {
  // ok, this is going to be expensive. Kids, you dont wanna try this at home ...
  final ResourceManager manager = process.getResourceManager();
  final ResourceKey baseKey = process.getContextKey();
  try {
    final ResourceKey key;
    if ( value instanceof ResourceKey ) {
      key = (ResourceKey) value;
    } else if ( baseKey == null ) {
      key = manager.createKey( value );
    } else if ( value instanceof String ) {
      key = manager.deriveKey( baseKey, (String) value );
    } else {
      throw new FunctionEvaluationException
        ( "Failed to create URI: Resource loading failed: Key not derivable" );
    }

    final Resource res = manager.create( key, baseKey, type );
    return new CSSResourceValue( res );
  } catch ( Exception e ) {
    throw new FunctionEvaluationException
      ( "Failed to create URI: Resource loading failed: " + e.getMessage(), e );
  }
}
 
Example 9
Source File: MemDocBundleUpdateTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testBug() throws IOException, ResourceException, InterruptedException {
  final Properties p1 = new Properties();
  p1.setProperty( "key", "value1" );

  final MemoryDocumentBundle bundle = new MemoryDocumentBundle();
  bundle.getWriteableDocumentMetaData().setBundleType( "text/plain" );
  final OutputStream outputStream = bundle.createEntry( "test.properties", "text/plain" );
  p1.store( outputStream, "run 1" );
  outputStream.close();

  final ResourceManager resourceManager = bundle.getResourceManager();
  final ResourceKey key = resourceManager.deriveKey( bundle.getBundleMainKey(), "test.properties" );
  final Resource res1 = resourceManager.create( key, null, Properties.class );
  assertEquals( p1, res1.getResource() );

  bundle.removeEntry( "test.properties" );

  final Properties p2 = new Properties();
  p2.setProperty( "key", "value2" );

  final OutputStream outputStream2 = bundle.createEntry( "test.properties", "text/plain" );
  p2.store( outputStream2, "run 2" );
  outputStream2.close();

  final Resource res2 = resourceManager.create( key, null, Properties.class );
  assertEquals( p2, res2.getResource() );
}
 
Example 10
Source File: KettleEmbeddedTransReadHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private byte[] loadDataFromBundle( final String href ) throws ResourceKeyCreationException, ResourceLoadingException {
  final ResourceKey key = getRootHandler().getSource();
  final ResourceManager manager = getRootHandler().getResourceManager();
  final ResourceKey derivedKey = manager.deriveKey( key, href );
  final ResourceData data = manager.load( derivedKey );
  return data.getResource( manager );
}
 
Example 11
Source File: KettleTransFromFileProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private ResourceKey createKey( final ResourceManager resourceManager,
                               final ResourceKey contextKey ) throws ResourceKeyCreationException {
  try {
    return resourceManager.deriveKey( contextKey, transformationFile );
  } catch ( ResourceKeyCreationException e ) {
    // failure is expected ..
  }

  return resourceManager.createKey( new File( transformationFile ) );
}
 
Example 12
Source File: URLResourceLoaderTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Tests the serialization of File based resource keys
 */
public void testSerialize() throws Exception {
  final URLResourceLoader resourceLoader = new URLResourceLoader();
  final ResourceManager manager = new ResourceManager();
  manager.registerDefaults();
  ResourceKey key = null;
  Map<ParameterKey, Object> factoryParameters = new LinkedHashMap<ParameterKey, Object>();
  String serializedVersion = null;

  // Test with null parameter
  try {
    serializedVersion = resourceLoader.serialize( null, key );
    fail( "Serialization with a null paramter should throw a NullPointerException" ); //$NON-NLS-1$
  } catch ( NullPointerException npe ) {
    // success
  }

  // Test with a file instead of a URL
  try {
    final File tempFile = File.createTempFile( "unittest", "test" );
    tempFile.deleteOnExit();
    key = manager.createKey( tempFile );
    serializedVersion = resourceLoader.serialize( key, key );
    fail( "The resource key should not handled by the URL resource loader" ); //$NON-NLS-1$
  } catch ( IllegalArgumentException iae ) {
    // success
  }

  // Create a key from the temp file
  key = manager.createKey( new URL( URL1 ) );
  serializedVersion = resourceLoader.serialize( key, key );
  assertNotNull( "The returned key should not be null", key ); //$NON-NLS-1$
  assertTrue( "Serialized verison does not start with the correct header", serializedVersion //$NON-NLS-1$
    .startsWith( STRING_SERIALIZATION_PREFIX ) );
  assertTrue( "Serialized version does not contain the correct schema information", serializedVersion //$NON-NLS-1$
    .startsWith( STRING_SERIALIZATION_PREFIX + resourceLoader.getClass().getName() + ';' ) );
  assertTrue( "Serialized version should contain the filename", serializedVersion.endsWith( URL1 ) ); //$NON-NLS-1$

  // Create a key as a relative path from the above key
  key = manager.deriveKey( key, "images/pentaho_logo.png" );
  assertNotNull( key );
  serializedVersion = resourceLoader.serialize( key, key );
  assertNotNull( serializedVersion );
  assertTrue( "Serialized verison does not start with the correct header", serializedVersion //$NON-NLS-1$
    .startsWith( STRING_SERIALIZATION_PREFIX ) );
  assertTrue( "Serialized version does not contain the correct schema information", serializedVersion //$NON-NLS-1$
    .startsWith( STRING_SERIALIZATION_PREFIX + resourceLoader.getClass().getName() + ';' ) );
  assertTrue( "Serialized version should contain the filename", serializedVersion.endsWith( URL2 ) ); //$NON-NLS-1$

  // Create a key with factory parameters
  factoryParameters.put( new FactoryParameterKey( "this" ), "that" );
  factoryParameters.put( new FactoryParameterKey( "null" ), null );
  key = manager.createKey( new URL( URL1 ), factoryParameters );
  serializedVersion = resourceLoader.serialize( key, key );

  assertEquals( "resourcekey:org.pentaho.reporting.libraries.resourceloader.loader.URLResourceLoader;" +
    "http://www.pentaho.com/index.html;\"\"\"f:this=that\"\":\"\"f:null=\"\"\"", serializedVersion );
}
 
Example 13
Source File: ClassloaderResourceLoaderTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testSerialize() throws Exception {
  final ResourceLoader resourceLoader = new ClassloaderResourceLoader();
  final ResourceManager manager = new ResourceManager();
  manager.registerDefaults();

  // Test failure - null key
  try {
    resourceLoader.serialize( null, null );
    fail( "Serialization of a null key should throw an exception" );
  } catch ( NullPointerException npe ) {
    // success 
  }

  // Test failure - not a Classloader resource key
  try {
    final File tempFile = File.createTempFile( "junit-test", ".tmp" );
    final ResourceKey tempKey = manager.createKey( tempFile );
    resourceLoader.serialize( null, tempKey );
    fail( "The Classloader Resource Loader should fail when handling a non-classloader resource key" );
  } catch ( IllegalArgumentException iae ) {
    // success
  }

  // Create key
  final String key1source = "res://org/pentaho/reporting/libraries/resourceloader/test1.properties";
  final ResourceKey key1 = manager.createKey( key1source );
  assertNotNull( key1 );

  // Serialize the key
  final String serKey1 = resourceLoader.serialize( null, key1 );
  assertNotNull( "The returned key should not be null", serKey1 ); //$NON-NLS-1$
  assertTrue( "Serialized verison does not start with the correct header",
    serKey1.startsWith( STRING_SERIALIZATION_PREFIX ) );
  assertTrue( "Serialized version does not contain the correct schema information",
    serKey1.startsWith( DESERIALIZE_PREFIX ) );
  assertTrue( "Serialized version should contain the identifier intact",
    serKey1.endsWith( key1.getIdentifier().toString() ) );

  // Serialize a key created from a derived key
  final String key2source = "test2.properties";
  final ResourceKey key2 = manager.deriveKey( key1, key2source );
  assertNotNull( key2 );

  final String serKey2 = resourceLoader.serialize( null, key2 );
  assertNotNull( "The returned key should not be null", serKey2 ); //$NON-NLS-1$
  assertTrue( "Serialized verison does not start with the correct header",
    serKey2.startsWith( STRING_SERIALIZATION_PREFIX ) );
  assertTrue( "Serialized version does not contain the correct schema information",
    serKey2.startsWith( DESERIALIZE_PREFIX ) );
  assertTrue( "Serialized version should contain the identifier intact",
    serKey2.endsWith( ";res://org/pentaho/reporting/libraries/resourceloader/test2.properties" ) );

  // Serialize a key with factory parameters
  final Map<ParameterKey, Object> factoryParams = new LinkedHashMap<ParameterKey, Object>();
  factoryParams.put( new FactoryParameterKey( "this" ), "that" );
  factoryParams.put( new FactoryParameterKey( "null" ), null );
  final ResourceKey key3 = manager.createKey( key1source, factoryParams );
  assertNotNull( key3 );

  final String serKey3 = resourceLoader.serialize( null, key3 );
  assertEquals( "resourcekey:org.pentaho.reporting.libraries.resourceloader" +
    ".loader.resource.ClassloaderResourceLoader;" +
    "res://org/pentaho/reporting/libraries/resourceloader/" +
    "test1.properties;\"\"\"f:this=that\"\":\"\"f:null=\"\"\"", serKey3 );

}
 
Example 14
Source File: FileResourceLoaderTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Tests the serialization of File based resource keys
 */
public void testSerialize() throws Exception {
  final FileResourceLoader fileResourceLoader = new FileResourceLoader();
  final ResourceManager manager = new ResourceManager();
  manager.registerDefaults();
  ResourceKey key = null;
  Map<ParameterKey, Object> factoryParameters = new HashMap<ParameterKey, Object>();
  String serializedVersion = null;

  // Test with null parameter
  try {
    serializedVersion = fileResourceLoader.serialize( null, key );
    fail( "Serialization with a null paramter should throw a NullPointerException" ); //$NON-NLS-1$
  } catch ( NullPointerException npe ) {
    // success
  }

  // Test with a resource key instead of a file key
  try {
    key = manager.createKey( "res://org/pentaho/reporting/libraries/resourceloader/test1.properties" ); //$NON-NLS-1$
    serializedVersion = fileResourceLoader.serialize( key, key );
    fail( "The resource key should not handles by the file resource loader" ); //$NON-NLS-1$
  } catch ( IllegalArgumentException iae ) {
    // success
  }

  // Create a key from the temp file
  key = manager.createKey( tempFile );
  serializedVersion = fileResourceLoader.serialize( key, key );
  assertNotNull( "The returned key should not be null", key ); //$NON-NLS-1$
  assertTrue( "Serialized verison does not start with the correct header", serializedVersion //$NON-NLS-1$
    .startsWith( STRING_SERIALIZATION_PREFIX ) );
  assertTrue( "Serialized version does not contain the correct schema information", serializedVersion //$NON-NLS-1$
    .startsWith( STRING_SERIALIZATION_PREFIX + fileResourceLoader.getClass().getName() + ';' ) );
  assertTrue( "Serialized version should contain the filename",
    serializedVersion.endsWith( tempFile.getName() ) ); //$NON-NLS-1$

  // Create a key as a relative path from the above key
  key = manager.deriveKey( key, tempRelativeFilename );
  assertNotNull( key );
  serializedVersion = fileResourceLoader.serialize( key, key );
  assertNotNull( serializedVersion );
  assertTrue( "Serialized verison does not start with the correct header", serializedVersion //$NON-NLS-1$
    .startsWith( STRING_SERIALIZATION_PREFIX ) );
  assertTrue( "Serialized version does not contain the correct schema information", serializedVersion //$NON-NLS-1$
    .startsWith( STRING_SERIALIZATION_PREFIX + fileResourceLoader.getClass().getName() + ';' ) );
  assertTrue(
    "Serialized version should contain the filename",
    serializedVersion.endsWith( tempSubFile.getCanonicalPath() ) ); //$NON-NLS-1$

  // Create a key with factory parameters
  factoryParameters.put( new FactoryParameterKey( "this" ), "that" );
  factoryParameters.put( new FactoryParameterKey( "null" ), null );
  key = manager.createKey( tempFile, factoryParameters );
  serializedVersion = fileResourceLoader.serialize( key, key );

  assertNotNull( "The returned key should not be null", key ); //$NON-NLS-1$
  assertTrue( "Serialized verison does not start with the correct header", serializedVersion //$NON-NLS-1$
    .startsWith( STRING_SERIALIZATION_PREFIX ) );
  assertTrue( "Serialized version does not contain the correct schema information", serializedVersion //$NON-NLS-1$
    .startsWith( STRING_SERIALIZATION_PREFIX + fileResourceLoader.getClass().getName() + ';' ) );
  assertTrue(
    "Serialized version should contain the filename",
    serializedVersion.indexOf( ";" + tempFile.getCanonicalPath() + ";" ) > -1 ); //$NON-NLS-1$
  assertTrue( "Serialized version should contain factory parameters", serializedVersion.indexOf( "this=that" ) > -1 );
  assertTrue( "Serialized version should contain factory parameters", serializedVersion.indexOf( ':' ) > -1 );
}