Advanced parsing and writing of a data source

In addition to the basic IlpDefaultDataSource API for parsing a data source and writing the data source content to an XML file, JViews TGO provides the classes IlpDataSourceLoader and IlpDataSourceOutput for a more advanced use. Both these classes are located in the package ilog.cpl.storage .

Parsing an XML File

The class IlpDataSourceLoader has the following functionality:
  • Reads the data source content directly from a SAX InputSource .
  • Gives access to the SAX XMLReader . You can modify the behavior of the XML reader before the parsing takes place (for example, to disable the validation of the XML schema).

How to modify the behavior of the XML reader

IlpDataSourceLoader loader = new IlpDataSourceLoader(inputSource,
                                                     dataSource);
XMLReader reader = loader.getXMLReader();
reader.setFeature("http://xml.org/sax/features/validation",false);
loader.parse();
  • Uses an identifier factory. An identifier factory lets you change the identifiers while the XML file is being loaded.
  • Loads new root objects as children of an object present in the data source.
  • Loads objects that match filter criteria.
The example below loads a template file into the data source. It shows how to create an identifier factory to modify parent identifiers of root objects. The complete sample code is located in the following directory:
<installdir>/tutorials/browser

How to load a file into a data source

final Object parentID = expandedObject.getIdentifier();

// Create an identifier factory that adds the parent ID as a prefix to
// all identifiers read from the template.
IlpIdentifierFactory idFactory = new IlpIdentifierFactory(){
  public Object getIdentifier (Object previousIdentifier){
    return parentID.toString() + "/" + previousIdentifier.toString();
  }
};
// Load the template into the datasource
// Load the template objects under the parent node, and transform
// their IDs so they are unique
IlpDataSourceLoader loader = new IlpDataSourceLoader(templateFileName,
                                                     mainDataSource);
loader.setIdentifierFactory(idFactory);
loader.setParentIdOfRootObjects(parentID);
loader.parse();

Writing to an XML file

The class IlpDataSourceOutput has the following functionality:
  • Writes the contents of the data source directly to a SAX ContentHandler . This content handler can be used to feed a DOM tree for example.
  • Enables or disables enhanced printing. By default, pretty printing is enabled but you can deactivate it to improve performance.
  • Uses an identifier factory. An identifier factory lets you change the identifiers while the XML file is being written. Its use is the same as for parsing.
  • Writes a hierarchy of objects by calling the method outputHierarchy(IlpObject) . The IlpObject parameter and all its children (which are defined by the container interface) are written to the file.
  • Writes a single object by calling outputObject(IlpObject) .
  • Writes only the objects that match the specified filter criteria.