Skip to content

Commit

Permalink
Allow list attributes to be marshalled as elements.
Browse files Browse the repository at this point in the history
AttributeDefinition.marshallAsElement(...) should defer to the marshaller to determine if the attribute can be marshalled as an element.
Fix return value of DefaultAttributeMarshaller.isMarshallableAsElement().
  • Loading branch information
pferraro committed Mar 13, 2015
1 parent 7a6943c commit 2d5f001
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,11 @@ public void marshallAsElement(final ModelNode resourceModel, final XMLStreamWrit
* if thrown by {@code writer}
*/
public void marshallAsElement(final ModelNode resourceModel, final boolean marshallDefault, final XMLStreamWriter writer) throws XMLStreamException{
throw ControllerLogger.ROOT_LOGGER.couldNotMarshalAttributeAsElement(getName());
if (this.attributeMarshaller.isMarshallableAsElement()) {
this.attributeMarshaller.marshallAsElement(this, resourceModel, marshallDefault, writer);
} else {
throw ControllerLogger.ROOT_LOGGER.couldNotMarshalAttributeAsElement(getName());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package org.jboss.as.controller;

import java.util.Iterator;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

Expand Down Expand Up @@ -82,27 +84,24 @@ public boolean isMarshallableAsElement(){
return false;
}

private static class ListMarshaller extends AttributeMarshaller {
private static class ListMarshaller extends DefaultAttributeMarshaller {
private final char delimiter;

private ListMarshaller(char delimiter) {
ListMarshaller(char delimiter) {
this.delimiter = delimiter;
}

@Override
public void marshallAsAttribute(AttributeDefinition attribute, ModelNode resourceModel, boolean marshallDefault, XMLStreamWriter writer) throws XMLStreamException {
protected String asString(ModelNode value) {
StringBuilder builder = new StringBuilder();
if (resourceModel.hasDefined(attribute.getName())) {
for (ModelNode p : resourceModel.get(attribute.getName()).asList()) {
builder.append(p.asString()).append(delimiter);
Iterator<ModelNode> values = value.asList().iterator();
while (values.hasNext()) {
builder.append(values.next().asString());
if (values.hasNext()) {
builder.append(this.delimiter);
}
}
if (builder.length() > 2) {
builder.setLength(builder.length() - 1);
}
if (builder.length() > 0) {
writer.writeAttribute(attribute.getXmlName(), builder.toString());
}
return builder.toString();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@
*/
public class DefaultAttributeMarshaller extends AttributeMarshaller {


@Override
public void marshallAsAttribute(final AttributeDefinition attribute, final ModelNode resourceModel, final boolean marshallDefault, final XMLStreamWriter writer) throws XMLStreamException {
if (isMarshallable(attribute, resourceModel, marshallDefault)) {
writer.writeAttribute(attribute.getXmlName(), resourceModel.get(attribute.getName()).asString());
writer.writeAttribute(attribute.getXmlName(), this.asString(resourceModel.get(attribute.getName())));
}
}

/**
* Use {@link #marshallAsElement(AttributeDefinition, ModelNode, boolean, XMLStreamWriter)} instead.
*/
@Deprecated
public void marshallAsElement(AttributeDefinition attribute, ModelNode resourceModel, XMLStreamWriter writer) throws XMLStreamException {
marshallAsElement(attribute, resourceModel, true, writer);
}
Expand All @@ -47,7 +51,7 @@ public void marshallAsElement(AttributeDefinition attribute, ModelNode resourceM
public void marshallAsElement(final AttributeDefinition attribute, final ModelNode resourceModel, final boolean marshallDefault, final XMLStreamWriter writer) throws XMLStreamException {
if (isMarshallable(attribute, resourceModel, marshallDefault)) {
writer.writeStartElement(attribute.getXmlName());
String content = resourceModel.get(attribute.getName()).asString();
String content = this.asString(resourceModel.get(attribute.getName()));
if (content.indexOf('\n') > -1) {
// Multiline content. Use the overloaded variant that staxmapper will format
writer.writeCharacters(content);
Expand All @@ -59,4 +63,13 @@ public void marshallAsElement(final AttributeDefinition attribute, final ModelNo
writer.writeEndElement();
}
}

@Override
public boolean isMarshallableAsElement() {
return true;
}

protected String asString(ModelNode value) {
return value.asString();
}
}

0 comments on commit 2d5f001

Please sign in to comment.