Skip to content

Commit

Permalink
handle the default datatype as no occurrence
Browse files Browse the repository at this point in the history
The default datatype is not added to the listOfSimpleDatatypes, so in an example it was serialized as -1 (the result of indexOf), so I changed the attributeDatatypeID elements to have a minOccurs of 0 and a lack of occurrence means the default datatype.
  • Loading branch information
amarant committed Sep 26, 2017
1 parent 02b5368 commit efb1009
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,7 @@ public ExiGrammars toGrammarsX(SchemaInformedGrammars grammars)
// simple type grammar
Characters chev = (Characters) typeGrammar
.getProduction(0).getEvent();
long l = listOfSimpleDatatypes.indexOf(chev
.getDatatype());
qnameContext.setGlobalSimpleTypeDatatypeID(l);
qnameContext.setGlobalSimpleTypeDatatypeID(getDatatypeIndex(chev.getDatatype(), false));
} else {
// complex type grammar
long gid = gpreps.getGrammarID(qnc
Expand All @@ -290,9 +288,7 @@ public ExiGrammars toGrammarsX(SchemaInformedGrammars grammars)
// global attribute
if (qnc.getGlobalAttribute() != null) {
Attribute at = qnc.getGlobalAttribute();
long l = listOfSimpleDatatypes.indexOf(at
.getDatatype());
qnameContext.setGlobalAttributeDatatypeID(l);
qnameContext.setGlobalAttributeDatatypeID(getDatatypeIndex(at.getDatatype(), false));
}

}
Expand Down Expand Up @@ -629,7 +625,7 @@ private static void setDatatype(
}

protected void printGrammar(SchemaInformedGrammar sir,
ExiGrammars.Grammars grs) throws IOException {
ExiGrammars.Grammars grs) throws IOException, EXIException {

com.siemens.ct.exi._2017.schemaforgrammars.ExiGrammars.Grammars.Grammar g = new com.siemens.ct.exi._2017.schemaforgrammars.ExiGrammars.Grammars.Grammar();
grs.getGrammar().add(g);
Expand Down Expand Up @@ -673,8 +669,46 @@ protected void printGrammar(SchemaInformedGrammar sir,
printGrammarProduction(sir, g.getProduction());
}

/**
* Get a datatype index from a Datatype instance using the listOfSimpleDatatypes,
* or null if this is the default datatype and {@code useNullAsDefaultDatatype} is true.
* @param datatype The datatype to get the index from.
* @param useNullAsDefaultDatatype Tells is it should return null for the built-in default datatype.
* @return The index of the datatype.
* @throws EXIException if the datatype can't be found in listOfSimpleDatatypes.
*/
private Long getDatatypeIndex(Datatype datatype, boolean useNullAsDefaultDatatype) throws EXIException {
if (datatype == BuiltIn.DEFAULT_DATATYPE && useNullAsDefaultDatatype) {
return null;
}
int datatypeIndex = listOfSimpleDatatypes.indexOf(datatype);
if (datatypeIndex < 0){
throw new EXIException("Can't find datatype: " + datatype);
}
return Long.valueOf(datatypeIndex);
}

/**
* Get the datatype instance from an index in the datatypes array,
* or get the built-in default datatype if the {@code index} is null and {@code useNullAsDefaultDatatype} is true.
* @param index The index of the datatype.
* @param datatypes The array of datatypes.
* @param useNullAsDefaultDatatype Tells is it should return null for the built-in default parameter.
* @return The datatype instance.
* @throws EXIException if the index is out of bounds from the datatypes array.
*/
private static Datatype getDatatype(Long index, Datatype[] datatypes, boolean useNullAsDefaultDatatype) throws EXIException {
if (index == null && useNullAsDefaultDatatype) {
return BuiltIn.DEFAULT_DATATYPE;
}
if (index == null || index >= datatypes.length || index < 0) {
throw new EXIException("Can't find datatype of index: " + index);
}
return datatypes[index.intValue()];
}

protected void printGrammarProduction(SchemaInformedGrammar sir, List<com.siemens.ct.exi._2017.schemaforgrammars.Production> productions)
throws IOException {
throws IOException, EXIException {

for (int i = 0; i < sir.getNumberOfEvents(); i++) {
if (STATS_ON) {
Expand Down Expand Up @@ -717,7 +751,8 @@ protected void printGrammarProduction(SchemaInformedGrammar sir, List<com.siemen
QNameContext atqname = at.getQNameContext();

p.setAttribute(of.createProductionAttribute());
p.getAttribute().setAttributeDatatypeID(listOfSimpleDatatypes.indexOf(at.getDatatype()));

p.getAttribute().setAttributeDatatypeID(getDatatypeIndex(at.getDatatype(), true));
p.getAttribute().setAttributeNamespaceID(atqname.getNamespaceUriID());
p.getAttribute().setAttributeLocalNameID(atqname.getLocalNameID());
break;
Expand All @@ -729,7 +764,7 @@ protected void printGrammarProduction(SchemaInformedGrammar sir, List<com.siemen
case CHARACTERS:
Characters ch = (Characters) event;
p.setCharacters(of.createProductionCharacters());
p.getCharacters().setCharactersDatatypeID(listOfSimpleDatatypes.indexOf(ch.getDatatype()));
p.getCharacters().setCharactersDatatypeID(getDatatypeIndex(ch.getDatatype(), false));
break;
case START_ELEMENT_GENERIC:
p.setStartElementGeneric(of.createProductionStartElementGeneric());
Expand Down Expand Up @@ -996,7 +1031,7 @@ public static SchemaInformedGrammars toGrammars(ExiGrammars exiGrammars) throws
com.siemens.ct.exi._2017.schemaforgrammars.Datatype dt = exiGrammars.getSimpleDatatypes().getSimpleDatatype().get(i);

QNameContext qnc = grammarUriContexts[(int)dt.getSchemaTypeNamespaceID()].getQNameContext((int)dt.getSchemaTypeLocalNameID());

if (dt.getList() != null) {
// list MUST be first (there can be a list of enums!)
Datatype listDatatype;
Expand All @@ -1020,7 +1055,7 @@ public static SchemaInformedGrammars toGrammars(ExiGrammars exiGrammars) throws
com.siemens.ct.exi._2017.schemaforgrammars.Datatype dt = exiGrammars.getSimpleDatatypes().getSimpleDatatype().get(i);

if(dt.getBaseDatatypeID() != null) {
datatypes[i].setBaseDatatype(datatypes[dt.getBaseDatatypeID().intValue()]);
datatypes[i].setBaseDatatype(getDatatype(dt.getBaseDatatypeID(), datatypes, false));
}
}

Expand Down Expand Up @@ -1107,15 +1142,15 @@ public static SchemaInformedGrammars toGrammars(ExiGrammars exiGrammars) throws
event = new EndElement();
} else if(prod.getAttribute() != null) {
QNameContext qnc = grammarUriContexts[(int)prod.getAttribute().getAttributeNamespaceID()].getQNameContext((int)prod.getAttribute().getAttributeLocalNameID());
Datatype datatype = datatypes[(int)prod.getAttribute().getAttributeDatatypeID()];
Datatype datatype = getDatatype(prod.getAttribute().getAttributeDatatypeID(), datatypes, true);
event = new Attribute(qnc, datatype);
} else if(prod.getAttributeNS() != null) {
GrammarUriContext guc = grammarUriContexts[prod.getStartElementNS().intValue()];
event = new AttributeNS(guc.getNamespaceUriID(), guc.getNamespaceUri());
} else if(prod.getAttributeGeneric() != null) {
event = new AttributeGeneric();
} else if(prod.getCharacters() != null) {
Datatype datatype = datatypes[(int)prod.getCharacters().getCharactersDatatypeID()];
Datatype datatype = getDatatype(prod.getCharacters().getCharactersDatatypeID(), datatypes, false);
event = new Characters(datatype);
} else if(prod.getCharactersGeneric() != null) {
event = new CharactersGeneric();
Expand Down Expand Up @@ -1150,7 +1185,7 @@ public static SchemaInformedGrammars toGrammars(ExiGrammars exiGrammars) throws
// global attribute
if(qnc.getGlobalAttributeDatatypeID() != null) {
QNameContext qncAT = grammarUriContexts[i].getQNameContext(k);
Datatype dt = datatypes[qnc.getGlobalAttributeDatatypeID().intValue()];
Datatype dt = getDatatype(qnc.getGlobalAttributeDatatypeID(), datatypes, false);
Attribute at = new Attribute(qncAT, dt);
grammarUriContexts[i].getQNameContext(k).setGlobalAttribute(at);
}
Expand All @@ -1163,7 +1198,7 @@ public static SchemaInformedGrammars toGrammars(ExiGrammars exiGrammars) throws
// Note: Simple Type grammars always look the same
// SimpleType0 : CH(schema-types) SimpleType1
// SimpleType1 : EE
Datatype dt = datatypes[qnc.getGlobalSimpleTypeDatatypeID().intValue()];
Datatype dt = getDatatype(qnc.getGlobalSimpleTypeDatatypeID(), datatypes, false);
SchemaInformedFirstStartTagGrammar sistg = new SchemaInformedFirstStartTag();
SchemaInformedElement elementContent = new SchemaInformedElement();
SchemaInformedElement sie = new SchemaInformedElement();
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/SchemaForGrammars.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@
<xs:element name="attributeNamespaceID" type="xs:unsignedInt"/>
<xs:element name="attributeLocalNameID" type="xs:unsignedInt"/>
<!-- attribute datatype -->
<xs:element name="attributeDatatypeID" type="xs:unsignedInt"/>
<xs:element name="attributeDatatypeID" type="xs:unsignedInt"
minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down

0 comments on commit efb1009

Please sign in to comment.