Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WFCORE-6779 Replace PersistentResourceXMLDescription with more robust API #6261

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@


import java.util.Collections;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.jboss.as.controller.operations.validation.ParameterValidator;
import org.jboss.as.controller.parsing.ParseUtils;
import org.jboss.as.controller.xml.XMLCardinality;
import org.jboss.dmr.ModelNode;
import org.jboss.staxmapper.XMLExtendedStreamReader;

Expand Down Expand Up @@ -96,6 +98,15 @@ public String getXmlName(final AttributeDefinition attribute){
return attribute.getXmlName();
}

/**
* Returns the cardinality of the XML particle for the specified attribute.
* @param attribute an attribute definition
* @return the cardinality of the XML particle for the specified attribute.
*/
public XMLCardinality getCardinality(AttributeDefinition attribute) {
return this.isParseAsElement() ? (attribute.isNillable() ? XMLCardinality.Single.OPTIONAL : XMLCardinality.Single.REQUIRED) : XMLCardinality.DISABLED;
}

public static final AttributeParser SIMPLE = new AttributeParser() {
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.xml.stream.XMLStreamException;

import org.jboss.as.controller.parsing.ParseUtils;
import org.jboss.as.controller.xml.XMLCardinality;
import org.jboss.dmr.ModelNode;
import org.jboss.staxmapper.XMLExtendedStreamReader;

Expand Down Expand Up @@ -95,39 +96,35 @@ public void parseElement(AttributeDefinition attribute, XMLExtendedStreamReader
assert attribute instanceof MapAttributeDefinition;
MapAttributeDefinition mapAttribute = (MapAttributeDefinition) attribute;

operation.get(attribute.getName()).setEmptyObject();//create empty attribute to address WFCORE-1448
if (!operation.hasDefined(attribute.getName())) {
// Create empty attribute to address WFCORE-1448
operation.get(attribute.getName()).setEmptyObject();
}
if (wrapElement) {
if (!reader.getLocalName().equals(wrapper)) {
throw ParseUtils.unexpectedElement(reader, Collections.singleton(wrapper));
} else {
// allow empty properties list
if (reader.nextTag() == END_ELEMENT) {
return;
}
}
}

do {
if (elementName.equals(reader.getLocalName())) {
//real parsing happens
parseSingleElement(mapAttribute, reader, operation);
} else {
throw ParseUtils.unexpectedElement(reader, Collections.singleton(elementName));
while (reader.hasNext() && (reader.nextTag() != END_ELEMENT)) {
this.readElement(mapAttribute, reader, operation);
}
} else {
this.readElement(mapAttribute, reader, operation);
}
}

} while (reader.hasNext() && reader.nextTag() != END_ELEMENT && reader.getLocalName().equals(elementName));

if (wrapElement) {
// To exit the do loop either we hit an END_ELEMENT or a START_ELEMENT not for 'elementName'
// The latter means a bad document
if (reader.getEventType() != END_ELEMENT) {
throw ParseUtils.unexpectedElement(reader, Collections.singleton(elementName));
}
private void readElement(MapAttributeDefinition attribute, XMLExtendedStreamReader reader, ModelNode operation) throws XMLStreamException {
if (!reader.getLocalName().equals(this.elementName)) {
throw ParseUtils.unexpectedElement(reader, Collections.singleton(this.elementName));
}
this.parseSingleElement(attribute, reader, operation);
}

public abstract void parseSingleElement(MapAttributeDefinition attribute, XMLExtendedStreamReader reader, ModelNode operation) throws XMLStreamException;

@Override
public XMLCardinality getCardinality(AttributeDefinition attribute) {
return this.wrapElement ? (attribute.isNillable() ? XMLCardinality.Single.OPTIONAL : XMLCardinality.Single.REQUIRED) : (attribute.isNillable() ? XMLCardinality.Unbounded.OPTIONAL : XMLCardinality.Unbounded.REQUIRED);
}
}


Expand Down Expand Up @@ -192,7 +189,6 @@ public void parseSingleElement(MapAttributeDefinition attribute, XMLExtendedStre
String key = reader.getAttributeValue(null, keyAttributeName);
ModelNode op = operation.get(attribute.getName(), key);
parseEmbeddedElement(objectType, reader, op, keyAttributeName);
ParseUtils.requireNoContent(reader);
}
}

Expand All @@ -218,9 +214,6 @@ public void parseElement(AttributeDefinition attribute, XMLExtendedStreamReader
} else {
throw ParseUtils.unexpectedElement(reader, Collections.singleton(attribute.getXmlName()));
}
if (!reader.isEndElement()) {
ParseUtils.requireNoContent(reader);
}
}

static void parseEmbeddedElement(ObjectTypeAttributeDefinition attribute, XMLExtendedStreamReader reader, ModelNode op, String... additionalExpectedAttributes) throws XMLStreamException {
Expand Down Expand Up @@ -261,8 +254,9 @@ static void parseEmbeddedElement(ObjectTypeAttributeDefinition attribute, XMLExt
throw ParseUtils.unexpectedElement(reader, attributeElements.keySet());
}
}
} else {
ParseUtils.requireNoContent(reader);
}

}

}
Expand All @@ -273,7 +267,6 @@ public boolean isParseAsElement() {
return true;
}


