it.tidalwave.semantic.io.json.RdfJsonParser 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: RdfJsonParser.java,v 012efc9c7b75 2010/07/11 12:34:22 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.semantic.io.json;
import org.openrdf.model.Statement;
import org.openrdf.model.Value;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Enumeration;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
import org.json.me.JSONTokener;
import it.tidalwave.semantic.io.json.impl.Literals;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.helpers.RDFParserBase;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class RdfJsonParser extends RDFParserBase
{
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Nonnull
public RDFFormat getRDFFormat()
{
throw new UnsupportedOperationException("Not supported yet.");
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void parse (final @Nonnull InputStream is, final @Nonnull String string)
throws IOException, RDFParseException, RDFHandlerException
{
parse(new InputStreamReader(is, "UTF-8"), string);
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void parse (final @Nonnull Reader r, final @Nonnull String string)
throws IOException, RDFParseException, RDFHandlerException
{
try
{
final JSONTokener jt = new JSONTokener(readString(r));
final JSONObject rootObject = (JSONObject)jt.nextValue();
unmarshalGraph(rootObject);
}
catch (JSONException e)
{
throw new RDFParseException(e);
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private void unmarshalGraph (final @Nonnull JSONObject parentObject)
throws JSONException, RDFParseException, RDFHandlerException
{
rdfHandler.startRDF();
for (final Enumeration e1 = parentObject.keys(); e1.hasMoreElements();)
{
final String subject = e1.nextElement();
final JSONObject object1 = parentObject.getJSONObject(subject);
for (Enumeration e2 = object1.keys(); e2.hasMoreElements(); )
{
final String predicate = e2.nextElement();
final JSONArray objects = object1.getJSONArray(predicate);
for (int i = 0; i < objects.length(); i++)
{
final Value object = Literals.unmarshalLiteral(objects.getJSONObject(i));
final Statement statement = createStatement(createURI(subject), createURI(predicate), object);
rdfHandler.handleStatement(statement);
}
}
}
rdfHandler.endRDF();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private static String readString (final @Nonnull Reader r)
throws IOException
{
final StringBuilder buffer = new StringBuilder();
for (;;)
{
int c = r.read();
if (c < 0)
{
break;
}
buffer.append((char)c);
}
return buffer.toString();
}
}