use of org.apache.xerces.xs.XSParticle in project winery by eclipse.
the class BackendUtils method deriveWPD.
/**
* Derives Winery's Properties Definition from an existing properties definition
*
* @param ci the entity type to try to modify the WPDs
* @param errors the list to add errors to
*/
public static void deriveWPD(TEntityType ci, List<String> errors) {
BackendUtils.LOGGER.trace("deriveWPD");
PropertiesDefinition propertiesDefinition = ci.getPropertiesDefinition();
QName element = propertiesDefinition.getElement();
if (element == null) {
BackendUtils.LOGGER.debug("only works for an element definition, not for types");
} else {
BackendUtils.LOGGER.debug("Looking for the definition of {" + element.getNamespaceURI() + "}" + element.getLocalPart());
// fetch the XSD defining the element
final XsdImportManager xsdImportManager = RepositoryFactory.getRepository().getXsdImportManager();
Map<String, RepositoryFileReference> mapFromLocalNameToXSD = xsdImportManager.getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
RepositoryFileReference ref = mapFromLocalNameToXSD.get(element.getLocalPart());
if (ref == null) {
String msg = "XSD not found for " + element.getNamespaceURI() + " / " + element.getLocalPart();
BackendUtils.LOGGER.debug(msg);
errors.add(msg);
return;
}
final Optional<XSModel> xsModelOptional = BackendUtils.getXSModel(ref);
if (!xsModelOptional.isPresent()) {
LOGGER.error("no XSModel found");
}
XSModel xsModel = xsModelOptional.get();
XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration(element.getLocalPart(), element.getNamespaceURI());
if (elementDeclaration == null) {
String msg = "XSD model claimed to contain declaration for {" + element.getNamespaceURI() + "}" + element.getLocalPart() + ", but it did not.";
BackendUtils.LOGGER.debug(msg);
errors.add(msg);
return;
}
// go through the XSD definition and
XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
if (typeDefinition instanceof XSComplexTypeDefinition) {
XSComplexTypeDefinition cTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
XSParticle particle = cTypeDefinition.getParticle();
if (particle == null) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Complex type does not contain particles");
} else {
XSTerm term = particle.getTerm();
if (term instanceof XSModelGroup) {
XSModelGroup modelGroup = (XSModelGroup) term;
if (modelGroup.getCompositor() == XSModelGroup.COMPOSITOR_SEQUENCE) {
XSObjectList particles = modelGroup.getParticles();
int len = particles.getLength();
boolean everyThingIsASimpleType = true;
PropertyDefinitionKVList list = new PropertyDefinitionKVList();
if (len != 0) {
for (int i = 0; i < len; i++) {
XSParticle innerParticle = (XSParticle) particles.item(i);
XSTerm innerTerm = innerParticle.getTerm();
if (innerTerm instanceof XSElementDeclaration) {
XSElementDeclaration innerElementDeclaration = (XSElementDeclaration) innerTerm;
String name = innerElementDeclaration.getName();
XSTypeDefinition innerTypeDefinition = innerElementDeclaration.getTypeDefinition();
if (innerTypeDefinition instanceof XSSimpleType) {
XSSimpleType xsSimpleType = (XSSimpleType) innerTypeDefinition;
String typeNS = xsSimpleType.getNamespace();
String typeName = xsSimpleType.getName();
if (typeNS.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
PropertyDefinitionKV def = new PropertyDefinitionKV();
def.setKey(name);
// convention at WPD: use "xsd" as prefix for XML Schema Definition
def.setType("xsd:" + typeName);
list.add(def);
} else {
everyThingIsASimpleType = false;
break;
}
} else {
everyThingIsASimpleType = false;
break;
}
} else {
everyThingIsASimpleType = false;
break;
}
}
}
if (everyThingIsASimpleType) {
// everything went allright, we can add a WPD
WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
wpd.setIsDerivedFromXSD(Boolean.TRUE);
wpd.setElementName(element.getLocalPart());
wpd.setNamespace(element.getNamespaceURI());
wpd.setPropertyDefinitionKVList(list);
ModelUtilities.replaceWinerysPropertiesDefinition(ci, wpd);
BackendUtils.LOGGER.debug("Successfully generated WPD");
} else {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not all types in the sequence are simple types");
}
} else {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Model group is not a sequence");
}
} else {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not a model group");
}
}
} else {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: No Complex Type Definition");
}
}
}
use of org.apache.xerces.xs.XSParticle in project iaf by ibissource.
the class XmlTypeToJsonSchemaConverter method buildObject.
private void buildObject(JsonObjectBuilder builder, XSObjectList particles, XSObjectList attributeUses, String textAttribute, XSTypeDefinition baseType) {
builder.add("type", "object");
builder.add("additionalProperties", false);
JsonObjectBuilder propertiesBuilder = Json.createObjectBuilder();
List<String> requiredProperties = new ArrayList<String>();
if (attributeUses != null) {
for (int i = 0; i < attributeUses.getLength(); i++) {
XSAttributeUse attributeUse = (XSAttributeUse) attributeUses.get(i);
XSAttributeDeclaration attributeDecl = attributeUse.getAttrDeclaration();
propertiesBuilder.add(attributePrefix + attributeDecl.getName(), getDefinition(attributeDecl.getTypeDefinition(), true));
}
}
if (textAttribute != null && ((attributeUses != null && attributeUses.getLength() > 0) || (particles != null && particles.getLength() > 0))) {
JsonObject elementType = baseType != null ? getDefinition(baseType, true) : Json.createObjectBuilder().add("type", "string").build();
propertiesBuilder.add(textAttribute, elementType);
}
if (particles != null) {
for (int i = 0; i < particles.getLength(); i++) {
XSParticle childParticle = (XSParticle) particles.item(i);
if (log.isTraceEnabled())
log.trace("childParticle [" + i + "][" + ToStringBuilder.reflectionToString(childParticle, ToStringStyle.MULTI_LINE_STYLE) + "]");
XSTerm childTerm = childParticle.getTerm();
if (childTerm instanceof XSElementDeclaration) {
XSElementDeclaration elementDeclaration = (XSElementDeclaration) childTerm;
String elementName = elementDeclaration.getName();
if (elementName != null && childParticle.getMinOccurs() != 0) {
requiredProperties.add(elementName);
}
}
handleParticle(propertiesBuilder, childParticle, null, true);
}
}
builder.add("properties", propertiesBuilder.build());
if (requiredProperties.size() > 0) {
JsonArrayBuilder requiredPropertiesBuilder = Json.createArrayBuilder();
for (String requiredProperty : requiredProperties) {
requiredPropertiesBuilder.add(requiredProperty);
}
builder.add("required", requiredPropertiesBuilder.build());
}
}
use of org.apache.xerces.xs.XSParticle in project winery by eclipse.
the class BackendUtils method deriveWPD.
/**
* Derives Winery's Properties Definition from an existing properties definition
*
* @param ci the entity type to try to modify the WPDs
* @param errors the list to add errors to
*/
// FIXME this is specifically for xml backends and therefore broken under the new canonical model
public static void deriveWPD(TEntityType ci, List<String> errors, IRepository repository) {
BackendUtils.LOGGER.trace("deriveWPD");
TEntityType.PropertiesDefinition propertiesDefinition = ci.getProperties();
if (propertiesDefinition == null) {
// if there's no properties definition, there's nothing to derive because we're in YAML mode
return;
}
if (!(propertiesDefinition instanceof TEntityType.XmlElementDefinition)) {
BackendUtils.LOGGER.debug("only works for an element definition, not for types");
return;
}
final QName element = ((TEntityType.XmlElementDefinition) propertiesDefinition).getElement();
BackendUtils.LOGGER.debug("Looking for the definition of {" + element.getNamespaceURI() + "}" + element.getLocalPart());
// fetch the XSD defining the element
final XsdImportManager xsdImportManager = repository.getXsdImportManager();
Map<String, RepositoryFileReference> mapFromLocalNameToXSD = xsdImportManager.getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
RepositoryFileReference ref = mapFromLocalNameToXSD.get(element.getLocalPart());
if (ref == null) {
String msg = "XSD not found for " + element.getNamespaceURI() + " / " + element.getLocalPart();
BackendUtils.LOGGER.debug(msg);
errors.add(msg);
return;
}
final Optional<XSModel> xsModelOptional = BackendUtils.getXSModel(ref, repository);
if (!xsModelOptional.isPresent()) {
LOGGER.error("no XSModel found");
}
XSModel xsModel = xsModelOptional.get();
XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration(element.getLocalPart(), element.getNamespaceURI());
if (elementDeclaration == null) {
String msg = "XSD model claimed to contain declaration for {" + element.getNamespaceURI() + "}" + element.getLocalPart() + ", but it did not.";
BackendUtils.LOGGER.debug(msg);
errors.add(msg);
return;
}
// go through the XSD definition and
XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
if (!(typeDefinition instanceof XSComplexTypeDefinition)) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: No Complex Type Definition");
return;
}
XSComplexTypeDefinition cTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
XSParticle particle = cTypeDefinition.getParticle();
if (particle == null) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Complex type does not contain particles");
return;
}
XSTerm term = particle.getTerm();
if (!(term instanceof XSModelGroup)) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not a model group");
return;
}
XSModelGroup modelGroup = (XSModelGroup) term;
if (modelGroup.getCompositor() != XSModelGroup.COMPOSITOR_SEQUENCE) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Model group is not a sequence");
return;
}
XSObjectList particles = modelGroup.getParticles();
int len = particles.getLength();
boolean everyThingIsASimpleType = true;
List<PropertyDefinitionKV> list = new ArrayList<>();
if (len != 0) {
for (int i = 0; i < len; i++) {
XSParticle innerParticle = (XSParticle) particles.item(i);
XSTerm innerTerm = innerParticle.getTerm();
if (innerTerm instanceof XSElementDeclaration) {
XSElementDeclaration innerElementDeclaration = (XSElementDeclaration) innerTerm;
String name = innerElementDeclaration.getName();
XSTypeDefinition innerTypeDefinition = innerElementDeclaration.getTypeDefinition();
if (innerTypeDefinition instanceof XSSimpleType) {
XSSimpleType xsSimpleType = (XSSimpleType) innerTypeDefinition;
String typeNS = xsSimpleType.getNamespace();
String typeName = xsSimpleType.getName();
if (typeNS.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
PropertyDefinitionKV def = new PropertyDefinitionKV();
def.setKey(name);
// convention at WPD: use "xsd" as prefix for XML Schema Definition
def.setType("xsd:" + typeName);
list.add(def);
} else {
everyThingIsASimpleType = false;
break;
}
} else {
everyThingIsASimpleType = false;
break;
}
} else {
everyThingIsASimpleType = false;
break;
}
}
}
if (!everyThingIsASimpleType) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not all types in the sequence are simple types");
return;
}
// everything went alright, we can add a WPD
WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
wpd.setIsDerivedFromXSD(Boolean.TRUE);
wpd.setElementName(element.getLocalPart());
wpd.setNamespace(element.getNamespaceURI());
wpd.setPropertyDefinitions(list);
ModelUtilities.replaceWinerysPropertiesDefinition(ci, wpd);
BackendUtils.LOGGER.debug("Successfully generated WPD");
}
use of org.apache.xerces.xs.XSParticle in project iaf by ibissource.
the class ToXml method handleComplexTypedElement.
protected void handleComplexTypedElement(XSElementDeclaration elementDeclaration, N node) throws SAXException {
String name = elementDeclaration.getName();
// List<XSParticle> childParticles = getChildElementDeclarations(typeDefinition);
if (log.isTraceEnabled())
log.trace("ToXml.handleComplexTypedElement() search for best path for available children of element [" + name + "]");
List<XSParticle> childParticles = getBestChildElementPath(elementDeclaration, node, false);
if (log.isTraceEnabled()) {
if (childParticles == null) {
log.trace("Examined node [" + name + "] deepSearch [" + isDeepSearch() + "] path found is null");
} else {
String msg = "Examined node [" + name + "] deepSearch [" + isDeepSearch() + "] found path length [" + childParticles.size() + "]: ";
boolean tail = false;
for (XSParticle particle : childParticles) {
if (tail) {
msg += ", ";
} else {
tail = true;
}
msg += particle.getTerm().getName();
}
log.trace(msg);
}
}
Set<String> processedChildren = new HashSet<String>();
if (childParticles != null) {
// }
for (int i = 0; i < childParticles.size(); i++) {
XSParticle childParticle = childParticles.get(i);
XSElementDeclaration childElementDeclaration = (XSElementDeclaration) childParticle.getTerm();
if (log.isTraceEnabled())
log.trace("ToXml.handleComplexTypedElement() processing child [" + i + "], name [" + childElementDeclaration.getName() + "]");
processChildElement(node, name, childElementDeclaration, childParticle.getMinOccurs() > 0, processedChildren);
}
}
Set<String> unProcessedChildren = getUnprocessedChildElementNames(elementDeclaration, node, processedChildren);
if (unProcessedChildren != null && !unProcessedChildren.isEmpty()) {
Set<String> unProcessedChildrenWorkingCopy = new HashSet<String>(unProcessedChildren);
log.warn("processing [" + unProcessedChildren.size() + "] unprocessed child elements" + (unProcessedChildren.size() > 0 ? ", first [" + unProcessedChildren.iterator().next() + "]" : ""));
// this loop is required to handle for mixed content element containing globally defined elements
for (String childName : unProcessedChildrenWorkingCopy) {
log.warn("processing unprocessed child element [" + childName + "]");
XSElementDeclaration childElementDeclaration = findElementDeclarationForName(null, childName);
if (childElementDeclaration == null) {
if (isIgnoreUndeclaredElements()) {
continue;
}
// this clause is hit for mixed content element containing elements that are not defined
throw new SAXException(MSG_CANNOT_NOT_FIND_ELEMENT_DECLARATION + " [" + childName + "]");
}
processChildElement(node, name, childElementDeclaration, false, processedChildren);
}
}
// the below is used for mixed content nodes containing text
if (processedChildren.isEmpty()) {
if (log.isTraceEnabled())
log.trace("ToXml.handleComplexTypedElement() handle element [" + name + "] as simple, because no children processed");
handleSimpleTypedElement(elementDeclaration, null, node);
}
}
use of org.apache.xerces.xs.XSParticle in project iaf by ibissource.
the class ToXml method getBestChildElementPath.
public List<XSParticle> getBestChildElementPath(XSElementDeclaration elementDeclaration, N node, boolean silent) throws SAXException {
XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
if (typeDefinition == null) {
log.warn("getBestChildElementPath typeDefinition is null");
return null;
}
switch(typeDefinition.getTypeCategory()) {
case XSTypeDefinition.SIMPLE_TYPE:
if (log.isTraceEnabled())
log.trace("getBestChildElementPath typeDefinition.typeCategory is SimpleType, no child elements");
return null;
case XSTypeDefinition.COMPLEX_TYPE:
XSComplexTypeDefinition complexTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
switch(complexTypeDefinition.getContentType()) {
case XSComplexTypeDefinition.CONTENTTYPE_EMPTY:
if (log.isTraceEnabled())
log.trace("getBestChildElementPath complexTypeDefinition.contentType is Empty, no child elements");
return null;
case XSComplexTypeDefinition.CONTENTTYPE_SIMPLE:
if (log.isTraceEnabled())
log.trace("getBestChildElementPath complexTypeDefinition.contentType is Simple, no child elements (only characters)");
return null;
case XSComplexTypeDefinition.CONTENTTYPE_ELEMENT:
case XSComplexTypeDefinition.CONTENTTYPE_MIXED:
XSParticle particle = complexTypeDefinition.getParticle();
if (particle == null) {
throw new IllegalStateException("getBestChildElementPath complexTypeDefinition.particle is null for Element or Mixed contentType");
// log.warn("typeDefinition particle is null, is this a problem?");
// return null;
}
if (log.isTraceEnabled())
log.trace("typeDefinition particle [" + ToStringBuilder.reflectionToString(particle, ToStringStyle.MULTI_LINE_STYLE) + "]");
List<XSParticle> result = new LinkedList<XSParticle>();
List<String> failureReasons = new LinkedList<String>();
if (getBestMatchingElementPath(elementDeclaration, node, particle, result, failureReasons)) {
return result;
}
String msg = "Cannot find path:";
for (String reason : failureReasons) {
msg += '\n' + reason;
}
if (!silent) {
handleError(msg);
}
return null;
default:
throw new IllegalStateException("getBestChildElementPath complexTypeDefinition.contentType is not Empty,Simple,Element or Mixed, but [" + complexTypeDefinition.getContentType() + "]");
}
default:
throw new IllegalStateException("getBestChildElementPath typeDefinition.typeCategory is not SimpleType or ComplexType, but [" + typeDefinition.getTypeCategory() + "] class [" + typeDefinition.getClass().getName() + "]");
}
}
Aggregations