boolean declareNs,
boolean declareXsiType)
{
Collection<AttributeBinding> attrBindings = type.getAttributes();
int attrsTotal = declareNs || declareXsiType ? nsRegistry.size() + attrBindings.size() + 1: attrBindings.size();
ctx.attrs = attrsTotal > 0 ? new AttributesImpl(attrsTotal) : null;
if(declareNs && nsRegistry.size() > 0)
{
declareNs(ctx.attrs);
}
String generatedPrefix = null;
if(declareXsiType)
{
generatedPrefix = declareXsiType(type.getQName(), ctx.attrs);
if(generatedPrefix != null)
{
String typeNsWithGeneratedPrefix = type.getQName().getNamespaceURI();
declareNs(ctx.attrs, generatedPrefix, typeNsWithGeneratedPrefix);
declareNamespace(generatedPrefix, typeNsWithGeneratedPrefix);
}
}
String elementNs = elementQName.getNamespaceURI();
String elementLocal = elementQName.getLocalPart();
String prefix = getPrefix(elementNs);
boolean genPrefix = prefix == null && elementNs != null && elementNs.length() > 0;
if(genPrefix)
{
// todo: it's possible that the generated prefix already mapped. this should be fixed
prefix = "ns_" + elementLocal;
declareNamespace(prefix, elementNs);
if(ctx.attrs == null)
{
ctx.attrs = new AttributesImpl(1);
}
declareNs(ctx.attrs, prefix, elementNs);
}
if(!attrBindings.isEmpty())
{
for(Iterator<AttributeBinding> i = attrBindings.iterator(); i.hasNext();)
{
AttributeBinding attrBinding = i.next();
QName attrQName = attrBinding.getQName();
if(Constants.QNAME_XMIME_CONTENTTYPE.equals(attrQName))
{
continue;
}
ctx.attr = attrBinding;
AttributeMarshaller marshaller = attrBinding.getMarshaller();
String marshalledAttr = marshaller.marshal(ctx);
if(marshalledAttr != null)
{
if(ctx.attrs == null)
{
ctx.attrs = new AttributesImpl(5);
}
String attrNs = attrQName.getNamespaceURI();
String attrLocal = attrQName.getLocalPart();
String attrPrefix = null;
if(attrNs != null)
{
attrPrefix = getPrefix(attrNs);
if(attrPrefix == null && attrNs != null && attrNs.length() > 0)
{
attrPrefix = "ns_" + attrLocal;
declareNs(ctx.attrs, attrPrefix, attrNs);
}
}
String prefixedName = prefixLocalName(attrPrefix, attrLocal);
ctx.attrs.add(attrNs, attrLocal, prefixedName, "CDATA", marshalledAttr);
}
}
ctx.attr = null;
}
String characters = null;
TypeBinding simpleType = type.getSimpleType();
if(simpleType != null && !Constants.QNAME_ANYTYPE.equals(type.getQName()))
{
String fieldName = ctx.getSimpleContentProperty();
CharactersMetaData charactersMetaData = type.getCharactersMetaData();
PropertyMetaData propertyMetaData = charactersMetaData == null ? null : charactersMetaData.getProperty();
if(propertyMetaData != null)
{
fieldName = propertyMetaData.getName();
}
if(fieldName != null)
{
boolean ignoreUnresolvedFieldOrClass = type.getSchemaBinding().isIgnoreUnresolvedFieldOrClass();
Object o = stack.peek();
Object value = getElementValue(o, fieldName, ignoreUnresolvedFieldOrClass);
if(value != null)
{
String typeName = simpleType.getQName().getLocalPart();
if(ctx.attrs == null && (SimpleTypeBindings.XS_QNAME_NAME.equals(typeName) ||
SimpleTypeBindings.XS_NOTATION_NAME.equals(typeName) ||
simpleType.getItemType() != null &&
(SimpleTypeBindings.XS_QNAME_NAME.equals(simpleType.getItemType().getQName().getLocalPart()) ||
SimpleTypeBindings.XS_NOTATION_NAME.equals(simpleType.getItemType().getQName().getLocalPart())
)
)
)
{
ctx.attrs = new AttributesImpl(5);
}
characters = marshalCharacters(elementNs, prefix, simpleType, value);
}
}