You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Dick Murray <da...@googlemail.com> on 2012/02/21 15:33:14 UTC

Looking for LiteralFactory.createLiteral(String v, String language) equivalent.

Hello all.

I'm looking for a LiteralFactory.createLiteral(String v, String 
language) equivalent. I know Model has a createLiteral(String v, String 
language) but I don't have a Model at the point I need to create the 
Literal. I've followed the Model createLiteral but either got lost or 
confused or both!

In context I have the following method which creates a Statement based 
on some parameters and uses ResourceFactory to create?.

     public Statement getStatement(VelocityContext context) {
         Resource subject = 
ResourceFactory.createResource(evaluate(context, getSubjectURI()));

         Property property = 
ResourceFactory.createProperty(evaluate(context, getPropertyURI()));

         Resource objectType = 
ResourceFactory.createResource(evaluate(context, getObjectType()));

         if (objectType.equals(RDFS.Resource)) {
             return ResourceFactory.createStatement(subject, property, 
ResourceFactory.createResource(evaluate(context, getObjectValue())));
         }

         if (objectType.equals(RDFS.Literal)) {
             if (hasObjectDatatype()) {
                 return ResourceFactory.createStatement(subject, 
property, ResourceFactory.createTypedLiteral(evaluate(context, 
getObjectValue()), 
TypeMapper.getInstance().getTypeByName(evaluate(context, 
getObjectDatatype()))));
             } else {
                 if (hasLanguage()) {
                     return ResourceFactory.createStatement(subject, 
property, NEED_TO_CREATE_A_LITERAL_WITH_LANGUAGE);
                 } else {
                     return ResourceFactory.createStatement(subject, 
property, ResourceFactory.createPlainLiteral(evaluate(context, 
getObjectValue())));
                 }
             }
         }

         throw new Exception(String.format("Unknown objectType [%s].", 
objectType));
     }


Re: Looking for LiteralFactory.createLiteral(String v, String language) equivalent.

Posted by Dick Murray <da...@googlemail.com>.
On 21/02/12 15:28, Chris Dollin wrote:
> Dave wrote:
>
>> On 21/02/12 14:33, Dick Murray wrote:
>>> Hello all.
>>>
>>> I'm looking for a LiteralFactory.createLiteral(String v, String
>>> language) equivalent. I know Model has a createLiteral(String v, String
>>> language) but I don't have a Model at the point I need to create the
>>> Literal. I've followed the Model createLiteral but either got lost or
>>> confused or both!
>> Looks like an omission from ResourceFactory.
>>
>> Possibly worth a Jira.
I've created https://issues.apache.org/jira/browse/JENA-213 based on 
your suggestion Dave.
>>
>> As a work around you could do something like:
>>
>>    public Literal createLangLiteral(String lex, String lang) {
>>       return new LiteralImpl( Node.createLiteral(lex, lang, null), null);
>>    }
This works :-)
> Another workaround is to have a static empty Model lying around,
> and ask it to create the literal.
>
> Chris
>
I thought about that but it seemed "a sledgehammer to crack a nut"...



Re: Looking for LiteralFactory.createLiteral(String v, String language) equivalent.

Posted by Chris Dollin <ch...@epimorphics.com>.
Dave wrote:

> On 21/02/12 14:33, Dick Murray wrote:
> > Hello all.
> >
> > I'm looking for a LiteralFactory.createLiteral(String v, String
> > language) equivalent. I know Model has a createLiteral(String v, String
> > language) but I don't have a Model at the point I need to create the
> > Literal. I've followed the Model createLiteral but either got lost or
> > confused or both!
> 
> Looks like an omission from ResourceFactory.
> 
> Possibly worth a Jira.
> 
> As a work around you could do something like:
> 
>   public Literal createLangLiteral(String lex, String lang) {
>      return new LiteralImpl( Node.createLiteral(lex, lang, null), null);
>   }

Another workaround is to have a static empty Model lying around,
and ask it to create the literal.

Chris

-- 
"I know it was late, but Mountjoy never bothers,                /Archer's Goon/
 so long as it's the full two thousand words."

Epimorphics Ltd, http://www.epimorphics.com
Registered address: Court Lodge, 105 High Street, Portishead, Bristol BS20 6PT
Epimorphics Ltd. is a limited company registered in England (number 7016688)

Re: Looking for LiteralFactory.createLiteral(String v, String language) equivalent.

Posted by Dave Reynolds <da...@gmail.com>.
On 21/02/12 14:33, Dick Murray wrote:
> Hello all.
>
> I'm looking for a LiteralFactory.createLiteral(String v, String
> language) equivalent. I know Model has a createLiteral(String v, String
> language) but I don't have a Model at the point I need to create the
> Literal. I've followed the Model createLiteral but either got lost or
> confused or both!

Looks like an omission from ResourceFactory.

Possibly worth a Jira.

As a work around you could do something like:

  public Literal createLangLiteral(String lex, String lang) {
     return new LiteralImpl( Node.createLiteral(lex, lang, null), null);
  }

Dave