Added more sophisticated RDF output for YMarks, including the folder

structure (b:Topic) and support for multiple tags (dc:subject) and
folders (b:hasTopic) via rdf:Bag container.
pull/1/head
apfelmaennchen 13 years ago
parent d31a632951
commit 59bd478ed1

@ -29,18 +29,19 @@ public class YMarks {
YMarkRDF rdf = new YMarkRDF("http://"+sb.peers.myAlternativeAddress()); YMarkRDF rdf = new YMarkRDF("http://"+sb.peers.myAlternativeAddress());
if(post != null && post.containsKey(YMarkEntry.BOOKMARKS_ID)) { if(post != null && post.containsKey(YMarkEntry.BOOKMARKS_ID)) {
final String id = post.get(YMarkEntry.BOOKMARKS_ID); final String id[] = post.get(YMarkEntry.BOOKMARKS_ID).split(":");
final int i = id.indexOf(':'); if(id[1].equals("b")) {
final String bmk_user = id.substring(0,i); final String bmk_user = id[0];
final String bmk_table = TABLES.BOOKMARKS.tablename(bmk_user); final String bmk_table = TABLES.BOOKMARKS.tablename(bmk_user);
final byte[] urlHash = UTF8.getBytes(id.substring(i+1, id.length())); final byte[] urlHash = UTF8.getBytes(id[2]);
Tables.Row bmk_row; Tables.Row bmk_row;
try { try {
bmk_row = sb.tables.select(bmk_table, urlHash); bmk_row = sb.tables.select(bmk_table, urlHash);
rdf.addBookmark(bmk_user, bmk_row); rdf.addBookmark(bmk_user, bmk_row);
} catch (IOException e) { } catch (IOException e) {
} catch (SpaceExceededException e) { } catch (SpaceExceededException e) {
} }
}
} else { } else {
final Iterator<String> iter = sb.tables.iterator(); final Iterator<String> iter = sb.tables.iterator();
while(iter.hasNext()) { while(iter.hasNext()) {
@ -57,7 +58,7 @@ public class YMarks {
} }
} }
} }
prop.put("rdf", rdf.getRDF("RDF/XML")); prop.put("rdf", rdf.getRDF("RDF/XML-ABBREV"));
return prop; return prop;
} }
if(isAdmin || isAuthUser) { if(isAdmin || isAuthUser) {

@ -13,6 +13,7 @@ import net.yacy.cora.lod.vocabulary.DCElements;
import net.yacy.cora.lod.vocabulary.Rdf; import net.yacy.cora.lod.vocabulary.Rdf;
import net.yacy.kelondro.blob.Tables; import net.yacy.kelondro.blob.Tables;
import com.hp.hpl.jena.rdf.model.Bag;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property; import com.hp.hpl.jena.rdf.model.Property;
@ -24,9 +25,11 @@ public class YMarkRDF {
public final static String USER = "USER"; public final static String USER = "USER";
public final static String TYPE = "TYPE"; public final static String TYPE = "TYPE";
public final static String SUBTOPIC = "SUBTOPIC";
private final Map<String, Property> property; private final Map<String, Property> property;
public final static String BOOKMARK = "/Ymarks.rdf?id="; public final static String BOOKMARK = "/YMarks.rdf?id=";
private final StringBuilder resourceURI; private final StringBuilder resourceURI;
private final int len; private final int len;
@ -54,6 +57,7 @@ public class YMarkRDF {
this.property.put(USER, this.model.createProperty(DCElements.creator.getNamespace(), DCElements.creator.name())); this.property.put(USER, this.model.createProperty(DCElements.creator.getNamespace(), DCElements.creator.name()));
this.property.put(TYPE, this.model.createProperty(Rdf.type.getNamespace(), Rdf.type.name())); this.property.put(TYPE, this.model.createProperty(Rdf.type.getNamespace(), Rdf.type.name()));
this.property.put(SUBTOPIC, this.model.createProperty(AnnoteaB.subTopicOf.getNamespace(), AnnoteaB.subTopicOf.name()));
} }
/** /**
@ -70,40 +74,83 @@ public class YMarkRDF {
} }
} }
public void addTopic(final String bmk_user, final String folder) {
this.resourceURI.append(bmk_user);
this.resourceURI.append(":f:");
this.resourceURI.append(UTF8.String(YMarkUtil.getKeyId(folder)));
final Resource topic = this.model.createResource(this.resourceURI.toString());
this.resourceURI.setLength(this.len);
topic.addProperty(this.property.get(YMarkEntry.BOOKMARK.DATE_MODIFIED.key()), YMarkUtil.EMPTY_STRING);
topic.addProperty(this.property.get(YMarkEntry.BOOKMARK.DATE_ADDED.key()), YMarkUtil.EMPTY_STRING);
topic.addProperty(this.property.get(USER), bmk_user);
topic.addProperty(this.property.get(YMarkEntry.BOOKMARK.DESC.key()), YMarkUtil.EMPTY_STRING);
final int i = folder.lastIndexOf(YMarkUtil.FOLDERS_SEPARATOR);
if(i>0)
topic.addProperty(this.property.get(SUBTOPIC), folder.substring(0, i));
topic.addProperty(this.property.get(YMarkEntry.BOOKMARK.TITLE.key()), folder);
topic.addProperty(this.property.get(TYPE), AnnoteaB.Topic.getPredicate());
}
public void addBookmark (final String bmk_user, final Tables.Row bmk_row) { public void addBookmark (final String bmk_user, final Tables.Row bmk_row) {
if(bmk_row == null || bmk_row.get(YMarkEntry.BOOKMARK.PUBLIC.key(), YMarkEntry.BOOKMARK.PUBLIC.deflt()).equals("false")) if(bmk_row == null || bmk_row.get(YMarkEntry.BOOKMARK.PUBLIC.key(), YMarkEntry.BOOKMARK.PUBLIC.deflt()).equals("false"))
return; return;
final Resource bmk;
// create an annotea bookmark resource // create an annotea bookmark resource
this.resourceURI.append(bmk_user); this.resourceURI.append(bmk_user);
this.resourceURI.append(':'); this.resourceURI.append(":b:");
this.resourceURI.append(UTF8.String(bmk_row.getPK())); this.resourceURI.append(UTF8.String(bmk_row.getPK()));
bmk = this.model.createResource(this.resourceURI.toString()); final Resource bmk = this.model.createResource(this.resourceURI.toString());
this.resourceURI.setLength(this.len); this.resourceURI.setLength(this.len);
// add properties // add properties
bmk.addProperty(this.property.get(TYPE), AnnoteaB.Bookmark.getPredicate());
bmk.addProperty(this.property.get(USER), bmk_user); bmk.addProperty(this.property.get(USER), bmk_user);
for (final YMarkEntry.BOOKMARK b : YMarkEntry.BOOKMARK.values()) { for (final YMarkEntry.BOOKMARK b : YMarkEntry.BOOKMARK.values()) {
switch(b) { switch(b) {
case FOLDERS: case FOLDERS:
final String[] folders = bmk_row.get(b.key(), b.deflt()).split(YMarkUtil.TAGS_SEPARATOR); final String[] folders = bmk_row.get(b.key(), b.deflt()).split(b.seperator());
for(String folder : folders) { if(folders.length > 1) {
bmk.addProperty(this.property.get(b.key()), folder); Bag topics = this.model.createBag();
// TODO add Topics to RDF for(String folder : folders) {
topics.add(folder);
this.addTopic(bmk_user, folder);
}
bmk.addProperty(this.property.get(b.key()), topics);
} else {
bmk.addProperty(this.property.get(b.key()), folders[0]);
this.addTopic(bmk_user, folders[0]);
}
break;
case TAGS:
final String[] tags = bmk_row.get(b.key(), b.deflt()).split(b.seperator());
if(tags.length > 1) {
Bag subjects = this.model.createBag();
for(String tag : tags) {
subjects.add(tag);
}
bmk.addProperty(this.property.get(b.key()), subjects);
} else {
bmk.addProperty(this.property.get(b.key()), tags[0]);
} }
break; break;
case DATE_ADDED: case DATE_ADDED:
case DATE_MODIFIED: case DATE_MODIFIED:
final YMarkDate date = new YMarkDate(bmk_row.get(b.key())); final YMarkDate date = new YMarkDate(bmk_row.get(b.key()));
bmk.addProperty(this.property.get(b.key()), date.toISO8601()); bmk.addProperty(this.property.get(b.key()), date.toISO8601());
break; break;
// these cases are inserted for better readable RDF output
case DESC:
case URL:
case TITLE:
bmk.addProperty(this.property.get(b.key()), bmk_row.get(b.key(), b.deflt()));
break;
default: default:
if(this.property.containsKey(b.key())) { if(this.property.containsKey(b.key())) {
bmk.addProperty(this.property.get(b.key()), bmk_row.get(b.key(), b.deflt())); bmk.addProperty(this.property.get(b.key()), bmk_row.get(b.key(), b.deflt()));
} }
} }
} }
bmk.addProperty(this.property.get(TYPE), AnnoteaB.Bookmark.getPredicate());
} }
public void addBookmarks(final String bmk_user, final Iterator<Tables.Row> riter) { public void addBookmarks(final String bmk_user, final Iterator<Tables.Row> riter) {

Loading…
Cancel
Save