sMeta: Getting Started

sMeta is a very simple, extensible Java API for reading metadata from files of various formats, such as ID3 tags from MP3 files, EXIF data from JPEG files, chunks from PNG files, etc.

Installation

sMeta is released as a JAR file, which you can add to your application classpath to make use of. Some of the metadata implementations depend on other JARs (included with this release in the "lib" directory). If you need to use those implementations, the dependant JARs must be included in your application classpath, too. See the JavaDoc documentation for the MetadataResource in question, it will say if it depends on some external library.

Usage

The general use of sMeta follows this pattern:

  1. Get a MetadataResourceFactoryManager instance.
  2. Get a MetadataResourceFactory for this file type you are using.
  3. Get a MetadataResource for this file.

Here is an example snippet of code that accomplishes these steps:

String filePath = "/path/to/a/file.mp3";


// 1: get a MetadataResourceFactoryManager instance

MetadataResourceFactoryManager manager 

= MetadataResourceFactoryManager.getDefaultManagerInstance();


File oneFile = new File(filePath);


// 2: get a MetadataResourceFactory for this file type

MetadataResourceFactory factory 

= manager.getMetadataResourceFactory(oneFile);


try {

// 3: get a MetadataResource for this file

MetadataResource metaResource 

= factory.getMetadataResourceInstance(oneFile);

// 4: print out all available metadata

Iterable<String> keys = metaResource.getParsedKeys();

for ( String key : keys ) {

Object metaValue = metaResource.getValue(key, Locale.getDefault());

// do something with metaValue here...

}

} catch ( MetadataNotSupportedException e ) {

System.err.println("File [" +oneFile.getAbsolutePath() 

+"] is not supported by the [" 

+factory.getClass().getName() +"] factory");

}

Configuration

sMeta's MetadataResourceFactoryManager uses a set of properties files to register support of different file types and MIME types. It will look in up to three locations for a given type registration, and use the first suitable implementation found. The search paths are classpath relative:

  1. MetadataResourceFactoryManager's configured managerProperties path
  2. smeta.properties
  3. META-INF/smeta.properties

The format of the properties file is just

smeta.factory.KEY = CLASS

where KEY is either a file extension or a MIME type. The CLASS is the fully-qualified class name of something that implements the magoffin.matt.meta.MetadataResourceFactory interface. For example (class names shorted for brevity):

smeta.factory.mp3 = meta.audio.ID3MetadataResourceFactory smeta.factory.audio/mpeg = meta.audio.ID3MetadataResourceFactory smeta.factory.jpg = meta.image.EXIFMetadataResourceFactory smeta.factory.image/jpeg = meta.image.EXIFMetadataResourceFactory

The sMeta JAR file comes with a META-INF/smeta.properties file pre-configured for the standard built-in types supported by sMeta. If you wish to customize or extend these settings, the easiest way is to create a new smeta.properties file and place that on your classpath. You can override any of the built-in settings here, as this configuration will be looked in before the build-in configuration.

SourceForge.net Logo