001package org.galaxyproject.gxformat2.v19_09.utils; 002 003import java.util.ArrayList; 004import java.util.HashMap; 005import java.util.List; 006import java.util.Map; 007import java.util.TreeSet; 008 009public class IdMapLoader<T> implements Loader<T> { 010 private final Loader<T> innerLoader; 011 private final String mapSubject; 012 private final String mapPredicate; 013 014 public IdMapLoader( 015 final Loader<T> innerLoader, final String mapSubject, final String mapPredicate) { 016 this.innerLoader = innerLoader; 017 this.mapSubject = mapSubject; 018 this.mapPredicate = mapPredicate; 019 } 020 021 public T load( 022 final Object doc_, 023 final String baseUri, 024 final LoadingOptions loadingOptions, 025 final String docRoot) { 026 Object doc = doc_; 027 if (doc instanceof Map) { 028 final Map<String, Object> docMap = (Map<String, Object>) doc; 029 final List<Object> asList = new ArrayList(); 030 final TreeSet<String> sortedKeys = new TreeSet<String>(); 031 sortedKeys.addAll(docMap.keySet()); 032 for (final String key : sortedKeys) { 033 final Object el = docMap.get(key); 034 if (el instanceof Map) { 035 final Map<String, Object> v2 = new HashMap<String, Object>((Map<String, Object>) el); 036 v2.put(this.mapSubject, key); 037 asList.add(v2); 038 } else { 039 if (this.mapPredicate != null) { 040 final Map<String, Object> v3 = new HashMap<String, Object>(); 041 v3.put(this.mapPredicate, el); 042 v3.put(this.mapSubject, key); 043 asList.add(v3); 044 } else { 045 throw new ValidationException("No mapPredicate"); 046 } 047 } 048 } 049 doc = asList; 050 } 051 return this.innerLoader.load(doc, baseUri, loadingOptions); 052 } 053}