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

it.tidalwave.bluebill.othermarshallers.StatementUnmarshallerFactoryImpl Maven / Gradle / Ivy

There is a newer version: 1.0.16
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Core - 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://kenai.com/hg/bluebill~core-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.othermarshallers;

import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.openide.util.lookup.ServiceProvider;
import it.tidalwave.util.Id;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.semantic.io.spi.StatementUnmarshaller;
import it.tidalwave.semantic.io.spi.StatementUnmarshallerFactory;
import it.tidalwave.semantic.io.spi.StatementUnmarshallerSupport;
import it.tidalwave.observation.simple.rdf.SimpleLocationUnmarshaller;
import it.tidalwave.observation.simple.rdf.SimpleObservableUnmarshaller;
import it.tidalwave.observation.simple.rdf.SimpleObservationItemUnmarshaller;
import it.tidalwave.observation.simple.rdf.SimpleObservationSetUnmarshaller;
import it.tidalwave.observation.simple.rdf.SimpleObservationUnmarshaller;
import it.tidalwave.observation.simple.rdf.SimpleObserverUnmarshaller;
import it.tidalwave.observation.simple.rdf.SimpleSourceUnmarshaller;
import it.tidalwave.observation.simple.rdf.Vocabulary;
import it.tidalwave.bluebill.othermarshallers.MediaUnmarshaller;
import it.tidalwave.semantic.io.impl.Converter;
import org.openrdf.model.Statement;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@ServiceProvider(service=StatementUnmarshallerFactory.class)
public class StatementUnmarshallerFactoryImpl implements StatementUnmarshallerFactory
  {
    private final Map unmarshallerMapByType = new HashMap();

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public StatementUnmarshallerFactoryImpl()
      {
        // FIXME: discover with Lookup
        unmarshallerMapByType.put(Vocabulary.OBSERVATION_SET,  new SimpleObservationSetUnmarshaller());
        unmarshallerMapByType.put(Vocabulary.OBSERVATION,      new SimpleObservationUnmarshaller());
        unmarshallerMapByType.put(Vocabulary.OBSERVATION_ITEM, new SimpleObservationItemUnmarshaller());
        unmarshallerMapByType.put(Vocabulary.OBSERVABLE,       new SimpleObservableUnmarshaller());
        unmarshallerMapByType.put(Vocabulary.OBSERVER,         new SimpleObserverUnmarshaller());
        unmarshallerMapByType.put(Vocabulary.DC_PUBLISHER,     new SimpleSourceUnmarshaller());
        unmarshallerMapByType.put(Vocabulary.LOCATION,         new SimpleLocationUnmarshaller());
//        factoryMapByType.put(Media.FOAF_DOCUMENT, new MediaTripleUnmarshaller());
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public StatementUnmarshaller findStatementUnmarshallerFor (final @Nonnull List statements)
      throws NotFoundException
      {
        StatementUnmarshaller unmarshaller = null; // = new EntityTripleUnmarshaller();

        // Find the best factory - FIXME: you should pick the most specialized one, but where to find hierarchy info?
        for (final Statement statement :statements)
          {
            StatementUnmarshallerSupport temp = null;

            if (statement.getPredicate().stringValue().equals(Vocabulary.RDF_TYPE.stringValue()))
              {
                temp = unmarshallerMapByType.get(Converter.valueToId(statement.getObject()));
              }
            else if (statement.getPredicate().stringValue().equals(Vocabulary.DC_TYPE.stringValue())
                  && statement.getObject().stringValue().startsWith("http://purl.org/dc/dcmitype/"))
              {
                temp = new MediaUnmarshaller();
              }

            if (temp != null)
              {
                unmarshaller = temp;
              }
          }

        return NotFoundException.throwWhenNull(unmarshaller, "No StatementUnmarshaller for %s", statements);
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy