
com.siemens.ct.exi.main.api.sax.SAXEncoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of exificient Show documentation
Show all versions of exificient Show documentation
EXIficient is an open source implementation of the W3C Efficient XML Interchange
(EXI) format specification written in the Java programming language. The EXI format is a very
compact representation for the Extensible Markup Language (XML) Information Set that is intended
to simultaneously optimize performance and the utilization of computational resources.
/*
* Copyright (c) 2007-2018 Siemens AG
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.siemens.ct.exi.main.api.sax;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.ext.DefaultHandler2;
import com.siemens.ct.exi.core.Constants;
import com.siemens.ct.exi.core.EXIBodyEncoder;
import com.siemens.ct.exi.core.EXIFactory;
import com.siemens.ct.exi.core.EXIStreamEncoder;
import com.siemens.ct.exi.core.attributes.AttributeFactory;
import com.siemens.ct.exi.core.attributes.AttributeList;
import com.siemens.ct.exi.core.exceptions.EXIException;
import com.siemens.ct.exi.core.values.StringValue;
/**
* Serializes SAX events to EXI stream.
*
* @author [email protected]
* @author [email protected]
*
*/
public class SAXEncoder extends DefaultHandler2 {
protected EXIFactory factory;
protected EXIStreamEncoder exiStream;
protected EXIBodyEncoder encoder;
// attributes
protected AttributeList exiAttributes;
public SAXEncoder(EXIFactory factory) throws EXIException {
this.factory = factory;
// exi stream
exiStream = factory.createEXIStreamEncoder();
// attribute list
AttributeFactory attFactory = AttributeFactory.newInstance();
exiAttributes = attFactory.createAttributeListInstance(factory);
}
public void setOutputStream(OutputStream os) throws EXIException,
IOException {
// buffer stream if not already
// TODO is there a *nice* way to detect whether a stream is buffered
if (!(os instanceof BufferedOutputStream
|| os instanceof ByteArrayOutputStream || os instanceof DataOutputStream)) {
os = new BufferedOutputStream(os);
}
// write header & get body encoder
this.encoder = exiStream.encodeHeader(os);
}
/*
* ======================================================================
* Interface ContentHandler
* ======================================================================
*/
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
this.exiAttributes.addNamespaceDeclaration(uri, prefix);
}
// @Override
// public void endPrefixMapping(String prefix) throws SAXException {
// globalPrefixMapping.remove(prefix);
// }
public void startElement(String uri, String local, String raw,
Attributes attributes) throws SAXException {
try {
// no prefix
this.startElementPfx(uri, local, null, attributes);
} catch (Exception e) {
throw new SAXException("startElement: " + raw, e);
}
}
protected void startElementPfx(String uri, String local, String prefix,
Attributes attributes) throws EXIException, IOException {
// start element
encoder.encodeStartElement(uri, local, prefix);
// add remaining attributes (if any)
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
exiAttributes.addAttribute(attributes.getURI(i),
attributes.getLocalName(i), getPrefixOf(attributes, i),
attributes.getValue(i));
}
}
// encode NS and attributes
encoder.encodeAttributeList(exiAttributes);
exiAttributes.clear();
}
private String getPrefixOf(Attributes atts, int index) {
String qname = atts.getQName(index);
String localName = atts.getLocalName(index);
int lengthDifference = qname.length() - localName.length();
return (lengthDifference == 0 ? Constants.XML_DEFAULT_NS_PREFIX : qname
.substring(0, lengthDifference - 1));
}
public void startDocument() throws SAXException {
try {
encoder.encodeStartDocument();
} catch (Exception e) {
throw new SAXException("startDocument", e);
}
}
public void endDocument() throws SAXException {
try {
encoder.encodeEndDocument();
encoder.flush();
} catch (Exception e) {
throw new SAXException("endDocument", e);
}
}
public void endElement(String uri, String local, String raw)
throws SAXException {
try {
encoder.encodeEndElement();
} catch (Exception e) {
throw new SAXException("endElement=" + raw, e);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
try {
encoder.encodeCharacters(new StringValue(new String(ch, start,
length)));
} catch (Exception e) {
throw new SAXException("characters="
+ new String(ch, start, length), e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy