-1

我正在尝试将我的电子邮件以 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()oraddLiteral()函数中输入的类的数据类型。
任何想法???

4

1 回答 1

1

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

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

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

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

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

于 2014-05-21T13:36:00.663 回答