it.tidalwave.semantic.io.json.RdfJsonWriter Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* 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: RdfJsonWriter.java,v 012efc9c7b75 2010/07/11 12:34:22 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.semantic.io.json;
import javax.annotation.concurrent.ThreadSafe;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.Writer;
import org.json.me.JSONException;
import org.json.me.JSONWriter;
import it.tidalwave.semantic.io.json.impl.Literals;
import it.tidalwave.util.Id;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFWriter;
/***********************************************************************************************************************
*
* See http://n2.talis.com/wiki/RDF_JSON_Specification
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
@ThreadSafe
public class RdfJsonWriter implements RDFWriter
{
@Nonnull
private final JSONWriter jw;
private Resource prevSubject = null;
private URI prevPredicate = null;
private final List objects = new ArrayList();
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public RdfJsonWriter (final @Nonnull Writer w)
{
this.jw = new JSONWriter(w);
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Nonnull
public RDFFormat getRDFFormat()
{
throw new UnsupportedOperationException(); // FIXME
// return new RDFFormat("RdfJson", "text/rdf+json", new Charset"UTF-8", "json", false, false);
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void startRDF()
throws RDFHandlerException
{
try
{
jw.object();
}
catch (JSONException e)
{
throw new RDFHandlerException(e);
}
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void endRDF()
throws RDFHandlerException
{
try
{
if (!objects.isEmpty())
{
jw.key(prevPredicate.stringValue());
marshalObjects(objects);
}
jw.endObject(); // pending object
jw.endObject(); // root object
}
catch (IOException e)
{
throw new RDFHandlerException(e);
}
catch (JSONException e)
{
throw new RDFHandlerException(e);
}
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void handleNamespace (final @Nonnull String string, final @Nonnull String string1)
throws RDFHandlerException
{
// do nothing
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void handleStatement (final @Nonnull Statement statement)
throws RDFHandlerException
{
try
{
final Resource subject = statement.getSubject();
if ((prevSubject == null) || !prevSubject.equals(subject))
{
if (prevSubject != null)
{
if (!objects.isEmpty())
{
jw.key(prevPredicate.stringValue());
marshalObjects(objects);
}
jw.endObject();
}
jw.key(subject.stringValue()).object();
prevSubject = subject;
objects.clear();
prevPredicate = statement.getPredicate();
}
final URI predicate = statement.getPredicate();
if (!predicate.equals(prevPredicate))
{
jw.key(prevPredicate.stringValue());
marshalObjects(objects);
objects.clear();
}
objects.add(statement.getObject());
prevPredicate = predicate;
}
catch (JSONException e)
{
throw new RDFHandlerException(e);
}
catch (IOException e)
{
throw new RDFHandlerException(e);
}
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void handleComment (final @Nonnull String string)
throws RDFHandlerException
{
// do nothing
}
/*******************************************************************************************************************
*
* Marshal the object part of a predicate.
*
******************************************************************************************************************/
private void marshalObjects (final @Nonnull Collection objects)
throws IOException, JSONException
{
jw.array();
for (final Value object : objects)
{
if (object.getClass().getSimpleName().equals("UriImplWithAs")) // FIXME
// if (object instanceof UriImplWithAs)
{
Literals.marshalReference(jw, new Id(object.stringValue()));
// Literals.marshalReference(jw, idFor((UriImplWithAs)object));
}
else
{
Literals.marshalLiteral(jw, object);
}
}
jw.endArray();
}
}