use of org.apache.xerces.xs.XSElementDeclaration in project intellij-community by JetBrains.
the class XsContentDFA method getElementDeclaration.
@Nullable
private static XSElementDeclaration getElementDeclaration(XmlTag tag, XSModel xsModel) {
List<XmlTag> ancestors = new ArrayList<>();
for (XmlTag t = tag; t != null; t = t.getParentTag()) {
ancestors.add(t);
}
Collections.reverse(ancestors);
XSElementDeclaration declaration = null;
SubstitutionGroupHandler fSubGroupHandler = new SubstitutionGroupHandler(new MyXSElementDeclHelper());
CMBuilder cmBuilder = new CMBuilder(new CMNodeFactory());
for (XmlTag ancestor : ancestors) {
if (declaration == null) {
declaration = xsModel.getElementDeclaration(ancestor.getLocalName(), ancestor.getNamespace());
if (declaration == null)
return null;
else
continue;
}
XSTypeDefinition typeDefinition = declaration.getTypeDefinition();
if (!(typeDefinition instanceof XSComplexTypeDecl)) {
return null;
}
XSCMValidator model = ((XSComplexTypeDecl) typeDefinition).getContentModel(cmBuilder);
int[] ints = model.startContentModel();
for (XmlTag subTag : ancestor.getParentTag().getSubTags()) {
QName qName = createQName(subTag);
Object o = model.oneTransition(qName, ints, fSubGroupHandler);
if (subTag == ancestor) {
if (o instanceof XSElementDecl) {
declaration = (XSElementDecl) o;
break;
} else
return null;
}
}
}
return declaration;
}
use of org.apache.xerces.xs.XSElementDeclaration in project intellij-community by JetBrains.
the class XsContentDFA method createContentDFA.
@Nullable
public static XmlContentDFA createContentDFA(@NotNull XmlTag parentTag) {
final PsiFile file = parentTag.getContainingFile().getOriginalFile();
if (!(file instanceof XmlFile))
return null;
XSModel xsModel = ApplicationManager.getApplication().runReadAction(new NullableComputable<XSModel>() {
@Override
public XSModel compute() {
return getXSModel((XmlFile) file);
}
});
if (xsModel == null) {
return null;
}
XSElementDeclaration decl = getElementDeclaration(parentTag, xsModel);
if (decl == null) {
return null;
}
return new XsContentDFA(decl, parentTag);
}
use of org.apache.xerces.xs.XSElementDeclaration in project intellij-community by JetBrains.
the class XmlConstraintsTest method testXercesIncomplete.
public void testXercesIncomplete() throws Exception {
XSModel xsModel = getXSModel("testIncomplete.xml", "test.xsd");
XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration("a", "");
XSComplexTypeDefinition typeDefinition = (XSComplexTypeDefinition) elementDeclaration.getTypeDefinition();
CMBuilder cmBuilder = new CMBuilder(new CMNodeFactory());
XSCMValidator validator = cmBuilder.getContentModel((XSComplexTypeDecl) typeDefinition, true);
int[] ints = validator.startContentModel();
Vector vector = validator.whatCanGoHere(ints);
XSElementDecl o = (XSElementDecl) vector.get(0);
assertEquals("b", o.getName());
}
use of org.apache.xerces.xs.XSElementDeclaration in project iaf by ibissource.
the class ToXml method getBestMatchingElementPath.
/**
* @param baseElementDeclaration TODO
* @param particle
* @param failureReasons returns the reasons why no match was found
* @param availableElements
* @param path: in this list the longest list of child elements, that matches the available, is maintained. Null if no matching.
* @return true when a matching path is found. if false, failureReasons will contain reasons why.
* @throws SAXException
*/
public boolean getBestMatchingElementPath(XSElementDeclaration baseElementDeclaration, N baseNode, XSParticle particle, List<XSParticle> path, List<String> failureReasons) throws SAXException {
if (particle == null) {
throw new NullPointerException("getBestMatchingElementPath particle is null");
}
XSTerm term = particle.getTerm();
if (term == null) {
throw new NullPointerException("getBestMatchingElementPath particle.term is null");
}
if (term instanceof XSModelGroup) {
XSModelGroup modelGroup = (XSModelGroup) term;
short compositor = modelGroup.getCompositor();
XSObjectList particles = modelGroup.getParticles();
if (DEBUG)
log.debug("getBestMatchingElementPath() modelGroup particles [" + ToStringBuilder.reflectionToString(particles, ToStringStyle.MULTI_LINE_STYLE) + "]");
switch(compositor) {
case XSModelGroup.COMPOSITOR_SEQUENCE:
case XSModelGroup.COMPOSITOR_ALL:
for (int i = 0; i < particles.getLength(); i++) {
XSParticle childParticle = (XSParticle) particles.item(i);
if (!getBestMatchingElementPath(baseElementDeclaration, baseNode, childParticle, path, failureReasons)) {
return false;
}
}
return true;
case XSModelGroup.COMPOSITOR_CHOICE:
List<XSParticle> bestPath = null;
List<String> choiceFailureReasons = new LinkedList<String>();
for (int i = 0; i < particles.getLength(); i++) {
XSParticle childParticle = (XSParticle) particles.item(i);
List<XSParticle> optionPath = new LinkedList<XSParticle>(path);
if (getBestMatchingElementPath(baseElementDeclaration, baseNode, childParticle, optionPath, choiceFailureReasons)) {
if (bestPath == null || bestPath.size() < optionPath.size()) {
bestPath = optionPath;
}
}
}
if (bestPath == null) {
failureReasons.addAll(choiceFailureReasons);
return false;
}
if (DEBUG)
log.debug("Replace path with best path of Choice Compositor, size [" + bestPath.size() + "]");
path.clear();
path.addAll(bestPath);
return true;
default:
throw new IllegalStateException("getBestMatchingElementPath modelGroup.compositor is not COMPOSITOR_SEQUENCE, COMPOSITOR_ALL or COMPOSITOR_CHOICE, but [" + compositor + "]");
}
}
if (term instanceof XSElementDeclaration) {
XSElementDeclaration elementDeclaration = (XSElementDeclaration) term;
String elementName = elementDeclaration.getName();
if (DEBUG)
log.debug("getBestMatchingElementPath().XSElementDeclaration name [" + elementName + "]");
if (!hasChild(baseElementDeclaration, baseNode, elementName)) {
if (isDeepSearch()) {
if (DEBUG)
log.debug("getBestMatchingElementPath().XSElementDeclaration element [" + elementName + "] not found, perform deep search");
try {
List<XSParticle> subList = getBestChildElementPath(elementDeclaration, baseNode, true);
if (subList != null && !subList.isEmpty()) {
path.add(particle);
if (DEBUG)
log.debug("getBestMatchingElementPath().XSElementDeclaration element [" + elementName + "] not found, nested elements found in deep search");
return true;
}
if (DEBUG)
log.debug("getBestMatchingElementPath().XSElementDeclaration element [" + elementName + "] not found, no nested elements found in deep search");
} catch (Exception e) {
if (DEBUG)
log.debug("getBestMatchingElementPath().XSElementDeclaration element [" + elementName + "] not found, no nested elements found in deep search: " + e.getMessage());
return false;
}
}
if (particle.getMinOccurs() > 0) {
// if (DEBUG) log.debug("getBestMatchingElementPath().XSElementDeclaration mandatory element ["+elementName+"] not found, path fails, autoInsertMandatory ["+isAutoInsertMandatory()+"]");
// if (isAutoInsertMandatory()) {
// path.add(particle);
// if (DEBUG) log.debug("getBestMatchingElementPath().XSElementDeclaration element ["+elementName+"] not found, nested elements found in deep search");
// return true;
// }
failureReasons.add(MSG_EXPECTED_ELEMENT + " [" + elementName + "]");
return false;
}
if (DEBUG)
log.debug("getBestMatchingElementPath().XSElementDeclaration optional element [" + elementName + "] not found, path continues");
return true;
}
for (XSParticle resultParticle : path) {
if (elementName.equals(resultParticle.getTerm().getName())) {
if (DEBUG)
log.debug("getBestMatchingElementPath().XSElementDeclaration element [" + elementName + "] found but required multiple times");
failureReasons.add("element [" + elementName + "] required multiple times");
return false;
}
}
if (DEBUG)
log.debug("getBestMatchingElementPath().XSElementDeclaration element [" + elementName + "] found");
path.add(particle);
return true;
}
if (term instanceof XSWildcard) {
XSWildcard wildcard = (XSWildcard) term;
String processContents;
switch(wildcard.getProcessContents()) {
case XSWildcard.PC_LAX:
processContents = "LAX";
break;
case XSWildcard.PC_SKIP:
processContents = "SKIP";
break;
case XSWildcard.PC_STRICT:
processContents = "STRICT";
break;
default:
throw new IllegalStateException("getBestMatchingElementPath wildcard.processContents is not PC_LAX, PC_SKIP or PC_STRICT, but [" + wildcard.getProcessContents() + "]");
}
String namespaceConstraint;
switch(wildcard.getConstraintType()) {
case XSWildcard.NSCONSTRAINT_ANY:
namespaceConstraint = "ANY";
break;
case XSWildcard.NSCONSTRAINT_LIST:
namespaceConstraint = "SKIP " + wildcard.getNsConstraintList();
break;
case XSWildcard.NSCONSTRAINT_NOT:
namespaceConstraint = "NOT " + wildcard.getNsConstraintList();
break;
default:
throw new IllegalStateException("getBestMatchingElementPath wildcard.namespaceConstraint is not ANY, LIST or NOT, but [" + wildcard.getConstraintType() + "]");
}
String msg = "term for element [" + baseElementDeclaration.getName() + "] is WILDCARD; namespaceConstraint [" + namespaceConstraint + "] processContents [" + processContents + "]. Please check if the element typed properly in the schema";
if (isFailOnWildcards()) {
throw new IllegalStateException(msg + ", or set failOnWildcards=\"false\"");
}
log.warn(msg);
return true;
}
throw new IllegalStateException("getBestMatchingElementPath unknown Term type [" + term.getClass().getName() + "]");
}
use of org.apache.xerces.xs.XSElementDeclaration 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 (DEBUG)
log.debug("ToXml.handleComplexTypedElement() search for best path for available children of element [" + name + "]");
List<XSParticle> childParticles = getBestChildElementPath(elementDeclaration, node, false);
if (DEBUG) {
if (childParticles == null) {
log.debug("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.debug(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 (DEBUG)
log.debug("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) {
// 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 (DEBUG)
log.debug("ToXml.handleComplexTypedElement() handle element [" + name + "] as simple, because none processed");
handleSimpleTypedElement(elementDeclaration, null, node);
}
}
Aggregations