// Path: src/main/java/com/siemens/ct/exi/main/api/sax/EXIResult.java
// public class EXIResult extends SAXResult {
// 
// 	protected SAXEncoder handler;
// 
// 	public EXIResult() throws IOException, EXIException {
// 		// use default exi-factory
// 		this(DefaultEXIFactory.newInstance());
// 	}
// 
// 	public EXIResult(EXIFactory exiFactory) throws EXIException {
// 		// DefaultHandler2 handler = exiFactory.createEXIWriter(os);
// 		handler = new SAXFactory(exiFactory).createEXIWriter();
// 		// set internal states
// 		setHandler(handler);
// 		setLexicalHandler(handler);
// 	}
// 
// 	public void setOutputStream(OutputStream os) throws EXIException,
// 			IOException {
// 		handler.setOutputStream(os);
// 	}
// 
// }
// 
// Path: src/main/java/com/siemens/ct/exi/main/api/sax/SAXFactory.java
// public class SAXFactory {
// 
// 	protected final EXIFactory exiFactory;
// 
// 	public SAXFactory(EXIFactory exiFactory) {
// 		this.exiFactory = exiFactory;
// 	}
// 
// 	/**
// 	 * Returns an <code>EXIReader</code>
// 	 * 
// 	 * @return reader using the previously set coding options.
// 	 * @throws EXIException
// 	 *             EXI exception
// 	 * 
// 	 */
// 	public XMLReader createEXIReader() throws EXIException {
// 		return new SAXDecoder(exiFactory);
// 	}
// 
// 	/**
// 	 * Returns a <code>SAXEncoder</code> that implements
// 	 * <code>DefaultHandler2</code>
// 	 * 
// 	 * <p>
// 	 * Note that the output stream MUST be set.
// 	 * </p>
// 	 * 
// 	 * @return writer using the previously set coding options.
// 	 * @throws EXIException
// 	 *             EXI exception
// 	 * 
// 	 */
// 	public SAXEncoder createEXIWriter() throws EXIException {
// 		final FidelityOptions fidelityOptions = exiFactory.getFidelityOptions();
// 		if (fidelityOptions.isFidelityEnabled(FidelityOptions.FEATURE_PREFIX)
// 				|| fidelityOptions
// 						.isFidelityEnabled(FidelityOptions.FEATURE_COMMENT)
// 				|| fidelityOptions
// 						.isFidelityEnabled(FidelityOptions.FEATURE_PI)
// 				|| fidelityOptions
// 						.isFidelityEnabled(FidelityOptions.FEATURE_DTD)) {
// 			return new SAXEncoderExtendedHandler(exiFactory);
// 		} else {
// 			return new SAXEncoder(exiFactory);
// 		}
// 	}
// 
// }


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import junit.framework.TestCase;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import com.siemens.ct.exi.core.CodingMode;
import com.siemens.ct.exi.core.Constants;
import com.siemens.ct.exi.core.EXIBodyDecoder;
import com.siemens.ct.exi.core.EXIBodyEncoder;
import com.siemens.ct.exi.core.EXIFactory;
import com.siemens.ct.exi.core.FidelityOptions;
import com.siemens.ct.exi.core.SelfContainedHandler;
import com.siemens.ct.exi.core.exceptions.EXIException;
import com.siemens.ct.exi.core.grammars.event.EventType;
import com.siemens.ct.exi.core.helpers.DefaultEXIFactory;
import com.siemens.ct.exi.core.io.channel.EncoderChannel;
import com.siemens.ct.exi.core.values.StringValue;
import com.siemens.ct.exi.core.values.Value;
import com.siemens.ct.exi.grammars.GrammarFactory;
import com.siemens.ct.exi.main.api.sax.EXIResult;
import com.siemens.ct.exi.main.api.sax.SAXFactory;

		xmlReader.parse(new InputSource(new StringReader(xmlAsString)));

		// decode overall document
		{
			TransformerFactory tf = TransformerFactory.newInstance();
			XMLReader exiReader = new SAXFactory(factory).createEXIReader();
			Transformer transformer = tf.newTransformer();

			ByteArrayOutputStream baosSC = new ByteArrayOutputStream();
			Result result = new StreamResult(baosSC);
			ByteArrayInputStream bais = new ByteArrayInputStream(
					baos.toByteArray());
			InputSource is = new InputSource(bais);
			SAXSource exiSource = new SAXSource(is);
			exiSource.setXMLReader(exiReader);
			transformer.transform(exiSource, result);

			// System.out.println(new String(baosSC.toByteArray()));
		}

		// decode each SC elements
		assertTrue("SC number issue", scHandler.getSCIndices().size() == 3);
		// Note: 2 important aspects
		// a) SC element is EXI Body only
		// b) SC element root starts with fragment grammar

		factory.setFragment(true); // see b)

		for (int i = 0; i < scHandler.getSCIndices().size(); i++) {
			TransformerFactory tf = TransformerFactory.newInstance();

// Path: src/main/java/com/siemens/ct/exi/main/api/sax/EXIResult.java
// public class EXIResult extends SAXResult {
// 
// 	protected SAXEncoder handler;
// 
// 	public EXIResult() throws IOException, EXIException {
// 		// use default exi-factory
// 		this(DefaultEXIFactory.newInstance());
// 	}
// 
// 	public EXIResult(EXIFactory exiFactory) throws EXIException {
// 		// DefaultHandler2 handler = exiFactory.createEXIWriter(os);
// 		handler = new SAXFactory(exiFactory).createEXIWriter();
// 		// set internal states
// 		setHandler(handler);
// 		setLexicalHandler(handler);
// 	}
// 
// 	public void setOutputStream(OutputStream os) throws EXIException,
// 			IOException {
// 		handler.setOutputStream(os);
// 	}
// 
// }
// 
// Path: src/main/java/com/siemens/ct/exi/main/api/sax/SAXFactory.java
// public class SAXFactory {
// 
// 	protected final EXIFactory exiFactory;
// 
// 	public SAXFactory(EXIFactory exiFactory) {
// 		this.exiFactory = exiFactory;
// 	}
// 
// 	/**
// 	 * Returns an <code>EXIReader</code>
// 	 * 
// 	 * @return reader using the previously set coding options.
// 	 * @throws EXIException
// 	 *             EXI exception
// 	 * 
// 	 */
// 	public XMLReader createEXIReader() throws EXIException {
// 		return new SAXDecoder(exiFactory);
// 	}
// 
// 	/**
// 	 * Returns a <code>SAXEncoder</code> that implements
// 	 * <code>DefaultHandler2</code>
// 	 * 
// 	 * <p>
// 	 * Note that the output stream MUST be set.
// 	 * </p>
// 	 * 
// 	 * @return writer using the previously set coding options.
// 	 * @throws EXIException
// 	 *             EXI exception
// 	 * 
// 	 */
// 	public SAXEncoder createEXIWriter() throws EXIException {
// 		final FidelityOptions fidelityOptions = exiFactory.getFidelityOptions();
// 		if (fidelityOptions.isFidelityEnabled(FidelityOptions.FEATURE_PREFIX)
// 				|| fidelityOptions
// 						.isFidelityEnabled(FidelityOptions.FEATURE_COMMENT)
// 				|| fidelityOptions
// 						.isFidelityEnabled(FidelityOptions.FEATURE_PI)
// 				|| fidelityOptions
// 						.isFidelityEnabled(FidelityOptions.FEATURE_DTD)) {
// 			return new SAXEncoderExtendedHandler(exiFactory);
// 		} else {
// 			return new SAXEncoder(exiFactory);
// 		}
// 	}
// 
// }


// Path: src/test/java/com/siemens/ct/exi/main/core/SelfContainedTestCase.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import junit.framework.TestCase;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import com.siemens.ct.exi.core.CodingMode;
import com.siemens.ct.exi.core.Constants;
import com.siemens.ct.exi.core.EXIBodyDecoder;
import com.siemens.ct.exi.core.EXIBodyEncoder;
import com.siemens.ct.exi.core.EXIFactory;
import com.siemens.ct.exi.core.FidelityOptions;
import com.siemens.ct.exi.core.SelfContainedHandler;
import com.siemens.ct.exi.core.exceptions.EXIException;
import com.siemens.ct.exi.core.grammars.event.EventType;
import com.siemens.ct.exi.core.helpers.DefaultEXIFactory;
import com.siemens.ct.exi.core.io.channel.EncoderChannel;
import com.siemens.ct.exi.core.values.StringValue;
import com.siemens.ct.exi.core.values.Value;
import com.siemens.ct.exi.grammars.GrammarFactory;
import com.siemens.ct.exi.main.api.sax.EXIResult;
import com.siemens.ct.exi.main.api.sax.SAXFactory;

		xmlReader.parse(new InputSource(new StringReader(xmlAsString)));

		// decode overall document
		{
			TransformerFactory tf = TransformerFactory.newInstance();
			XMLReader exiReader = new SAXFactory(factory).createEXIReader();
			Transformer transformer = tf.newTransformer();

			ByteArrayOutputStream baosSC = new ByteArrayOutputStream();
			Result result = new StreamResult(baosSC);
			ByteArrayInputStream bais = new ByteArrayInputStream(
					baos.toByteArray());
			InputSource is = new InputSource(bais);
			SAXSource exiSource = new SAXSource(is);
			exiSource.setXMLReader(exiReader);
			transformer.transform(exiSource, result);

			// System.out.println(new String(baosSC.toByteArray()));
		}

		// decode each SC elements
		assertTrue("SC number issue", scHandler.getSCIndices().size() == 3);
		// Note: 2 important aspects
		// a) SC element is EXI Body only
		// b) SC element root starts with fragment grammar

		factory.setFragment(true); // see b)

		for (int i = 0; i < scHandler.getSCIndices().size(); i++) {
			TransformerFactory tf = TransformerFactory.newInstance();

			XMLReader exiReader = new SAXFactory(factory).createEXIReader();
