addproperty和addliteral函数中的错误

时间:2014-05-21 05:27:43

标签: rdf jena tdb

我正在尝试将rdf模型中的电子邮件存储到apche tdb中。这是我的代码:

/*Program to download the mails from mail servers and then to store
  them in tdb store using rdf model*/
//import all the classes needed  




import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.Flags.Flag;
import javax.mail.internet.*;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import static com.hp.hpl.jena.query.ReadWrite.READ ;
import static com.hp.hpl.jena.query.ReadWrite.WRITE ;
import com.hp.hpl.jena.query.ReadWrite ;
import com.hp.hpl.jena.query.Dataset ;
import com.hp.hpl.jena.query.Query ;
import com.hp.hpl.jena.query.QueryExecution ;
import com.hp.hpl.jena.query.QueryExecutionFactory ;
import com.hp.hpl.jena.query.QueryFactory ;
import com.hp.hpl.jena.query.QuerySolution ;
import com.hp.hpl.jena.query.ResultSet ;
import com.hp.hpl.jena.tdb.TDBFactory ;
import email.*; // import this to add properties as entities of email
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;


public class test2 {


public static void main(String[] args) throws MessagingException, IOException {
    IMAPFolder folder = null;
    Store store = null;
    String subjec = null;
    Flag flag = null;
    //String too;
    //Directory where the tdb files will be stored
     String directory = "MYDATABASES/DATA2" ;
     //create the dataset for the tdb store
    Dataset ds = TDBFactory.createDataset(directory) ;
    //create default rdf model
    Model model = ds.getDefaultModel() ;
    //write to the tdb dataset
    ds.begin(ReadWrite.WRITE);
    try 
    { //connecting to the server to download the emails
      Properties props = System.getProperties();
      props.setProperty("mail.store.protocol", "imaps");

      Session session = Session.getDefaultInstance(props, null);

      store = session.getStore("imaps");
      store.connect("imap.gmail.com","hjcooljohny75@gmail.com", "ferry@786");

      //folder = (IMAPFolder) store.getFolder("[Gmail]/Spam"); // This doesn't work for enter code here`other email account
      //to select the paticular types of mails
      folder = (IMAPFolder) store.getFolder("inbox");// This works for both email account


      if(!folder.isOpen())
      folder.open(Folder.READ_WRITE);
      Message[] messages = folder.getMessages();
      System.out.println("No of Messages : " + folder.getMessageCount());
      System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
      System.out.println(messages.length);
      //Displaying the info. of the messages
       for (int i=messages.length-1; i > messages.length-10;i--) 
      {

        System.out.println("*****************************************************************************");
        System.out.println("MESSAGE " + (i + 1) + ":");
        MimeMessage msg = (MimeMessage) messages[i];
        //creating rdf model of the message
        //too=toString(msg.getAllRecipients()[0]);
         Resource mail= model.createResource(msg.getMessageID())
        .addProperty(EMAILRDF.SUBJECT, msg.getSubject())  
        .addProperty(EMAILRDF.TO,msg.getAllRecipients()[0])
        .addLiteral(EMAILRDF.FROM,msg.getFrom()[0])
        .addLiteral(EMAILRDF.ENCODING,msg.getEncoding())
        .addLiteral(EMAILRDF.CONTENT,msg.getContent())
        .addLiteral(EMAILRDF.DATE,msg.getReceivedDate())
        .addLiteral(EMAILRDF.CONTENT_TYPE,msg.getContentType());

    // list the statements in the graph
    StmtIterator iter = model.listStatements();

    // print out the predicate, subject and object of each statement
    while (iter.hasNext()) {
        Statement stmt      = iter.nextStatement();         // get next statement
        Resource  subject   = stmt.getSubject();   // get the subject
        Property  predicate = stmt.getPredicate(); // get the predicate
        RDFNode   object    = stmt.getObject();    // get the object

        System.out.print(subject.toString());
        System.out.print(" "+ predicate.toString() + " ");
        if (object instanceof Resource) {
            System.out.print(object.toString());
        } else {
            // object is a literal
            System.out.print(" \"" + object.toString() + "\"");
        }
        System.out.println(" .");
    }
        //System.out.println(msg.getMessageNumber());
        //Object String;
        //System.out.println(folder.getUID(msg)

        //subject = msg.getSubject();

        //System.out.println("Subject: " + subject);
        //System.out.println("From: " + msg.getFrom()[0]);
        //System.out.println("To: "+msg.getAllRecipients()[0]);
        //System.out.println("Date: "+msg.getReceivedDate());
        //System.out.println("Size: "+msg.getSize());
        //System.out.println("Id: "+msg.getMessageID());
        //System.out.println(msg.getFlags());
        //System.out.println("Body: \n"+ msg.getContent());
        //System.out.println(msg.getContentType());

      }
    }
    finally 
    {  //closing the connection
      if (folder != null && folder.isOpen()) { folder.close(true); }
      if (store != null) { store.close(); }
    }


 //closing the dataset
ds.commit();
ds.end();
}
}

但是这会在addLteral()方法中出错。我尝试使用AddProperty()但是也会出错。

error: WARN [main] (LiteralLabelImpl.java:141) - inventing a datatype for class com.sun.mail.imap.protocol.IMAPAddress
Exception in thread "main" java.lang.NullPointerException
    at com.hp.hpl.jena.datatypes.TypeMapper.getTypeByValue(TypeMapper.java:156)
    at com.hp.hpl.jena.graph.impl.LiteralLabelImpl.<init>(LiteralLabelImpl.java:131)
    at com.hp.hpl.jena.graph.impl.LiteralLabelFactory.create(LiteralLabelFactory.java:77)
    at com.hp.hpl.jena.rdf.model.impl.ModelCom.createTypedLiteral(ModelCom.java:781)
    at com.hp.hpl.jena.rdf.model.impl.ResourceImpl.addLiteral(ResourceImpl.java:263)
    at test2.main(test2.java:80)

我认为这可能是由于我在addproperty()addLiteral()函数中输入的类的数据类型。
任何想法???

1 个答案:

答案 0 :(得分:1)

JavaDoc可能已经充分回答了这个问题。 Resource定义了许多addLiteral方法,每个方法都接受一个属性,第二个参数类型为boolean,char,double,float,Literal,long或Object。基于警告“WARN [main](LiteralLabelImpl.java:141) - 为

创建数据类型
  

class com.sun.mail.imap.protocol.IMAPAddress,“听起来你正在使用addLiteral(Property,Object)方法。

当你这样做时,正如JavaDoc所说,你正试图

  

将具有typed-literal值o的属性p添加到此资源,即   将(this,p,typed(o))添加到此模型中。 ......   typed literal等于使用构造的文字   this.getModel()。createTypedLiteral(○)。

您可以从stacktrace中看到正在调用createTypedLiteral,并且没有为com.sun.mail.imap.protocol.IMAPAddress定义数据类型也就不足为奇了。无论哪个对addLiteral的调用都有一个IMAPAddress作为第二个参数,您需要将第二个参数替换为可以转换为类型文字的调用。最简单的方法可能是使用它的toString()方法。