Revision 34901
Added by Claudio Atzori over 9 years ago
XmlRecordFactory.java | ||
---|---|---|
97 | 97 |
|
98 | 98 |
public XmlRecordFactory(final EntityConfigTable entityConfigTable, final ContextMapper contextMapper, final RelClasses relClasses, |
99 | 99 |
final String schemaLocation, final boolean entityDefaults, final boolean relDefaults, final boolean childDefeaults) |
100 |
throws TransformerConfigurationException, TransformerFactoryConfigurationError { |
|
100 |
throws TransformerConfigurationException, TransformerFactoryConfigurationError {
|
|
101 | 101 |
this.entityConfigTable = entityConfigTable; |
102 | 102 |
this.contextMapper = contextMapper; |
103 | 103 |
this.relClasses = relClasses; |
... | ... | |
137 | 137 |
|
138 | 138 |
public String build() { |
139 | 139 |
|
140 |
OafEntityDecoder entity = mainEntity.decodeEntity(); |
|
140 |
final OafEntityDecoder entity = mainEntity.decodeEntity();
|
|
141 | 141 |
// System.out.println("building"); |
142 | 142 |
// System.out.println("main: " + mainEntity); |
143 | 143 |
// System.out.println("rel: " + relations); |
... | ... | |
148 | 148 |
final List<String> metadata = decodeType(entity, null, entityDefaults, false); |
149 | 149 |
|
150 | 150 |
// rels has to be processed before the contexts because they enrich the contextMap with the funding info. |
151 |
List<String> rels = listRelations(); |
|
151 |
final List<String> rels = listRelations();
|
|
152 | 152 |
metadata.addAll(buildContexts(type)); |
153 | 153 |
metadata.add(parseDataInfo(mainEntity)); |
154 | 154 |
|
... | ... | |
159 | 159 |
} |
160 | 160 |
|
161 | 161 |
private String parseDataInfo(final OafDecoder decoder) { |
162 |
DataInfo dataInfo = decoder.getOaf().getDataInfo(); |
|
162 |
final DataInfo dataInfo = decoder.getOaf().getDataInfo();
|
|
163 | 163 |
|
164 |
StringBuilder sb = new StringBuilder(); |
|
164 |
final StringBuilder sb = new StringBuilder();
|
|
165 | 165 |
sb.append("<datainfo>"); |
166 | 166 |
sb.append(asXmlElement("inferred", dataInfo.getInferred() + "", null, null)); |
167 | 167 |
sb.append(asXmlElement("deletedbyinference", dataInfo.getDeletedbyinference() + "", null, null)); |
... | ... | |
179 | 179 |
metadata.addAll(listFields(decoder.getMetadata(), filter, defaults, expandingRel)); |
180 | 180 |
metadata.addAll(listFields(decoder.getOafEntity(), filter, defaults, expandingRel)); |
181 | 181 |
|
182 |
if (decoder.getEntity() instanceof Result && !expandingRel) {
|
|
182 |
if ((decoder.getEntity() instanceof Result) && !expandingRel) {
|
|
183 | 183 |
metadata.add(asXmlElement("bestlicense", "", getBestLicense(), null)); |
184 | 184 |
|
185 | 185 |
metadata.addAll(listFields(decoder.getEntity(), filter, defaults, expandingRel)); |
186 | 186 |
} |
187 |
if (decoder.getEntity() instanceof Person && !expandingRel) {
|
|
187 |
if ((decoder.getEntity() instanceof Person) && !expandingRel) {
|
|
188 | 188 |
metadata.addAll(listFields(decoder.getEntity(), filter, defaults, expandingRel)); |
189 | 189 |
} |
190 |
if (decoder.getEntity() instanceof Project && !expandingRel) {
|
|
190 |
if ((decoder.getEntity() instanceof Project) && !expandingRel) {
|
|
191 | 191 |
metadata.addAll(listFields(decoder.getEntity(), filter, defaults, expandingRel)); |
192 | 192 |
} |
193 | 193 |
|
... | ... | |
196 | 196 |
|
197 | 197 |
private Qualifier getBestLicense() { |
198 | 198 |
Qualifier bestLicense = getQualifier("UNKNOWN", "not available", "dnet:access_modes"); |
199 |
LicenseComparator lc = new LicenseComparator(); |
|
200 |
for (Instance instance : ((Result) mainEntity.decodeEntity().getEntity()).getInstanceList()) { |
|
199 |
final LicenseComparator lc = new LicenseComparator();
|
|
200 |
for (final Instance instance : ((Result) mainEntity.decodeEntity().getEntity()).getInstanceList()) {
|
|
201 | 201 |
if (lc.compare(bestLicense, instance.getLicence()) > 0) { |
202 | 202 |
bestLicense = instance.getLicence(); |
203 | 203 |
} |
... | ... | |
213 | 213 |
|
214 | 214 |
final List<String> rels = Lists.newArrayList(); |
215 | 215 |
|
216 |
for (OafDecoder decoder : this.relations) { |
|
216 |
for (final OafDecoder decoder : this.relations) {
|
|
217 | 217 |
|
218 | 218 |
final OafRel rel = decoder.getOafRel(); |
219 | 219 |
final OafEntity cachedTarget = rel.getCachedTarget(); |
... | ... | |
223 | 223 |
if (relDecoder.getRelSourceId().equals(key) || relDecoder.getRelTargetId().equals(key)) { |
224 | 224 |
|
225 | 225 |
final List<String> metadata = Lists.newArrayList(); |
226 |
Type targetType = relDecoder.getTargetType(mainEntity.getEntity().getType()); |
|
227 |
Set<String> relFilter = entityConfigTable.getFilter(targetType, relDecoder.getRelDescriptor()); |
|
226 |
final Type targetType = relDecoder.getTargetType(mainEntity.getEntity().getType());
|
|
227 |
final Set<String> relFilter = entityConfigTable.getFilter(targetType, relDecoder.getRelDescriptor());
|
|
228 | 228 |
metadata.addAll(listFields(relDecoder.getSubRel(), relFilter, false, true)); |
229 | 229 |
|
230 | 230 |
String semanticclass = ""; |
231 | 231 |
String semanticscheme = ""; |
232 | 232 |
|
233 |
RelDescriptor relDescriptor = relDecoder.getRelDescriptor(); |
|
233 |
final RelDescriptor relDescriptor = relDecoder.getRelDescriptor();
|
|
234 | 234 |
|
235 |
if (cachedTarget != null && cachedTarget.isInitialized()) {
|
|
235 |
if ((cachedTarget != null) && cachedTarget.isInitialized()) {
|
|
236 | 236 |
|
237 | 237 |
final Set<String> filter = entityConfigTable.getFilter(targetType, relDescriptor); |
238 | 238 |
metadata.addAll(decodeType(OafEntityDecoder.decode(cachedTarget), filter, relDefaults, true)); |
239 | 239 |
} |
240 | 240 |
|
241 |
RelMetadata relMetadata = relDecoder.getRelMetadata(); |
|
241 |
final RelMetadata relMetadata = relDecoder.getRelMetadata();
|
|
242 | 242 |
// debug |
243 | 243 |
if (relMetadata == null) { |
244 | 244 |
// System.err.println(this); |
245 |
semanticclass = semanticscheme = "UNKNOWN";
|
|
245 |
semanticclass = semanticscheme = ""; |
|
246 | 246 |
} else { |
247 | 247 |
semanticclass = relClasses.getInverse(relMetadata.getSemantics().getClassname()); |
248 | 248 |
semanticscheme = relMetadata.getSemantics().getSchemename(); |
... | ... | |
250 | 250 |
|
251 | 251 |
incrementCounter(relDescriptor.getSubRelType().toString()); |
252 | 252 |
|
253 |
LinkDescriptor ld = entityConfigTable.getDescriptor(relDecoder.getTargetType(mainEntity.getEntity().getType()), relDescriptor); |
|
253 |
final LinkDescriptor ld = entityConfigTable.getDescriptor(relDecoder.getTargetType(mainEntity.getEntity().getType()), relDescriptor);
|
|
254 | 254 |
|
255 |
String relId = ld != null && !ld.isSymmetric() ? relDecoder.getRelTargetId() : relDecoder.getRelSourceId();
|
|
255 |
final String relId = (ld != null) && !ld.isSymmetric() ? relDecoder.getRelTargetId() : relDecoder.getRelSourceId();
|
|
256 | 256 |
|
257 |
DataInfo info = decoder.getOaf().getDataInfo(); |
|
257 |
final DataInfo info = decoder.getOaf().getDataInfo();
|
|
258 | 258 |
|
259 | 259 |
rels.add(templateFactory.getRel(targetType, relId, metadata, semanticclass, semanticscheme, info.getInferred(), info.getTrust(), |
260 | 260 |
info.getInferenceprovenance(), info.getProvenanceaction().getClassid())); |
... | ... | |
266 | 266 |
private List<String> listChildren() { |
267 | 267 |
|
268 | 268 |
final List<String> children = Lists.newArrayList(); |
269 |
for (OafDecoder decoder : this.children) { |
|
270 |
OafEntity cachedTarget = decoder.getOafRel().getCachedTarget(); |
|
269 |
for (final OafDecoder decoder : this.children) {
|
|
270 |
final OafEntity cachedTarget = decoder.getOafRel().getCachedTarget();
|
|
271 | 271 |
addChildren(children, cachedTarget, decoder.getRelDescriptor()); |
272 | 272 |
} |
273 |
OafEntityDecoder entity = mainEntity.decodeEntity(); |
|
273 |
final OafEntityDecoder entity = mainEntity.decodeEntity();
|
|
274 | 274 |
if (entity.getType().equals(Type.result)) { |
275 |
for (Instance instance : ((Result) entity.getEntity()).getInstanceList()) { |
|
275 |
for (final Instance instance : ((Result) entity.getEntity()).getInstanceList()) {
|
|
276 | 276 |
children.add(templateFactory.getInstance(instance.getHostedby().getKey(), listFields(instance, null, false, false), |
277 | 277 |
listMap(instance.getUrlList(), new UnaryFunction<String, String>() { |
278 | 278 |
|
... | ... | |
282 | 282 |
} |
283 | 283 |
}))); |
284 | 284 |
} |
285 |
for (ExternalReference er : ((Result) entity.getEntity()).getExternalReferenceList()) { |
|
285 |
for (final ExternalReference er : ((Result) entity.getEntity()).getExternalReferenceList()) {
|
|
286 | 286 |
// Set<String> filters = entityConfigTable.getFilter(Type.result, RelType.resultResult); |
287 |
List<String> fields = listFields(er, null, false, false); |
|
287 |
final List<String> fields = listFields(er, null, false, false);
|
|
288 | 288 |
children.add(templateFactory.getChild("externalreference", null, fields)); |
289 | 289 |
} |
290 | 290 |
} |
... | ... | |
295 | 295 |
private void addChildren(final List<String> children, final OafEntity target, final RelDescriptor relDescriptor) { |
296 | 296 |
final OafEntityDecoder decoder = OafEntityDecoder.decode(target); |
297 | 297 |
incrementCounter(relDescriptor.getSubRelType().toString()); |
298 |
Set<String> filters = entityConfigTable.getFilter(target.getType(), relDescriptor); |
|
298 |
final Set<String> filters = entityConfigTable.getFilter(target.getType(), relDescriptor);
|
|
299 | 299 |
children.add(templateFactory.getChild(decoder.getType().toString(), decoder.getId(), listFields(decoder.getMetadata(), filters, childDefaults, false))); |
300 | 300 |
} |
301 | 301 |
|
... | ... | |
307 | 307 |
|
308 | 308 |
if (fields != null) { |
309 | 309 |
|
310 |
Set<String> seen = Sets.newHashSet(); |
|
311 |
for (Entry<FieldDescriptor, Object> e : filterFields(fields, filter)) { |
|
310 |
final Set<String> seen = Sets.newHashSet();
|
|
311 |
for (final Entry<FieldDescriptor, Object> e : filterFields(fields, filter)) {
|
|
312 | 312 |
|
313 | 313 |
// final String name = getFieldName(e.getKey().getName()); |
314 | 314 |
final String name = e.getKey().getName(); |
... | ... | |
318 | 318 |
} |
319 | 319 |
|
320 | 320 |
if (defaults) { |
321 |
for (FieldDescriptor fd : fields.getDescriptorForType().getFields()) { |
|
321 |
for (final FieldDescriptor fd : fields.getDescriptorForType().getFields()) {
|
|
322 | 322 |
if (!seen.contains(fd.getName())) { |
323 | 323 |
addFieldValue(metadata, fd, getDefault(fd), expandingRel); |
324 | 324 |
} |
... | ... | |
335 | 335 |
case BYTES: |
336 | 336 |
return "".getBytes(); |
337 | 337 |
case MESSAGE: { |
338 |
if (Qualifier.getDescriptor().equals(fd.getMessageType())) { return defaultQualifier(); }
|
|
339 |
if (StructuredProperty.getDescriptor().equals(fd.getMessageType())) { return StructuredProperty.newBuilder().setValue("")
|
|
340 |
.setQualifier(defaultQualifier()).build(); }
|
|
341 |
if (KeyValue.getDescriptor().equals(fd.getMessageType())) { return KeyValue.newBuilder().setKey("").setValue("").build(); }
|
|
342 |
if (StringField.getDescriptor().equals(fd.getMessageType())) { return StringField.newBuilder().setValue("").build(); }
|
|
338 |
if (Qualifier.getDescriptor().equals(fd.getMessageType())) return defaultQualifier();
|
|
339 |
if (StructuredProperty.getDescriptor().equals(fd.getMessageType())) return StructuredProperty.newBuilder().setValue("") |
|
340 |
.setQualifier(defaultQualifier()).build(); |
|
341 |
if (KeyValue.getDescriptor().equals(fd.getMessageType())) return KeyValue.newBuilder().setKey("").setValue("").build();
|
|
342 |
if (StringField.getDescriptor().equals(fd.getMessageType())) return StringField.newBuilder().setValue("").build();
|
|
343 | 343 |
return null; |
344 | 344 |
} |
345 | 345 |
case SFIXED32: |
... | ... | |
366 | 366 |
|
367 | 367 |
@SuppressWarnings("unchecked") |
368 | 368 |
private void addFieldValue(final List<String> metadata, final FieldDescriptor fd, final Object value, final boolean expandingRel) { |
369 |
if (fd.getName().equals("dateofcollection") || fd.getName().equals("id") || fd.getName().equals("url") || value == null) { return; }
|
|
369 |
if (fd.getName().equals("dateofcollection") || fd.getName().equals("id") || fd.getName().equals("url") || (value == null)) return;
|
|
370 | 370 |
|
371 | 371 |
if (fd.getName().equals("datasourcetype")) { |
372 |
String classid = ((Qualifier) value).getClassid(); |
|
372 |
final String classid = ((Qualifier) value).getClassid();
|
|
373 | 373 |
|
374 |
Qualifier.Builder q = Qualifier.newBuilder((Qualifier) value); |
|
374 |
final Qualifier.Builder q = Qualifier.newBuilder((Qualifier) value);
|
|
375 | 375 |
if (specialDatasourceTypes.contains(classid)) { |
376 | 376 |
q.setClassid("other").setClassname("other"); |
377 | 377 |
} |
378 | 378 |
metadata.add(asXmlElement("datasourcetypeui", "", q.build(), null)); |
379 | 379 |
} |
380 | 380 |
|
381 |
if (fd.isRepeated() && value instanceof List<?>) {
|
|
382 |
for (Object o : (List<Object>) value) { |
|
381 |
if (fd.isRepeated() && (value instanceof List<?>)) {
|
|
382 |
for (final Object o : (List<Object>) value) {
|
|
383 | 383 |
guessType(metadata, fd, o, expandingRel); |
384 | 384 |
} |
385 | 385 |
} else { |
... | ... | |
392 | 392 |
if (fd.getType().equals(FieldDescriptor.Type.MESSAGE)) { |
393 | 393 |
|
394 | 394 |
if (Qualifier.getDescriptor().equals(fd.getMessageType())) { |
395 |
Qualifier qualifier = (Qualifier) o; |
|
395 |
final Qualifier qualifier = (Qualifier) o;
|
|
396 | 396 |
metadata.add(asXmlElement(fd.getName(), "", qualifier, null)); |
397 | 397 |
} |
398 | 398 |
|
399 | 399 |
if (StructuredProperty.getDescriptor().equals(fd.getMessageType())) { |
400 |
StructuredProperty sp = (StructuredProperty) o; |
|
400 |
final StructuredProperty sp = (StructuredProperty) o;
|
|
401 | 401 |
metadata.add(asXmlElement(fd.getName(), sp.getValue(), sp.getQualifier(), sp.hasDataInfo() ? sp.getDataInfo() : null)); |
402 | 402 |
} |
403 | 403 |
|
404 | 404 |
if (KeyValue.getDescriptor().equals(fd.getMessageType())) { |
405 |
KeyValue kv = (KeyValue) o; |
|
405 |
final KeyValue kv = (KeyValue) o;
|
|
406 | 406 |
metadata.add("<" + fd.getName() + " name=\"" + escapeXml(kv.getValue()) + "\" id=\"" + escapeXml(removePrefix(kv.getKey())) + "\"/>"); |
407 | 407 |
} |
408 | 408 |
|
... | ... | |
410 | 410 |
if (fd.getName().contains("fundingtree")) { |
411 | 411 |
handleFundingTree(metadata, fd, o, expandingRel); |
412 | 412 |
} else { |
413 |
StringField sf = (StringField) o; |
|
414 |
StringBuilder sb = new StringBuilder("<" + fd.getName()); |
|
413 |
final StringField sf = (StringField) o;
|
|
414 |
final StringBuilder sb = new StringBuilder("<" + fd.getName());
|
|
415 | 415 |
if (sf.hasDataInfo()) { |
416 |
DataInfo dataInfo = sf.getDataInfo(); |
|
416 |
final DataInfo dataInfo = sf.getDataInfo();
|
|
417 | 417 |
dataInfoAsAttributes(sb, dataInfo); |
418 | 418 |
} |
419 | 419 |
sb.append(">" + escapeXml(sf.getValue()) + "</" + fd.getName() + ">"); |
... | ... | |
421 | 421 |
} |
422 | 422 |
} |
423 | 423 |
|
424 |
if (Journal.getDescriptor().equals(fd.getMessageType()) && o != null) {
|
|
425 |
Journal j = (Journal) o; |
|
424 |
if (Journal.getDescriptor().equals(fd.getMessageType()) && (o != null)) {
|
|
425 |
final Journal j = (Journal) o;
|
|
426 | 426 |
metadata.add("<journal " + "issn=\"" + escapeXml(j.getIssnPrinted()) + "\" " + "eissn=\"" + escapeXml(j.getIssnOnline()) + "\" " + "lissn=\"" |
427 | 427 |
+ escapeXml(j.getIssnLinking()) + "\">" + escapeXml(j.getName()) + "</journal>"); |
428 | 428 |
} |
429 | 429 |
|
430 |
if (Context.getDescriptor().equals(fd.getMessageType()) && o != null) {
|
|
430 |
if (Context.getDescriptor().equals(fd.getMessageType()) && (o != null)) {
|
|
431 | 431 |
contextes.add(((Result.Context) o).getId()); |
432 | 432 |
} |
433 | 433 |
|
434 |
if (ExtraInfo.getDescriptor().equals(fd.getMessageType()) && o != null) {
|
|
434 |
if (ExtraInfo.getDescriptor().equals(fd.getMessageType()) && (o != null)) {
|
|
435 | 435 |
|
436 |
ExtraInfo e = (ExtraInfo) o; |
|
437 |
StringBuilder sb = new StringBuilder("<" + fd.getName() + " "); |
|
436 |
final ExtraInfo e = (ExtraInfo) o;
|
|
437 |
final StringBuilder sb = new StringBuilder("<" + fd.getName() + " ");
|
|
438 | 438 |
|
439 | 439 |
sb.append("name=\"" + e.getName() + "\" "); |
440 | 440 |
sb.append("typology=\"" + e.getTypology() + "\" "); |
... | ... | |
448 | 448 |
} |
449 | 449 |
|
450 | 450 |
} else if (fd.getType().equals(FieldDescriptor.Type.ENUM)) { |
451 |
if (fd.getFullName().equals("eu.dnetlib.data.proto.OafEntity.type")) { return; }
|
|
451 |
if (fd.getFullName().equals("eu.dnetlib.data.proto.OafEntity.type")) return;
|
|
452 | 452 |
metadata.add(asXmlElement(fd.getName(), ((EnumValueDescriptor) o).getName(), null, null)); |
453 | 453 |
} else { |
454 | 454 |
metadata.add(asXmlElement(fd.getName(), o.toString(), null, null)); |
... | ... | |
466 | 466 |
private List<String> buildContexts(final Type type) { |
467 | 467 |
final List<String> res = Lists.newArrayList(); |
468 | 468 |
|
469 |
if (contextMapper != null && !contextMapper.isEmpty() && type.equals(Type.result)) {
|
|
469 |
if ((contextMapper != null) && !contextMapper.isEmpty() && type.equals(Type.result)) {
|
|
470 | 470 |
|
471 | 471 |
XMLTag document = XMLDoc.newDocument(true).addRoot("contextRoot"); |
472 | 472 |
|
473 |
for (String id : contextes) { |
|
473 |
for (final String id : contextes) {
|
|
474 | 474 |
|
475 |
StringTokenizer st = new StringTokenizer(id, "::"); |
|
475 |
final StringTokenizer st = new StringTokenizer(id, "::");
|
|
476 | 476 |
String token = ""; |
477 | 477 |
while (st.hasMoreTokens()) { |
478 | 478 |
token += st.nextToken(); |
479 | 479 |
|
480 | 480 |
final ContextDef def = contextMapper.get(token); |
481 | 481 |
|
482 |
if (def == null) { throw new IllegalStateException("cannot find context for id: " + token); }
|
|
482 |
if (def == null) throw new IllegalStateException("cannot find context for id: " + token);
|
|
483 | 483 |
|
484 | 484 |
if (def.getName().equals("context")) { |
485 |
String xpath = "//context/@id='" + def.getId() + "'"; |
|
485 |
final String xpath = "//context/@id='" + def.getId() + "'";
|
|
486 | 486 |
if (!document.gotoRoot().rawXpathBoolean(xpath, new Object())) { |
487 | 487 |
document = addContextDef(document.gotoRoot(), def); |
488 | 488 |
} |
489 | 489 |
} |
490 | 490 |
|
491 | 491 |
if (def.getName().equals("category")) { |
492 |
String rootId = StringUtils.substringBefore(def.getId(), "::"); |
|
492 |
final String rootId = StringUtils.substringBefore(def.getId(), "::");
|
|
493 | 493 |
document = addContextDef(document.gotoRoot().gotoTag("//context[./@id='" + rootId + "']", new Object()), def); |
494 | 494 |
} |
495 | 495 |
|
... | ... | |
500 | 500 |
} |
501 | 501 |
} |
502 | 502 |
|
503 |
for (org.w3c.dom.Element x : document.gotoRoot().getChildElement()) { |
|
503 |
for (final org.w3c.dom.Element x : document.gotoRoot().getChildElement()) {
|
|
504 | 504 |
try { |
505 | 505 |
res.add(asStringElement(x)); |
506 |
} catch (TransformerException e) { |
|
506 |
} catch (final TransformerException e) {
|
|
507 | 507 |
throw new RuntimeException(e); |
508 | 508 |
} |
509 | 509 |
} |
... | ... | |
514 | 514 |
|
515 | 515 |
private XMLTag addContextDef(final XMLTag tag, final ContextDef def) { |
516 | 516 |
tag.addTag(def.getName()).addAttribute("id", def.getId()).addAttribute("label", def.getLabel()); |
517 |
if (def.getType() != null && !def.getType().isEmpty()) {
|
|
517 |
if ((def.getType() != null) && !def.getType().isEmpty()) {
|
|
518 | 518 |
tag.addAttribute("type", def.getType()); |
519 | 519 |
} |
520 | 520 |
return tag; |
521 | 521 |
} |
522 | 522 |
|
523 | 523 |
private String asStringElement(final org.w3c.dom.Element element) throws TransformerException { |
524 |
StringWriter buffer = new StringWriter(); |
|
524 |
final StringWriter buffer = new StringWriter();
|
|
525 | 525 |
transformer.transform(new DOMSource(element), new StreamResult(buffer)); |
526 | 526 |
return buffer.toString(); |
527 | 527 |
} |
528 | 528 |
|
529 | 529 |
@SuppressWarnings("unchecked") |
530 | 530 |
private void handleFundingTree(final List<String> metadata, final FieldDescriptor fd, final Object o, final boolean expandingRel) { |
531 |
String xmlTree = asXmlJSon(fd.getName(), o instanceof StringField ? ((StringField) o).getValue() : o.toString()); |
|
531 |
final String xmlTree = asXmlJSon(fd.getName(), o instanceof StringField ? ((StringField) o).getValue() : o.toString());
|
|
532 | 532 |
if (expandingRel) { |
533 | 533 |
try { |
534 |
Document ftree = new SAXReader().read(new StringReader(xmlTree)); |
|
534 |
final Document ftree = new SAXReader().read(new StringReader(xmlTree));
|
|
535 | 535 |
|
536 | 536 |
int i = 0; |
537 | 537 |
String funding = "<funding>"; |
538 | 538 |
String _id = ""; |
539 | 539 |
|
540 |
for (Object id : Lists.reverse(ftree.selectNodes("//fundingtree//name"))) { |
|
540 |
for (final Object id : Lists.reverse(ftree.selectNodes("//fundingtree//name"))) {
|
|
541 | 541 |
_id += ((Element) id).getText(); |
542 | 542 |
funding += "<funding_level_" + i + ">" + escapeXml(_id) + "</funding_level_" + i + ">"; |
543 | 543 |
_id += "::"; |
... | ... | |
546 | 546 |
funding += "</funding>"; |
547 | 547 |
// System.out.println("-------------------------------\n" + xmlTree + "\n" + funding); |
548 | 548 |
metadata.add(funding); |
549 |
} catch (DocumentException e) { |
|
549 |
} catch (final DocumentException e) {
|
|
550 | 550 |
System.err.println("unable to parse funding tree: " + xmlTree + "\n" + e.getMessage()); |
551 | 551 |
} |
552 | 552 |
} else { |
... | ... | |
556 | 556 |
|
557 | 557 |
private String asXmlJSon(final String root, final String json) { |
558 | 558 |
try { |
559 |
if (json == null || json.isEmpty()) { return "<" + root + "/>"; }
|
|
560 |
JSONObject o = new JSONObject(json.replace("'", "")); |
|
559 |
if ((json == null) || json.isEmpty()) return "<" + root + "/>";
|
|
560 |
final JSONObject o = new JSONObject(json.replace("'", ""));
|
|
561 | 561 |
|
562 |
String contextId = parseFundingJson(o).toLowerCase(); |
|
562 |
final String contextId = parseFundingJson(o).toLowerCase();
|
|
563 | 563 |
contextes.add(contextId); |
564 | 564 |
|
565 |
String xml = org.json.XML.toString(o, root); |
|
565 |
final String xml = org.json.XML.toString(o, root);
|
|
566 | 566 |
return xml; |
567 |
} catch (Exception e) { |
|
567 |
} catch (final Exception e) {
|
|
568 | 568 |
System.err.println("unable to parse json: " + json + "\n" + e.getMessage()); |
569 | 569 |
return "<" + root + "/>"; |
570 | 570 |
} |
... | ... | |
572 | 572 |
|
573 | 573 |
private String parseFundingJson(final JSONObject o) { |
574 | 574 |
try { |
575 |
String key = (String) Iterators.getOnlyElement(o.keys()); |
|
576 |
JSONObject obj = o.getJSONObject(key); |
|
575 |
final String key = (String) Iterators.getOnlyElement(o.keys());
|
|
576 |
final JSONObject obj = o.getJSONObject(key);
|
|
577 | 577 |
|
578 | 578 |
String id = obj.getString("id").toLowerCase(); |
579 | 579 |
if (id.startsWith("welcometrust::")) { |
... | ... | |
611 | 611 |
} |
612 | 612 |
|
613 | 613 |
return id; |
614 |
} catch (JSONException e) { |
|
614 |
} catch (final JSONException e) {
|
|
615 | 615 |
throw new RuntimeException(e); |
616 | 616 |
} |
617 | 617 |
} |
... | ... | |
630 | 630 |
if (dataInfo != null) { |
631 | 631 |
sb = dataInfoAsAttributes(sb, dataInfo); |
632 | 632 |
} |
633 |
if (value == null || value.isEmpty()) {
|
|
633 |
if ((value == null) || value.isEmpty()) {
|
|
634 | 634 |
sb.append("/>"); |
635 | 635 |
return sb.toString(); |
636 | 636 |
// return "<" + name + getAttributes(q) + "/>"; |
... | ... | |
648 | 648 |
} |
649 | 649 |
|
650 | 650 |
private String getAttributes(final Qualifier q) { |
651 |
if (q == null) { return ""; }
|
|
651 |
if (q == null) return "";
|
|
652 | 652 |
|
653 |
StringBuilder sb = new StringBuilder(); |
|
654 |
for (Entry<FieldDescriptor, Object> e : q.getAllFields().entrySet()) { |
|
653 |
final StringBuilder sb = new StringBuilder();
|
|
654 |
for (final Entry<FieldDescriptor, Object> e : q.getAllFields().entrySet()) {
|
|
655 | 655 |
// sb.append(" " + e.getKey().getName() + "=\"" + escapeXml(e.getValue().toString()) + "\""); |
656 | 656 |
sb.append(" "); |
657 | 657 |
sb.append(e.getKey().getName()); |
... | ... | |
665 | 665 |
private Set<Entry<FieldDescriptor, Object>> filterFields(final GeneratedMessage fields, final Set<String> filter) { |
666 | 666 |
|
667 | 667 |
if (filter != null) { |
668 |
Predicate<FieldDescriptor> p = new Predicate<FieldDescriptor>() { |
|
668 |
final Predicate<FieldDescriptor> p = new Predicate<FieldDescriptor>() {
|
|
669 | 669 |
|
670 | 670 |
@Override |
671 | 671 |
public boolean apply(final FieldDescriptor descriptor) { |
672 |
if (fields == null) { |
|
673 |
return false; |
|
674 |
} |
|
675 |
String name = descriptor.getName(); |
|
672 |
if (fields == null) return false; |
|
673 |
final String name = descriptor.getName(); |
|
676 | 674 |
return filter.contains(name); |
677 | 675 |
} |
678 | 676 |
}; |
679 |
Map<FieldDescriptor, Object> filtered = Maps.filterKeys(fields.getAllFields(), p); |
|
677 |
final Map<FieldDescriptor, Object> filtered = Maps.filterKeys(fields.getAllFields(), p);
|
|
680 | 678 |
// System.out.println( |
681 | 679 |
// "filtered " + type.toString() + ": " + toString(filterEntries.keySet()) + "\n" + |
682 | 680 |
// "builder " + fields.getDescriptorForType().getFullName() + ": " + toString(fields.getAllFields().keySet())); |
... | ... | |
686 | 684 |
} |
687 | 685 |
|
688 | 686 |
public static String removePrefix(final String s) { |
689 |
if (s.contains("|")) { return StringUtils.substringAfter(s, "|"); }
|
|
687 |
if (s.contains("|")) return StringUtils.substringAfter(s, "|");
|
|
690 | 688 |
return s; |
691 | 689 |
} |
692 | 690 |
|
... | ... | |
696 | 694 |
} |
697 | 695 |
|
698 | 696 |
private List<String> countersAsXml() { |
699 |
List<String> out = Lists.newArrayList(); |
|
700 |
for (Entry<String, Integer> e : counters.entrySet()) { |
|
697 |
final List<String> out = Lists.newArrayList();
|
|
698 |
for (final Entry<String, Integer> e : counters.entrySet()) {
|
|
701 | 699 |
out.add(String.format("<counter_%s value=\"%s\"/>", e.getKey(), e.getValue())); |
702 | 700 |
} |
703 | 701 |
return out; |
... | ... | |
713 | 711 |
|
714 | 712 |
@Override |
715 | 713 |
public String toString() { |
716 |
StringBuilder sb = new StringBuilder(); |
|
714 |
final StringBuilder sb = new StringBuilder();
|
|
717 | 715 |
sb.append("################################################\n"); |
718 | 716 |
sb.append("ID: ").append(key).append("\n"); |
719 | 717 |
if (mainEntity != null) { |
... | ... | |
721 | 719 |
} |
722 | 720 |
if (relations != null) { |
723 | 721 |
sb.append("\nRELATIONS:\n"); |
724 |
for (OafDecoder decoder : relations) { |
|
722 |
for (final OafDecoder decoder : relations) {
|
|
725 | 723 |
sb.append(decoder.getOafRel().toString() + "\n"); |
726 | 724 |
} |
727 | 725 |
} |
728 | 726 |
if (children != null) { |
729 | 727 |
sb.append("\nCHILDREN:\n"); |
730 |
for (OafDecoder decoder : children) { |
|
728 |
for (final OafDecoder decoder : children) {
|
|
731 | 729 |
sb.append(decoder.getOafRel().toString() + "\n"); |
732 | 730 |
} |
733 | 731 |
} |
Also available in: Unified diff
temporary commit