com.siemens.ct.exi.grammars.persistency.Grammars2JSON Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of exificient-grammars Show documentation
Show all versions of exificient-grammars 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.
package com.siemens.ct.exi.grammars.persistency;
/*
* Copyright (c) 2007-2016 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.
*
*/
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import com.siemens.ct.exi.context.GrammarContext;
import com.siemens.ct.exi.context.GrammarUriContext;
import com.siemens.ct.exi.context.QNameContext;
import com.siemens.ct.exi.datatype.Datatype;
import com.siemens.ct.exi.datatype.DatetimeDatatype;
import com.siemens.ct.exi.datatype.EnumerationDatatype;
import com.siemens.ct.exi.datatype.ListDatatype;
import com.siemens.ct.exi.datatype.NBitUnsignedIntegerDatatype;
import com.siemens.ct.exi.datatype.RestrictedCharacterSetDatatype;
import com.siemens.ct.exi.datatype.charset.RestrictedCharacterSet;
import com.siemens.ct.exi.exceptions.EXIException;
import com.siemens.ct.exi.grammars.SchemaInformedGrammars;
import com.siemens.ct.exi.grammars.XSDGrammarsBuilder;
import com.siemens.ct.exi.grammars.event.Attribute;
import com.siemens.ct.exi.grammars.event.AttributeNS;
import com.siemens.ct.exi.grammars.event.Characters;
import com.siemens.ct.exi.grammars.event.DatatypeEvent;
import com.siemens.ct.exi.grammars.event.Event;
import com.siemens.ct.exi.grammars.event.EventType;
import com.siemens.ct.exi.grammars.event.StartElement;
import com.siemens.ct.exi.grammars.event.StartElementNS;
import com.siemens.ct.exi.grammars.grammar.DocEnd;
import com.siemens.ct.exi.grammars.grammar.Document;
import com.siemens.ct.exi.grammars.grammar.Fragment;
import com.siemens.ct.exi.grammars.grammar.Grammar;
import com.siemens.ct.exi.grammars.grammar.SchemaInformedDocContent;
import com.siemens.ct.exi.grammars.grammar.SchemaInformedElement;
import com.siemens.ct.exi.grammars.grammar.SchemaInformedFirstStartTagGrammar;
import com.siemens.ct.exi.grammars.grammar.SchemaInformedFragmentContent;
import com.siemens.ct.exi.grammars.grammar.SchemaInformedGrammar;
import com.siemens.ct.exi.grammars.grammar.SchemaInformedStartTagGrammar;
import com.siemens.ct.exi.grammars.production.Production;
import com.siemens.ct.exi.types.BuiltIn;
import com.siemens.ct.exi.types.BuiltInType;
import com.siemens.ct.exi.util.PrintfUtils;
import com.siemens.ct.exi.values.IntegerValue;
import com.siemens.ct.exi.values.Value;
public class Grammars2JSON {
public static final boolean STATS_ON = true;
protected int statsCountTransitions = 0;
protected int statsCountStates = 0;
GrammarsPreperation gpreps = new GrammarsPreperation();
public Grammars2JSON() {
super();
}
protected void clear() {
gpreps.clear();
}
public void toGrammarsJSON(SchemaInformedGrammars grammars, OutputStream os)
throws IOException, EXIException {
// clear
clear();
// prepare grammar rules
gpreps.prepareGrammars(grammars);
GrammarContext grammarContext = grammars.getGrammarContext();
Writer w = new OutputStreamWriter(os);
List listOfDatatypes = new ArrayList();
for (int i = 0; i < gpreps.getNumberOfGrammars(); i++) {
Grammar r = gpreps.getGrammar(i);
for(int k=0; k listOfDatatypes) throws IOException {
PrintfUtils.printfIndLn(w, ind, "{" );
ind++;
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%d\" ,", GrammarsConstants.GRAMMAR_ID, gpreps.getGrammarID(sir)); // GrammarID not necessary but useful
if (sir instanceof SchemaInformedFirstStartTagGrammar) {
SchemaInformedFirstStartTagGrammar fst = (SchemaInformedFirstStartTagGrammar) sir;
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.TYPE, GrammarsConstants.FIRST_START_TAG_CONTENT);
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %s ,", GrammarsConstants.IS_TYPE_CASTABLE, fst.isTypeCastable());
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %s ,", GrammarsConstants.IS_NILLABLE, fst.isNillable());
} else if (sir instanceof SchemaInformedStartTagGrammar) {
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.TYPE, GrammarsConstants.START_TAG_CONTENT);
} else if (sir instanceof SchemaInformedElement) {
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.TYPE, GrammarsConstants.ELEMENT_CONTENT);
} else if (sir instanceof Document) {
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.TYPE, GrammarsConstants.DOCUMENT);
} else if (sir instanceof SchemaInformedDocContent) {
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.TYPE, GrammarsConstants.DOC_CONTENT);
} else if (sir instanceof DocEnd) {
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.TYPE, GrammarsConstants.DOC_END);
} else if (sir instanceof Fragment) {
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.TYPE, GrammarsConstants.FRAGMENT);
} else if (sir instanceof SchemaInformedFragmentContent) {
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.TYPE, GrammarsConstants.FRAGMENT_CONTENT);
} else {
// System.err.println("Unkown Rule type: " + sir);
throw new RuntimeException("Unkown Rule type: " + sir);
}
if (STATS_ON) {
statsCountStates++;
}
PrintfUtils.printfIndLn(w, ind, "\"%s\" : [ ", GrammarsConstants.PRODUCTION);
ind++;
printGrammarProduction(w, ind, sir, listOfDatatypes); // , ruleStateID);
ind--;
PrintfUtils.printfIndLn(w, ind, "]");
ind--;
PrintfUtils.printfIndLn(w, ind, "}" );
}
protected void printGrammarProduction(Writer w, int ind, SchemaInformedGrammar sir,
List listOfDatatypes) throws IOException {
for (int i = 0; i < sir.getNumberOfEvents(); i++) {
if (STATS_ON) {
statsCountTransitions++;
}
PrintfUtils.printfIndLn(w, ind, "{");
ind++;
Production ei = sir.getProduction(i);
Event event = ei.getEvent();
EventType eventType = event.getEventType();
switch (eventType) {
case START_DOCUMENT:
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.START_DOCUMENT);
break;
case END_DOCUMENT:
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.END_DOCUMENT);
break;
case START_ELEMENT:
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.START_ELEMENT);
{
StartElement se = (StartElement) event;
QNameContext eqname = se.getQNameContext();
// qname
// PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.START_ELEMENT_QNAME_ID, eqname.getQNameID());
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.START_ELEMENT_NAMESPACE_ID, eqname.getNamespaceUriID());
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.START_ELEMENT_LOCALNAME_ID, eqname.getLocalNameID());
// grammar
Grammar seRule = se.getGrammar();
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.START_ELEMENT_GRAMMAR_ID, gpreps.getGrammarID(seRule));
}
break;
case START_ELEMENT_NS:
PrintfUtils.printfIndLn(w, ind, "\"%\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.START_ELEMENT_NS);
{
StartElementNS seNS = (StartElementNS) event;
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.START_ELEMENT_NAMESPACE_ID, seNS.getNamespaceUriID());
}
break;
case END_ELEMENT:
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.END_ELEMENT);
break;
case ATTRIBUTE:
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.ATTRIBUTE);
{
Attribute at = (Attribute) event;
QNameContext eqname = at.getQNameContext();
// qname
// PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.ATTRIBUTE_QNAME_ID, eqname.getQNameID());
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.ATTRIBUTE_NAMESPACE_ID, eqname.getNamespaceUriID());
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.ATTRIBUTE_LOCALNAME_ID, eqname.getLocalNameID());
// datatype
Datatype dt = at.getDatatype();
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.ATTRIBUTE_DATATYPE_ID, listOfDatatypes.indexOf(dt));
}
break;
case ATTRIBUTE_NS:
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.ATTRIBUTE_NS);
{
AttributeNS atNS = (AttributeNS) event;
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.ATTRIBUTE_NAMESPACE_ID, atNS.getNamespaceUriID());
}
break;
case CHARACTERS:
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.CHARACTERS);
{
Characters ch = (Characters) event;
Datatype dt = ch.getDatatype();
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d ,", GrammarsConstants.CHARACTERS_DATATYPE_ID, listOfDatatypes.indexOf(dt));
}
break;
case START_ELEMENT_GENERIC:
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.START_ELEMENT_GENERIC);
break;
case ATTRIBUTE_GENERIC:
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.ATTRIBUTE_GENERIC);
break;
case CHARACTERS_GENERIC:
PrintfUtils.printfIndLn(w, ind, "\"%s\" : \"%s\" ,", GrammarsConstants.EVENT, GrammarsConstants.CHARACTERS_GENERIC);
break;
default:
System.err.println("Unknown Event " + ei.getEvent());
}
// next state ID
{
Grammar nextRule = ei.getNextGrammar();
if (nextRule.getNumberOfEvents() > 0) {
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d", GrammarsConstants.NEXT_GRAMMAR_ID, gpreps.getGrammarID(nextRule));
} else {
// No events anymore, eg. EE, ED
PrintfUtils.printfIndLn(w, ind, "\"%s\" : %d", GrammarsConstants.NEXT_GRAMMAR_ID, -1);
}
}
ind--;
PrintfUtils.printfIndLn(w, ind, "}");
if(i < (sir.getNumberOfEvents()-1)) {
PrintfUtils.printfIndLn(w, ind, ", ");
}
}
}
protected void printBuiltInDatatype(Writer w, int ind, Datatype dt) throws IOException {
printBuiltInDatatype(w, ind, dt, "type");
}
protected void printBuiltInDatatype(Writer w, int ind, Datatype dt, String typeID) throws IOException {
BuiltInType bit = dt.getBuiltInType();
PrintfUtils.printfInd(w, ind, "\"%s\": \"%s\"", typeID, bit);
switch (bit) {
case NBIT_UNSIGNED_INTEGER:
PrintfUtils.printfLn(w, ",", bit);
NBitUnsignedIntegerDatatype nbdt = (NBitUnsignedIntegerDatatype) dt;
IntegerValue ivLower = nbdt.getLowerBound();
PrintfUtils.printfIndLn(w, ind, "\"%s\": %s,", GrammarsConstants.LOWER_BOUND, ivLower.toString());
IntegerValue ivUpper = nbdt.getUpperBound();
PrintfUtils.printfIndLn(w, ind, "\"%s\": %s", GrammarsConstants.UPPER_BOUND, ivUpper.toString());
break;
case LIST:
PrintfUtils.printfLn(w, ",", bit);
ListDatatype ldt = (ListDatatype) dt;
this.printBuiltInDatatype(w, ind, ldt.getListDatatype(), "listType"); //,grammarContext);
break;
case ENUMERATION:
PrintfUtils.printfLn(w, ",", bit);
EnumerationDatatype edt = (EnumerationDatatype) dt;
PrintfUtils.printfInd(w, ind, "\"%s\": [", "enumValues");
for(int i=0; i