*/
public boolean toRDF(Resource subject, Element elem, Seq seq, Context ctx)
throws Exception {
populate();
Model m = ctx.getModel();
String value = getValue(elem);
if (value==null && _default != null) value = _default;
String uri = createURI(m,ctx);
Property prop = null;
if (uri != null) prop = m.createProperty(uri);
Statement stmt=null;
// the element may be defined locally or globally (follow ref)
element def = getDefinition(ctx.getModel(),ctx);
// explicit xsi:type
if (elem.hasAttributeNS(schema.XSI,"type")) {
String t = elem.getAttributeNS(schema.XSI,"type");
String fullname = expandQName(ctx.getDefaultNS(),t,elem,ctx.getModel());
complexType c = ctx.getComplexType(fullname);
if (c!=null) {
String id = c.getID(elem,ctx);
Resource o = id==null?m.createResource():m.createResource(addFragment(ctx.getBaseMap(), id).toString());
stmt = m.createStatement(subject,prop,o);
// the new resource, o, becomes the subject
if (is_nillable()
&& elem.hasAttributeNS(schema.XSI,"nil")
&& elem.getAttributeNS(schema.XSI,"nil").equals("true"))
o.addProperty(RDF.value,RDF.nil);
Seq subSeq = null;
if (ctx.isSequenced() && elem.hasChildNodes() && c.needSeq(new HashSet<String>(), ctx))
subSeq = m.getSeq(o.addProperty(RDF.type, RDF.Seq));
int index = c.toRDF(o, elem, 0, subSeq,null,true,ctx);
// mop up remaining values in sequence
produceMixed(subSeq, index, elem);
m.add(stmt);
if (seq != null) seq.add(stmt.createReifiedStatement());
return true;
}
else Gloze.logger.warn("undefined type: "+fullname);
}
// predefined schema datatype
String type = expandQName(ctx.getDefaultNS(),def.getType(),m);
if (type!=null && type.startsWith(schema.XSD_URI)) {
if (is_nillable() && elem.hasAttributeNS(schema.XSI,"nil") && elem.getAttributeNS(schema.XSI,"nil").equals("true"))
stmt = m.createStatement(subject,prop,RDF.nil);
else {
if (type.equals(schema.XSD_URI+"#ID") && value!=null && !value.trim().equals("")) {
Resource o = m.createResource(addFragment(ctx.getBaseMap(), value.trim()).toString());
stmt = m.createStatement(subject,prop,o);
}
else stmt = schema.toRDFStatement(subject, elem, prop, value, type, ctx);
}
}
// user defined simple type
XMLBean t = def.get_type(ctx);
if (t instanceof simpleType) {
// an empty, as opposed to nil, element is null
if (value==null) value = "";
if (is_nillable() && elem.hasAttributeNS(schema.XSI,"nil") && elem.getAttributeNS(schema.XSI,"nil").equals("true"))
stmt = m.createStatement(subject,prop,RDF.nil);
// if a simple type is nil there is no content or attributes
// initialise restriction set according to level of restriction checking required
else {
Boolean b = ((simpleType) t).toRDF(subject, prop, elem, value, seq, null, ctx);
if (!b) Gloze.logger.warn("cannot map value "+value+" into simpleType "+type);
}
}
else if (t instanceof complexType) {
// look for a global ID
String id = ((complexType) t).getID(elem,ctx);
Resource o = id==null?m.createResource():m.createResource(addFragment(ctx.getBaseMap(), id).toString());
stmt = m.createStatement(subject,prop,o);
// the new resource, o, becomes the subject
if (is_nillable() && elem.hasAttributeNS(schema.XSI,"nil") && elem.getAttributeNS(schema.XSI,"nil").equals("true"))
o.addProperty(RDF.value,RDF.nil);
Seq subSeq = null;
if (ctx.isSequenced() && elem.hasChildNodes() && ((complexType) t).needSeq(new HashSet<String>(),ctx))
subSeq = m.getSeq(o.addProperty(RDF.type, RDF.Seq));
int index = ((complexType) t).toRDF(o, elem, 0, subSeq, null,true,ctx);
// mop up remaining values in sequence
produceMixed(subSeq, index, elem);
}
else if (value!=null && stmt==null){ // untyped!
Literal l = m.createLiteral(schema.processWhitespace(elem,value,null,ctx));
stmt = m.createStatement(subject,prop,l);
}
else if (type==null){ // empty
if (is_nillable() && elem.hasAttributeNS(schema.XSI,"nil") && elem.getAttributeNS(schema.XSI,"nil").equals("true"))
stmt = m.createStatement(subject,prop,RDF.nil);
// link to anonymous resource
else stmt = m.createStatement(subject,prop,m.createResource());
}
// if the statement was boxed, only add the link to the box
if (stmt !=null) {
m.add(stmt);
if (seq != null) seq.add(stmt.createReifiedStatement());
}
return true;
}