public ServiceDescriptions read(XMLStreamReader reader, ProcessorContext context) throws XMLStreamException,
ContributionReadException {
int event = reader.getEventType();
ServiceDescriptions sds = null;
ServiceDescription sd = null;
String propertyName = null;
String propertyType = "String";
Object propertyValue = null;
String propertyLiteral = null;
boolean xml = false;
boolean multiValued = false;
while (true) {
switch (event) {
case XMLStreamConstants.START_ELEMENT:
QName name = reader.getName();
if (ServiceDescriptions.SERVICE_DESCRIPTIONS_QNAME.equals(name)) {
sds = factory.createServiceDescriptions();
} else if (ServiceDescriptions.SERVICE_DESCRIPTION_QNAME.equals(name)) {
sd = factory.createServiceDescription();
sds.add(sd);
} else if ("property".equals(name.getLocalPart())) {
multiValued = false;
propertyName = reader.getAttributeValue(null, "name");
propertyType = reader.getAttributeValue(null, "value-type");
if (propertyType == null) {
propertyType = "String";
}
propertyLiteral = reader.getAttributeValue(null, "value");
// if (propertyLiteral == null) {
// propertyLiteral = reader.getElementText();
// }
if (propertyLiteral != null) {
propertyLiteral = propertyLiteral.trim();
propertyValue = getPropertyValue(reader, propertyName, propertyLiteral, propertyType);
}
} else if ("list".equals(name.getLocalPart())) {
if (propertyValue != null) {
throw new IllegalArgumentException("@value and <list> are both present");
}
propertyValue = new ArrayList<Object>();
multiValued = true;
} else if ("array".equals(name.getLocalPart())) {
if (propertyValue != null) {
throw new IllegalArgumentException("@value and <array> are both present");
}
propertyValue = new ArrayList<Object>();
multiValued = true;
} else if ("set".equals(name.getLocalPart())) {
if (propertyValue != null) {
throw new IllegalArgumentException("@value and <set> are both present");
}
propertyValue = new HashSet<Object>();
multiValued = true;
} else if ("xml".equals(name.getLocalPart())) {
xml = true;
} else if ("value".equals(name.getLocalPart())) {
propertyLiteral = reader.getElementText();
if (propertyLiteral != null) {
propertyLiteral = propertyLiteral.trim();
Object value = getPropertyValue(reader, propertyName, propertyLiteral, propertyType);
if (multiValued && (propertyValue instanceof Collection)) {
((Collection)propertyValue).add(value);
} else if (propertyValue == null) {
propertyValue = value;
}
}
} else {
// FIXME: [rfeng] The rsa spec says the XML should be saved as String
Object value = processor.read(reader, context);
if (xml) {
if (multiValued && (propertyValue instanceof Collection)) {
((Collection)propertyValue).add(value);
} else if (propertyValue == null) {
propertyValue = value;
}
}
}
break;
case XMLStreamConstants.END_ELEMENT:
name = reader.getName();
if (ServiceDescriptions.SERVICE_DESCRIPTION_QNAME.equals(name)) {
// Reset the sd
sd = null;
} else if (ServiceDescriptions.PROPERTY_QNAME.equals(name)) {
if (sd != null && propertyName != null) {
if (propertyValue == null) {
throw new IllegalArgumentException("No value is defined for " + propertyName);
}
sd.getProperties().put(propertyName, propertyValue);
}
propertyName = null;
propertyType = "String";
propertyValue = null;
multiValued = false;