Search in sources :

Example 1 with XSWildcard

use of org.apache.xerces.xs.XSWildcard 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() + "]");
}
Also used : XSObjectList(org.apache.xerces.xs.XSObjectList) XSTerm(org.apache.xerces.xs.XSTerm) XSParticle(org.apache.xerces.xs.XSParticle) XSWildcard(org.apache.xerces.xs.XSWildcard) LinkedList(java.util.LinkedList) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModelGroup(org.apache.xerces.xs.XSModelGroup)

Example 2 with XSWildcard

use of org.apache.xerces.xs.XSWildcard in project iaf by ibissource.

the class XmlAligner method determineIsParentOfSingleMultipleOccurringChildElement.

protected int determineIsParentOfSingleMultipleOccurringChildElement(XSParticle particle) {
    if (particle == null) {
        log.warn("Particle is null, is this a problem? Appearantly not");
        return CHILD_OCCURRENCE_EMPTY;
    }
    XSTerm term = particle.getTerm();
    if (term == null) {
        throw new IllegalStateException("determineIsParentOfSingleMultipleOccurringChildElement particle.term is null");
    }
    if (DEBUG)
        log.debug("determineIsParentOfSingleMultipleOccurringChildElement() term name [" + term.getName() + "] occurring unbounded [" + particle.getMaxOccursUnbounded() + "] max occur [" + particle.getMaxOccurs() + "] term [" + ToStringBuilder.reflectionToString(term) + "]");
    if (term instanceof XSModelGroup) {
        XSModelGroup modelGroup = (XSModelGroup) term;
        short compositor = modelGroup.getCompositor();
        XSObjectList particles = modelGroup.getParticles();
        switch(compositor) {
            case XSModelGroup.COMPOSITOR_SEQUENCE:
            case XSModelGroup.COMPOSITOR_ALL:
                {
                    if (DEBUG)
                        log.debug("determineIsParentOfSingleMultipleOccurringChildElement() sequence or all particles [" + ToStringBuilder.reflectionToString(particles) + "]");
                    int result = CHILD_OCCURRENCE_EMPTY;
                    for (int i = 0; i < particles.getLength(); i++) {
                        XSParticle childParticle = (XSParticle) particles.item(i);
                        int current = determineIsParentOfSingleMultipleOccurringChildElement(childParticle);
                        if (DEBUG)
                            log.debug("determineIsParentOfSingleMultipleOccurringChildElement() sequence or all, particle [" + i + "] current result [" + CHILD_OCCURRENCE_DESCRIPTION[current] + "]");
                        switch(current) {
                            case CHILD_OCCURRENCE_EMPTY:
                                break;
                            case CHILD_OCCURRENCE_ONE_SINGLE_OCCURRING_ELEMENT:
                            case CHILD_OCCURRENCE_ONE_MULTIPLE_OCCURRING_ELEMENT:
                                if (result > CHILD_OCCURRENCE_EMPTY) {
                                    if (DEBUG)
                                        log.debug("determineIsParentOfSingleMultipleOccurringChildElement() sequence or all, result [" + CHILD_OCCURRENCE_DESCRIPTION[result] + "] current [" + CHILD_OCCURRENCE_DESCRIPTION[current] + "]");
                                    return CHILD_OCCURRENCE_MULTIPLE_ELEMENTS_OR_NOT_MULTIPLE_OCCURRING;
                                }
                                result = current;
                                break;
                            case CHILD_OCCURRENCE_MULTIPLE_ELEMENTS_OR_NOT_MULTIPLE_OCCURRING:
                                return CHILD_OCCURRENCE_MULTIPLE_ELEMENTS_OR_NOT_MULTIPLE_OCCURRING;
                            default:
                                throw new IllegalStateException("determineIsParentOfSingleMultipleOccurringChildElement child occurrence [" + CHILD_OCCURRENCE_DESCRIPTION[current] + "]");
                        }
                    }
                    if (DEBUG)
                        log.debug("determineIsParentOfSingleMultipleOccurringChildElement() end of sequence or all, returning [" + CHILD_OCCURRENCE_DESCRIPTION[result] + "]");
                    return result;
                }
            case XSModelGroup.COMPOSITOR_CHOICE:
                {
                    if (DEBUG)
                        log.debug("determineIsParentOfSingleMultipleOccurringChildElement() choice particles [" + ToStringBuilder.reflectionToString(particles) + "]");
                    if (particles.getLength() == 0) {
                        if (DEBUG)
                            log.debug("determineIsParentOfSingleMultipleOccurringChildElement() choice length 0, returning [" + CHILD_OCCURRENCE_DESCRIPTION[CHILD_OCCURRENCE_MULTIPLE_ELEMENTS_OR_NOT_MULTIPLE_OCCURRING] + "]");
                        return CHILD_OCCURRENCE_EMPTY;
                    }
                    int result = determineIsParentOfSingleMultipleOccurringChildElement((XSParticle) particles.item(0));
                    if (result == CHILD_OCCURRENCE_MULTIPLE_ELEMENTS_OR_NOT_MULTIPLE_OCCURRING) {
                        if (DEBUG)
                            log.debug("determineIsParentOfSingleMultipleOccurringChildElement() choice single mixed, returning [" + CHILD_OCCURRENCE_DESCRIPTION[CHILD_OCCURRENCE_MULTIPLE_ELEMENTS_OR_NOT_MULTIPLE_OCCURRING] + "]");
                        return CHILD_OCCURRENCE_MULTIPLE_ELEMENTS_OR_NOT_MULTIPLE_OCCURRING;
                    }
                    for (int i = 1; i < particles.getLength(); i++) {
                        XSParticle childParticle = (XSParticle) particles.item(i);
                        int current = determineIsParentOfSingleMultipleOccurringChildElement(childParticle);
                        if (current != result) {
                            if (DEBUG)
                                log.debug("determineIsParentOfSingleMultipleOccurringChildElement() break out of choice, returning [" + CHILD_OCCURRENCE_DESCRIPTION[CHILD_OCCURRENCE_MULTIPLE_ELEMENTS_OR_NOT_MULTIPLE_OCCURRING] + "]");
                            return CHILD_OCCURRENCE_MULTIPLE_ELEMENTS_OR_NOT_MULTIPLE_OCCURRING;
                        }
                    }
                    if (DEBUG)
                        log.debug("determineIsParentOfSingleMultipleOccurringChildElement() end of choice, returning [" + CHILD_OCCURRENCE_DESCRIPTION[result] + "]");
                    return result;
                }
            default:
                throw new IllegalStateException("determineIsParentOfSingleMultipleOccurringChildElement 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("determineIsParentOfSingleMultipleOccurringChildElement() ElementDeclaration name [" + elementName + "] unbounded [" + particle.getMaxOccursUnbounded() + "] maxOccurs [" + particle.getMaxOccurs() + "]");
        if (particle.getMaxOccursUnbounded() || particle.getMaxOccurs() > 1) {
            return CHILD_OCCURRENCE_ONE_MULTIPLE_OCCURRING_ELEMENT;
        }
        if (particle.getMaxOccurs() == 1) {
            return CHILD_OCCURRENCE_ONE_SINGLE_OCCURRING_ELEMENT;
        }
        return CHILD_OCCURRENCE_EMPTY;
    }
    if (term instanceof XSWildcard) {
        return CHILD_OCCURRENCE_MULTIPLE_ELEMENTS_OR_NOT_MULTIPLE_OCCURRING;
    }
    throw new IllegalStateException("determineIsParentOfSingleMultipleOccurringChildElement unknown Term type [" + term.getClass().getName() + "]");
}
Also used : XSObjectList(org.apache.xerces.xs.XSObjectList) XSTerm(org.apache.xerces.xs.XSTerm) XSParticle(org.apache.xerces.xs.XSParticle) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSWildcard(org.apache.xerces.xs.XSWildcard) XSModelGroup(org.apache.xerces.xs.XSModelGroup)

Aggregations

XSElementDeclaration (org.apache.xerces.xs.XSElementDeclaration)2 XSModelGroup (org.apache.xerces.xs.XSModelGroup)2 XSObjectList (org.apache.xerces.xs.XSObjectList)2 XSParticle (org.apache.xerces.xs.XSParticle)2 XSTerm (org.apache.xerces.xs.XSTerm)2 XSWildcard (org.apache.xerces.xs.XSWildcard)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1