builder.startDocument();
builder.startElement(RESOURCE_FUNCTIONS, null);
for(final RestXqService service : services) {
final ResourceFunction resourceFn = service.getResourceFunction();
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute(null, XQUERY_URI, "", "string", resourceFn.getXQueryLocation().toString());
builder.startElement(RESOURCE_FUNCTION, attrs);
//identity
attrs = new AttributesImpl();
attrs.addAttribute(null, NAMESPACE, "", "string", resourceFn.getFunctionSignature().getName().getNamespaceURI());
attrs.addAttribute(null, LOCAL_NAME, "", "string", resourceFn.getFunctionSignature().getName().getLocalPart());
attrs.addAttribute(null, ARITY, "", "int", Integer.toString(resourceFn.getFunctionSignature().getArgumentCount()));
builder.startElement(RESOURCE_FUNCTION_IDENTITY, attrs);
builder.endElement();
//annotations
builder.startElement(ANNOTATIONS, null);
final List<Annotation> annotations = new ArrayList<Annotation>();
annotations.addAll(resourceFn.getHttpMethodAnnotations());
annotations.addAll(resourceFn.getConsumesAnnotations());
annotations.addAll(resourceFn.getProducesAnnotations());
for(final Annotation annotation : annotations) {
builder.startElement(QName.fromJavaQName(annotation.getName()), null);
final Literal literals[] = annotation.getLiterals();
if(literals != null) {
for(final Literal literal : literals) {
if(annotation instanceof ConsumesAnnotation || annotation instanceof ProducesAnnotation) {
builder.startElement(INTERNET_MEDIA_TYPE, null);
builder.characters(literal.getValue());
builder.endElement();
}
}
}
builder.endElement();
}
if(resourceFn.getPathAnnotation() != null) {
final PathAnnotation pathAnnotation = resourceFn.getPathAnnotation();
attrs = new AttributesImpl();
attrs.addAttribute(null, SPECIFICITY_METRIC, "", "string", Long.toString(pathAnnotation.getPathSpecificityMetric()));
builder.startElement(QName.fromJavaQName(pathAnnotation.getName()), attrs);
final String[] segments = pathAnnotation.getLiterals()[0].getValue().split("/");
for(final String segment : segments) {
if(!segment.isEmpty()) {
builder.startElement(SEGMENT, null);
builder.characters(segment);
builder.endElement();
}
}
builder.endElement();
}
for(final ParameterAnnotation parameterAnnotation : resourceFn.getParameterAnnotations()) {
final Literal[] literals = parameterAnnotation.getLiterals();
attrs = new AttributesImpl();
attrs.addAttribute(null, NAME, "", "string", literals[0].getValue());
attrs.addAttribute(null, ARGUMENT, "", "string", literals[1].getValue());
if(literals.length == 3) {
attrs.addAttribute(null, DEFAULT_VALUE, "", "string", literals[2].getValue());
}
builder.startElement(QName.fromJavaQName(parameterAnnotation.getName()), attrs);
builder.endElement();
}
for(final SerializationAnnotation serializationAnnotation : resourceFn.getSerializationAnnotations()) {
builder.startElement(QName.fromJavaQName(serializationAnnotation.getName()), attrs);
//TODO add parameters for Serialization Annotations
builder.endElement();