better abstraction for result writers using controlled vocabularies and

URIRefs
pull/1/head
Michael Peter Christen 13 years ago
parent 136fcb1ad9
commit dcc72799c4

@ -35,6 +35,8 @@ import java.util.Set;
import net.yacy.cora.date.GenericFormatter; import net.yacy.cora.date.GenericFormatter;
import net.yacy.cora.date.ISO8601Formatter; import net.yacy.cora.date.ISO8601Formatter;
import net.yacy.cora.lod.vocabulary.DublinCore;
import net.yacy.cora.lod.vocabulary.Geo;
import net.yacy.cora.protocol.HeaderFramework; import net.yacy.cora.protocol.HeaderFramework;
import net.yacy.kelondro.data.meta.DigestURI; import net.yacy.kelondro.data.meta.DigestURI;
@ -42,30 +44,29 @@ public class RSSMessage implements Hit, Comparable<RSSMessage>, Comparator<RSSMe
public static enum Token { public static enum Token {
title("title,atom:title,rss:title"), title(new String[]{"title","atom:title","rss:title",DublinCore.Title.getURIref()}),
link("link,rss:link,atom:link"), link(new String[]{"link","atom:link","rss:link"}),
description("description,rss:description,subtitle,atom:subtitle"), description(new String[]{"description","subtitle","atom:subtitle","rss:description", DublinCore.Description.getURIref()}),
pubDate("pubDate,lastBuildDate,rss:lastBuildDate,updated,rss:updated"), pubDate(new String[]{"pubDate","lastBuildDate","updated","rss:lastBuildDate","rss:updated"}),
copyright("copyright,dc:publisher,publisher"), copyright(new String[]{"copyright","publisher",DublinCore.Publisher.getURIref()}),
author("author,dc:creator,creator"), author(new String[]{"author","creator",DublinCore.Creator.getURIref()}),
subject("subject,dc:subject"), subject(new String[]{"subject",DublinCore.Subject.getURIref()}),
category("category"), category(new String[]{"category"}),
referrer("referrer,referer"), referrer(new String[]{"referrer","referer"}),
language("language"), language(new String[]{"language",DublinCore.Language.getURIref()}),
guid("guid"), guid(new String[]{"guid"}),
ttl("ttl"), ttl(new String[]{"ttl"}),
docs("docs"), docs(new String[]{"docs"}),
size("size,length"), size(new String[]{"size","length"}),
lon("geo:long,geo:lon"), lon(new String[]{"geo:lon",Geo.Long.getURIref()}),
lat("geo:lat"); lat(new String[]{Geo.Lat.getURIref()});
//point("gml:pos,georss:point,coordinates"); //point("gml:pos,georss:point,coordinates");
private Set<String> keys; private Set<String> keys;
private Token(final String keylist) { private Token(final String[] keylist) {
final String[] k = keylist.split(",");
this.keys = new HashSet<String>(); this.keys = new HashSet<String>();
this.keys.addAll(Arrays.asList(k)); this.keys.addAll(Arrays.asList(keylist));
} }
public String valueFrom(final Map<String, String> map, final String dflt) { public String valueFrom(final Map<String, String> map, final String dflt) {

@ -55,6 +55,14 @@ public interface Vocabulary {
*/ */
public String getPredicate(); public String getPredicate();
/**
* The URI Reference as defined in http://www.w3.org/TR/rdf-concepts/ 2.2.3
* This is a combination of the namespace prefic and the constant name,
* concatenated with ':'.
* @return
*/
public String getURIref();
/** /**
* get a set of literals that are allowed for the predicate as values * get a set of literals that are allowed for the predicate as values
* @return * @return

@ -204,4 +204,9 @@ public enum CreativeCommons implements Vocabulary {
public String getPredicate() { public String getPredicate() {
return this.predicate; return this.predicate;
} }
@Override
public String getURIref() {
return PREFIX + ':' + this.name();
}
} }

@ -41,4 +41,9 @@ public enum DCTerms implements Vocabulary {
public String getPredicate() { public String getPredicate() {
return this.predicate; return this.predicate;
} }
@Override
public String getURIref() {
return PREFIX + ':' + this.name();
}
} }

@ -51,10 +51,11 @@ public enum DublinCore implements Vocabulary {
public final static String IDENTIFIER = "http://dublincore.org/documents/2010/10/11/dces/"; public final static String IDENTIFIER = "http://dublincore.org/documents/2010/10/11/dces/";
public final static String PREFIX = "dc"; public final static String PREFIX = "dc";
private final String predicate; private final String predicate, uriref;
private DublinCore() { private DublinCore() {
this.predicate = IDENTIFIER + this.name().toLowerCase(); this.predicate = IDENTIFIER + this.name().toLowerCase();
this.uriref = PREFIX + ':' + this.name().toLowerCase();
} }
@Override @Override
@ -76,4 +77,9 @@ public enum DublinCore implements Vocabulary {
public String getPredicate() { public String getPredicate() {
return this.predicate; return this.predicate;
} }
@Override
public String getURIref() {
return this.uriref;
}
} }

@ -11,12 +11,12 @@
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * version 2.1 of the License, or (at your option) any later version.
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt * along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>. * If not, see <http://www.gnu.org/licenses/>.
@ -47,7 +47,7 @@ public enum Foaf implements Vocabulary {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }
@Override @Override
public Set<Literal> getLiterals() { public Set<Literal> getLiterals() {
return null; return null;
@ -59,4 +59,10 @@ public enum Foaf implements Vocabulary {
return null; return null;
} }
@Override
public String getURIref() {
// TODO Auto-generated method stub
return null;
}
} }

@ -37,10 +37,11 @@ public enum Geo implements Vocabulary {
public final static String NAMESPACE = "http://www.w3.org/2003/01/geo/wgs84_pos#"; public final static String NAMESPACE = "http://www.w3.org/2003/01/geo/wgs84_pos#";
public final static String PREFIX = "geo"; public final static String PREFIX = "geo";
private final String predicate; private final String predicate, uriref;
private Geo() { private Geo() {
this.predicate = NAMESPACE + this.name().toLowerCase(); this.predicate = NAMESPACE + this.name().toLowerCase();
this.uriref = PREFIX + ':' + this.name().toLowerCase();
} }
@Override @Override
@ -62,4 +63,9 @@ public enum Geo implements Vocabulary {
public String getPredicate() { public String getPredicate() {
return this.predicate; return this.predicate;
} }
@Override
public String getURIref() {
return this.uriref;
}
} }

@ -114,4 +114,9 @@ public enum HttpHeader implements Vocabulary {
public String getPredicate() { public String getPredicate() {
return this.predicate; return this.predicate;
} }
@Override
public String getURIref() {
return PREFIX + ':' + this.name();
}
} }

@ -61,4 +61,9 @@ public enum Owl implements Vocabulary {
public String getPredicate() { public String getPredicate() {
return this.predicate; return this.predicate;
} }
@Override
public String getURIref() {
return PREFIX + ':' + this.name();
}
} }

@ -66,4 +66,9 @@ public enum Rdf implements Vocabulary {
return this.predicate; return this.predicate;
} }
@Override
public String getURIref() {
return PREFIX + ':' + this.name();
}
} }

@ -112,4 +112,8 @@ public enum YaCyMetadata implements Vocabulary {
return this.predicate; return this.predicate;
} }
@Override
public String getURIref() {
return PREFIX + ':' + this.name();
}
} }

@ -171,19 +171,21 @@ public class EnhancedXMLResponseWriter implements QueryResponseWriter {
private static void writeField(final Writer writer, final FieldType type, final String name, final String value) throws IOException { private static void writeField(final Writer writer, final FieldType type, final String name, final String value) throws IOException {
String typeName = type.getTypeName(); String typeName = type.getTypeName();
if (typeName.equals("text_general") || typeName.equals("string") || typeName.equals("text_en_splitting_tight")) { if (typeName.equals(SolrType.text_general.printName()) ||
typeName.equals(SolrType.string.printName()) ||
typeName.equals(SolrType.text_en_splitting_tight.printName())) {
writeTag(writer, "str", name, value, true); writeTag(writer, "str", name, value, true);
} else if (typeName.equals("boolean")) { } else if (typeName.equals(SolrType.bool.printName())) {
writeTag(writer, "bool", name, "F".equals(value) ? "false" : "true", true); writeTag(writer, "bool", name, "F".equals(value) ? "false" : "true", true);
} else if (typeName.equals("int")) { } else if (typeName.equals(SolrType.integer.printName())) {
writeTag(writer, "int", name, value, true); writeTag(writer, "int", name, value, true);
} else if (typeName.equals("long")) { } else if (typeName.equals(SolrType.tlong.printName())) {
writeTag(writer, "long", name, value, true); writeTag(writer, "long", name, value, true);
} else if (typeName.equals("date")) { } else if (typeName.equals(SolrType.date.printName())) {
writeTag(writer, "date", name, DateField.formatExternal(new Date(Long.parseLong(value))), true); writeTag(writer, "date", name, DateField.formatExternal(new Date(Long.parseLong(value))), true);
} else if (typeName.equals("float")) { } else if (typeName.equals(SolrType.tfloat.printName())) {
writeTag(writer, "float", name, value, true); writeTag(writer, "float", name, value, true);
} else if (typeName.equals("double")) { } else if (typeName.equals(SolrType.tdouble.printName())) {
writeTag(writer, "double", name, value, true); writeTag(writer, "double", name, value, true);
} }
} }

@ -26,6 +26,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import net.yacy.cora.document.RSSMessage;
import net.yacy.cora.lod.vocabulary.DublinCore;
import net.yacy.search.index.YaCySchema;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.Fieldable;
import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.NamedList;
@ -102,11 +106,11 @@ public class OpensearchResponseWriter implements QueryResponseWriter {
//resHead.maxScore = response.maxScore(); //resHead.maxScore = response.maxScore();
// write header // write header
startTagOpen(writer, "channel"); openTag(writer, "channel");
solitaireTag(writer, "opensearch:totalResults", Integer.toString(resHead.numFound)); solitaireTag(writer, "opensearch:totalResults", Integer.toString(resHead.numFound));
solitaireTag(writer, "opensearch:startIndex", Integer.toString(resHead.offset)); solitaireTag(writer, "opensearch:startIndex", Integer.toString(resHead.offset));
solitaireTag(writer, "opensearch:itemsPerPage", Integer.toString(resHead.rows)); solitaireTag(writer, "opensearch:itemsPerPage", Integer.toString(resHead.rows));
solitaireTag(writer, "title", this.title); solitaireTag(writer, RSSMessage.Token.title.name(), this.title);
//solitaireTag(writer, "description", ""); //solitaireTag(writer, "description", "");
//solitaireTag(writer, "link", ""); //solitaireTag(writer, "link", "");
//solitaireTag(writer, "image", ""); //solitaireTag(writer, "image", "");
@ -116,7 +120,7 @@ public class OpensearchResponseWriter implements QueryResponseWriter {
SolrIndexSearcher searcher = request.getSearcher(); SolrIndexSearcher searcher = request.getSearcher();
DocIterator iterator = response.iterator(); DocIterator iterator = response.iterator();
for (int i = 0; i < responseCount; i++) { for (int i = 0; i < responseCount; i++) {
startTagOpen(writer, "item"); openTag(writer, "item");
int id = iterator.nextDoc(); int id = iterator.nextDoc();
Document doc = searcher.doc(id, DEFAULT_FIELD_LIST); Document doc = searcher.doc(id, DEFAULT_FIELD_LIST);
List<Fieldable> fields = doc.getFields(); List<Fieldable> fields = doc.getFields();
@ -126,19 +130,50 @@ public class OpensearchResponseWriter implements QueryResponseWriter {
for (int j = 0; j < fieldc; j++) { for (int j = 0; j < fieldc; j++) {
Fieldable f1 = fields.get(j); Fieldable f1 = fields.get(j);
String fieldName = f1.name(); String fieldName = f1.name();
if ("id".equals(fieldName)) {writer.write("<guid isPermaLink=\"false\">"); writer.write(f1.stringValue()); writer.write("</guid>"); writer.write(lb); continue;} if (YaCySchema.id.name().equals(fieldName)) {
if ("sku".equals(fieldName)) {solitaireTag(writer, "link", f1.stringValue()); continue;} solitaireTag(writer, RSSMessage.Token.guid.name(), f1.stringValue(), "isPermaLink=\"false\"");
if ("title".equals(fieldName)) {solitaireTag(writer, "title", f1.stringValue()); texts.add(f1.stringValue()); continue;} continue;
if ("last_modified".equals(fieldName)) {solitaireTag(writer, "pubDate", f1.stringValue()); continue;} }
if ("".equals(fieldName)) {solitaireTag(writer, "dc:publisher", f1.stringValue()); continue;} if (YaCySchema.sku.name().equals(fieldName)) {
if ("".equals(fieldName)) {solitaireTag(writer, "dc:creator", f1.stringValue()); continue;} solitaireTag(writer, RSSMessage.Token.link.name(), f1.stringValue());
if ("description".equals(fieldName)) {description = f1.stringValue(); solitaireTag(writer, "dc:subject", description); texts.add(description); continue;} continue;
if ("text_t".equals(fieldName)) {texts.add(f1.stringValue()); continue;} }
if ("h1_txt".equals(fieldName) || "h2_txt".equals(fieldName) || "h3_txt".equals(fieldName) || if (YaCySchema.title.name().equals(fieldName)) {
"h4_txt".equals(fieldName) || "h5_txt".equals(fieldName) || "h6_txt".equals(fieldName)) {texts.add(f1.stringValue()); continue;} solitaireTag(writer, RSSMessage.Token.title.name(), f1.stringValue());
texts.add(f1.stringValue());
continue;
}
if (YaCySchema.last_modified.name().equals(fieldName)) {
solitaireTag(writer, RSSMessage.Token.pubDate.name(), f1.stringValue());
continue;
}
if (YaCySchema.publisher_t.name().equals(fieldName)) {
solitaireTag(writer, DublinCore.Publisher.getURIref(), f1.stringValue());
continue;
}
if (YaCySchema.author.name().equals(fieldName)) {
solitaireTag(writer, DublinCore.Creator.getURIref(), f1.stringValue());
continue;
}
if (YaCySchema.description.name().equals(fieldName)) {
description = f1.stringValue();
solitaireTag(writer, DublinCore.Description.getURIref(), description);
texts.add(description);
continue;
}
if (YaCySchema.text_t.name().equals(fieldName)) {
texts.add(f1.stringValue());
continue;
}
if (YaCySchema.h1_txt.name().equals(fieldName) || YaCySchema.h2_txt.name().equals(fieldName) ||
YaCySchema.h3_txt.name().equals(fieldName) || YaCySchema.h4_txt.name().equals(fieldName) ||
YaCySchema.h5_txt.name().equals(fieldName) || YaCySchema.h6_txt.name().equals(fieldName)) {
texts.add(f1.stringValue());
continue;
}
} }
// compute snippet from texts // compute snippet from texts
solitaireTag(writer, "description", description); solitaireTag(writer, RSSMessage.Token.description.name(), description);
closeTag(writer, "item"); closeTag(writer, "item");
} }
@ -146,7 +181,7 @@ public class OpensearchResponseWriter implements QueryResponseWriter {
writer.write(XML_STOP); writer.write(XML_STOP);
} }
public static void startTagOpen(final Writer writer, final String tag) throws IOException { public static void openTag(final Writer writer, final String tag) throws IOException {
writer.write('<'); writer.write(tag); writer.write('>'); writer.write(lb); writer.write('<'); writer.write(tag); writer.write('>'); writer.write(lb);
} }
@ -155,11 +190,22 @@ public class OpensearchResponseWriter implements QueryResponseWriter {
} }
public static void solitaireTag(final Writer writer, final String tagname, String value) throws IOException { public static void solitaireTag(final Writer writer, final String tagname, String value) throws IOException {
if (value == null || value.length() == 0) return;
writer.write("<"); writer.write(tagname); writer.write('>'); writer.write("<"); writer.write(tagname); writer.write('>');
writer.write(value); writer.write(value);
writer.write("</"); writer.write(tagname); writer.write('>'); writer.write(lb); writer.write("</"); writer.write(tagname); writer.write('>'); writer.write(lb);
} }
public static void solitaireTag(final Writer writer, final String tagname, String value, String attr) throws IOException {
if (value == null || value.length() == 0) return;
writer.write("<"); writer.write(tagname);
if (attr.charAt(0) != ' ') writer.write(' ');
writer.write(attr);
writer.write('>');
writer.write(value);
writer.write("</"); writer.write(tagname); writer.write('>'); writer.write(lb);
}
} }
/* /*

@ -31,8 +31,10 @@ public enum SolrType {
text_en_splitting_tight, text_en_splitting_tight,
date, date,
integer("int"), integer("int"),
tdouble, bool("boolean"),
bool("boolean"); tlong("long"),
tfloat("float"),
tdouble("double");
private String printName; private String printName;
private SolrType() { private SolrType() {

@ -9,12 +9,12 @@
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * version 2.1 of the License, or (at your option) any later version.
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt * along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>. * If not, see <http://www.gnu.org/licenses/>.
@ -28,18 +28,18 @@ import net.yacy.cora.lod.Literal;
import net.yacy.cora.lod.Vocabulary; import net.yacy.cora.lod.Vocabulary;
public enum MetadataVocabulary implements Vocabulary { public enum MetadataVocabulary implements Vocabulary {
moddate, url; moddate, url;
public final static String IDENTIFIER = "http://yacy.net/metadata"; public final static String IDENTIFIER = "http://yacy.net/metadata";
public final static String PREFIX = "ym"; public final static String PREFIX = "ym";
private final String predicate; private final String predicate;
private MetadataVocabulary() { private MetadataVocabulary() {
this.predicate = PREFIX + ":" + this.name().toLowerCase(); this.predicate = PREFIX + ":" + this.name().toLowerCase();
} }
@Override @Override
public String getNamespace() { public String getNamespace() {
return IDENTIFIER; return IDENTIFIER;
@ -59,4 +59,9 @@ public enum MetadataVocabulary implements Vocabulary {
public Set<Literal> getLiterals() { public Set<Literal> getLiterals() {
return null; return null;
} }
@Override
public String getURIref() {
return PREFIX + ':' + this.name();
}
} }

Loading…
Cancel
Save