-
Notifications
You must be signed in to change notification settings - Fork 5
/
JsonMetaDataReader.js
170 lines (131 loc) · 5.79 KB
/
JsonMetaDataReader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var metaDataReader = function (doc) {
var e = java.lang.Exception;
if (doc !== null && doc.getId() !== null && doc.getFirstFieldValue("parse") !== null) {
try {
var JSONArray = org.json.JSONArray;
var JSONObject = org.json.JSONObject;
var Iterator = java.util.Iterator;
var json = new JSONObject(jsonStr);
/*
* Here is a sample of how to work with the JSONObject and JSONArray
var sample_json_string = json.get("dc:description");
var sample_json_array = payload.getJSONArray("corning:resource-types.es");
*/
var iter = json.keys();
var key;
var obj;
while (iter.hasNext()) {
key = iter.next();
obj = json.get(key);
logger.info("Key: " + key + " is of type: " + obj.getClass().getSimpleName());
doc.addField(key, obj);
}
} catch (e) {
logger.error(e);
}
}
return doc;
}
/*
function fromHex(bs) {
var Integer = java.lang.Integer;
var String = java.lang.String;
var s = new String(bs);
for (var i = 0; i < s.length(); i += 2) {
bs[i / 2] = Integer.parseInt(s.substring(i, i + 2), 16);
}
return new String(bs, "UTF8");
}
function getChildren(json){
var arr = json.fields;
logger.info("There are: "+fields.length+" in this docuemnt");
return json.fields;
}
*/
var parser_v3 = function (doc) {
if (doc !== null && doc.getFirstFieldValue("_raw_content_") !== null) {
logger.info("PARSING NESTED DOCUMENTS...");
logger.info("*** RAW: " + doc.getFirstFieldValue("_raw_content_"));
// var raw = doc.getFirstFieldValue("_raw_content_");
// if(raw !== null){
// logger.info("Raw class: "+ raw.getClass().getSimpleName());
// }
var doclist = java.util.ArrayList;
var Jsoup = org.jsoup.Jsoup;
var jdoc = org.jsoup.nodes.Document;
var ex = java.lang.Exception;
var Parser = org.jsoup.parser.Parser;
var element = org.jsoup.Element;
var pipelineDoc = com.lucidworks.apollo.common.pipeline.PipelineDocument;
var xmlstr = java.lang.String;
var docurl = java.lang.String;
var elements = org.jsoup.select.Elements;
var ele = org.jsoup.Element;
var outdocs = java.util.ArrayList;
var ObjectMapper = org.codehaus.jackson.map.ObjectMapper;
var SerializationConfig = org.codehaus.jackson.map.SerializationConfig;
var String = java.lang.String;
var e = java.lang.Exception;
var base64 = java.util.Base64;
var String = java.lang.String;
var decoder = base64.getDecoder();
var mapper = new ObjectMapper();
var pretty = true;
var result = new String("");
try {
var rawlist = doc.getFieldValues("_raw_content_");
raw = rawlist[0];
// logger.info("raw: "+ raw);
logger.info("Raw class: " + raw.getClass().getSimpleName());
var stdin = new String(raw, "UTF8");
var JSONArray = org.json.JSONArray;
var JSONObject = org.json.JSONObject;
var Iterator = java.util.Iterator;
var json = JSON.parse(stdin);//new JSONObject(stdin);
// var stdin = fromHex(raw);//new String(decoder.decode(raw));
logger.info("JSON: " + stdin);
mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
var children = json[0].fields;//doc.getFirstFieldValue("_childDocuments_"); // _childDocuments
if (children !== null) {
// result = JSON.stringify(result);//mapper.writerWithDefaultPrettyPrinter().writeValueAsString(children);
// logger.info("Children: " + children);
for(var i=0; i<children.length; i++){
var item = children[i];
if(item.name === "_childDocuments"){
logger.info("FOUND CHILD DOCUMENTS ! ");
}
}
} else {
logger.info("NO CHILDREN FOUND");
try {
mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
if (pretty) {
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(doc);
} else {
result = mapper.writeValueAsString(doc);
}
logger.info("**** DOCUMENT " + result);
var fields = doc.getFields();
for each (var fieldName in fields.keySet()) {
var field = doc.getFirstField(fieldName);
// result += "Field Name: "+field.getName()+" val: "+field.getValue()+"\n";
}
} catch (e) {
logger.error(e.getLocalizedMessage());
}
}
} catch (ex) {
logger.error(ex.getLocalizedMessage());
}
return outdocs;
} else {
logger.info("Doc ID was null");
return doc;
}
return doc;
}