/** * @param args * <p>read *.avro file convert into java tree view. return tree */ public Tree generateInputTree() { GenericDatumReader<GenericData> genericReader = new GenericDatumReader<GenericData>(); // DatumReader inputDatumReader = new SpecificDatumReader(UserInfo.class); DataFileReader<GenericData> dataFileReader; Tree root = new Tree(); // root tree for Tree data struture try { /// // home/lali/old/svn/trunck/eclipse/data-mapper/org.wso2.developerstudio.visualdatamapper.diagram/resource/inputs.avro String path = DataMapperCreationWizardPage .avroFilePath; // path for avro file selected in Create Datamapper Diagram swizard dataFileReader = new DataFileReader<GenericData>(new File(path), genericReader); Schema schm = dataFileReader.getSchema(); multipleData = false; root.setName(schm.getName()); List<Field> list = dataFileReader.getSchema().getFields(); Iterator<Field> it = list.iterator(); while (it.hasNext()) { Field field = it.next(); fetchToTree(field, root); } } catch (IOException e) { e.printStackTrace(); } return root; }
/* * for each avro field, search for nested schema. * if field is nested, create tree and recursive * else fetch field as a element */ private static void fetchToTree(Field field, Tree parent) { // System.out.println(" Field: " + field.name()); if (field.schema().getType().toString().equalsIgnoreCase("RECORD")) { // if(!multipleData) // multipleData = true; // Tree child = new Tree(parent); child.setName(field.name()); List<Field> list = field.schema().getFields(); Iterator<Field> it = list.iterator(); while (it.hasNext()) { Field fieldOfField = it.next(); fetchToTree(fieldOfField, child); } parent.getTrees().add(child); } else if (field.schema().getType().getName().equalsIgnoreCase("ARRAY")) { if (field.schema().getElementType().getType().name().toString().equalsIgnoreCase("RECORD")) { if (!multipleData) multipleData = true; Schema arraySchema = field.schema().getElementType(); Tree childParent = new Tree(parent); childParent.setName(field.name()); // employee // parent.getTrees().add(childParent); // Tree child = new Tree(childParent); // child.setName(arraySchema.getName());//employeerecord List<Field> list = arraySchema.getFields(); Iterator<Field> it = list.iterator(); while (it.hasNext()) { Field fieldOfField = it.next(); fetchToTree(fieldOfField, childParent); } parent.getTrees().add(childParent); } } else { Element elementNew = new Element(parent); elementNew.setName(field.name()); parent.getElements().add(elementNew); } // return parent; }
/** @generated NOT */ private void createTree(Tree treeN, TreeNode treeNode) { TreeNode treeNodeNew = DataMapperFactory.eINSTANCE.createTreeNode(); // treeNodeNew.setName(treeN.getCount() + "," + treeN.getName()); treeNodeNew.setName(treeN.getName()); treeNodeNew.setLevel(treeN.getCount()); if (treeN.getSchemaType() != null) { switch (treeN.getSchemaType()) { case ARRAY: treeNodeNew.setSchemaDataType(SchemaDataType.ARRAY); break; case BOOLEAN: treeNodeNew.setSchemaDataType(SchemaDataType.BOOLEAN); break; case BYTES: treeNodeNew.setSchemaDataType(SchemaDataType.BYTES); break; case DOUBLE: treeNodeNew.setSchemaDataType(SchemaDataType.DOUBLE); break; case ENUM: treeNodeNew.setSchemaDataType(SchemaDataType.ENUM); break; case FIXED: treeNodeNew.setSchemaDataType(SchemaDataType.FIXED); break; case FLOAT: treeNodeNew.setSchemaDataType(SchemaDataType.FLOAT); break; case INT: treeNodeNew.setSchemaDataType(SchemaDataType.INT); break; case LONG: treeNodeNew.setSchemaDataType(SchemaDataType.LONG); break; case MAP: treeNodeNew.setSchemaDataType(SchemaDataType.MAP); break; case NULL: treeNodeNew.setSchemaDataType(SchemaDataType.NULL); break; case RECORD: treeNodeNew.setSchemaDataType(SchemaDataType.RECORD); break; case STRING: treeNodeNew.setSchemaDataType(SchemaDataType.STRING); break; case UNION: treeNodeNew.setSchemaDataType(SchemaDataType.UNION); break; default: break; } } treeNode.getNode().add(treeNodeNew); if (!(treeN.getTrees().isEmpty())) { for (Tree treeNew : treeN.getTrees()) { createTree(treeNew, treeNodeNew); } } if (!(treeN.getElements().isEmpty())) { for (dataMapper.diagram.tree.model.Element element : treeN.getElements()) { createElement(element, treeNodeNew); } } if (!(treeN.getAttributes().isEmpty())) { for (dataMapper.diagram.tree.model.Attribute attribute : treeN.getAttributes()) { createAttribute(attribute, treeNodeNew); } } }