-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path154.java
More file actions
142 lines (119 loc) · 5.06 KB
/
Copy path154.java
File metadata and controls
142 lines (119 loc) · 5.06 KB
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
/*
* Licensed to David Pilato (the "Author") under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Author licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package fr.pilato.elasticsearch.crawler.fs.tika;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static fr.pilato.elasticsearch.crawler.fs.framework.MetaParser.mapper;
/**
* Parse a XML document and generate a FSCrawler Doc
*/
public class XmlDocParser {
private final static Logger logger = LogManager.getLogger(XmlDocParser.class);
private static final ObjectMapper xmlMapper;
static {
xmlMapper = new XmlMapper();
xmlMapper.registerModule(new SimpleModule()
.addDeserializer(Object.class, new FixedUntypedObjectDeserializer()));
}
public static String generate(InputStream inputStream) throws IOException {
logger.trace("Converting XML document [{}]");
// Extracting XML content
// See #185: https://github.com/dadoonet/fscrawler/issues/185
Map<String, Object> map = generateMap(inputStream);
// Serialize to JSON
String json = mapper.writeValueAsString(map);
logger.trace("Generated JSON: {}", json);
return json;
}
/**
* Extracting XML content. See #185: https://github.com/dadoonet/fscrawler/issues/185
* @param inputStream The XML Stream
* @return The XML Content as a map
*/
public static Map<String, Object> generateMap(InputStream inputStream) {
logger.trace("Converting XML document [{}]");
Map<String, Object> map = asMap(inputStream);
logger.trace("Generated JSON: {}", map);
return map;
}
private static Map<String, Object> asMap(InputStream stream) {
try {
return (Map<String, Object>) xmlMapper.readValue(stream, Object.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// This code is coming from the gist provided at https://github.com/FasterXML/jackson-dataformat-xml/issues/205
@SuppressWarnings({"deprecation", "serial"})
public static class FixedUntypedObjectDeserializer extends UntypedObjectDeserializer {
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
protected Object mapObject(JsonParser p, DeserializationContext ctx) throws IOException {
String firstKey;
JsonToken t = p.getCurrentToken();
if (t == JsonToken.START_OBJECT) {
firstKey = p.nextFieldName();
} else if (t == JsonToken.FIELD_NAME) {
firstKey = p.getCurrentName();
} else {
if (t != JsonToken.END_OBJECT) {
throw ctx.mappingException(handledType(), p.getCurrentToken());
}
firstKey = null;
}
// empty map might work; but caller may want to modify... so better
// just give small modifiable
Map<String, Object> resultMap = new LinkedHashMap<>(2);
if (firstKey == null) {
return resultMap;
}
p.nextToken();
resultMap.put(firstKey, deserialize(p, ctx));
String nextKey;
while ((nextKey = p.nextFieldName()) != null) {
p.nextToken();
if (resultMap.containsKey(nextKey)) {
Object listObject = resultMap.get(nextKey);
if (!(listObject instanceof List)) {
listObject = new ArrayList<>();
((List) listObject).add(resultMap.get(nextKey));
resultMap.put(nextKey, listObject);
}
((List) listObject).add(deserialize(p, ctx));
} else {
resultMap.put(nextKey, deserialize(p, ctx));
}
}
return resultMap;
}
}
}