metadataparser.jar

Description
this bundle provides classes to parser XML metadata in a generic way
For more details on how to use this bundle, refer to the /doc/readme.html file embedded in the bundle jarfile.

Contributors

License
ASL 1.1

Services

Properties

Requirements


Download

Build

  1. configure the common.properties if necessary
  2. configure the project.properties if necessary
  3. build with ant

Settings
None

Run the demo
To run the demo of this bundle, enter the following commands in the OSCAR shell

start http://www-adele.imag.fr/~donsez/dev/osgi/metadataparser/metadataparser.jar
start http://www-adele.imag.fr/~donsez/dev/osgi/metadataparsertest/metadataparsertest.jar
obr start "OSGi Util"
start http://www-adele.imag.fr/~donsez/dev/osgi/metadataparsertest/metadataparsertest.jar
rem or obr fstart http://www-adele.imag.fr/~donsez/dev/osgi/metadataparsertest/metadataparsertest.jar
rem instead of the 2 previous command lines

Metadata Parsing
UNDER CONSTRUCTION
see the res/metadata.xml in http://www-adele.imag.fr/~donsez/dev/osgi/metadataparsertest/metadataparsertest-src.jar
The generic metadata parser can be use to parse XML metadata in a generic way
You have to set a metadata type (HashMap, ArrayList, String, yours, ...) for each element in the metadata

		// metadata file can be a path in the bundle classpath or a remote document over the Web
		// 1) get the input stream
		InputStream is=getClass().getResourceAsStream("/metadata.xml");
			

		// 2) create a metadata handler
		// * KXmlMetadataHandler for kXML parser (light XML parser)
		// * XmlMetadataHandler for SAX compliant parser (JAXP, Xerces, ...)
		KXmlMetadataHandler handler=new KXmlMetadataHandler();
		// SaxMetadataHandler handler=new SaxMetadataHandler();

		// 3) set the corresponding type for each PI
		handler.setMissingPIExceptionFlag(false);
		handler.addPI("add", MappingProcessingInstructionHandler.class);


		// 4) set the corresponding type for each element
		handler.addType("bundles", ArrayList.class);
			// java.util.ArrayList is used for a element which is a list of sub-elements
		// or handler.addType("bundles", new ArrayListFactory()); if ArrayListFactory.class have a newInstance method
			
		handler.addType("bundle", fr.imag.adele.collection.MultivalueMap.class);
			// attributes and sub-elements are put in MultivalueMap. When sub-elements have the same name, the values are stored in a java.util.List. 

		handler.addType("import", PackageDeclaration.class);
		handler.addType("export", PackageDeclaration.class);
			// the specified type must have setter or adder methods for each attributes and sub-elements
		
		// 5) set a default type (String, HashMap, ...) for the other elements 
		// if the default type is not set, then the parsing of other elements fails 
		handler.setDefaultType(String.class);
			// String is for a terminal element (ie EMPTY with a value attribute or PCDATA)

		// 7) parse the stream
		handler.parse(is);	
		
		// 8) use the metadata	
		return handler.getMetadata();

In this example, the instruction 3 enables to add types during parsing by adding the following processing instructions in the metadata files:
	
<?mapping element="native" class="test.NativeDeclaration"?>
<?mapping defaultclass="java.util.HashMap"?>
The processing instructions overwrite the calls to addType/setDefaultType methods of the handler

Typical Usages

  • parse XML metadata in OBR, WireAdminBinder, Metatype (OSGi SPec R3 chapter 18), ...
  • Tested gateways

    Versions history

    TODO (contributions are welcome)