Search in sources :

Example 1 with ValueParseException

use of org.springframework.ide.vscode.commons.util.ValueParseException in project sts4 by spring-projects.

the class SchemaBasedYamlASTReconciler method getRegion.

protected DocumentRegion getRegion(Exception e, IDocument doc, Node node) {
    DocumentRegion region = new DocumentRegion(doc, node.getStartMark().getIndex(), node.getEndMark().getIndex());
    if (e instanceof ValueParseException) {
        ValueParseException parseException = (ValueParseException) e;
        int start = parseException.getStartIndex() >= 0 ? Math.min(node.getStartMark().getIndex() + parseException.getStartIndex(), node.getEndMark().getIndex()) : node.getStartMark().getIndex();
        int end = parseException.getEndIndex() >= 0 ? Math.min(node.getStartMark().getIndex() + parseException.getEndIndex(), node.getEndMark().getIndex()) : node.getEndMark().getIndex();
        region = new DocumentRegion(doc, start, end);
    }
    return region;
}
Also used : DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) Constraint(org.springframework.ide.vscode.commons.yaml.schema.constraints.Constraint)

Example 2 with ValueParseException

use of org.springframework.ide.vscode.commons.util.ValueParseException in project sts4 by spring-projects.

the class SpringPropertiesReconcileEngine method reconcileType.

private void reconcileType(DocumentRegion escapedValue, Type expectType, IProblemCollector problems) {
    TypeUtil typeUtil = typeUtilProvider.getTypeUtil(escapedValue.getDocument());
    ValueParser parser = typeUtil.getValueParser(expectType);
    if (parser != null) {
        try {
            String valueStr = PropertiesFileEscapes.unescape(escapedValue.toString());
            if (!valueStr.contains("${")) {
                // Don't check strings that look like they use variable substitution.
                parser.parse(valueStr);
            }
        } catch (ValueParseException e) {
            problems.accept(problem(ApplicationPropertiesProblemType.PROP_VALUE_TYPE_MISMATCH, ExceptionUtil.getMessage(e), e.getHighlightRegion(escapedValue)));
        } catch (Exception e) {
            problems.accept(problem(ApplicationPropertiesProblemType.PROP_VALUE_TYPE_MISMATCH, "Expecting '" + typeUtil.niceTypeName(expectType) + "'", escapedValue));
        }
    }
}
Also used : ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) TypeUtil(org.springframework.ide.vscode.boot.metadata.types.TypeUtil) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 3 with ValueParseException

use of org.springframework.ide.vscode.commons.util.ValueParseException in project sts4 by spring-projects.

the class BoshValueParsers method url.

public static ValueParser url(String... _schemes) {
    return new ValueParser() {

        private final ImmutableList<String> validSchemes = ImmutableList.copyOf(_schemes);

        @Override
        public Object parse(String s) throws Exception {
            URI uri = new URI(s);
            String scheme = uri.getScheme();
            if (scheme == null) {
                throw new ValueParseException(message());
            } else if (!validSchemes.contains(scheme.toLowerCase())) {
                int start = s.indexOf(scheme);
                if (start >= 0) {
                    int end = start + scheme.length();
                    throw new ValueParseException(message(), start, end);
                } else {
                    // Trouble finding exact location of underlined region so underline whole url
                    throw new ValueParseException(message());
                }
            }
            return uri;
        }

        private String message() {
            return "Url scheme must be one of " + validSchemes;
        }
    };
}
Also used : ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) ImmutableList(com.google.common.collect.ImmutableList) URI(java.net.URI) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException)

Example 4 with ValueParseException

use of org.springframework.ide.vscode.commons.util.ValueParseException in project sts4 by spring-projects.

the class GithubValueParsers method uri.

public static ValueParser uri(GithubInfoProvider github) {
    return new ValueParser() {

        @Override
        public GithubRepoReference parse(String _str) throws Exception {
            TextDocument doc = new TextDocument(null, LanguageId.PLAINTEXT);
            doc.setText(_str);
            DocumentRegion str = new DocumentRegion(doc);
            GithubRepoReference repo = parseFormat(str);
            if (repo != null) {
                Collection<String> knownRepos;
                try {
                    knownRepos = github.getReposForOwner(repo.owner.toString());
                } catch (Exception e) {
                    // Couldn't read info from github. Ignore this in reconciler context.
                    return repo;
                }
                if (knownRepos == null) {
                    throw unknownEntity("User or Organization not found: '" + repo.owner + "'", repo.owner);
                } else {
                    if (!knownRepos.contains(repo.name.toString())) {
                        throw unknownEntity("Repo not found: '" + repo.name + "'", repo.name);
                    }
                }
            }
            return repo;
        }

        private GithubRepoReference parseFormat(DocumentRegion str) throws Exception {
            String prefix = checkPrefix(str);
            if (prefix != null) {
                DocumentRegion ownerAndName = str.subSequence(prefix.length());
                // Should end with '.git'
                if (ownerAndName.endsWith(".git")) {
                    ownerAndName = ownerAndName.subSequence(0, ownerAndName.length() - 4);
                } else {
                    DocumentRegion highlight = ownerAndName.textAtEnd(1);
                    throw new ValueParseException("GitHub repo uri should end with '.git'", highlight.getStart(), highlight.getEnd());
                }
                int slash = ownerAndName.indexOf('/');
                if (slash >= 0) {
                    return new GithubRepoReference(ownerAndName.subSequence(0, slash), ownerAndName.subSequence(slash + 1));
                } else {
                    throw new ValueParseException("Expecting something of the form '${owner}/${repo}'.", ownerAndName.getStart(), ownerAndName.getEnd());
                }
            }
            return null;
        }

        private String checkPrefix(DocumentRegion str) throws ValueParseException {
            for (String expectedPrefix : GithubRepoContentAssistant.URI_PREFIXES) {
                int lastChar = expectedPrefix.length() - 1;
                if (str.startsWith(expectedPrefix.substring(0, lastChar))) {
                    if (str.charAt(lastChar) == expectedPrefix.charAt(lastChar)) {
                        return expectedPrefix;
                    }
                    throw new ValueParseException("Expecting a '" + expectedPrefix.charAt(lastChar) + "'", lastChar, lastChar + 1);
                }
            }
            return null;
        }
    };
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) ReconcileException(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException)

Example 5 with ValueParseException

use of org.springframework.ide.vscode.commons.util.ValueParseException in project sts4 by spring-projects.

the class RouteValueParser method dynamicValidation.

private Object dynamicValidation(String str, Matcher matcher) throws Exception {
    if (domains == null) {
        // If domains is unknown we can't do the dynamic checks, so bail out.
        return str;
    }
    try {
        Collection<String> cloudDomains = Collections.emptyList();
        try {
            cloudDomains = domains.call();
        } catch (ValueParseException e) {
            /*
				 * If domains hint provider throws exception it is
				 * ValueParserException not NoTargetsException. This means no
				 * communication with CF -> abort dyncamic validation
				 */
            return matcher;
        }
        if (cloudDomains == null) {
            // If domains is unknown we can't do the dynamic checks, so bail out.
            return str;
        }
        CFRoute route = CFRoute.builder().from(str, cloudDomains).build();
        if (route.getDomain() == null || route.getDomain().isEmpty()) {
            throw new ValueParseException("Domain is missing.");
        }
        if ((route.getPath() != null && !route.getPath().isEmpty()) && (route.getPort() != CFRoute.NO_PORT)) {
            throw new ValueParseException("Unable to determine type of route. HTTP port may have a path but no port. TCP route may have port but no path.");
        }
        if (route.getPort() > MAX_PORT_NUMBER) {
            String portAndColumn = matcher.group(2);
            int start = str.indexOf(portAndColumn) + 1;
            int end = start + portAndColumn.length() - 1;
            throw new ValueParseException("Invalid port number. Port range must be between 1 and " + MAX_PORT_NUMBER, start, end);
        }
        if (!cloudDomains.contains(route.getDomain())) {
            String hostDomain = matcher.group(1);
            throw new ReconcileException("Unknown 'Domain'. Valid domains are: " + cloudDomains, ManifestYamlSchemaProblemsTypes.UNKNOWN_DOMAIN_PROBLEM, hostDomain.lastIndexOf(route.getDomain()), hostDomain.length());
        }
        return str;
    } catch (ConnectionException | NoTargetsException e) {
        // No connection to CF? Abort dynamic validation
        return str;
    }
}
Also used : NoTargetsException(org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.NoTargetsException) ReconcileException(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException) CFRoute(org.springframework.ide.vscode.commons.cloudfoundry.client.CFRoute) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) ConnectionException(org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.ConnectionException)

Aggregations

ValueParseException (org.springframework.ide.vscode.commons.util.ValueParseException)5 ValueParser (org.springframework.ide.vscode.commons.util.ValueParser)3 ReconcileException (org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException)2 DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)2 ImmutableList (com.google.common.collect.ImmutableList)1 URI (java.net.URI)1 TypeUtil (org.springframework.ide.vscode.boot.metadata.types.TypeUtil)1 CFRoute (org.springframework.ide.vscode.commons.cloudfoundry.client.CFRoute)1 ConnectionException (org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.ConnectionException)1 NoTargetsException (org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.NoTargetsException)1 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)1 TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)1 Constraint (org.springframework.ide.vscode.commons.yaml.schema.constraints.Constraint)1