001package org.galaxyproject.gxformat2.v19_09.utils;
002
003import java.util.ArrayList;
004import java.util.List;
005
006public class UriLoader<T> implements Loader<T> {
007  private final Loader<T> innerLoader;
008  private final boolean scopedId;
009  private final boolean vocabTerm;
010  private final Integer scopedRef;
011
012  public UriLoader(
013      final Loader<T> innerLoader,
014      final boolean scopedId,
015      final boolean vocabTerm,
016      final Integer scopedRef) {
017    this.innerLoader = innerLoader;
018    this.scopedId = scopedId;
019    this.vocabTerm = vocabTerm;
020    this.scopedRef = scopedRef;
021  }
022
023  private Object expandUrl(
024      final Object object, final String baseUri, final LoadingOptions loadingOptions) {
025    if (object instanceof String) {
026      return loadingOptions.expandUrl(
027          (String) object, baseUri, this.scopedId, this.vocabTerm, this.scopedRef);
028    } else {
029      return object;
030    }
031  }
032
033  public T load(
034      final Object doc_,
035      final String baseUri,
036      final LoadingOptions loadingOptions,
037      final String docRoot) {
038    Object doc = doc_;
039    if (doc instanceof List) {
040      List<Object> docList = (List<Object>) doc;
041      List<Object> docWithExpansion = new ArrayList<Object>();
042      for (final Object el : docList) {
043        docWithExpansion.add(this.expandUrl(el, baseUri, loadingOptions));
044      }
045      doc = docWithExpansion;
046    }
047    if (doc instanceof String) {
048      doc = this.expandUrl(doc, baseUri, loadingOptions);
049    }
050    return this.innerLoader.load(doc, baseUri, loadingOptions);
051  }
052}