use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.
the class XmlFieldReader method getFieldsForPath.
private List<Field> getFieldsForPath(AtlasInternalSession session, Optional<XmlNamespaces> xmlNamespaces, Element node, Field field, XmlPath path, int depth) throws AtlasException {
List<Field> fields = new ArrayList<>();
List<XmlSegmentContext> segments = path.getXmlSegments(false);
if (segments.size() < depth) {
throw new AtlasException(String.format("depth '%s' exceeds segment size '%s'", depth, segments.size()));
}
if (segments.size() == depth) {
if (!(field instanceof XmlEnumField) && field.getFieldType() == FieldType.COMPLEX) {
FieldGroup group = (FieldGroup) field;
populateChildFields(session, xmlNamespaces, node, group, path);
fields.add(group);
} else {
XmlField xmlField = new XmlField();
AtlasXmlModelFactory.copyField(field, xmlField, true);
if (field instanceof XmlEnumField && xmlField.getFieldType() == FieldType.COMPLEX) {
// enum has COMPLEX by default
xmlField.setFieldType(FieldType.STRING);
}
copyValue(session, xmlNamespaces, segments.get(depth - 1), node, xmlField);
// reset index for subfields
xmlField.setIndex(null);
fields.add(xmlField);
}
return fields;
}
// segments.size() > depth
XmlSegmentContext segment = segments.get(depth);
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing segment: " + segment.getName());
}
if (depth == 0) {
if (segment.getName().startsWith(XmlIOHelper.getNodeNameWithoutNamespaceAlias(node))) {
Optional<String> rootNamespace = Optional.empty();
if (segment.getNamespace() != null) {
rootNamespace = getNamespace(xmlNamespaces, segment.getNamespace());
}
if (!rootNamespace.isPresent() || rootNamespace.get().equals(node.getNamespaceURI())) {
// "/XOA/contact<>/firstName", skip.
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping root segment: " + segment);
}
if (segments.size() > 1) {
depth = 1;
segment = segments.get(depth);
}
}
}
}
if (segment.isAttribute() && segments.size() == depth + 1) {
// if last segment is attribute
List<Field> attrFields = getFieldsForPath(session, xmlNamespaces, node, field, path, depth + 1);
fields.addAll(attrFields);
return fields;
}
String fieldName = segment.getName();
String fieldNamespace = segment.getNamespace();
Optional<String> namespace = getNamespace(xmlNamespaces, fieldNamespace);
List<Element> children = XmlIOHelper.getChildrenWithNameStripAlias(fieldName, namespace, node);
if (children == null || children.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping source value set, couldn't find children with name '" + fieldName + "', for segment: " + segment);
}
return fields;
}
if (segment.getCollectionType() == CollectionType.NONE) {
List<Field> childFields = getFieldsForPath(session, xmlNamespaces, children.get(0), field, path, depth + 1);
fields.addAll(childFields);
return fields;
}
// collection
Integer index = segment.getCollectionIndex();
if (index != null) {
if (index < children.size()) {
List<Field> arrayFields = getFieldsForPath(session, xmlNamespaces, children.get(index), field, path, depth + 1);
fields.addAll(arrayFields);
} else if (LOG.isDebugEnabled()) {
LOG.debug("Skipping source value set, children list can't fit index " + index + ", children list size: " + children.size());
}
} else {
// if index not included, iterate over all
for (int i = 0; i < children.size(); i++) {
Field itemField;
if (field instanceof FieldGroup) {
itemField = AtlasXmlModelFactory.cloneFieldGroup((FieldGroup) field);
AtlasPath.setCollectionIndexRecursively((FieldGroup) itemField, depth + 1, i);
} else {
itemField = AtlasXmlModelFactory.cloneField((XmlField) field, false);
AtlasPath itemPath = new AtlasPath(field.getPath());
itemPath.setCollectionIndex(depth + 1, i);
itemField.setPath(itemPath.toString());
}
List<Field> arrayFields = getFieldsForPath(session, xmlNamespaces, children.get(i), itemField, new XmlPath(itemField.getPath()), depth + 1);
fields.addAll(arrayFields);
}
}
return fields;
}
use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.
the class KafkaConnectFieldReader method populateCollectionItems.
private FieldGroup populateCollectionItems(AtlasInternalSession session, List<Object> values, Field field) throws AtlasException {
FieldGroup group = AtlasModelFactory.createFieldGroupFrom(field, true);
for (int i = 0; i < values.size(); i++) {
AtlasPath itemPath = new AtlasPath(group.getPath());
List<SegmentContext> segments = itemPath.getSegments(true);
itemPath.setCollectionIndex(segments.size() - 1, i);
if (field instanceof FieldGroup) {
FieldGroup itemGroup = AtlasKafkaConnectModelFactory.cloneFieldGroup((FieldGroup) field);
AtlasPath.setCollectionIndexRecursively(itemGroup, segments.size() - 1, i);
populateChildFields(session, (Struct) values.get(i), itemGroup);
group.getField().add(itemGroup);
} else {
KafkaConnectField itemField = AtlasKafkaConnectModelFactory.cloneField((KafkaConnectField) field, false);
itemField.setPath(itemPath.toString());
Object converted = conversionService.convertType(values.get(i), itemField.getFormat(), itemField.getFieldType(), null);
itemField.setValue(converted);
group.getField().add(itemField);
}
}
return group;
}
use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.
the class KafkaConnectFieldReader method nestComplexCollection.
private void nestComplexCollection(AtlasInternalSession session, List<Object> collection, FieldGroup parent, int depth) throws AtlasException {
AtlasPath path = new AtlasPath(parent.getPath());
SegmentContext segment = path.getSegments(true).get(depth);
if (segment.getCollectionIndex() != null) {
int index = segment.getCollectionIndex();
Struct struct = (Struct) collection.get(index);
populateChildFields(session, struct, parent);
return;
}
List<Field> processed = new LinkedList<>();
for (int index = 0; index < collection.size(); index++) {
FieldGroup itemGroup = AtlasKafkaConnectModelFactory.cloneFieldGroup(parent);
AtlasPath.setCollectionIndexRecursively(itemGroup, depth, index);
processed.add(itemGroup);
Struct struct = (Struct) collection.get(index);
populateChildFields(session, struct, itemGroup);
}
parent.getField().clear();
parent.getField().addAll(processed);
}
use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.
the class KafkaConnectInspector method createDocument.
private KafkaConnectDocument createDocument(org.apache.kafka.connect.data.Schema schema) {
KafkaConnectDocument doc = AtlasKafkaConnectModelFactory.createKafkaConnectDocument();
doc.setRootSchemaType(schema.type());
doc.setName(schema.name());
Schema connectSchema = schema;
AtlasPath path;
if (Type.ARRAY == connectSchema.type()) {
path = new AtlasPath(AtlasPath.PATH_SEPARATOR + AtlasPath.PATH_LIST_SUFFIX);
doc.setCollectionType(CollectionType.LIST);
connectSchema = connectSchema.valueSchema();
} else if (Type.MAP == connectSchema.type()) {
path = new AtlasPath(AtlasPath.PATH_SEPARATOR + AtlasPath.PATH_MAP_SUFFIX);
doc.setCollectionType(CollectionType.MAP);
connectSchema = connectSchema.valueSchema();
} else {
path = new AtlasPath("");
}
doc.setPath(path.toString());
if (connectSchema.parameters() != null) {
doc.setEnumeration(true);
List<KafkaConnectEnumField> symbols = doc.getEnumFields().getKafkaConnectEnumField();
for (Entry<String, String> entry : connectSchema.parameters().entrySet()) {
if ("io.confluent".equals(entry.getKey())) {
continue;
}
KafkaConnectEnumField f = new KafkaConnectEnumField();
f.setName(entry.getValue());
symbols.add(f);
}
doc.setFieldType(KafkaConnectUtil.getFieldType(connectSchema.type()));
} else if (!connectSchema.type().isPrimitive()) {
doc.setFieldType(FieldType.COMPLEX);
List<KafkaConnectField> children = populateFields(connectSchema.fields(), path);
doc.getFields().getField().addAll(children);
} else {
doc.setFieldType(KafkaConnectUtil.getFieldType(connectSchema.type()));
}
return doc;
}
use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.
the class KafkaConnectInspector method populateFields.
private List<KafkaConnectField> populateFields(List<org.apache.kafka.connect.data.Field> kcFields, AtlasPath parentPath) {
List<KafkaConnectField> answer = new ArrayList<>();
for (org.apache.kafka.connect.data.Field connectField : kcFields) {
KafkaConnectField field;
AtlasPath path = parentPath.clone();
CollectionType collectionType = CollectionType.NONE;
Schema connectSchema = connectField.schema();
if (Type.ARRAY == connectSchema.type()) {
path.appendField(connectField.name() + AtlasPath.PATH_LIST_SUFFIX);
collectionType = CollectionType.LIST;
connectSchema = connectSchema.valueSchema();
} else if (Type.MAP == connectSchema.type()) {
path.appendField(connectField.name() + AtlasPath.PATH_MAP_SUFFIX);
collectionType = CollectionType.MAP;
connectSchema = connectSchema.valueSchema();
} else {
path.appendField(connectField.name());
}
if (connectSchema.parameters() != null) {
KafkaConnectComplexType complex = AtlasKafkaConnectModelFactory.createKafkaConnectComplexType();
complex.setKafkaConnectEnumFields(new KafkaConnectEnumFields());
complex.setEnumeration(true);
List<KafkaConnectEnumField> symbols = complex.getKafkaConnectEnumFields().getKafkaConnectEnumField();
boolean first = true;
for (Entry<String, String> entry : connectSchema.parameters().entrySet()) {
// FIXME dirty hack - it seems the first entry is for some control, the others are the actual values
if (first) {
first = false;
continue;
}
KafkaConnectEnumField f = new KafkaConnectEnumField();
f.setName(entry.getValue());
symbols.add(f);
}
field = complex;
} else if (connectSchema.type().isPrimitive()) {
field = AtlasKafkaConnectModelFactory.createKafkaConnectField();
field.setFieldType(KafkaConnectUtil.getFieldType(connectSchema.type()));
} else {
KafkaConnectComplexType complex = AtlasKafkaConnectModelFactory.createKafkaConnectComplexType();
List<KafkaConnectField> children = populateFields(connectSchema.fields(), path);
if ("io.confluent.connect.avro.Union".equals(connectSchema.name())) {
// We don't support union until it's built into Kafka Connect Schema
complex.setStatus(FieldStatus.UNSUPPORTED);
}
complex.getKafkaConnectFields().getKafkaConnectField().addAll(children);
field = complex;
}
field.setName(connectField.name());
field.setPath(path.toString());
field.setCollectionType(collectionType);
answer.add(field);
}
return answer;
}
Aggregations