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

it.tidalwave.semantic.io.json.impl.Literals Maven / Gradle / Ivy

There is a newer version: 1.0.16
Show newest version
/***********************************************************************************************************************
 *
 * 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: Literals.java,v c43881ee6e21 2010/07/11 12:03:30 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.semantic.io.json.impl;

import javax.annotation.Nonnull;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.DatatypeConfigurationException;
import org.json.me.JSONException;
import org.json.me.JSONObject;
import org.json.me.JSONWriter;
import it.tidalwave.util.Id;
import org.openrdf.model.Literal;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.vocabulary.XMLSchema;
import org.openrdf.model.impl.ValueFactoryImpl;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public final class Literals
  {
    private static final String KEY_DATATYPE = "datatype";
    private static final String KEY_TYPE = "type";
    private static final String KEY_VALUE = "value";
    
    private static final String TYPE_LITERAL = "literal";
    private static final String TYPE_URI = "uri";
    private static final String TYPE_BNODE = "bnode";

    private static final String DATE_PATTERN = "yyyy-MM-dd'T'hh:mm:ss";

    private static final ValueFactory valueFactory = new ValueFactoryImpl(); // FIXME: use Locator

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private Literals()
      {
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public static void marshalLiteral (final @Nonnull JSONWriter jw, final @Nonnull Value value)
      throws JSONException
      {
        if (value instanceof URI)
          {
            marshalUri(jw, value.stringValue());
          }
        else
          {
            final Literal literal = (Literal)value;
            final URI dataType = literal.getDatatype();

            if ((dataType == null) || dataType.equals(XMLSchema.STRING))
              {
                marshalString(jw, literal.stringValue());
              }
            else if (dataType.stringValue().equals("urn:bnode"))
              {
                marshalReference(jw, new Id(literal.stringValue()));
              }
            else if (dataType.equals(XMLSchema.DATETIME))
              {
                // TODO: literal.calendarValue().getXmlFormat()?
                marshalDate(jw, literal.calendarValue().toGregorianCalendar().getTime());
              }
            else
              {
                throw new IllegalArgumentException("Unknown dataType: " + dataType);
              }
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public static void marshalReference (final @Nonnull JSONWriter jw, final @Nonnull Id id)
      throws JSONException
      {
        jw.object().key(KEY_VALUE).value(id.stringValue())
                   .key(KEY_TYPE).value(TYPE_BNODE)
                   .endObject();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public static Value unmarshalLiteral (final @Nonnull JSONObject object)
      throws JSONException
      {
        final String type = object.getString(KEY_TYPE);

        if (type.equals(TYPE_BNODE))
          {
//            return new UriImplWithAs(object.getString(KEY_VALUE), null);
            return valueFactory.createURI(object.getString(KEY_VALUE));
          }
        
        final String dataType = object.optString(KEY_DATATYPE);

        if (dataType.equals(XMLSchema.DATETIME.stringValue()))
          {
            try
              {
                final DateFormat df = new SimpleDateFormat(DATE_PATTERN);
                final GregorianCalendar gregorianCalendar = (GregorianCalendar)GregorianCalendar.getInstance();
                gregorianCalendar.setTime(df.parse(object.getString(KEY_VALUE)));
                return valueFactory.createLiteral(DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar));
              }
            catch (DatatypeConfigurationException e)
              {
                throw new JSONException(e);
              }
            catch (ParseException e)
              {
                throw new JSONException(e);
              }
          }

        if (dataType.equals(""))
          {
            return valueFactory.createLiteral(object.getString(KEY_VALUE), XMLSchema.STRING);
          }

        throw new IllegalArgumentException("datatype=" + dataType);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private static void marshalString (final @Nonnull JSONWriter jw, final @Nonnull String string)
      throws JSONException
      {
        jw.object().key(KEY_VALUE).value(string)
                   .key(KEY_TYPE).value(TYPE_LITERAL)
                   .endObject();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private static void marshalDate (final @Nonnull JSONWriter jw, final @Nonnull Date date)
      throws JSONException
      {
        final DateFormat df = new SimpleDateFormat(DATE_PATTERN);
        jw.object().key(KEY_VALUE).value(df.format(date))
                   .key(KEY_TYPE).value(TYPE_LITERAL)
                   .key(KEY_DATATYPE).value(XMLSchema.DATETIME.stringValue())
                   .endObject();// FIXME: needs datatype definition
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private static void marshalUri (final @Nonnull JSONWriter jw, final @Nonnull String uri)
      throws JSONException
      {
        jw.object().key(KEY_VALUE).value(uri)
                   .key(KEY_TYPE).value(TYPE_URI)
                   .endObject();
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy