use of org.apache.commons.lang3.StringUtils.split in project oap by oaplatform.
the class SchemaPath method traverse.
public static Result traverse(SchemaAST root, String path) {
SchemaAST schemaAST = root;
val additionalProperties = new MutableObject<Boolean>(null);
final Supplier<Result> empty = () -> new Result(Optional.empty(), Optional.ofNullable(additionalProperties.getValue()));
for (val item : StringUtils.split(path, '.')) {
if (schemaAST instanceof ObjectSchemaAST) {
val objectSchemaAST = (ObjectSchemaAST) schemaAST;
schemaAST = objectSchemaAST.properties.get(item);
objectSchemaAST.additionalProperties.ifPresent(additionalProperties::setValue);
if (schemaAST == null)
return empty.get();
} else if (schemaAST instanceof ArraySchemaAST) {
if (!"items".equals(item))
return empty.get();
schemaAST = ((ArraySchemaAST) schemaAST).items;
} else {
return empty.get();
}
}
return new Result(Optional.of(schemaAST), Optional.ofNullable(additionalProperties.getValue()));
}
use of org.apache.commons.lang3.StringUtils.split in project oap by oaplatform.
the class JavaCTemplate method addPathOr.
private void addPathOr(Class<T> clazz, String delimiter, StringBuilder c, AtomicInteger num, FieldStack fields, boolean last, AtomicInteger tab, String[] orPath, int orIndex, TLine line) throws NoSuchFieldException, NoSuchMethodException {
val currentPath = orPath[orIndex].trim();
val m = mapper.get(currentPath);
if (m != null) {
printDefaultValue(tab(c, tab), m.get(), line);
} else {
int sp = 0;
StringBuilder newPath = new StringBuilder("s.");
MutableObject<Type> lc = new MutableObject<>(clazz);
AtomicInteger psp = new AtomicInteger(0);
AtomicInteger opts = new AtomicInteger(0);
while (sp >= 0) {
sp = currentPath.indexOf('.', sp + 1);
if (sp > 0) {
Pair<Type, String> pnp = fields.computeIfAbsent(currentPath.substring(0, sp), Try.map((key) -> {
String prefix = StringUtils.trim(psp.get() > 1 ? key.substring(0, psp.get() - 1) : "");
String suffix = StringUtils.trim(key.substring(psp.get()));
boolean optional = isOptional(lc.getValue());
Type declaredFieldType = getDeclaredFieldOrFunctionType(optional ? getOptionalArgumentType(lc.getValue()) : lc.getValue(), suffix);
String classType = toJavaType(declaredFieldType);
String field = "field" + num.incrementAndGet();
Optional<Pair<Type, String>> rfP = Optional.ofNullable(fields.get(prefix));
String rf = rfP.map(p -> p._2 + (optional ? ".get()" : "") + "." + suffix).orElse("s." + key);
if (optional) {
tab(c, tab).append("if( ").append(rfP.map(p -> p._2).orElse("s")).append(".isPresent() ) {\n");
opts.incrementAndGet();
tabInc(tab);
fields.up();
}
tab(c, tab).append(" ").append(classType).append(" ").append(field).append(" = ").append(rf).append(";\n");
lc.setValue(declaredFieldType);
return __(lc.getValue(), field);
}));
newPath = new StringBuilder(pnp._2 + ".");
lc.setValue(pnp._1);
psp.set(sp + 1);
} else {
newPath.append(currentPath.substring(psp.get()));
}
}
int in = newPath.toString().lastIndexOf('.');
String pField = in > 0 ? newPath.substring(0, in) : newPath.toString();
String cField = newPath.substring(in + 1);
boolean isJoin = cField.startsWith("{");
String[] cFields = isJoin ? StringUtils.split(cField.substring(1, cField.length() - 1), ',') : new String[] { cField };
Type parentClass = lc.getValue();
boolean isOptionalParent = isOptional(parentClass) && !cField.startsWith("isPresent");
String optField = null;
if (isOptionalParent) {
opts.incrementAndGet();
tab(c, tab).append("if( ").append(pField).append(".isPresent() ) {\n");
fields.up();
tabInc(tab);
parentClass = getOptionalArgumentType(parentClass);
optField = "opt" + num.incrementAndGet();
tab(c, tab).append(" ").append(toJavaType(parentClass)).append(" ").append(optField).append(" = ").append(pField).append(".get();\n");
}
for (int i = 0; i < cFields.length; i++) {
cField = StringUtils.trim(cFields[i]);
Optional<Join> join = isJoin ? Optional.of(new Join(i, cFields.length)) : Optional.empty();
if (cField.startsWith("\"")) {
tab(c.append("\n"), tab);
map.map(c, String.class, line, cField, delimiter, join);
} else {
if (isOptionalParent) {
newPath = new StringBuilder(in > 0 ? optField + "." + cField : cField);
} else {
newPath = new StringBuilder(in > 0 ? pField + "." + cField : "s." + cField);
}
Type cc = in > 0 ? getDeclaredFieldOrFunctionType(parentClass, cField) : parentClass;
add(c, num, newPath.toString(), cc, parentClass, true, tab, orPath, orIndex, clazz, delimiter, fields, last || (i < cFields.length - 1), line, join);
}
}
c.append("\n");
for (int i = 0; i < opts.get(); i++) {
fields.down();
tabDec(tab);
tab(c, tab).append("} else {\n");
fields.up();
tabInc(tab);
if (orIndex + 1 < orPath.length) {
addPathOr(clazz, delimiter, c, num, fields, last, new AtomicInteger(tab.get() + 2), orPath, orIndex + 1, line);
} else {
printDefaultValue(c, line.defaultValue, line);
if (!map.ignoreDefaultValue())
printDelimiter(delimiter, c, last, tab);
}
tabDec(tab);
fields.down();
tab(c, tab).append("}\n");
}
}
}
use of org.apache.commons.lang3.StringUtils.split in project eol-globi-data by jhpoelen.
the class StudyImporterForHurlbert method importInteraction.
protected void importInteraction(Set<String> regions, Set<String> locales, Set<String> habitats, Record record, Study study, String preyTaxonName, String predatorName) throws StudyImporterException {
try {
Taxon predatorTaxon = new TaxonImpl(predatorName);
Specimen predatorSpecimen = nodeFactory.createSpecimen(study, predatorTaxon);
setBasisOfRecordAsLiterature(predatorSpecimen);
Taxon preyTaxon = new TaxonImpl(preyTaxonName);
String preyNameId = StringUtils.trim(columnValueOrNull(record, "Prey_Name_ITIS_ID"));
if (NumberUtils.isDigits(preyNameId)) {
preyTaxon.setExternalId(TaxonomyProvider.ITIS.getIdPrefix() + preyNameId);
}
Specimen preySpecimen = nodeFactory.createSpecimen(study, preyTaxon);
setBasisOfRecordAsLiterature(preySpecimen);
String preyStage = StringUtils.trim(columnValueOrNull(record, "Prey_Stage"));
if (StringUtils.isNotBlank(preyStage)) {
Term lifeStage = nodeFactory.getOrCreateLifeStage("HULBERT:" + StringUtils.replace(preyStage, " ", "_"), preyStage);
preySpecimen.setLifeStage(lifeStage);
}
String preyPart = StringUtils.trim(columnValueOrNull(record, "Prey_Part"));
if (StringUtils.isNotBlank(preyPart)) {
Term term = nodeFactory.getOrCreateBodyPart("HULBERT:" + StringUtils.replace(preyPart, " ", "_"), preyPart);
preySpecimen.setBodyPart(term);
}
Date date = addCollectionDate(record, study);
nodeFactory.setUnixEpochProperty(predatorSpecimen, date);
nodeFactory.setUnixEpochProperty(preySpecimen, date);
LocationImpl location = new LocationImpl(null, null, null, null);
String longitude = columnValueOrNull(record, "Longitude_dd");
String latitude = columnValueOrNull(record, "Latitude_dd");
if (NumberUtils.isNumber(latitude) && NumberUtils.isNumber(longitude)) {
try {
LatLng latLng = LocationUtil.parseLatLng(latitude, longitude);
String altitude = columnValueOrNull(record, "Altitude_mean_m");
Double altitudeD = NumberUtils.isNumber(altitude) ? Double.parseDouble(altitude) : null;
location = new LocationImpl(latLng.getLat(), latLng.getLng(), altitudeD, null);
} catch (InvalidLocationException e) {
getLogger().warn(study, "found invalid (lat,lng) pair: (" + latitude + "," + longitude + ")");
}
}
String locationRegion = columnValueOrNull(record, "Location_Region");
String locationSpecific = columnValueOrNull(record, "Location_Specific");
location.setLocality(StringUtils.join(Arrays.asList(locationRegion, locationSpecific), ":"));
Location locationNode = nodeFactory.getOrCreateLocation(location);
String habitat_type = columnValueOrNull(record, "Habitat_type");
List<Term> habitatList = Arrays.stream(StringUtils.split(StringUtils.defaultIfBlank(habitat_type, ""), ";")).map(StringUtils::trim).map(habitat -> new TermImpl(idForHabitat(habitat), habitat)).collect(Collectors.toList());
nodeFactory.addEnvironmentToLocation(locationNode, habitatList);
preySpecimen.caughtIn(locationNode);
predatorSpecimen.caughtIn(locationNode);
predatorSpecimen.ate(preySpecimen);
} catch (NodeFactoryException e) {
throw new StudyImporterException("failed to create interaction between [" + predatorName + "] and [" + preyTaxonName + "]", e);
}
}
use of org.apache.commons.lang3.StringUtils.split in project molgenis by molgenis.
the class RestService method convertMref.
private List<?> convertMref(Attribute attr, Object paramValue) {
List<?> value;
if (paramValue != null) {
List<?> mrefParamValues;
if (paramValue instanceof String) {
mrefParamValues = asList(StringUtils.split((String) paramValue, ','));
} else if (paramValue instanceof List<?>) {
mrefParamValues = (List<?>) paramValue;
} else {
throw new MolgenisDataException(format("Attribute [%s] value is of type [%s] instead of [%s] or [%s]", attr.getName(), paramValue.getClass().getSimpleName(), String.class.getSimpleName(), List.class.getSimpleName()));
}
EntityType mrefEntity = attr.getRefEntity();
Attribute mrefEntityIdAttr = mrefEntity.getIdAttribute();
value = mrefParamValues.stream().map(mrefParamValue -> toEntityValue(mrefEntityIdAttr, mrefParamValue, null)).map(mrefIdValue -> entityManager.getReference(mrefEntity, mrefIdValue)).collect(toList());
} else {
value = emptyList();
}
return value;
}
use of org.apache.commons.lang3.StringUtils.split in project spring-roo by spring-projects.
the class RepositoryJpaCustomImplMetadataProviderImpl method buildFieldNamesMap.
/**
* Build a Map<String, String> with field names and "path" field names
* and adds it to the typesFieldMaps Map.
*
* @param entity
* @param projection
* @param entityProjectionAnnotation
* @param typesFieldMaps
*/
private void buildFieldNamesMap(JavaType entity, JavaType projection, AnnotationMetadata entityProjectionAnnotation, Map<JavaType, List<Pair<String, String>>> typesFieldMaps) {
List<Pair<String, String>> projectionFieldNames = new ArrayList<Pair<String, String>>();
if (!typesFieldMaps.containsKey(projection)) {
AnnotationAttributeValue<?> projectionFields = entityProjectionAnnotation.getAttribute("fields");
if (projectionFields != null) {
@SuppressWarnings("unchecked") List<StringAttributeValue> values = (List<StringAttributeValue>) projectionFields.getValue();
// Get entity name as a variable name for building constructor expression
String entityVariableName = StringUtils.uncapitalize(entity.getSimpleTypeName());
for (StringAttributeValue field : values) {
String[] splittedByDot = StringUtils.split(field.getValue(), ".");
StringBuffer propertyName = new StringBuffer();
for (int i = 0; i < splittedByDot.length; i++) {
if (i == 0) {
propertyName.append(splittedByDot[i]);
} else {
propertyName.append(StringUtils.capitalize(splittedByDot[i]));
}
}
String pathName = entityVariableName.concat(".").concat(field.getValue());
projectionFieldNames.add(Pair.of(propertyName.toString(), pathName));
typesFieldMaps.put(projection, projectionFieldNames);
}
}
}
}
Aggregations