}
for(int i = 0; i < attributeUses.getLength(); ++i)
{
currentAttribute = (XSAttributeUse)attributeUses.item(i);
XSAttributeDeclaration attrDec = currentAttribute.getAttrDeclaration();
String attrNs = attrDec.getNamespace();
String attrLocal = attrDec.getName();
Object attrValue = provider.getAttributeValue(o, ctx, attrNs, attrLocal);
if(attrValue != null)
{
if(attrs == null)
{
attrs = new AttributesImpl(5);
}
String attrPrefix = null;
if(attrNs != null)
{
attrPrefix = getPrefix(attrNs);
if(attrPrefix == null && attrNs != null && attrNs.length() > 0)
{
attrPrefix = "ns_" + attrLocal;
attrs.add(null, attrPrefix, "xmlns:" + attrPrefix, null, attrNs);
}
}
String qName = attrPrefix == null || attrPrefix.length() == 0 ? attrLocal : attrPrefix + ":" + attrLocal;
// todo: this is a quick fix for boolean pattern (0|1 or true|false) should be refactored
XSSimpleTypeDefinition attrType = attrDec.getTypeDefinition();
if(attrType.getItemType() != null)
{
XSSimpleTypeDefinition itemType = attrType.getItemType();
if(Constants.NS_XML_SCHEMA.equals(itemType.getNamespace()))
{
List list;
if(attrValue instanceof List)
{
list = (List)attrValue;
}
else if(attrValue.getClass().isArray())
{
list = Arrays.asList((Object[])attrValue);
}
else
{
throw new JBossXBRuntimeException("Expected value for list type is an array or " +
List.class.getName() +
" but got: " +
attrValue
);
}
if(Constants.QNAME_QNAME.getLocalPart().equals(itemType.getName()))
{
for(int listInd = 0; listInd < list.size(); ++listInd)
{
QName item = (QName)list.get(listInd);
String itemNs = item.getNamespaceURI();
if(itemNs != null && itemNs.length() > 0)
{
String itemPrefix;
if(itemNs.equals(elementNsUri))
{
itemPrefix = prefix;
}
else
{
itemPrefix = getPrefix(itemNs);
if(itemPrefix == null)
{
itemPrefix = attrLocal + listInd;
declareNs(attrs, itemPrefix, itemNs);
}
}
item = new QName(item.getNamespaceURI(), item.getLocalPart(), itemPrefix);
list.set(listInd, item);
}
}
}
attrValue = SimpleTypeBindings.marshalList(itemType.getName(), list, null);
}
else
{
throw new JBossXBRuntimeException("Marshalling of list types with item types not from " +
Constants.NS_XML_SCHEMA + " is not supported."
);
}
}
else if(attrType.getLexicalPattern().item(0) != null
&&
attrType.derivedFrom(Constants.NS_XML_SCHEMA,
Constants.QNAME_BOOLEAN.getLocalPart(),
XSConstants.DERIVATION_RESTRICTION
))
{
String item = attrType.getLexicalPattern().item(0);
if(item.indexOf('0') != -1 && item.indexOf('1') != -1)
{
attrValue = ((Boolean)attrValue).booleanValue() ? "1" : "0";
}
else
{
attrValue = ((Boolean)attrValue).booleanValue() ? "true" : "false";
}
}
else if(Constants.QNAME_QNAME.getNamespaceURI().equals(attrType.getNamespace()) &&
Constants.QNAME_QNAME.getLocalPart().equals(attrType.getName()))
{
QName qNameValue = (QName)attrValue;
String qNamePrefix = null;
boolean declarePrefix = false;
String ns = qNameValue.getNamespaceURI();
if(ns != null && ns.length() > 0)
{
qNamePrefix = getPrefix(ns);
if(qNamePrefix == null)
{
qNamePrefix = qNameValue.getPrefix();
if(qNamePrefix == null || qNamePrefix.length() == 0)
{
qNamePrefix = "ns_" + qNameValue.getLocalPart();
}
declareNs(attrs, qNamePrefix, ns);
nsRegistry.addPrefixMapping(qNamePrefix, ns);
declarePrefix = true;
}
}
attrValue = SimpleTypeBindings.marshalQName(qNameValue, nsRegistry);
if(declarePrefix)
{
nsRegistry.removePrefixMapping(qNamePrefix);
}
}
else
{
attrValue = attrValue.toString();
}
attrs.add(attrNs,
attrLocal,
qName,
attrDec.getTypeDefinition().getName(),
attrValue.toString()
);
}
}
currentAttribute = null;