Skip to content

Commit

Permalink
PersistentResourceXMLDescription: properties parsed separately
Browse files Browse the repository at this point in the history
  • Loading branch information
tadamski authored and kabir committed Apr 27, 2015
1 parent af8f100 commit 912b0d3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class PersistentResourceXMLDescription {
@Deprecated
protected final LinkedHashMap<String, AttributeDefinition> attributes; //we dont use it anymore, it is here just for backward compatibilty and make sure PermissionResourceDefinition works
protected final LinkedHashMap<String, LinkedHashMap<String, AttributeDefinition>> attributesByGroup;
protected final LinkedHashMap<String, PropertiesAttributeDefinition> propertyAttributes;
protected final List<PersistentResourceXMLDescription> children;
protected final boolean useValueAsElementName;
protected final boolean noAddOperation;
Expand All @@ -64,6 +65,7 @@ protected PersistentResourceXMLDescription(final PersistentResourceDefinition re
this.attributes = attributes;
this.attributesByGroup = new LinkedHashMap<>();
this.attributesByGroup.put(null, attributes);
this.propertyAttributes = new LinkedHashMap<>();
this.children = children;
this.useValueAsElementName = useValueAsElementName;
this.noAddOperation = noAddOperation;
Expand All @@ -81,25 +83,34 @@ private PersistentResourceXMLDescription(PersistentResourceXMLBuilder builder) {
this.attributes = new LinkedHashMap<>();
this.useElementsForGroups = builder.useElementsForGroups;
this.attributesByGroup = new LinkedHashMap<>();
this.propertyAttributes = new LinkedHashMap<>();
this.namespaceURI = builder.namespaceURI;
if (useElementsForGroups) {
// Ensure we have a map for the default group even if there are no attributes so we don't NPE later
this.attributesByGroup.put(null, new LinkedHashMap<String, AttributeDefinition>());
this.attributeGroups = new HashSet<>();
// Segregate attributes by group
for (AttributeDefinition ad : builder.attributeList) {
LinkedHashMap<String, AttributeDefinition> forGroup = this.attributesByGroup.get(ad.getAttributeGroup());
if (forGroup == null) {
forGroup = new LinkedHashMap<>();
this.attributesByGroup.put(ad.getAttributeGroup(), forGroup);
this.attributeGroups.add(ad.getAttributeGroup());
if (ad instanceof PropertiesAttributeDefinition) {
addProperty((PropertiesAttributeDefinition) ad);
} else {
LinkedHashMap<String, AttributeDefinition> forGroup = this.attributesByGroup.get(ad.getAttributeGroup());
if (forGroup == null) {
forGroup = new LinkedHashMap<>();
this.attributesByGroup.put(ad.getAttributeGroup(), forGroup);
this.attributeGroups.add(ad.getAttributeGroup());
}
forGroup.put(ad.getXmlName(), ad);
}
forGroup.put(ad.getXmlName(), ad);
}
} else {
LinkedHashMap<String,AttributeDefinition> attrs = new LinkedHashMap<>();
for (AttributeDefinition ad : builder.attributeList) {
attrs.put(ad.getXmlName(), ad);
if (ad instanceof PropertiesAttributeDefinition) {
addProperty((PropertiesAttributeDefinition) ad);
} else {
attrs.put(ad.getXmlName(), ad);
}
}
// Ignore attribute-group, treat all as if they are in the default group
this.attributesByGroup.put(null, attrs);
Expand All @@ -116,6 +127,14 @@ private PersistentResourceXMLDescription(PersistentResourceXMLBuilder builder) {
this.attributeMarshallers = builder.attributeMarshallers;
}

private void addProperty(final PropertiesAttributeDefinition ad) {
if (ad.isWrapped()) {
propertyAttributes.put(ad.getWrapperElement(), ad);
} else {
propertyAttributes.put(ad.getXmlName(), ad);
}
}

public PathElement getPathElement() {
return resourceDefinition.getPathElement();
}
Expand All @@ -142,13 +161,14 @@ public void parse(final XMLExtendedStreamReader reader, PathAddress parentAddres
}

private String parseAttributeGroups(final XMLExtendedStreamReader reader, ModelNode op, boolean wildcard) throws XMLStreamException {
String name;
if (useElementsForGroups && !attributeGroups.isEmpty()) {
name = parseAttributes(reader, op, attributesByGroup.get(null), wildcard); //parse attributes not belonging to a group
String name = parseAttributes(reader, op, attributesByGroup.get(null), wildcard); //parse attributes not belonging to a group
if (!attributeGroups.isEmpty() || !propertyAttributes.isEmpty()) {
while (reader.hasNext() && reader.nextTag() != XMLStreamConstants.END_ELEMENT) {
if (attributeGroups.contains(reader.getLocalName())) {
parseAttributes(reader, op, attributesByGroup.get(reader.getLocalName()), wildcard);
ParseUtils.requireNoContent(reader);
} else if(propertyAttributes.containsKey(reader.getLocalName())) {
parseProperty(reader, op);
} else {
//don't break, as we read all attributes, we set that child was already read so readChildren wont do .nextTag()
childAlreadyRead = true;
Expand All @@ -157,11 +177,29 @@ private String parseAttributeGroups(final XMLExtendedStreamReader reader, ModelN
}
flushRequired = false;
} else {
name = parseAttributes(reader, op, attributesByGroup.get(null), wildcard);
}
return name;
}

private void parseProperty(final XMLExtendedStreamReader reader, final ModelNode op)
throws XMLStreamException {
final PropertiesAttributeDefinition property = propertyAttributes.get(reader.getLocalName());
if (!property.isWrapped()) {
property.parse(reader, op);
} else {
while (reader.hasNext()) {
if (reader.nextTag() == XMLStreamConstants.END_ELEMENT) {
if (property.getWrapperElement().equals(reader.getLocalName())) {
break;
}
// else continue to the next children
continue;
}
property.parse(reader, op);
}
}
}

private String parseAttributes(final XMLExtendedStreamReader reader, ModelNode op, Map<String, AttributeDefinition> attributes, boolean wildcard) throws XMLStreamException {
String name = null;
for (int i = 0; i < reader.getAttributeCount(); i++) {
Expand All @@ -178,14 +216,6 @@ private String parseAttributes(final XMLExtendedStreamReader reader, ModelNode o
throw ParseUtils.unexpectedAttribute(reader, i, attributes.keySet());
}
}
for (AttributeDefinition attributeDefinition : attributes.values()) {
if (attributeDefinition instanceof PropertiesAttributeDefinition) {
PropertiesAttributeDefinition attribute = (PropertiesAttributeDefinition) attributeDefinition;
// TODO what if this attribute isn't required and isn't in the xml?
attribute.parse(reader, op);
flushRequired = false;
}
}
return name;
}

Expand Down Expand Up @@ -344,6 +374,10 @@ private void persistAttributes(XMLExtendedStreamWriter writer, ModelNode model,
}
}
} // else we will only have attributes under the 'null' group
for (AttributeDefinition def : propertyAttributes.values()) {
AttributeMarshaller marshaller = attributeMarshallers.containsKey(def.getName()) ? attributeMarshallers.get(def.getName()) : def.getAttributeMarshaller();
marshaller.marshallAsAttribute(def, model, marshalDefault, writer);
}
}

public void persistChildren(XMLExtendedStreamWriter writer, ModelNode model) throws XMLStreamException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import javax.xml.stream.XMLStreamConstants;

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

Expand All @@ -47,12 +47,18 @@
*
* @author Jason T. Greene
* @author Tomaz Cerar<
* @author <a href=mailto:tadamski@redhat.com>Tomasz Adamski</a>
*/
//todo maybe replace with SimpleMapAttributeDefinition?
public final class PropertiesAttributeDefinition extends MapAttributeDefinition {

final boolean wrapXmlElement;
final String wrapperElement;

private PropertiesAttributeDefinition(final Builder builder) {
super(builder);
this.wrapXmlElement = builder.wrapXmlElement;
this.wrapperElement = builder.wrapperElement;
}

@Override
Expand Down Expand Up @@ -95,15 +101,17 @@ public static Map<String, String> unwrapModel(final ExpressionResolver context,
}

public void parse(final XMLExtendedStreamReader reader, final ModelNode operation) throws XMLStreamException {
while (reader.hasNext() && reader.nextTag() != XMLStreamConstants.END_ELEMENT) {
if (reader.getLocalName().equals(getXmlName())) {
final String[] array = requireAttributes(reader, org.jboss.as.controller.parsing.Attribute.NAME.getLocalName(), org.jboss.as.controller.parsing.Attribute.VALUE.getLocalName());
parseAndAddParameterElement(array[0], array[1], operation, reader);
ParseUtils.requireNoContent(reader);
} else {
throw ParseUtils.unexpectedElement(reader);
}
}
final String[] array = requireAttributes(reader, org.jboss.as.controller.parsing.Attribute.NAME.getLocalName(), org.jboss.as.controller.parsing.Attribute.VALUE.getLocalName());
parseAndAddParameterElement(array[0], array[1], operation, reader);
ParseUtils.requireNoContent(reader);
}

public boolean isWrapped() {
return wrapXmlElement;
}

public String getWrapperElement() {
return wrapperElement;
}

private static class PropertiesAttributeMarshaller extends AttributeMarshaller {
Expand Down

0 comments on commit 912b0d3

Please sign in to comment.