ObjectTypeAttributeDefinition getObjectType(AttributeDefinition attribute) {
assert attribute instanceof ObjectListAttributeDefinition;
ObjectListAttributeDefinition list = ((ObjectListAttributeDefinition) attribute);
Expand All @@ -287,7 +280,6 @@ public void parseElement(AttributeDefinition attribute, XMLExtendedStreamReader
ObjectListAttributeDefinition list = ((ObjectListAttributeDefinition) attribute);
ObjectTypeAttributeDefinition objectType = list.getValueType();


ModelNode listValue = new ModelNode();
listValue.setEmptyList();
while (reader.hasNext() && reader.nextTag() != END_ELEMENT) {
Expand All @@ -297,9 +289,6 @@ public void parseElement(AttributeDefinition attribute, XMLExtendedStreamReader
} else {
throw ParseUtils.unexpectedElement(reader, Collections.singleton(objectType.getXmlName()));
}
if (!reader.isEndElement()) {
ParseUtils.requireNoContent(reader);
}
}
operation.get(attribute.getName()).set(listValue);
}
Expand Down Expand Up @@ -327,9 +316,11 @@ public void parseElement(AttributeDefinition attribute, XMLExtendedStreamReader
} else {
throw ParseUtils.unexpectedElement(reader, Collections.singleton(xmlName));
}
if (!reader.isEndElement()) {
ParseUtils.requireNoContent(reader);
}
}

@Override
public XMLCardinality getCardinality(AttributeDefinition attribute) {
return attribute.isNillable() ? XMLCardinality.Unbounded.OPTIONAL : XMLCardinality.Unbounded.REQUIRED;
}
}

Expand All @@ -348,6 +339,11 @@ public void parseElement(AttributeDefinition ad, XMLExtendedStreamReader reader,
addPermissionMapper.get(ad.getName()).add(name);
ParseUtils.requireNoContent(reader);
}

@Override
public XMLCardinality getCardinality(AttributeDefinition attribute) {
return attribute.isNillable() ? XMLCardinality.Unbounded.OPTIONAL : XMLCardinality.Unbounded.REQUIRED;
}
}

static AttributeParser getObjectMapAttributeParser(String keyElementName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
* simplify the process of creating parsers and persisters.
*
* @author <a href="mailto:[email protected]">Tomaz Cerar</a> (c) 2013 Red Hat Inc.
* @deprecated Build {@link ResourceDefinition} via {@link ResourceDefinition#builder(ResourceRegistration, ResourceDescriptionResolver)} or extend {@link SimpleResourceDefinition} directly.
*/
@Deprecated(forRemoval = true, since = "28.0")
public abstract class PersistentResourceDefinition extends SimpleResourceDefinition {

protected PersistentResourceDefinition(PathElement pathElement, ResourceDescriptionResolver descriptionResolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
*
* @author Tomaz Cerar
* @author Stuart Douglas
* @deprecated Superseded by {@link org.jboss.as.controller.persistence.xml.ResourceXMLParticleFactory}
*/
@Deprecated(forRemoval = true, since = "28.0")
public final class PersistentResourceXMLDescription implements ResourceParser, ResourceMarshaller {

private final PathElement pathElement;
Expand Down Expand Up @@ -133,13 +135,18 @@ public PathElement getPathElement() {
return this.pathElement;
}

String getNamespaceURI() {
return this.namespaceURI;
}

/**
* Parse xml from provided <code>reader</code> and add resulting operations to passed list
* @param reader xml reader to parse from
* @param parentAddress address of the parent, used as base for all child elements
* @param list list of operations where result will be put to.
* @throws XMLStreamException if any error occurs while parsing
*/
@Override
public void parse(final XMLExtendedStreamReader reader, PathAddress parentAddress, List<ModelNode> list) throws XMLStreamException {
if (decoratorElement != null) {
parseDecorator(reader, parentAddress, list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
/**
* An {@link XMLElementReader} based on a {@link PersistentResourceXMLDescription}.
* @author Paul Ferraro
* @deprecated Superseded by {@link org.jboss.as.controller.persistence.xml.SubsystemResourceXMLElementReader}.
*/
@Deprecated(forRemoval = true, since = "28.0")
public class PersistentResourceXMLDescriptionReader implements XMLElementReader<List<ModelNode>> {
private final Supplier<PersistentResourceXMLDescription> description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
/**
* An {@link XMLElementWriter} based on a {@link PersistentResourceXMLDescription}.
* @author Paul Ferraro
* @deprecated Superseded by {@link org.jboss.as.controller.persistence.xml.SubsystemResourceXMLElementWriter}.
*/
@Deprecated(forRemoval = true, since = "28.0")
public class PersistentResourceXMLDescriptionWriter implements XMLElementWriter<SubsystemMarshallingContext> {
private final Supplier<PersistentResourceXMLDescription> description;

Expand Down
Loading
Loading