use of com.google.common.collect.Maps.EntryTransformer in project hale by halestudio.
the class KVPUtil method addTypeNameParameter.
/**
* Add typename and namespace parameters for a WFS request to the given URI
* builder.
*
* @param builder the builder for the WFS request URI
* @param selected the type names to include
* @param version the targeted WFS version
*/
public static void addTypeNameParameter(URIBuilder builder, Iterable<QName> selected, WFSVersion version) {
// namespaces mapped to prefixes
Map<String, String> namespaces = new HashMap<>();
// type names with updated prefix
Set<QName> typeNames = new HashSet<>();
for (QName type : selected) {
String prefix;
if (type.getNamespaceURI() != null && !type.getNamespaceURI().isEmpty()) {
prefix = namespaces.get(type.getNamespaceURI());
if (prefix == null) {
// no mapping yet for namespace
String candidate = type.getPrefix();
prefix = addPrefix(candidate, type.getNamespaceURI(), namespaces);
}
} else {
// default namespace
prefix = XMLConstants.DEFAULT_NS_PREFIX;
}
// add updated type
typeNames.add(new QName(type.getNamespaceURI(), type.getLocalPart(), prefix));
}
final String paramNamespaces;
final String paramTypenames;
final String prefixNamespaceDelim;
switch(version) {
case V1_1_0:
paramNamespaces = "NAMESPACE";
paramTypenames = "TYPENAME";
prefixNamespaceDelim = "=";
break;
case V2_0_0:
case V2_0_2:
/*
* XXX below are the values as defined in the WFS 2 specification.
* There have been problems with some GeoServer instances if used in
* that manner.
*/
paramNamespaces = "NAMESPACES";
paramTypenames = "TYPENAMES";
prefixNamespaceDelim = ",";
break;
default:
// fall-back to WFS 1.1
paramNamespaces = "NAMESPACE";
paramTypenames = "TYPENAME";
prefixNamespaceDelim = "=";
}
// add namespace prefix definitions
if (!namespaces.isEmpty()) {
builder.addParameter(paramNamespaces, Joiner.on(',').join(Maps.transformEntries(namespaces, new EntryTransformer<String, String, String>() {
@Override
public String transformEntry(String namespace, String prefix) {
StringBuilder sb = new StringBuilder();
sb.append("xmlns(");
sb.append(prefix);
sb.append(prefixNamespaceDelim);
sb.append(namespace);
sb.append(")");
return sb.toString();
}
}).values()));
}
// add type names
if (!typeNames.isEmpty()) {
builder.addParameter(paramTypenames, Joiner.on(',').join(Iterables.transform(typeNames, new Function<QName, String>() {
@Override
public String apply(QName typeName) {
String prefix = typeName.getPrefix();
if (prefix == null || prefix.isEmpty()) {
return typeName.getLocalPart();
}
return prefix + ":" + typeName.getLocalPart();
}
})));
}
}
use of com.google.common.collect.Maps.EntryTransformer in project hale by halestudio.
the class DefaultCellMigrator method updateCell.
@Override
public MutableCell updateCell(final Cell originalCell, final AlignmentMigration migration, final MigrationOptions options, SimpleLog log) {
MutableCell result = new DefaultCell(originalCell);
SimpleLog cellLog = SimpleLog.all(log, new CellLog(result, CELL_LOG_CATEGORY));
final AtomicBoolean replacedEntities = new AtomicBoolean(false);
EntryTransformer<String, Entity, Entity> entityTransformer = new EntryTransformer<String, Entity, Entity>() {
@Override
public Entity transformEntry(String key, Entity value) {
EntityDefinition org = value.getDefinition();
Optional<EntityDefinition> replace = migration.entityReplacement(org, cellLog);
EntityDefinition entity = replace.orElse(org);
if (!Objects.equal(entity, org)) {
replacedEntities.set(true);
}
if (entity instanceof PropertyEntityDefinition) {
return new DefaultProperty((PropertyEntityDefinition) entity);
} else if (entity instanceof TypeEntityDefinition) {
return new DefaultType((TypeEntityDefinition) entity);
} else {
throw new IllegalStateException("Invalid entity definition for creating entity");
}
}
};
// update source entities
if (options.updateSource() && result.getSource() != null && !result.getSource().isEmpty()) {
result.setSource(ArrayListMultimap.create(Multimaps.transformEntries(result.getSource(), entityTransformer)));
}
// update target entities
if (options.updateTarget() && result.getTarget() != null && !result.getTarget().isEmpty()) {
result.setTarget(ArrayListMultimap.create(Multimaps.transformEntries(result.getTarget(), entityTransformer)));
}
// TODO anything else?
postUpdateCell(result, options, replacedEntities.get(), cellLog);
return result;
}
Aggregations