convert xml to java hashmap jsoup - java

I am trying to convert any given xml to hash map. I know this can somehow be done using JAXB. I was trying using jsoup. My code is below
public static Map<String,Object> xmlToMapAll(String xml){
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
try{
Document xmlDoc = Jsoup.parse(xml, "", Parser.xmlParser());
Elements eles =xmlDoc.getAllElements();
for(Element ele: eles){
Map<String,Object> mi = new HashMap<String,Object>();
if(ele.children().size()>1){
mi = getChilds(ele.children());
}else{
mi.put(ele.tagName(), ele.ownText());
}
list.add(mi);
//map.putAll(mi);
}
map.put("data", list);
map.put("Status", "SUCCESS");
}catch(Exception ce){
log.error("IndoXMLParseUtil.xmlToMapAll() ce "+IndoUtil.getFullLog(ce));
}
return map;
}
public static Map<String,Object> getChilds(Elements childs){
Map<String,Object> map = new HashMap<String,Object>();
for(Element child: childs){
if(child.children().size()>0){
map = getChilds(child.children());
}else{
map.put(child.tagName(), child.ownText());
}
}
return map;
}
public static void main(String args[]){
String xml="<ExtMessage xmlns=\"com/test/schema/evExtQMainPkgQuotaResp\">
<ExtQMainPkgQuotaResp>
<ServiceNumber>1234567</ServiceNumber>
<Source><a>10</a><b>11</b><a>12</a></Source>
<Status>Success</Status>
<ErrorMessage/><InitialQuota>2621440</InitialQuota>
<UsedQuota>62859.49</UsedQuota>
</ExtQMainPkgQuotaResp> </ExtMessage> ";
Map<String, Object> ds = xmlToMapAll(xml);
System.out.println("IndoXMLParseUtil.main() "+ds);
}
output:
{Status=SUCCESS, data=[{#root=}, {extmessage=}, {errormessage=, b=11,
status=Success, a=12, initialquota=2621440, usedquota=62859.49},
{servicenumber=6285770355730}, {b=11, a=12}, {a=10}, {b=11}, {a=12},
{status=Success}, {errormessage=}, {initialquota=2621440},
{usedquota=62859.49}]}
Problem is I am getting repeated data. Also I believe there are always better ideas out here.

This is an simple example using W3C DOM parser, so there's a lot that can be improved.
Also, you'll have to define a criteria to resolve possible key collission. With this code, only the last <a> from <Source> will be saved to the map.
public static Map<String, Object> xmlToMapAll(String xml) throws ParserConfigurationException
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
try
{
InputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
Document xmlDoc = db.parse(stream);
Element rootNode = xmlDoc.getDocumentElement();
NodeList eles = rootNode.getChildNodes();
for (int i = 0; i < eles.getLength(); i++)
{
Node ele = (Node) eles.item(i);
Map<String, Object> mi = new HashMap<String, Object>();
if (ele.getChildNodes().getLength() > 1 && ele.getNodeType() == Node.ELEMENT_NODE)
{
mi.put(ele.getNodeName(), getChilds(ele.getChildNodes()));
}
else
{
Node subChild = ele.getFirstChild();
if (subChild != null)
{
if (!subChild.hasChildNodes())
{
mi.put(ele.getNodeName(), subChild.getNodeValue());
}
else
{
mi.put(ele.getNodeName(), "");
}
}
}
if (!mi.isEmpty())
{
list.add(mi);
}
}
map.put("data", list);
map.put("Status", "SUCCESS");
}
catch (Exception ce)
{
log.error("IndoXMLParseUtil.xmlToMapAll() ce " + IndoUtil.getFullLog(ce));
}
return map;
}
public static Map<String, Object> getChilds(NodeList childs)
{
Map<String, Object> map = new HashMap<String, Object>();
for (int a = 0; a < childs.getLength(); a++)
{
Node child = (Node) childs.item(a);
if (child.getNodeType() == Node.ELEMENT_NODE)
{
if (child.getChildNodes().getLength() > 1)
{
map.put(child.getNodeName(), getChilds(child.getChildNodes()));
}
else
{
Node subChild = child.getFirstChild();
if (subChild != null && !subChild.hasChildNodes())
{
map.put(child.getNodeName(), subChild.getNodeValue());
}
else
{
map.put(child.getNodeName(), "");
}
}
}
}
return map;
}
public static String getNodeValue(Node node)
{
String result = "";
final NodeList childs = node.getChildNodes();
for (int i = 0; i < childs.getLength(); i++)
{
final Node child = childs.item(i);
if (child != null)
{
if ((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))
{
result += child.getNodeValue().trim();
}
}
}
return result;
}

