6

I'm looking for a Java sample program for encoding/decoding EXI (Efficient XML Interchange) streams, using either EXIficient or OpenEXI.

Could someone help? I can't seem to find a sample app.

edit: Alternatively, if someone could point me towards documentation that would allow me to use EXIficient or OpenEXI, that would be helpful. I found the javadoc but I have no idea which classes to use.

Or, as @StaxMan points out, is there a particular mention/discussion of the appropriate top-level classes to use with one of the standard XML APIs?

Sled
  • 18,541
  • 27
  • 119
  • 168
Jason S
  • 184,598
  • 164
  • 608
  • 970

5 Answers5

4

Using Exificient, I've managed to encode & decode a sample XML as EXI using the snippet from their "Help" page and their "demo", I've managed to get the following working using just the Maven dependency

<dependency>
    <groupId>com.siemens.ct.exi</groupId>
    <artifactId>exificient</artifactId>
    <version>0.9.4</version>
</dependency>

with the code

import com.siemens.ct.exi.EXIFactory;
import com.siemens.ct.exi.GrammarFactory;
import com.siemens.ct.exi.api.sax.EXIResult;
import com.siemens.ct.exi.api.sax.EXISource;
import com.siemens.ct.exi.helpers.DefaultEXIFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;


// based on: https://sourceforge.net/p/exificient/code/HEAD/tree/tags/exificient-0.9.4/src/sample/java/EXIficientDemo.java?format=raw
public class MinimalistExificientSample {

    static final boolean USE_SCHEMA = true;

    public static void main(String[] args) throws Exception{
        File xmlIn  = new File( "/home/artb/spring-core-4.1.6.RELEASE.pom.xml" );
        FileInputStream xmlIns = new FileInputStream( xmlIn );

        File exi = new File( "/home/artb/spring-core-4.1.6.RELEASE.pom." +(USE_SCHEMA? "schema":"schemaless")+  ".exi" )
        FileOutputStream exiOuts = new FileOutputStream( exi );

        File xmlOut  = new File( "/home/artb/spring-core-4.1.6.RELEASE.pom." +(USE_SCHEMA? "schema":"schemaless")+ ".xml" );

        //settings
        EXIFactory exiFactory = DefaultEXIFactory.newInstance();
        exiFactory.setGrammars( GrammarFactory.newInstance().createGrammars( "/home/artb/maven-4.0.0.xsd" ) );

        // encode
        InputSource xmlIs = new InputSource( xmlIns );
        EXIResult exiResult = USE_SCHEMA ? new EXIResult(exiFactory) : new EXIResult();
        exiResult.setOutputStream( exiOuts );
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setContentHandler( exiResult.getHandler() );
        xmlReader.parse( xmlIs );

        // decode
        FileOutputStream xmlOuts = new FileOutputStream( xmlOut );
        FileInputStream exiIns = new FileInputStream( exi );
        InputSource exiIs = new InputSource( exiIns );
        EXISource exiSource = USE_SCHEMA ? new EXISource(exiFactory) : new EXISource();
        exiSource.setInputSource(exiIs);
        exiSource.setXMLReader( exiSource.getXMLReader() );
        TransformerFactory.newInstance().newTransformer().transform(exiSource, new StreamResult(xmlOuts));
    }

}

use the Maven XSD and the Spring 4.1.6 POM from Maven central as sample data.

Nb: that the output XML ought to be equivalent to the input, but not necessarily equal since comments and whitespace are lost and the namespace aliases(? might be using the wrong term here) are arbitrarily assigned.

Sled
  • 18,541
  • 27
  • 119
  • 168
2

I am not familiar with the two libraries mentioned; but I assume that as any sane XML libraries they implement one of well-known Java XML processing APIs, such as SAX, Stax or DOM (possibly with some extensions). This because EXI is just different encoding of standard XML Infoset; so for application developer things may look exactly like normal XML processing.

So, you may just need to figure out API(s) they implement and find tutorials for using that API?

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • ....which is exactly the question I'm trying to answer. – Jason S Jul 01 '11 at 13:34
  • Right, it's just that you didn't explicitly ask which APIs they might implement, which could be easier to answer (although project web pages seemed surprisingly mum about this important piece of information). So I thought I'll mention it just in case. – StaxMan Jul 01 '11 at 17:05
  • OK, fair enough -- I'll edit my question. – Jason S Jul 01 '11 at 17:11
0

There are some unit tests in EXIfiecient code base that you can use as example.

Andrey Adamovich
  • 20,285
  • 14
  • 94
  • 132
0

The way to use OpenEXI is summerized below where steps 2 and 3 are optional.

  1. Use EXISchemaFactory in org.openexi.fujitsu.scomp package to convert XML Schemas into EXISchema.
  2. Serialize EXISchema to a file.
  3. Load EXISchema from a file.
  4. Instantiate GrammarCache in org.openexi.fujitsu.proc.grammars package by specifying the EXISchema to the constructor.
  5. Instantiate Transmogrifier in org.openexi.fujitsu.sax package, and set the GrammarCache using setEXISchema(grammarCache) method.
  6. Encode an XML document into EXI by calling encode(InputSource is) method.
  7. Use EXIDecoder in org.openexi.fujitsu.proc to decode EXI into events. After instantiating EXIDecoder, set GrammarCache and InputStream to it. Calling processHeader() returns a Scanner, and the Scanner lets you navigate through EXI. Simply call nextEvent() on Scanner to get the next event until it eventually returns null.

If you do not use schemas, you can use null when you instantiate GrammarCache above.

Please let me know if you have any troubles.

taki

takuki
  • 124
  • 1
  • 5
  • Does OpenEXI implement some of standard XML processing APIs? Or is it only via a proprietary interface? – StaxMan Jul 01 '11 at 17:06
  • 1
    SAX API is coming very shortly. On the encoding side, it already supports SAX. Transmogrifier's getSAXTransmogrifier() method returns SAX handler. On the decoding side, a foundation that would make SAX-based decoder layer performant has just been in place this week, and the final work of SAX-based deocoding API is just one step away due in one or two weeks, depending on the availability of labor. – takuki Jul 01 '11 at 17:29
0

OpenEXI now provides tutorial [1] on how to program with it.

[1] http://openexi.sourceforge.net/tutorial/

Also, this OpenEXI discussion forum post [2] shows code to convert from binary XML to a (JaXB) Java object by using the "EXIReader" SAX XMLReader implementation:

[2] http://sourceforge.net/p/openexi/discussion/800031/thread/3103260f/?limit=50#2191

user3483102
  • 303
  • 3
  • 5
takuki
  • 124
  • 1
  • 5