<commit_before>/*
 * Copyright 2013 Esri.
 *
 * Licensed 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 com.esri.gpt.control.webharvest.client.dcat;

import com.esri.gpt.framework.dcat.DcatIterableAdaptor;
import com.esri.gpt.framework.dcat.dcat.DcatDistribution;
import com.esri.gpt.framework.dcat.dcat.DcatDistributionList;
import com.esri.gpt.framework.dcat.dcat.DcatRecord;
import com.esri.gpt.framework.resource.api.Publishable;
import com.esri.gpt.framework.util.ReadOnlyIterator;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * DCAT adaptor.
 */
class DCATAdaptor implements Iterable<Publishable> {
  private DCATProxy proxy;
  private DcatIterableAdaptor adaptor;

  public DCATAdaptor(DCATProxy proxy, DcatIterableAdaptor adaptor) {
    this.proxy = proxy;
    this.adaptor = adaptor;
  }

  public void close() {
    if (adaptor!=null) {
      adaptor.close();
      adaptor = null;
    }
  }
  
  @Override
  public Iterator<Publishable> iterator() {
    return new DCATIterator();
  }
  
  private class DCATIterator extends ReadOnlyIterator<Publishable> {
    private Iterator<DcatRecord> iterator = adaptor!=null? adaptor.iterator(): null;
    private String rootAccessUrl = null;
    private String accessUrl;

    @Override
    public boolean hasNext() {
      if (iterator==null) {
        return false;
      }
      if (accessUrl!=null) {
        return true;
      }
      if (!iterator.hasNext()) {
        return false;
      }
      while(iterator.hasNext()) {
        DcatRecord record = iterator.next();
        if (rootAccessUrl==null) {
          rootAccessUrl = record.getAccessURL();
        }
        String url = readAccessUrl(record);
        if (!url.isEmpty()) {
          accessUrl = url;
          return true;
        }
      }
      return false;
    }

    @Override
    public Publishable next() {
      if (accessUrl==null) {
        throw new NoSuchElementException();
      }
      DCATRecord record = new DCATRecord(proxy,accessUrl);
      accessUrl = null;
      return record;
    }
    
    private String readAccessUrl(DcatRecord r) {
      DcatDistributionList distList = r.getDistribution();
      for (DcatDistribution d : distList) {
        if (d.getFormat().equalsIgnoreCase("xml")) {
          return d.getAccessURL();
        }
      }
      return "";<commit_msg>Proper reading accessUrl in DCATAdaptor.<commit_after>/*
 * Copyright 2013 Esri.
 *
 * Licensed 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 com.esri.gpt.control.webharvest.client.dcat;

import com.esri.gpt.framework.dcat.DcatIterableAdaptor;
import com.esri.gpt.framework.dcat.dcat.DcatDistribution;
import com.esri.gpt.framework.dcat.dcat.DcatDistributionList;
import com.esri.gpt.framework.dcat.dcat.DcatRecord;
import com.esri.gpt.framework.resource.api.Publishable;
import com.esri.gpt.framework.util.ReadOnlyIterator;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * DCAT adaptor.
 */
class DCATAdaptor implements Iterable<Publishable> {
  private DCATProxy proxy;
  private DcatIterableAdaptor adaptor;

  public DCATAdaptor(DCATProxy proxy, DcatIterableAdaptor adaptor) {
    this.proxy = proxy;
    this.adaptor = adaptor;
  }

  public void close() {
    if (adaptor!=null) {
      adaptor.close();
      adaptor = null;
    }
  }
  
  @Override
  public Iterator<Publishable> iterator() {
    return new DCATIterator();
  }
  
  private class DCATIterator extends ReadOnlyIterator<Publishable> {
    private Iterator<DcatRecord> iterator = adaptor!=null? adaptor.iterator(): null;
    private String accessUrl;

    @Override
    public boolean hasNext() {
      if (iterator==null) {
        return false;
      }
      if (accessUrl!=null) {
        return true;
      }
      if (!iterator.hasNext()) {
        return false;
      }
      while(iterator.hasNext()) {
        DcatRecord record = iterator.next();
        String url = readAccessUrl(record);
        if (!url.isEmpty()) {
          accessUrl = url;
          return true;
        }
      }
      return false;
    }

    @Override
    public Publishable next() {
      if (accessUrl==null) {
        throw new NoSuchElementException();
      }
      DCATRecord record = new DCATRecord(proxy,accessUrl);
      accessUrl = null;
      return record;
    }
    
    private String readAccessUrl(DcatRecord r) {
      DcatDistributionList distList = r.getDistribution();
      for (DcatDistribution d : distList) {
        if (d.getFormat().equalsIgnoreCase("xml")) {
          return d.getAccessURL();
        }
      }
      return "";
{'fraction_non_alphanumeric': 0.09953621120228327, 'fraction_numerical': 0.0028540849090260435, 'mean_word_length': 2.9182389937106916, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '681251', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1660, 'n_words': 440}