Related

How to put an Ontology in Javafx TreeView with OWL API?

I tried first to put all Classe(key) and SuperClasses(value :ArrayList, direct superClasses) of the ontology (I want that the last element of the ontology be the root of my treeView) in a HashMap (hm).
However I don't know how to put properly this hashmap in a TreeView.
A recursive tree would suit much better but I don't know how to do that. (I have a method who convert a recursive data structure in a treeView)
Would anyone be able to give me some clues for my problem?
Thanks !
method who return the hashmap :
public HashMap<String, ArrayList<String>> getClassesHashMap() {
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
OWLReasoner reasonerFactory3 = reasonerFactory.createReasoner(ontology);
OWLDataFactory fac3 = ontology.getOWLOntologyManager().getOWLDataFactory();
for (String s : getClassesName()) {
IRI docIRI = IRI.create(ontology.getOntologyID().getOntologyIRI().get() + "#" + s);
OWLClass pizza = fac3.getOWLClass(docIRI);
NodeSet<OWLClass> subClses = reasonerFactory3.getSuperClasses(pizza, true);
Set<OWLClass> clses = subClses.getFlattened();
System.out.println("Subclasses of " + s + " : ");
//instanciate the hashmap
for (OWLClass cls1 : clses) {
//cls1.getIRI().getShortForm() is the name of an ontology classes in String
//all the classe is a key, and their superclasses is the value
if (!cls1.getIRI().getShortForm().equals("Thing")) {
if (!hm.containsKey(s)) {
hm.put(s, new ArrayList<String>());
}
hm.get(s).add(cls1.getIRI().getShortForm());
System.out.println(" " + cls1.getIRI().getShortForm());
}
System.out.println();
}
}
// Tree<String> res = new Tree<String>("");
return hm;
}
Method who instanciate the TreeView
private void initTreeView() throws OWLException {
// TreeItem<String> root = new TreeItem<String>("Private_Policy");
tree.setFixedCellSize(25);
//Privacy Policy is the Ontology classe I Use
TreeItem<String> root = new TreeItem<String>("PrivacyPolicy");
root.setExpanded(true);
OntologieDAO ont = new OntologieDAO("WotPriv.owl");
//hashmap of the ontology
HashMap<String, ArrayList<String>> hm = ont.getClassesHashMap();
TreeItem<String> children = new TreeItem<String>("");
System.out.println(hm);
int i = 0;
//work only for my ontology
for (String s : hm.get(root.getValue())) {
TreeItem<String> tI = new TreeItem<String>(s);
root.getChildren().add(tI);
if (hm.containsKey(s)) {
for (String s1 : hm.get(s)) {
TreeItem<String> n = new TreeItem<String>(s1);
TreeItem<String> tI1=root.getChildren().get(root.getChildren().indexOf(tI));
tI1.getChildren().add(n);
if (hm.containsKey(s1)) {
for (String s2 : hm.get(s1)) {
tI1.getChildren().get(tI1.getChildren().indexOf(n)).getChildren().add(new TreeItem<String>(s2));
}}
}
}
}
tree.setRoot(root);
tree.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
System.out.println(ont.getClassesName());
}
A recursive data structure and a method to convert it in treeview :
import java.util.HashSet;
import java.util.Set;
public class MyType<N> {
private Set<MyType<N>> children = new HashSet<>();
private N Value;
public MyType(N value) {
super();
Value = value;
}
public Set<MyType<N>> getChildren() {
return children;
}
public void setChildren(Set<MyType<N>> children) {
this.children = children;
}
public N getValue() {
return Value;
}
public void setValue(N value) {
Value = value;
}
}
private TreeItem<MyType<String>> buildSubtree(MyType<String> root) {
TreeItem<MyType<String>> result = new TreeItem<>(root);
if (root.getChildren() != null) {
for (MyType<String> child : root.getChildren()) {
result.getChildren().add(buildSubtree(child));
}
}
return result;
}
(Image) Result I want to have (I already have it but my code is really dirty and would'nt work with other ontology)

How to retrieve tables which exists in a pdf using AWS Textract in java

I found article below to do in python.
https://docs.aws.amazon.com/textract/latest/dg/examples-export-table-csv.html
also I used article below to extract text.
https://docs.aws.amazon.com/textract/latest/dg/detecting-document-text.html
but above article helped to get only text, I also used function "block.getBlockType()"
of Block but none of block returned its type as "CELL" even tables are there in image/pdf.
Help me found java library similar to "boto3" to extract all tables.
What I did, I created models of each dataset in the json response and can use this models to build a table view in jsf.
public static List<TableModel> getTablesFromTextract(TextractModel textractModel) {
List<TableModel> tables = null;
try {
if (textractModel != null) {
tables = new ArrayList<>();
List<BlockModel> tableBlocks = new ArrayList<>();
Map<String, BlockModel> blockMap = new HashMap<>();
for (BlockModel block : textractModel.getBlocks()) {
if (block.getBlockType().equals("TABLE")) {
tableBlocks.add(block);
}
blockMap.put(block.getId(), block);
}
for (BlockModel blockModel : tableBlocks) {
Map<Long, Map<Long, String>> rowMap = new HashMap<>();
for (RelationshipModel relationship : blockModel.getRelationships()) {
if (relationship.getType().equals("CHILD")) {
for (String id : relationship.getIds()) {
BlockModel cell = blockMap.get(id);
if (cell.getBlockType().equals("CELL")) {
long rowIndex = cell.getRowIndex();
long columnIndex = cell.getColumnIndex();
if (!rowMap.containsKey(rowIndex)) {
rowMap.put(rowIndex, new HashMap<>());
}
Map<Long, String> columnMap = rowMap.get(rowIndex);
columnMap.put(columnIndex, getCellText(cell, blockMap));
}
}
}
}
tables.add(new TableModel(blockModel, rowMap));
}
System.out.println("row Map " + tables.toString());
}
} catch (Exception e) {
LOG.error("Could not get table from textract model", e);
}
return tables;
}
private static String getCellText(BlockModel cell, Map<String, BlockModel> blockMap) {
String text = "";
try {
if (cell != null
&& CollectionUtils.isNotEmpty(cell.getRelationships())) {
for (RelationshipModel relationship : cell.getRelationships()) {
if (relationship.getType().equals("CHILD")) {
for (String id : relationship.getIds()) {
BlockModel word = blockMap.get(id);
if (word.getBlockType().equals("WORD")) {
text += word.getText() + " ";
} else if (word.getBlockType().equals("SELECTION_ELEMENT")) {
if (word.getSelectionStatus().equals("SELECTED")) {
text += "X ";
}
}
}
}
}
}
} catch (Exception e) {
LOG.error("Could not get cell text of table", e);
}
return text;
}
TableModel to create the view from:
public class TableModel {
private BlockModel table;
private Map<Long, Map<Long, String>> rowMap;
public TableModel(BlockModel table, Map<Long, Map<Long, String>> rowMap) {
this.table = table;
this.rowMap = rowMap;
}
public BlockModel getTable() {
return table;
}
public void setTable(BlockModel table) {
this.table = table;
}
public Map<Long, Map<Long, String>> getRowMap() {
return rowMap;
}
public void setRowMap(Map<Long, Map<Long, String>> rowMap) {
this.rowMap = rowMap;
}
#Override
public String toString() {
return table.getId() + " - " + rowMap.toString();
}
I have something similar:
public class AnalyzeDocument {
public DocumentModel startProcess(byte[] content) {
Region region = Region.EU_WEST_2;
TextractClient textractClient = TextractClient.builder().region(region)
.credentialsProvider(EnvironmentVariableCredentialsProvider.create()).build();
return analyzeDoc(textractClient, content);
}
public DocumentModel analyzeDoc(TextractClient textractClient, byte[] content) {
try {
SdkBytes sourceBytes = SdkBytes.fromByteArray(content);
Util util = new Util();
Document myDoc = Document.builder().bytes(sourceBytes).build();
List<FeatureType> featureTypes = new ArrayList<FeatureType>();
featureTypes.add(FeatureType.FORMS);
featureTypes.add(FeatureType.TABLES);
AnalyzeDocumentRequest analyzeDocumentRequest = AnalyzeDocumentRequest.builder().featureTypes(featureTypes)
.document(myDoc).build();
AnalyzeDocumentResponse analyzeDocument = textractClient.analyzeDocument(analyzeDocumentRequest);
List<Block> docInfo = analyzeDocument.blocks();
// util.displayBlockInfo(docInfo);
PageModel pageModel = util.getTableResults(docInfo);
DocumentModel documentModel = new DocumentModel();
documentModel.getPages().add(pageModel);
Iterator<Block> blockIterator = docInfo.iterator();
while (blockIterator.hasNext()) {
Block block = blockIterator.next();
log.debug("The block type is " + block.blockType().toString());
}
return documentModel;
} catch (TextractException e) {
System.err.println(e.getMessage());
}
return null;
}
and this is the util file:
public PageModel getTableResults(List<Block> blocks) {
List<Block> tableBlocks = new ArrayList<>();
Map<String, Block> blockMap = new HashMap<>();
for (Block block : blocks) {
blockMap.put(block.id(), block);
if (block.blockType().equals(BlockType.TABLE)) {
tableBlocks.add(block);
log.debug("added table: " + block.text());
}
}
PageModel page = new PageModel();
if (tableBlocks.size() == 0) {
return null;
}
int i = 0;
for (Block table : tableBlocks) {
page.getTables().add(generateTable(table, blockMap, i++));
}
return page;
}
private TableModel generateTable(Block table, Map<String, Block> blockMap, int index) {
TableModel model = new TableModel();
Map<Integer, Map<Integer, String>> rows = getRowsColumnsMap(table, blockMap);
model.setTableId("Table_" + index);
for (Map.Entry<Integer, Map<Integer, String>> entry : rows.entrySet()) {
RowModel rowModel = new RowModel();
Map<Integer, String> value = entry.getValue();
for (int i = 0; i < value.size(); i++) {
rowModel.getCells().add(value.get(i));
}
model.getRows().add(rowModel);
}
return model;
}
private Map<Integer, Map<Integer, String>> getRowsColumnsMap(Block block, Map<String, Block> blockMap) {
Map<Integer, Map<Integer, String>> rows = new HashMap<>();
for (Relationship relationship : block.relationships()) {
if (relationship.type().equals(RelationshipType.CHILD)) {
for (String childId : relationship.ids()) {
Block cell = blockMap.get(childId);
if (cell != null) {
int rowIndex = cell.rowIndex();
int colIndex = cell.columnIndex();
if (rows.get(rowIndex) == null) {
Map<Integer, String> row = new HashMap<>();
rows.put(rowIndex, row);
}
rows.get(rowIndex).put(colIndex, getText(cell, blockMap));
}
}
}
}
return rows;
}
public String getText(Block block, Map<String, Block> blockMap) {
String text = "";
if (block.relationships() != null && block.relationships().size() > 0) {
for (Relationship relationship : block.relationships()) {
if (relationship.type().equals(RelationshipType.CHILD)) {
for (String childId : relationship.ids()) {
Block wordBlock = blockMap.get(childId);
if (wordBlock != null && wordBlock.blockType() != null) {
if (wordBlock.blockType().equals(BlockType.WORD))) {
text += wordBlock.text() + " ";
}
}
}
}
}
}
return text;
}

EHCache returning null after getCache

I'm saving some content from a xml file in a ArrayList, to show in a carousel, so everytime someone enter the website, the xml is downloaded and etc, i don't want that, so i'm saving it in cache using ehcache. But i'm having a problem, in the method to get the Array that is stored, the cache return null, there's nothing there. I'm kinda stuck in this part... maybe i'm getting the cache in a wrong way.
public class xmlCache {
//getting the xml file in a Document
public static Document loadXMLDocument() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
return factory.newDocumentBuilder().parse(new URL("https://www.w3schools.com/xml/cd_catalog.xml").openStream());
}
//putting the xml content in a ArrayList
public static ArrayList<String> listXML(){
ArrayList<String> xmlList = new ArrayList<String>();
try {
Document doc = xmlCache.loadXMLDocument();
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("CD");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
xmlList.add(eElement.getElementsByTagName("TITLE").item(0).getTextContent() + "|" +
eElement.getElementsByTagName("ARTIST").item(0).getTextContent() + "|" +
eElement.getElementsByTagName("COUNTRY").item(0).getTextContent() + "|" +
eElement.getElementsByTagName("COMPANY").item(0).getTextContent() + "|" +
eElement.getElementsByTagName("YEAR").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return xmlList;
}
//putting the ArrayList in cache
public static void cachingXML () {
ArrayList<String> xmlList = xmlCache.listXML();
CacheManager cacheManager = newCacheManagerBuilder().withCache("basicCache", newCacheConfigurationBuilder(Long.class, ArrayList.class, heap(300).offheap(5, MB)))
.build(true);
Cache<Long, ArrayList> basicCache = cacheManager.getCache("basicCache", Long.class, ArrayList.class);
basicCache.put(1L, xmlList);
ArrayList<String> teste = new ArrayList<>();
// if i do that here, is ok, the return is ok
teste = basicCache.get(1L);
System.out.println(teste.size());
}
//accessing the ArrayList stored
public static ArrayList<String> getTicker() {
ArrayList<String> ticker = new ArrayList<>();
try(CacheManager cacheManager = newCacheManagerBuilder().withCache("basicCache", newCacheConfigurationBuilder(Long.class, ArrayList.class, heap(500).offheap(3, MB)))
.build(true)){
Cache<Long, ArrayList> basicCache = cacheManager.getCache("basicCache", Long.class, ArrayList.class);
//the return is false
System.out.println(basicCache.containsKey(1L));
}catch (Throwable e) {
e.printStackTrace();
}
return ticker;
}
public static void main(String[] args) throws MalformedURLException, SAXException, IOException, ParserConfigurationException {
//this one is ok
xmlCache.cachingXML();
//this one is not
xmlCache.getTicker();
}
}

Java parse a String with Stream API

Here's the problem:
public static <T> T execute(String query, Function<List<JsonNode>, T> function) {
String jsonString = HttpRequest.get("http://overpass-api.de/api/interpreter", true, "data", query).body();
List<JsonNode> list = toJsonNodeList(jsonString);
T res = function.apply(list);
return res;
}
This method:
perform a query that returns a json string
transform that string into a JsonNode list
finally, transform each JsonNode into a particular object
This is an example of function that convert each JsonNode into a geojson geometry and returns a geojson result set:
public class GeojsonMapper implements Function<List<JsonNode>, GeojsonSingleListResultSet> {
#Override
public GeojsonSingleListResultSet apply(List<JsonNode> list) {
List<Element> elementList = list.parallelStream()
.map(jsonNode -> {
String id = jsonNode.get("id").asText();
JsonNode tags = jsonNode.get("tags");
switch (jsonNode.get("type").asText()) {
case "node":
return new Point(jsonNode.get("lat").asDouble(), jsonNode.get("lon").asDouble(), id, tags);
case "way":
ArrayList<Point> points = new ArrayList<>();
JsonNode nodeList = jsonNode.get("geometry");
for (int j = 0; j < nodeList.size(); j++) {
JsonNode wayNode = nodeList.get(j);
points.add(j, new Point(wayNode.get("lat").asDouble(), wayNode.get("lon").asDouble()));
}
if (Polygon.isPolygon(points, tags)) {
return new Polygon(points, id, tags);
} else {
return new LineString(points, id, tags);
}
default:
Iterator<JsonNode> iterator = jsonNode.get("members").getElements();
List<List<Point>> rings = new ArrayList<>();
List<Point> ring = null;
while (iterator.hasNext()) {
JsonNode member = iterator.next();
JsonNode geometry = member.get("geometry");
ring = new ArrayList<>();
for (int ringIndex = 0; ringIndex < geometry.size(); ringIndex++) {
JsonNode coordinates = geometry.get(ringIndex);
ring.add(new Point(coordinates.get("lat").asDouble(), coordinates.get("lon").asDouble()));
}
rings.add(ring);
}
return new Multipolygon(Polygon.buildPolygons(rings), id, tags);
}
})
.collect(toList());
return new GeojsonSingleListResultSet(elementList);
}
}
Everything works well, but the toJsonNodeList() method is very slow in comparison to the method function.apply() that use a Stream. This is the toJsonNodeList()'s code:
private static List<JsonNode> toJsonNodeList(String s){
ObjectMapper mapper = new ObjectMapper();
List<JsonNode> list = new ArrayList<>();
try{
JsonNode resultSet = mapper.readTree(s).get("elements");
Iterator<JsonNode> iterator = resultSet.getElements();
while (iterator.hasNext()) {
list.add(iterator.next());
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
Is there a way to parse a json string using a parallelStream so as to extract each element and convert it to a JsonNode?

JSON to JSON conversion in Java

I am trying to acheive JSON to JSON conversion in java based on the specification given on runtime.
Example : if at runtime source : com.gsdetails.gname,target : com.track.trackName (i.e source field should be mapped to target field in generated JSON)
My approach was to create N-array tree for the specification part and do breadth first travesal (get queue with it to craeate structure for resulting json)
I am using Jackson api to create tree from input JSON and traversing both queue(bfs) and input tree to create resulting json.
Unable to get expected output
PS : I thought of using JOLT api but it will not serve my purpose
Tree (for specification)
public class TrieBuilder {
public static void main(String[] args) throws Exception {
createSpec();
}
public static Trie createSpec() throws Exception {
BufferedReader reader = new BufferedReader(
new FileReader(""));
String currentLine = reader.readLine();
Trie trie = new Trie();
while (currentLine != null) {
String[] lines = currentLine.split(",");
String sourceLine = lines[0];
String targetLine = lines[1];
String sourcePath = sourceLine.split("=")[1];
String targetPath = targetLine.split("=")[1];
trie.insertWord(sourcePath.trim(), targetPath.trim());
currentLine = reader.readLine();
}
return trie;
}
}
class TrieNode {
String source;// consider this as content/reference point of a tree
String target;
boolean isEnd;
int count;
List childList;
boolean isRoot;
/* Constructor */
public TrieNode(String source, String target) {
childList = new ArrayList<TrieNode>();
isEnd = false;
count = 0;
this.source = source;
this.target = target;
}
public TrieNode subNodeWord(String word) {
if (childList != null) {
for (TrieNode eachChild : childList)
if (eachChild.source.equals(word))
return eachChild;
}
return null;
}
}
class Trie {
public TrieNode root;
/* Constructor */
public Trie() {
root = new TrieNode("", "");
}
public void insertWord(String sourceWords, String targetWords) {
if (searchWord(sourceWords) == true)
return;
TrieNode current = root;
String[] sourceArray = sourceWords.split(":");
String[] targetArray = targetWords.split(":");
for (int i = 0; i < sourceArray.length; i++) {
TrieNode child = current.subNodeWord(sourceArray[i]);
if (child != null) {
current = child;
} else {
current.childList.add(new TrieNode(sourceArray[i],
targetArray[i]));
current = current.subNodeWord(sourceArray[i]);
}
current.count++;
}
current.isEnd = true;
}
public boolean searchWord(String words) {
TrieNode current = root;
for (String word : words.split(":")) {
if (current.subNodeWord(word) == null) {
return false;
} else {
current = current.subNodeWord(word);
}
}
if (current.isEnd == true)
return true;
return false;
}
public Queue<TrieNode> bfsTraversal(TrieNode node) {
// TODO need to add logic for bfs/dfs for traversing the trie
Queue<TrieNode> queue = new LinkedList<>();
Queue<TrieNode> tempQueue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TrieNode tempNode = queue.poll();
tempQueue.add(tempNode);
int counter = tempNode.childList.size(), i = 0;
if (tempNode == null)
break;
if (!tempNode.source.isEmpty())
System.out.println("Source :" + tempNode.source
+ " Target : " + tempNode.target);
while (i < counter) {
queue.add(tempNode.childList.get(i++));
}
}
tempQueue.poll();
return tempQueue;
}
Source to target mapping file :
source = com:track:trackDetails:fname, target = gsUser:gsProp:gsDetails:gsFirstName
source = com:track:trackDetails:lname, target = gsUser:gsProp:gsDetails:gsLastName
helper class (Actual transform):
public class JsonHelperClass{
// private Files file = null;// create a tempfile
private JsonNodeFactory factory;
private JsonFactory jsonFactory;
private ObjectMapper mapper;
private JsonNode jsonRoot;
private Queue<TrieNode> queue;
// private JsonParser jsonParser =
public JsonHelperClass() throws JsonProcessingException, IOException {
this.factory = JsonNodeFactory.instance;
this.jsonFactory = new JsonFactory();
this.mapper = new ObjectMapper();
this.jsonRoot = mapper.readTree(new File("json with data"));
}
public static void main(String[] args) throws Exception, Exception {
JsonHelperClass helperClass = new JsonHelperClass();
helperClass.jsonCreator();
ObjectNode objectNode = null;
ObjectNode result = helperClass.createJsonRecursively(objectNode);
System.out.println(result.toString());
}
public void jsonCreator() throws Exception {
Trie trie = TrieBuilder.createSpec();
queue = trie.bfsTraversal(trie.root);
}
public ObjectNode createJsonRecursively(ObjectNode outputJson) throws Exception {
TrieNode nodeOfQueue = queue.poll();
if(outputJson == null){
// create a root of the JSON
outputJson = factory.objectNode();
outputJson.put(nodeOfQueue.target, createJsonRecursively(outputJson));
}else if (jsonRoot.get(nodeOfQueue.source).isObject()){
// create an object to conatin other values/object
ObjectNode objectNode = factory.objectNode();
objectNode.put(nodeOfQueue.target,createJsonRecursively(outputJson));
outputJson.putAll(objectNode);
}else if(jsonRoot.get(nodeOfQueue.source).isArray()){
// create an array node and call for to create value it contains
ArrayNode arrayNode = factory.arrayNode();
int size = jsonRoot.get(nodeOfQueue.source).size();
for(int index = 0 ; index < size ; index++){
arrayNode.add(jsonRoot.get(nodeOfQueue.source).get(index));
}
outputJson.put(nodeOfQueue.target,arrayNode);
}else if(nodeOfQueue.isEnd){
// create leaf node
outputJson.put(nodeOfQueue.target, jsonRoot.get(nodeOfQueue.source));
return outputJson;
}
return outputJson;
}

Categories