001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.dwc;
015
016import org.gbif.dwc.record.Record;
017import org.gbif.dwc.record.StarRecord;
018import org.gbif.dwc.terms.DwcTerm;
019import org.gbif.utils.file.FileUtils;
020
021import java.io.IOException;
022import java.nio.file.Path;
023
024import org.junit.jupiter.api.Test;
025
026public class UsageExampleTest {
027
028  @Test
029  public void testUsageExample() throws IOException {
030    Path myArchiveFile = FileUtils.getClasspathFile("checklist_980.zip").toPath();
031    Path extractToFolder = FileUtils.createTempDir().toPath();
032    extractToFolder.toFile().deleteOnExit();
033
034    // Open the Darwin Core Archive
035    Archive dwcArchive = DwcFiles.fromCompressed(myArchiveFile, extractToFolder);
036
037    System.out.println("Reading archive from " + dwcArchive.getLocation().getAbsolutePath());
038    System.out.println("Archive of rowtype " + dwcArchive.getCore().getRowType() + " with " + dwcArchive.getExtensions().size() + " extensions");
039
040    // Loop over core records and display id, genus, specific epithet
041    for (Record rec : dwcArchive.getCore()) {
042      System.out.printf("%s: %s %s%n", rec.id(), rec.value(DwcTerm.genus), rec.value(DwcTerm.specificEpithet));
043    }
044
045    // Loop over star records and display id, core record data, and extension data
046    for (StarRecord rec : dwcArchive) {
047      System.out.printf("%s: %s %s%n", rec.core().id(), rec.core().value(DwcTerm.genus), rec.core().value(DwcTerm.specificEpithet));
048      if (rec.hasExtension(DwcTerm.Occurrence)) {
049        for (Record extRec : rec.extension(DwcTerm.Occurrence)) {
050          System.out.println(" - " + extRec.value(DwcTerm.country));
051        }
052      }
053    }
054  }
055}