All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.resources.TaxonomyJSONExporter Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueBill Resources - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it
 * SCM: https://java.net/hg/bluebill~resources-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.resources;

import javax.annotation.Nonnull;
import java.util.Locale;
import java.util.Stack;
import java.io.Writer;
import it.tidalwave.util.Id;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import it.tidalwave.bluebill.taxonomy.TaxonomyVisitorSupport;
import it.tidalwave.bluebill.taxonomy.TaxonomyTraverser;
import it.tidalwave.bluebill.taxonomy.Taxon;
import it.tidalwave.bluebill.taxonomy.spi.DefaultTaxonomyTraverser;
import org.json.me.JSONException;
import org.json.me.JSONWriter;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@Slf4j
public class TaxonomyJSONExporter
  {
    private static final String[] LANGUAGES = { "af", "en", "en_us", "it", "de", "da", "no", "fi", "sv", "fr", "fr_ca", "es", "nl" };

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void export (final @Nonnull Taxonomy taxonomy,
                        final @Nonnull Writer wr)
      throws JSONException
      {
        log.info("export({})", taxonomy.getDisplayName());
        final JSONWriter jsonWriter = new JSONWriter(wr);
        jsonWriter.object().key("Taxonomy");
        jsonWriter.object();
        jsonWriter.key("name").value(taxonomy.getDisplayName());
        jsonWriter.key("Class").array();

        // FIXME: taxonomy.as(TaxonomyTraverser).accept(...)
        final TaxonomyTraverser traverser = new DefaultTaxonomyTraverser(taxonomy);
        traverser.accept(new TaxonomyVisitorSupport()
          {
            // FIXME: use types to dynamically get type names
            // FIXME: dummy is needed since it opens one more level even if empty
            private final String[] levels = { "Class", "Order", "Family", "Genus", "Species", "SubSpecies", "Dummy" };
            private final Stack path = new Stack();

            @Override
            public void preVisit (final @Nonnull Taxon taxon)
              {
                log.debug(">>>> processing {}", taxon);
                  
                try
                  {
                    jsonWriter.object();
                    path.push(taxon);
                    jsonWriter.key("name").value(taxon.getDisplayName());

                    try
                      {
                        final Id scientificNameId = taxon.getScientificNameId();
                        jsonWriter.key("id").value(scientificNameId.stringValue());
                      }
                    catch (NotFoundException e)
                      {
                        if (!"Aves".equals(taxon.getDisplayName()))
                          {
                            throw new RuntimeException("No id for " + taxon.getDisplayName());
                          }
                      }

                    if (levels[path.size() - 1].endsWith("Species"))
                      {
                        for (final String language : LANGUAGES)
                          {
                            final String localizedDisplayName = taxon.getDisplayName(new Locale(language));

                            if (!localizedDisplayName.equals(taxon.getDisplayName()))
                              {
                                jsonWriter.key("lang_" + language).value(localizedDisplayName);
                              }
                          }
//                        for (final Locale locale : concept.getLocales()) // FIXME: doesn't get Italian
//                          {
//                            jsonWriter.key("lang_" + locale.getISO3Language()).value(concept.getDisplayName(locale));
//                          }
                      }

                    jsonWriter.key(levels[path.size()]).array();
                  }
                catch (JSONException e)
                  {
                    throw new RuntimeException(e);
                  }
              }

            @Override
            public void postVisit (final @Nonnull Taxon taxon)
              {
                try
                  {
                    jsonWriter.endArray();
                    jsonWriter.endObject();
                    path.pop();
                  }
                catch (JSONException e)
                  {
                    throw new RuntimeException(e);
                  }
              }
          });

        jsonWriter.endArray();
        jsonWriter.endObject();
        jsonWriter.endObject();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected static String pathToString (final @Nonnull Stack stack)
      {
        final StringBuilder buffer = new StringBuilder();

        for (final Taxon taxon : stack.toArray(new Taxon[0]))
          {
            buffer.append(taxon.getDisplayName()).append("/");
          }

        return buffer.toString();
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy