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

it.tidalwave.semantic.io.spi.StatementUnmarshallerSupport 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.semantic.io.spi;

import it.tidalwave.semantic.Property;
import org.openrdf.model.Statement;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import it.tidalwave.util.Id;
import it.tidalwave.util.NotFoundException;
import org.openrdf.model.Literal;
import org.openrdf.model.vocabulary.XMLSchema;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public abstract class StatementUnmarshallerSupport implements StatementUnmarshaller
  {
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected Statement findStatementWithPredicate (final @Nonnull List statements,
                                                    final @Nonnull Id predicate)
      throws NotFoundException
      {
        return NotFoundException.throwWhenEmpty(findStatementsWithPredicate(statements, predicate),
                                                "No statements with predicate %s in subject %s",
                                                predicate, statements.get(0).getSubject()).
                                                get(0);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected List findStatementsWithPredicate (final @Nonnull List statements,
                                                           final @Nonnull Id predicate)
      {
        final List result = new ArrayList();

        for (final Statement statement : statements)
          {
            if (statement.getPredicate().stringValue().equals(predicate.stringValue()))
              {
                result.add(statement);
              }
          }

        return result;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @CheckForNull
    protected Object getLiteral (final List statements, final @Nonnull Id predicate)
      {
        try
          {
            return deserialize((Literal)findStatementWithPredicate(statements, predicate).getObject());
          }
        catch (NotFoundException e)
          {
            throw new RuntimeException("Cannot get literal for predicate: " + predicate);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @CheckForNull
    protected Object findEntity (final @Nonnull Context context,
                                 final @Nonnull List statements,
                                 final @Nonnull Id predicate)
      {
        try
          {
            return context.find(findStatementWithPredicate(statements, predicate).getObject());
          }
        catch (NotFoundException e)
          {
            return null;
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected void addCapabilities (final @Nonnull List capabilities,
                                    final @Nonnull List statements,
                                    final @Nonnull Id ... ids)
      {
        for (final Id id : ids)
          {
            try
              {
                capabilities.add(new Property(id, deserialize((Literal)findStatementWithPredicate(statements, id).getObject())));
              }
            catch (NotFoundException e)
              {
                // ok, it doesn't exist
              }
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private static Object deserialize (final @Nonnull Literal literal)
      {
        if (XMLSchema.DATETIME.equals(literal.getDatatype()))
          {
            return literal.calendarValue().toGregorianCalendar().getTime();
          }
        else if (XMLSchema.STRING.equals(literal.getDatatype()) || literal.getDatatype() == null)
          {
            return literal.stringValue();
          }

        throw new RuntimeException("Cannot deserialize " + literal);
      }
  }