use of org.springframework.ide.vscode.commons.util.ValueParser in project sts4 by spring-projects.
the class BoshValueParserTest method urlOkay.
@Test
public void urlOkay() throws Exception {
ValueParser urlParser = BoshValueParsers.url("http", "https", "file");
urlParser.parse("http://foobar.com/munhings.tar.gz");
urlParser.parse("https://foobar.com/munhings.tar.gz");
urlParser.parse("hTTp://foobar.com/munhings.tar.gz");
urlParser.parse("HTTPS://foobar.com/munhings.tar.gz");
urlParser.parse("file://local/file");
urlParser.parse("file:///local/file");
urlParser.parse("FILE:///local/file");
}
use of org.springframework.ide.vscode.commons.util.ValueParser in project sts4 by spring-projects.
the class TypeUtil method getValueParser.
public ValueParser getValueParser(Type type) {
ValueParser simpleParser = VALUE_PARSERS.get(type.getErasure());
if (simpleParser != null) {
return simpleParser;
}
Collection<StsValueHint> enumValues = getAllowedValues(type, EnumCaseMode.ALIASED);
if (enumValues != null) {
// assigning anything to it is an error.
return new EnumValueParser(niceTypeName(type), getBareValues(enumValues));
}
if (isMap(type)) {
// provide a parser that allows throws
return new AlwaysFailingParser(niceTypeName(type));
}
if (isSequencable(type)) {
// Trying to parse list from scalars is possible if the domain type is parseable. Spring boot
// will try to interpret the string as a comma-separated list
Type elType = getDomainType(type);
if (elType != null) {
ValueParser elParser = getValueParser(elType);
if (elParser != null) {
return new DelimitedStringParser(elParser);
}
}
}
return null;
}
use of org.springframework.ide.vscode.commons.util.ValueParser in project sts4 by spring-projects.
the class PropertyNavigator method dotNavigate.
/**
* Handle dot navigation into given type, after a '.' was
* was found at given offset. Assumes the type has already been
* checked to be 'dotable'.
*/
private Type dotNavigate(int offset, Type type) {
if (TypeUtil.isMap(type)) {
int keyStart = offset + 1;
Type domainType = TypeUtil.getDomainType(type);
int keyEnd = -1;
if (typeUtil.isDotable(domainType)) {
// '.' should be interpreted as navigation.
keyEnd = nextNavOp(".[", offset + 1);
} else {
// '.' should *not* be interpreted as navigation.
keyEnd = nextNavOp("[", offset + 1);
}
String key = textBetween(keyStart, keyEnd);
Type keyType = typeUtil.getKeyType(type);
if (keyType != null) {
ValueParser keyParser = typeUtil.getValueParser(keyType);
if (keyParser != null) {
try {
keyParser.parse(key);
} catch (Exception e) {
problemCollector.accept(problem(ApplicationPropertiesProblemType.PROP_VALUE_TYPE_MISMATCH, "Expecting " + typeUtil.niceTypeName(keyType), keyStart, keyEnd - keyStart));
}
}
}
return navigate(keyEnd, domainType);
} else {
// dot navigation into object properties
int keyStart = offset + 1;
int keyEnd = nextNavOp(".[", offset + 1);
if (keyEnd < 0) {
keyEnd = region.getEnd();
}
String key = StringUtil.camelCaseToHyphens(textBetween(keyStart, keyEnd));
List<TypedProperty> properties = typeUtil.getProperties(type, EnumCaseMode.ALIASED, BeanPropertyNameMode.ALIASED);
if (properties != null) {
TypedProperty prop = null;
for (TypedProperty p : properties) {
if (p.getName().equals(key)) {
prop = p;
break;
}
}
if (prop == null) {
problemCollector.accept(problem(ApplicationPropertiesProblemType.PROP_INVALID_BEAN_PROPERTY, "Type '" + typeUtil.niceTypeName(type) + "' has no property '" + key + "'", keyStart, keyEnd - keyStart));
} else {
if (prop.isDeprecated()) {
problemCollector.accept(problemDeprecated(type, prop, keyStart, keyEnd - keyStart));
}
return navigate(keyEnd, prop.getType());
}
}
}
return null;
}
use of org.springframework.ide.vscode.commons.util.ValueParser 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));
}
}
}
use of org.springframework.ide.vscode.commons.util.ValueParser in project sts4 by spring-projects.
the class ManifestYmlValueParsers method healthCheckEndpointPath.
/**
* Parses an HTTP health check endpoint path. Note that this not parse the path portion of an HTTP URI, but verifies
* that the WHOLE value is a valid HTTP path, and therefore needs to start with an '/'
* <p/>
* Example: /appPath, /?check=true, /appPath/test.txt
* @return
*/
public static ValueParser healthCheckEndpointPath() {
return new ValueParser() {
@Override
public Object parse(String pathVal) throws Exception {
String parsed = pathVal;
if (!StringUtil.hasText(pathVal)) {
throw new IllegalArgumentException("Path requires a value staring with '/'");
} else {
URI uri = URI.create(pathVal);
if (uri.getScheme() != null) {
throw new IllegalArgumentException("Path contains scheme: " + uri.getScheme());
}
if (uri.getHost() != null) {
throw new IllegalArgumentException("Path contains host: " + uri.getHost());
}
if (uri.getPort() != -1) {
throw new IllegalArgumentException("Path contains port: " + uri.getPort());
}
if (uri.getAuthority() != null) {
throw new IllegalArgumentException("Path contains authority: " + uri.getAuthority());
}
Path path = Paths.get(uri.getPath());
if (!path.startsWith("/")) {
throw new IllegalArgumentException("Path must start with a '/'");
}
}
return parsed;
}
};
}
Aggregations