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

it.tidalwave.semantic.io.impl.GraphMarshallerImpl 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.impl;

import javax.annotation.concurrent.NotThreadSafe;
import java.util.Collection;
import java.util.IdentityHashMap;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.semantic.io.spi.StatementMarshallerFactory;
import it.tidalwave.semantic.io.spi.AsStatementMarshaller;
import java.util.ArrayList;
import java.util.List;
import org.openrdf.model.Statement;
import it.tidalwave.semantic.io.spi.StatementMarshaller;
import org.openrdf.model.impl.StatementImpl;
//import it.tidalwave.mobile.media.Media;
import it.tidalwave.semantic.io.GraphMarshaller;
import it.tidalwave.util.As;
import it.tidalwave.util.AsException;
import it.tidalwave.util.Id;
import it.tidalwave.role.Identifiable;
import it.tidalwave.util.NotFoundException;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.annotation.Nonnull;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import org.openrdf.model.Graph;
import org.openrdf.model.Resource;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.GraphImpl;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@NotThreadSafe
public class GraphMarshallerImpl implements GraphMarshaller, StatementMarshaller.Context
  {
    private List pendingEntities = new ArrayList();

    private IdentityHashMap idMapByObject = new IdentityHashMap();

    private Graph graph = new GraphImpl();

    private int idSequence;

    private final ValueFactory valueFactory = graph.getValueFactory();

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Graph marshal (final @Nonnull As entity)
      {
        visitEntity(entity);
        
        for (final As as : pendingEntities)
          {
            findStatementMarshallerFor(as).marshal(as, this);
          }

        return graph;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Id idFor (final @Nonnull Object oo)
      {
        return idFor(((UriWithAs)oo).getAs());
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Id idFor (final @Nonnull As as)
      {
//        if (as instanceof Media)
//          {
//            try
//              {
//                return ((Media)as).get(Media.ID); // FIXME: refactor as as(Identifiable).getId()
//              }
//            catch (NotFoundException e)
//              {
//                throw new RuntimeException(e);
//              }
//          }

        try
          {
            return as.as(Identifiable.class).getId();
          }
        catch (AsException e)
          {
            Id id = idMapByObject.get(as);

            if (id == null)
              {
                id = new Id("_:x" + (idSequence++));
                idMapByObject.put(as, id);
              }

            return id;
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private static StatementMarshaller findStatementMarshallerFor (final @Nonnull As object)
      {
        try 
          {
            return Locator.find(StatementMarshallerFactory.class).findStatementMarshallerFor(object);
          }
        catch (NotFoundException ex)
          {
            return new AsStatementMarshaller();
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void visitEntity (final @Nonnull As entity)
      {
        enqueueEntity(entity);

        final Graph graphSave = graph;
        graph = new GraphImpl();
        findStatementMarshallerFor(entity).marshal(entity, this);
        final Collection statements = graph;
        graphSave.addAll(graph);
        graph = graphSave;

        for (final Statement statement : statements)
          {
            final Object object = statement.getObject();

            if (object instanceof UriWithAs)
              {
                final As as = ((UriWithAs)object).getAs();

                if (!visited(as))
                  {
                    visitEntity(as);
                  }
              }
            
            final Resource subject = statement.getSubject();

            if (subject instanceof UriWithAs)
              {
                final As as = ((UriWithAs)subject).getAs();

                if (!visited(as))
                  {
                    visitEntity(as);
                  }
              }
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private boolean visited (final @Nonnull Object object)
      {
        // don't use pendingEntities.contains(object) because we don't want to call equals()
        for (final Object o : pendingEntities)
          {
            if (o == object)
              {
                return true;
              }
          }

        return false;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void enqueueEntity (final @Nonnull As object)
      {
        if (!pendingEntities.contains(object))
          {
            pendingEntities.add(object);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public void addStatement (final @Nonnull As subject,
                              final @Nonnull Id predicate,
                              final @Nonnull Object object)
      {
        // FIXME: use the factory
        graph.add(new StatementImpl(objectToResource(subject),
                                    Converter.idToUri(predicate),
                                    objectToValue(object)));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Resource objectToResource (final @Nonnull Object object)
      {
//        if (object instanceof Media) // FIXME: Media must be As Identifiable
//          {
//            try
//              {
//                return new UriImplWithAs(((Media)object).get(Media.ID).stringValue(), (As)object);
//              }
//            catch (NotFoundException e)
//              {
//                throw new RuntimeException(e);
//              }
//          }

        if (object instanceof As)
          {
            try
              {
                return new UriImplWithAs(((As)object).as(Identifiable.class).getId().stringValue(), (As)object);
              }
            catch (AsException e)
              {
                return new UriImplWithAs(idFor((As)object).stringValue(), (As)object);
              }
          }

        throw new RuntimeException("Cannot convert to resource: " + object);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Value objectToValue (final @Nonnull Object object)
      {
//        if (object instanceof Media) // FIXME: Media must be As Identifiable
//          {
//            try
//              {
//                return new UriImplWithAs(((Media)object).get(Media.ID).stringValue(), (As)object);
//              }
//            catch (NotFoundException e)
//              {
//                throw new RuntimeException(e);
//              }
//          }

        if (object instanceof As)
          {
            try
              {
                return new UriImplWithAs(((As)object).as(Identifiable.class).getId().stringValue(), (As)object);
              }
            catch (AsException e)
              {
                return new UriImplWithAs(idFor((As)object).stringValue(), (As)object);
              }
          }

        if (object instanceof Id)
          {
            return valueFactory.createURI(((Id)object).stringValue());
          }

        if (object instanceof String)
          {
            return valueFactory.createLiteral((String)object);
//            return valueFactory.createLiteral((String)object, XMLSchema.STRING);
          }

        if (object instanceof Date)
          {
            final GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime((Date)object);

            try
              {
                return valueFactory. createLiteral(DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar));
              }
            catch (DatatypeConfigurationException e)
              {
                throw new RuntimeException(e);
              }
          }

        throw new RuntimeException("Cannot convert to value: " + object);
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy