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

it.tidalwave.bluebill.taxonomy.birds.TaxonomyJSONExporter Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: TaxonomyJSONExporter.java,v 480a0fffbfc6 2010/07/24 22:17:36 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.birds;

import it.tidalwave.bluebill.taxonomy.TaxonomyVisitorAdapter;
import it.tidalwave.bluebill.taxonomy.TaxonomyVisitController;
import javax.annotation.Nonnull;
import java.util.Locale;
import java.util.Stack;
import javax.xml.namespace.QName;
import java.io.Writer;
import org.openrdf.elmo.Entity;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import it.tidalwave.bluebill.taxonomy.TaxonomyConcept;
import org.json.me.JSONException;
import org.json.me.JSONWriter;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: TaxonomyJSONExporter.java,v 480a0fffbfc6 2010/07/24 22:17:36 fabrizio $
 *
 **********************************************************************************************************************/
public class TaxonomyJSONExporter
  {
    private static final String[] LANGUAGES = { "en", "en_us", "it", "de", "da", "no", "fi", "sv", "fr", "fr_ca", "es", "nl" };

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

        final TaxonomyVisitController visitController = new TaxonomyVisitController(taxonomy);
        visitController.visit(new TaxonomyVisitorAdapter()
          {
            // 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 begin (@Nonnull final TaxonomyConcept concept)
              {
                try
                  {
                    jsonWriter.object();
                    path.push(concept);
//                    System.err.println(pathToString(path));
                    jsonWriter.key("name").value(concept.getDisplayName());

                    try
                      {
                        final QName qName = concept.getAnonymousSynonym().as(Entity.class).getQName();
                        jsonWriter.key("id").value(qName.getNamespaceURI() + qName.getLocalPart());
                      }
                    catch (NotFoundException e)
                      {
                      }

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

                            if (!localizedDisplayName.equals(concept.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 end (@Nonnull final TaxonomyConcept concept)
              {
                try
                  {
                    jsonWriter.endArray();
                    jsonWriter.endObject();
                    path.pop();
                  }
                catch (JSONException e)
                  {
                    throw new RuntimeException(e);
                  }
              }

            @Override
            public void visit (@Nonnull final TaxonomyConcept concept)
              {
              }
          });

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

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

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

        return buffer.toString();
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy