Java 类org.jdom2.located.LocatedElement 实例源码

项目:BPMNspector    文件:Ext002Checker.java   
private void createViolation(Attribute firstAttrib, Attribute secondAttrib,
        ValidationResult validationResult) {
    try {
        Resource firstResource = ResourceUtils
                .determineAndCreateResourceFromString(firstAttrib.getDocument().getBaseURI(), null);

        int file1Line = ((LocatedElement) firstAttrib.getParent()).getLine();
        int file1Column = ((LocatedElement) secondAttrib.getParent()).getColumn();
        String file1XPath = XPathHelper.getAbsolutePath(firstAttrib);

        Resource secondResource = ResourceUtils
                .determineAndCreateResourceFromString(secondAttrib.getDocument().getBaseURI(), null);
        int file2Line = ((LocatedElement) secondAttrib.getParent()).getLine();
        int file2Column = ((LocatedElement) secondAttrib.getParent()).getColumn();
        String file2XPath = XPathHelper.getAbsolutePath(secondAttrib);

        Location location = new Location(firstResource, new LocationCoordinate(file1Line, file1Column), file1XPath);
        Violation violation = new Violation(location, "Resources have id duplicates", CONSTRAINTNUMBER);
        validationResult.addViolation(violation);

        Location location2 = new Location(secondResource, new LocationCoordinate(file2Line, file2Column), file2XPath);
        Violation violation2 = new Violation(location2, "Resources have id duplicates", CONSTRAINTNUMBER);
        validationResult.addViolation(violation2);

        LOGGER.info("violation of constraint {} found.", CONSTRAINTNUMBER);
    } catch (ValidationException e) {
        throw new IllegalArgumentException("Invalid baseURI detected.", e);
    }
}
项目:BPMNspector    文件:ProcessImporter.java   
private Violation createViolation(BPMNProcess parent, Element importElement, String msg) {
    int line = ((LocatedElement) importElement).getLine();
    int column = ((LocatedElement) importElement).getColumn();
    String xpath = XPathHelper.getAbsolutePath(importElement);

    Location location = new Location(Paths.get(parent.getBaseURI()), new LocationCoordinate(line, column), xpath);
    return new Violation(location, msg, "EXT.001");
}
项目:BPMNspector    文件:SchematronBPMNValidator.java   
/**
 * tries to locate errors in the specific files
 *
 * @param baseProcess
 *            the file where the error must be located
 * @param validationResult
 *            the result of the validation to add new found errors
 * @param failedAssert
 *            the error of the schematron validation
 */
private void handleSchematronErrors(BPMNProcess baseProcess,
        ValidationResult validationResult, FailedAssert failedAssert) {
    String message = failedAssert.getText().trim();
    String constraint = message.substring(0, message.indexOf('|'));
    String errorMessage = message.substring(message.indexOf('|') + 1);

    Location violationLocation = null;

    String location = fixXPathExpression(failedAssert.getLocation());

    LOGGER.debug("XPath to evaluate: "+location);
    XPathFactory fac = XPathFactory.instance();
    Element foundElem = fac.compile(location, Filters.element(), null,
            Namespace.getNamespace("bpmn", ConstantHelper.BPMN_NAMESPACE)).evaluateFirst(
            baseProcess.getProcessAsDoc());

    String logText;
    if(foundElem != null) {
        int line = ((LocatedElement) foundElem).getLine();
        int column = ((LocatedElement) foundElem).getColumn();
        String fileName = baseProcess.getBaseURI();
        violationLocation = new Location(
                Paths.get(fileName).toAbsolutePath(),
                new LocationCoordinate(line, column), location);
        logText = String.format(
                "violation of constraint %s found in %s at line %s.",
                constraint, violationLocation.getResource(), violationLocation.getLocation().getRow());
    } else {
        try {
            String xpathId = "";
            if (failedAssert.getDiagnosticReferenceCount() > 0) {
                xpathId = failedAssert.getDiagnosticReference().get(0)
                        .getText().trim();
            }
            LOGGER.debug("Trying to locate in files: "+xpathId);
            violationLocation = searchForViolationFile(xpathId, baseProcess);
            logText = String.format(
                    "violation of constraint %s found in %s at line %s.",
                    constraint, violationLocation.getResource(), violationLocation.getLocation().getRow());
        } catch (ValidationException | StringIndexOutOfBoundsException e) {
            LOGGER.error("Line of affected Element could not be determined.");
            logText = String.format("Found violation of constraint %s but the correct location could not be determined.",
                    constraint);
        }
    }

    LOGGER.info(logText);
    if("EXT.076".equals(constraint)) {
        Warning warning = new Warning(errorMessage, violationLocation);
        validationResult.addWarning(warning);
    } else {
        Violation violation = new Violation(violationLocation, errorMessage, constraint);
        validationResult.addViolation(violation);
    }
}
项目:BPMNspector    文件:SchematronBPMNValidator.java   
/**
 * searches for the file and line, where the violation occured
 *
 * @param xpathExpression
 *            the expression, through which the file and line should be
 *            identified
 * @param baseProcess
 *            baseProcess used for validation
 * @return the violation Location
 * @throws ValidationException
 *             if no element can be found
 */
private Location searchForViolationFile(String xpathExpression,
        BPMNProcess baseProcess) throws ValidationException {

    String namespacePrefix = xpathExpression.substring(0,
            xpathExpression.indexOf('_'));

    Optional<BPMNProcess> optional = baseProcess.findProcessByGeneratedPrefix(namespacePrefix);
    if(optional.isPresent()) {
        String fileName = optional.get().getBaseURI();

        int line = -1;
        int column = -1;

        // use ID with generated prefix for lookup
        String xpathObjectId = createIdBpmnExpression(xpathExpression);

        LOGGER.debug("Expression to evaluate: "+xpathObjectId);
        XPathFactory fac = XPathFactory.instance();
        List<Element> elems = fac.compile(xpathObjectId, Filters.element(), null,
                Namespace.getNamespace("bpmn", ConstantHelper.BPMN_NAMESPACE)).evaluate(optional.get().getProcessAsDoc());

        if(elems.size()==1) {
            line = ((LocatedElement) elems.get(0)).getLine();
            column = ((LocatedElement) elems.get(0)).getColumn();
            //use ID without prefix  (=original ID) as Violation xPath
            xpathObjectId = createIdBpmnExpression(xpathExpression
                    .substring(xpathExpression.indexOf('_') + 1));
        }

        if (line==-1 || column==-1) {
            throw new ValidationException("BPMN Element couldn't be found in file '"+fileName+"'!");
        }

        return new Location(Paths.get(fileName).toAbsolutePath(), new LocationCoordinate(line, column), xpathObjectId);

    } else {
        // File not found
        throw new ValidationException("BPMN Element couldn't be found as no corresponding file could be found.");
    }
}