Merge branch 'develop' of https://github.com/vivo-project/Vitro into develop

I have no idea why this is a merge. My develop branch is behind and I want to push changes to git, and this form is what I got.
This commit is contained in:
tworrall 2013-02-11 16:15:56 -05:00
commit 1f66e0eca4
8 changed files with 905 additions and 256 deletions

View file

@ -5,7 +5,8 @@ package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
//import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -129,7 +130,7 @@ public class UrlBuilder {
return getUrl(Route.LOGOUT);
}
public static class ParamMap extends HashMap<String, String> {
public static class ParamMap extends LinkedHashMap<String, String> {
private static final long serialVersionUID = 1L;
public ParamMap() { }
@ -276,7 +277,7 @@ public class UrlBuilder {
}
if (profileUrl != null) {
HashMap<String, String> specialParams = getModelParams(vreq);
LinkedHashMap<String, String> specialParams = getModelParams(vreq);
if(specialParams.size() != 0) {
profileUrl = addParams(profileUrl, new ParamMap(specialParams));
}
@ -325,9 +326,9 @@ public class UrlBuilder {
//To be used in different property templates so placing method for reuse here
//Check if special params included, specifically for menu management and other models
public static HashMap<String,String> getModelParams(VitroRequest vreq) {
public static LinkedHashMap<String,String> getModelParams(VitroRequest vreq) {
HashMap<String,String> specialParams = new HashMap<String, String>();
LinkedHashMap<String,String> specialParams = new LinkedHashMap<String, String>();
if(vreq != null) {
//this parameter is sufficient to switch to menu model
String useMenuModelParam = vreq.getParameter(DisplayVocabulary.SWITCH_TO_DISPLAY_MODEL);

View file

@ -84,7 +84,8 @@ public class I18n {
/**
* Get an I18nBundle by this name. The request provides the preferred
* Locale, the theme directory and the development mode flag.
* Locale, the application directory, the theme directory and the
* development mode flag.
*
* If the request indicates that the system is in development mode, then the
* cache is cleared on each request.
@ -155,7 +156,8 @@ public class I18n {
// ----------------------------------------------------------------------
/**
* Instead of looking in the classpath, look in the theme directory.
* Instead of looking in the classpath, look in the theme i18n directory and
* the application i18n directory.
*/
private static class ThemeBasedControl extends ResourceBundle.Control {
private static final String BUNDLE_DIRECTORY = "i18n/";
@ -177,7 +179,8 @@ public class I18n {
/**
* Don't look in the class path, look in the current servlet context, in
* the bundle directory under the theme directory.
* the bundle directory under the theme directory and in the bundle
* directory under the application directory.
*/
@Override
public ResourceBundle newBundle(String baseName, Locale locale,

View file

@ -72,9 +72,9 @@ public class LocaleSelectionFilter implements Filter {
*/
private static class LocaleSelectionRequestWrapper extends
HttpServletRequestWrapper {
private final HttpServletRequest request;
private final Locale selectedLocale;
private final List<Locale> locales;
@SuppressWarnings("unchecked")
public LocaleSelectionRequestWrapper(HttpServletRequest request,
Locale selectedLocale) {
super(request);
@ -82,31 +82,32 @@ public class LocaleSelectionFilter implements Filter {
if (request == null) {
throw new NullPointerException("request may not be null.");
}
this.request = request;
if (selectedLocale == null) {
throw new NullPointerException(
"selectedLocale may not be null.");
}
this.selectedLocale = selectedLocale;
Locale selectedLanguage = new Locale(selectedLocale.getLanguage());
locales = EnumerationUtils.toList(request.getLocales());
locales.remove(selectedLanguage);
locales.add(0, selectedLanguage);
locales.remove(selectedLocale);
locales.add(0, selectedLocale);
}
@Override
public Locale getLocale() {
return selectedLocale;
return locales.get(0);
}
/**
* Put the selected Locale on the front of the list of acceptable
* Locales.
* Get the modified list of locales.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings("rawtypes")
@Override
public Enumeration getLocales() {
List list = EnumerationUtils.toList(request.getLocales());
list.remove(selectedLocale);
list.add(0, selectedLocale);
return Collections.enumeration(list);
return Collections.enumeration(locales);
}
}

View file

@ -11,7 +11,6 @@ import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -43,23 +42,17 @@ public class LanguageFilteringRDFService implements RDFService {
}
private List<String> normalizeLangs(List<String> langs) {
List<String> normalizedLangs = new ArrayList<String>();
String currentBaseLang = null;
log.debug("Preferred languages:" + langs);
List<String> normalizedLangs = new ArrayList<String>(langs);
for (String lang : langs) {
String normalizedLang = StringUtils.lowerCase(lang);
String baseLang = normalizedLang.split("-")[0];
if (currentBaseLang == null) {
currentBaseLang = baseLang;
} else if (!currentBaseLang.equals(baseLang)) {
if (!normalizedLangs.contains(currentBaseLang)) {
normalizedLangs.add(currentBaseLang);
}
currentBaseLang = baseLang;
String baseLang = lang.split("-")[0];
if (!normalizedLangs.contains(baseLang)) {
normalizedLangs.add(baseLang);
}
}
if (currentBaseLang != null && !normalizedLangs.contains(currentBaseLang)) {
normalizedLangs.add(currentBaseLang);
}
log.debug("Normalized languages:" + normalizedLangs);
return normalizedLangs;
}
@ -106,6 +99,7 @@ public class LanguageFilteringRDFService implements RDFService {
}
private Model filterModel(Model m) {
log.debug("filterModel");
List<Statement> retractions = new ArrayList<Statement>();
StmtIterator stmtIt = m.listStatements();
while (stmtIt.hasNext()) {
@ -117,6 +111,7 @@ public class LanguageFilteringRDFService implements RDFService {
continue;
}
Collections.sort(candidatesForRemoval, new StatementSortByLang());
log.debug("sorted statements: " + showSortedStatements(candidatesForRemoval));
Iterator<Statement> candIt = candidatesForRemoval.iterator();
String langRegister = null;
boolean chuckRemaining = false;
@ -142,9 +137,27 @@ public class LanguageFilteringRDFService implements RDFService {
return m;
}
private String showSortedStatements(List<Statement> candidatesForRemoval) {
List<String> langStrings = new ArrayList<String>();
for (Statement stmt: candidatesForRemoval) {
if (stmt == null) {
langStrings.add("null stmt");
} else {
RDFNode node = stmt.getObject();
if (!node.isLiteral()) {
langStrings.add("not literal");
} else {
langStrings.add(node.asLiteral().getLanguage());
}
}
}
return langStrings.toString();
}
@Override
public InputStream sparqlSelectQuery(String query,
ResultFormat resultFormat) throws RDFServiceException {
log.debug("sparqlSelectQuery: " + query.replaceAll("\\s+", " "));
ResultSet resultSet = ResultSetFactory.fromJSON(
s.sparqlSelectQuery(query, RDFService.ResultFormat.JSON));
List<QuerySolution> solnList = getSolutionList(resultSet);
@ -178,6 +191,7 @@ public class LanguageFilteringRDFService implements RDFService {
continue;
}
Collections.sort(candidatesForRemoval, new RowIndexedLiteralSortByLang());
log.debug("sorted RowIndexedLiterals: " + showSortedRILs(candidatesForRemoval));
Iterator<RowIndexedLiteral> candIt = candidatesForRemoval.iterator();
String langRegister = null;
boolean chuckRemaining = false;
@ -223,6 +237,14 @@ public class LanguageFilteringRDFService implements RDFService {
return new ByteArrayInputStream(outputStream.toByteArray());
}
private String showSortedRILs(List<RowIndexedLiteral> candidatesForRemoval) {
List<String> langstrings = new ArrayList<String>();
for (RowIndexedLiteral ril: candidatesForRemoval) {
langstrings.add(ril.getLiteral().getLanguage());
}
return langstrings.toString();
}
private class RowIndexedLiteral {
private Literal literal;
@ -324,37 +346,44 @@ public class LanguageFilteringRDFService implements RDFService {
}
private class LangSort {
// any inexact match is worse than any exact match
private int inexactMatchPenalty = langs.size();
// no language is worse than any inexact match (if the preferred list does not include "").
private int noLanguage = 2 * inexactMatchPenalty;
// no match is worse than no language.
private int noMatch = noLanguage + 1;
protected int compareLangs(String t1lang, String t2lang) {
t1lang = StringUtils.lowerCase(t1lang);
t2lang = StringUtils.lowerCase(t2lang);
if ( t1lang == null && t2lang == null) {
return 0;
} else if (t1lang == null) {
return 1;
} else if (t2lang == null) {
return -1;
} else {
int t1langPref = langs.indexOf(t1lang);
int t2langPref = langs.indexOf(t2lang);
if (t1langPref == -1 && t2langPref == -1) {
if ("".equals(t1lang) && "".equals(t2lang)) {
return 0;
} else if ("".equals(t1lang) && !("".equals(t2lang))) {
return -1;
} else {
return 1;
return languageIndex(t1lang) - languageIndex(t2lang);
}
} else if (t1langPref > -1 && t2langPref == -1) {
return -1;
} else if (t1langPref == -1 && t2langPref > -1) {
return 1;
} else {
return t1langPref - t2langPref;
/**
* Return index of exact match, or index of partial match, or
* language-free, or no match.
*/
private int languageIndex(String lang) {
if (lang == null) {
lang = "";
}
int index = langs.indexOf(lang);
if (index >= 0) {
return index;
}
if (lang.length() > 2) {
index = langs.indexOf(lang.substring(0, 2));
if (index >= 0) {
return index + inexactMatchPenalty;
}
}
if (lang.isEmpty()) {
return noLanguage;
}
return noMatch;
}
}
private class RowIndexedLiteralSortByLang extends LangSort implements Comparator<RowIndexedLiteral> {

View file

@ -0,0 +1,308 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.NameFileFilter;
import org.apache.commons.io.filefilter.NotFileFilter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
/**
* Check to see that web.xml doesn't include any constructs that are permitted
* by Tomcat but prohibited by the Servlet 2.4 Specification.
*
* These are things that might not be noticed when testing Vitro on Tomcat, but
* might show up as problems on other containers like GlassFish or WebLogic.
* <ul>
* <li>
* The contents of <dispatcher/> elements must be upper-case. Tomcat permits
* lower-case, but the specification does not.</li>
* <li>
* All <servlet-class/> tags must point to existing classes. Tomcat does not try
* to load a servlet until it is necessary to service a request, but WebLogic
* loads them on startup. Either method is permitted by the specification.</li>
* </ul>
*
* As long as we're here, let's check some things that would cause Vitro to fail
* in any servlet container.
* <ul>
* <li>
* All <listener-class/> tags must point to existing classes.</li>
* <li>
* All <filter-class/> tags must point to existing classes.</li>
* <li>
* All <taglib-location/> tags must point to existing files.</li>
* </ul>
*/
@RunWith(value = Parameterized.class)
public class WebXmlTest extends AbstractTestClass {
private static final Log log = LogFactory.getLog(WebXmlTest.class);
@Parameters
public static Collection<Object[]> findWebXmlFiles() {
IOFileFilter fileFilter = new NameFileFilter("web.xml");
IOFileFilter dirFilter = new NotFileFilter(new NameFileFilter(".build"));
Collection<File> files = FileUtils.listFiles(new File("."), fileFilter,
dirFilter);
if (files.isEmpty()) {
System.out.println("WARNING: could not find web.xml");
} else {
if (files.size() > 1) {
System.out
.println("WARNING: testing more than one web.xml file: "
+ files);
}
}
Collection<Object[]> parameters = new ArrayList<Object[]>();
for (File file : files) {
parameters.add(new Object[] { file });
}
return parameters;
}
private static DocumentBuilder docBuilder = createDocBuilder();
private static XPath xpath = createXPath();
private static DocumentBuilder createDocBuilder() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setNamespaceAware(true); // never forget this!
return factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
}
private static XPath createXPath() {
XPath xp = XPathFactory.newInstance().newXPath();
xp.setNamespaceContext(new StupidNamespaceContext());
return xp;
}
private File webXmlFile;
private Document webXmlDoc;
private List<String> messages = new ArrayList<String>();
public WebXmlTest(File file) {
this.webXmlFile = file;
}
@Before
public void parseWebXml() throws SAXException, IOException {
if (webXmlDoc == null) {
webXmlDoc = docBuilder.parse(webXmlFile);
}
}
@Test
public void checkAll() throws IOException {
checkDispatcherValues();
checkServletClasses();
checkListenerClasses();
checkFilterClasses();
checkTaglibLocations();
if (!messages.isEmpty()) {
for (String message : messages) {
System.out.println(message);
}
fail("Found these problems with '" + webXmlFile.getCanonicalPath()
+ "'\n " + StringUtils.join(messages, "\n "));
}
}
private void checkDispatcherValues() {
List<String> okValues = Arrays.asList(new String[] { "FORWARD",
"REQUEST", "INCLUDE", "ERROR" });
for (Node n : findNodes("//j2ee:dispatcher")) {
String text = n.getTextContent();
if (!okValues.contains(text)) {
messages.add("<dispatcher>" + text
+ "</dispatcher> is not valid. Acceptable values are "
+ okValues);
}
}
}
private void checkServletClasses() {
for (Node n : findNodes("//j2ee:servlet-class")) {
String text = n.getTextContent();
String problem = confirmClassNameIsValid(text, HttpServlet.class);
if (problem != null) {
messages.add("<servlet-class>" + text
+ "</servlet-class> is not valid: " + problem);
}
}
}
private void checkListenerClasses() {
for (Node n : findNodes("//j2ee:listener-class")) {
String text = n.getTextContent();
String problem = confirmClassNameIsValid(text,
ServletContextListener.class);
if (problem != null) {
messages.add("<listener-class>" + text
+ "</listener-class> is not valid: " + problem);
}
}
}
private void checkFilterClasses() {
for (Node n : findNodes("//j2ee:filter-class")) {
String text = n.getTextContent();
String problem = confirmClassNameIsValid(text, Filter.class);
if (problem != null) {
messages.add("<filter-class>" + text
+ "</filter-class> is not valid: " + problem);
}
}
}
private void checkTaglibLocations() {
// TODO Don't know how to do this one. Where do we look for the taglibs?
}
// ----------------------------------------------------------------------
// Helper methods
// ----------------------------------------------------------------------
/**
* Search for an Xpath, returning a handy list.
*/
private List<Node> findNodes(String pattern) {
try {
XPathExpression xpe = xpath.compile(pattern);
NodeList nodes = (NodeList) xpe.evaluate(
webXmlDoc.getDocumentElement(), XPathConstants.NODESET);
List<Node> list = new ArrayList<Node>();
for (int i = 0; i < nodes.getLength(); i++) {
list.add(nodes.item(i));
}
return list;
} catch (XPathExpressionException e) {
throw new RuntimeException(e);
}
}
/**
* Check that the supplied className can be instantiated with a
* zero-argument constructor, and assigned to a variable of the target
* class.
*/
private String confirmClassNameIsValid(String className,
Class<?> targetClass) {
try {
Class<?> specifiedClass = Class.forName(className);
Object o = specifiedClass.newInstance();
if (!targetClass.isInstance(o)) {
return specifiedClass.getSimpleName()
+ " is not a subclass of "
+ targetClass.getSimpleName() + ".";
}
} catch (ClassNotFoundException e) {
return "The class does not exist.";
} catch (InstantiationException e) {
return "The class does not have a public constructor "
+ "that takes zero arguments.";
} catch (IllegalAccessException e) {
return "The class does not have a public constructor "
+ "that takes zero arguments.";
}
return null;
}
/**
* Dump the first 20 nodes of an XML context, excluding comments and blank
* text nodes.
*/
@SuppressWarnings("unused")
private int dumpXml(Node xmlNode, int... parms) {
int remaining = (parms.length == 0) ? 20 : parms[0];
int level = (parms.length < 2) ? 1 : parms[1];
Node n = xmlNode;
if (Node.COMMENT_NODE == n.getNodeType()) {
return 0;
}
if (Node.TEXT_NODE == n.getNodeType()) {
if (StringUtils.isBlank(n.getTextContent())) {
return 0;
}
}
int used = 1;
System.out.println(StringUtils.repeat("-->", level) + n);
NodeList nl = n.getChildNodes();
for (int i = 0; (i < nl.getLength() && remaining > used); i++) {
used += dumpXml(nl.item(i), remaining - used, level + 1);
}
return used;
}
// ----------------------------------------------------------------------
// Helper classes
// ----------------------------------------------------------------------
private static class StupidNamespaceContext implements NamespaceContext {
@Override
public String getNamespaceURI(String prefix) {
if ("j2ee".equals(prefix)) {
return "http://java.sun.com/xml/ns/j2ee";
} else {
throw new UnsupportedOperationException();
}
}
@Override
public String getPrefix(String namespaceURI) {
throw new UnsupportedOperationException();
}
@Override
public Iterator<?> getPrefixes(String namespaceURI) {
throw new UnsupportedOperationException();
}
}
}

View file

@ -0,0 +1,309 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.rdfservice.filter;
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.junit.Before;
import org.junit.Test;
import stubs.com.hp.hpl.jena.rdf.model.LiteralStub;
import com.hp.hpl.jena.rdf.model.Literal;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
/**
* This is the matching order we expect to see:
*
* <pre>
* exact match to preferred, by order.
* partial match to preferred, by order.
* vanilla or null (no language)
* no match
* </pre>
*/
public class LanguageFilteringRDFServiceTest extends AbstractTestClass {
private static final Log log = LogFactory
.getLog(LanguageFilteringRDFServiceTest.class);
private static final String COLLATOR_CLASSNAME = "edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringRDFService$RowIndexedLiteralSortByLang";
private static final String RIL_CLASSNAME = "edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringRDFService$RowIndexedLiteral";
private LanguageFilteringRDFService filteringRDFService;
private List<Object> listOfRowIndexedLiterals;
private int literalIndex;
private List<String> preferredLanguages;
private List<String> availableLanguages;
private List<String> expectedSortOrders;
@Before
public void setup() {
setLoggerLevel(this.getClass(), Level.DEBUG);
setLoggerLevel(LanguageFilteringRDFService.class, Level.DEBUG);
}
// ----------------------------------------------------------------------
// The tests
// ----------------------------------------------------------------------
@Test
public void singleMatch() {
preferredLanguages = list("en-US");
availableLanguages = list("en-US");
expectedSortOrders = list("en-US");
testArbitraryOrder();
}
@Test
public void singleNoMatch() {
preferredLanguages = list("en-US");
availableLanguages = list("es-MX");
expectedSortOrders = list("es-MX");
testArbitraryOrder();
}
@Test
public void doubleMatch() {
preferredLanguages = list("en-US", "es-MX");
availableLanguages = list("en-US", "es-MX");
expectedSortOrders = list("en-US", "es-MX");
testBothWays();
}
@Test
public void noMatches() {
preferredLanguages = list("es-MX");
availableLanguages = list("en-US", "fr-FR");
expectedSortOrders = list("en-US", "fr-FR");
testArbitraryOrder();
}
@Test
public void partialMatches() {
preferredLanguages = list("en", "es");
availableLanguages = list("en-US", "es-MX");
expectedSortOrders = list("en-US", "es-MX");
testBothWays();
}
@Test
public void matchIsBetterThanNoMatch() {
preferredLanguages = list("en-US", "es-MX");
availableLanguages = list("en-US", "fr-FR");
expectedSortOrders = list("en-US", "fr-FR");
testBothWays();
}
@Test
public void matchIsBetterThanPartialMatch() {
preferredLanguages = list("es-ES", "en-US");
availableLanguages = list("en-US", "es-MX");
expectedSortOrders = list("en-US", "es-MX");
testBothWays();
}
@Test
public void exactMatchIsBetterThanPartialMatch() {
preferredLanguages = list("es");
availableLanguages = list("es", "es-MX");
expectedSortOrders = list("es", "es-MX");
testBothWays();
}
@Test
public void matchIsBetterThanVanilla() {
preferredLanguages = list("en-US");
availableLanguages = list("en-US", "");
expectedSortOrders = list("en-US", "");
testBothWays();
}
@Test
public void partialMatchIsBetterThanVanilla() {
preferredLanguages = list("es-MX");
availableLanguages = list("es-ES", "");
expectedSortOrders = list("es-ES", "");
testBothWays();
}
@Test
public void vanillaIsBetterThanNoMatch() {
preferredLanguages = list("es-MX");
availableLanguages = list("en-US", "");
expectedSortOrders = list("", "en-US");
testBothWays();
}
@Test
public void omnibus() {
preferredLanguages = list("es-MX", "es", "en-UK", "es-PE", "fr");
availableLanguages = list("es-MX", "es", "fr", "es-ES", "fr-FR", "",
"de-DE");
expectedSortOrders = list("es-MX", "es", "fr", "es-ES", "fr-FR", "",
"de-DE");
testBothWays();
}
// ----------------------------------------------------------------------
// Helper methods
// ----------------------------------------------------------------------
/**
* Sort the available languages as they are presented. Then reverse them and
* sort again.
*/
private void testBothWays() {
createLanguageFilter();
buildListOfLiterals();
sortListOfLiterals();
assertLanguageOrder("sort literals");
buildReversedListOfLiterals();
sortListOfLiterals();
assertLanguageOrder("sort reversed literals");
}
/**
* Sort the available languages, without caring what the eventual sorted
* order is. Really, this is just a test to see that no exceptions are
* thrown, and no languages are "lost in translation".
*/
private void testArbitraryOrder() {
createLanguageFilter();
buildListOfLiterals();
sortListOfLiterals();
assertLanguages("sort literals");
buildReversedListOfLiterals();
sortListOfLiterals();
assertLanguages("sort reversed literals");
}
private List<String> list(String... strings) {
return new ArrayList<String>(Arrays.asList(strings));
}
private void createLanguageFilter() {
filteringRDFService = new LanguageFilteringRDFService(null,
preferredLanguages);
}
private void buildListOfLiterals() {
List<Object> list = new ArrayList<Object>();
for (String language : availableLanguages) {
list.add(buildRowIndexedLiteral(language));
}
listOfRowIndexedLiterals = list;
}
private void buildReversedListOfLiterals() {
List<Object> list = new ArrayList<Object>();
for (String language : availableLanguages) {
list.add(0, buildRowIndexedLiteral(language));
}
listOfRowIndexedLiterals = list;
}
private void sortListOfLiterals() {
log.debug("before sorting: "
+ languagesFromLiterals(listOfRowIndexedLiterals));
Comparator<Object> comparator = buildRowIndexedLiteralSortByLang();
Collections.sort(listOfRowIndexedLiterals, comparator);
}
private void assertLanguageOrder(String message) {
List<String> expectedLanguages = expectedSortOrders;
log.debug("expected order: " + expectedLanguages);
List<String> actualLanguages = languagesFromLiterals(listOfRowIndexedLiterals);
log.debug("actual order: " + actualLanguages);
assertEquals(message, expectedLanguages, actualLanguages);
}
private void assertLanguages(String message) {
Set<String> expectedLanguages = new HashSet<String>(expectedSortOrders);
log.debug("expected languages: " + expectedLanguages);
Set<String> actualLanguages = new HashSet<String>(
languagesFromLiterals(listOfRowIndexedLiterals));
log.debug("actual languages: " + actualLanguages);
assertEquals(message, expectedLanguages, actualLanguages);
}
private List<String> languagesFromLiterals(List<Object> literals) {
List<String> actualLanguages = new ArrayList<String>();
for (Object ril : literals) {
actualLanguages.add(getLanguageFromRowIndexedLiteral(ril));
}
return actualLanguages;
}
// ----------------------------------------------------------------------
// Reflection methods to get around "private" declarations.
// ----------------------------------------------------------------------
private Object buildRowIndexedLiteral(String language) {
try {
Class<?> clazz = Class.forName(RIL_CLASSNAME);
Class<?>[] argTypes = { LanguageFilteringRDFService.class,
Literal.class, Integer.TYPE };
Constructor<?> constructor = clazz.getDeclaredConstructor(argTypes);
constructor.setAccessible(true);
Literal l = new LiteralStub(language);
int i = literalIndex++;
return constructor.newInstance(filteringRDFService, l, i);
} catch (Exception e) {
throw new RuntimeException(
"Could not create a row-indexed literal", e);
}
}
@SuppressWarnings("unchecked")
private Comparator<Object> buildRowIndexedLiteralSortByLang() {
try {
Class<?> clazz = Class.forName(COLLATOR_CLASSNAME);
Class<?>[] argTypes = { LanguageFilteringRDFService.class };
Constructor<?> constructor = clazz.getDeclaredConstructor(argTypes);
constructor.setAccessible(true);
return (Comparator<Object>) constructor
.newInstance(filteringRDFService);
} catch (Exception e) {
throw new RuntimeException("Could not create a collator", e);
}
}
private String getLanguageFromRowIndexedLiteral(Object ril) {
try {
Method m = ril.getClass().getDeclaredMethod("getLiteral");
m.setAccessible(true);
Literal l = (Literal) m.invoke(ril);
return l.getLanguage();
} catch (Exception e) {
throw new RuntimeException(
"Could not get the Literal from a RowIndexedLiteral", e);
}
}
}

View file

@ -0,0 +1,179 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.com.hp.hpl.jena.rdf.model;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.RDFVisitor;
import com.hp.hpl.jena.rdf.model.Resource;
/**
* Only implemented what I needed so far. The rest is left as an exercise for
* the student.
*/
public class LiteralStub implements Literal {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
final String language;
public LiteralStub(String language) {
this.language = language;
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public boolean isLiteral() {
return true;
}
@Override
public boolean isAnon() {
return false;
}
@Override
public boolean isResource() {
return false;
}
@Override
public boolean isURIResource() {
return false;
}
@Override
public Literal asLiteral() {
return this;
}
@Override
public Resource asResource() {
throw new ClassCastException();
}
@Override
public String getLanguage() {
return language;
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public <T extends RDFNode> T as(Class<T> view) {
throw new RuntimeException("LiteralStub.as() not implemented.");
}
@Override
public <T extends RDFNode> boolean canAs(Class<T> arg0) {
throw new RuntimeException("LiteralStub.canAs() not implemented.");
}
@Override
public Model getModel() {
throw new RuntimeException("LiteralStub.getModel() not implemented.");
}
@Override
public Object visitWith(RDFVisitor arg0) {
throw new RuntimeException("LiteralStub.visitWith() not implemented.");
}
@Override
public Node asNode() {
throw new RuntimeException("LiteralStub.asNode() not implemented.");
}
@Override
public boolean getBoolean() {
throw new RuntimeException("LiteralStub.getBoolean() not implemented.");
}
@Override
public byte getByte() {
throw new RuntimeException("LiteralStub.getByte() not implemented.");
}
@Override
public char getChar() {
throw new RuntimeException("LiteralStub.getChar() not implemented.");
}
@Override
public RDFDatatype getDatatype() {
throw new RuntimeException("LiteralStub.getDatatype() not implemented.");
}
@Override
public String getDatatypeURI() {
throw new RuntimeException(
"LiteralStub.getDatatypeURI() not implemented.");
}
@Override
public double getDouble() {
throw new RuntimeException("LiteralStub.getDouble() not implemented.");
}
@Override
public float getFloat() {
throw new RuntimeException("LiteralStub.getFloat() not implemented.");
}
@Override
public int getInt() {
throw new RuntimeException("LiteralStub.getInt() not implemented.");
}
@Override
public String getLexicalForm() {
throw new RuntimeException(
"LiteralStub.getLexicalForm() not implemented.");
}
@Override
public long getLong() {
throw new RuntimeException("LiteralStub.getLong() not implemented.");
}
@Override
public short getShort() {
throw new RuntimeException("LiteralStub.getShort() not implemented.");
}
@Override
public String getString() {
throw new RuntimeException("LiteralStub.getString() not implemented.");
}
@Override
public Object getValue() {
throw new RuntimeException("LiteralStub.getValue() not implemented.");
}
@Override
public Literal inModel(Model arg0) {
throw new RuntimeException("LiteralStub.inModel() not implemented.");
}
@Override
public boolean isWellFormedXML() {
throw new RuntimeException(
"LiteralStub.isWellFormedXML() not implemented.");
}
@Override
public boolean sameValueAs(Literal arg0) {
throw new RuntimeException("LiteralStub.sameValueAs() not implemented.");
}
}

View file

@ -122,8 +122,8 @@
<filter-mapping>
<filter-name>VitroRequestPrep</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>request</dispatcher>
<dispatcher>forward</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter>
@ -133,7 +133,7 @@
<filter-mapping>
<filter-name>PageRoutingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>request</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<!-- Spring setup **************************************************** -->
@ -164,25 +164,6 @@
-->
<!-- Servlets ********************************************************** -->
<servlet>
<!--adding only trimSpaces param to defaults to clean up html output-->
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>trimSpaces</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet>
<servlet-name>IndexController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.search.controller.IndexController</servlet-class>
@ -201,15 +182,6 @@
<url-pattern>/RecomputeInferences</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SDBSetupController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.freemarker.SDBSetupController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SDBSetupController</servlet-name>
<url-pattern>/sdbsetup</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>MenuManagementEdit</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.edit.MenuManagementEdit</servlet-class>
@ -228,24 +200,6 @@
<url-pattern>/ajax/sparqlQuery</url-pattern>
</servlet-mapping>
<!-- This is the new navigation controller. It is not ready for the 1.1 release
see http://issues.library.cornell.edu/browse/NIHVIVO-597
<servlet>
<servlet-name>NavigationController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.freemarker.NavigationController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NavigationController</servlet-name>
<url-pattern>/nav/*</url-pattern>
</servlet-mapping>
-->
<servlet>
<servlet-name>fetch</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.QueryServlet</servlet-class>
<!--load-on-startup>2</load-on-startup-->
</servlet>
<servlet>
<servlet-name>AboutController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.freemarker.AboutController</servlet-class>
@ -389,15 +343,6 @@
<url-pattern>/editRequestAJAX</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FlagUpdateController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.edit.FlagUpdateController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FlagUpdateController</servlet-name>
<url-pattern>/flagUpdate</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>RDFUploadFormController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.jena.RDFUploadFormController</servlet-class>
@ -452,24 +397,6 @@
<url-pattern>/jenaXmlFileUpload/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>OwlImportController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.owl.OwlImportController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OwlImportController</servlet-name>
<url-pattern>/owl</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>OwlImportServlet</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.owl.ProtegeOwlImportServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OwlImportServlet</servlet-name>
<url-pattern>/importOwl</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>JenaAdminServlet</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.jena.JenaAdminActions</servlet-class>
@ -569,16 +496,6 @@
<url-pattern>/datapropEdit</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>KeywordEditController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.edit.KeywordEditController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>KeywordEditController</servlet-name>
<url-pattern>/keywordEdit</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>OntologyEditController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.edit.OntologyEditController</servlet-class>
@ -777,24 +694,6 @@
<url-pattern>/admin/wait</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>StatementChangeListingController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.edit.listing.jena.StatementChangeListingController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StatementChangeListingController</servlet-name>
<url-pattern>/statementHistory</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>WriteOutChangesController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.edit.listing.jena.WriteOutChangesController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WriteOutChangesController</servlet-name>
<url-pattern>/writeOutChanges</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ListVClassWebappsController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.freemarker.ListVClassWebappsController</servlet-class>
@ -957,15 +856,6 @@
<url-pattern>/edit/reorder</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AdminController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.AdminController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdminController</servlet-name>
<url-pattern>/adminCon</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>TermsOfUseController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.freemarker.TermsOfUseController</servlet-class>
@ -1105,32 +995,6 @@
<url-pattern>/browse</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>pubsbyorg</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.vclass.PubsByDepartmentServlet</servlet-class>
<init-param>
<param-name>workspaceDir</param-name>
<param-value>/usr/local/services/vivo/logs</param-value>
</init-param>
<!--load-on-startup>2</load-on-startup-->
</servlet>
<servlet>
<servlet-name>coauthors</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.vclass.CoAuthorServlet</servlet-class>
<init-param>
<param-name>workspaceDir</param-name>
<param-value>/usr/local/services/vivo/logs</param-value>
</init-param>
<!--load-on-startup>2</load-on-startup-->
</servlet>
<servlet>
<servlet-name>generic_create</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.GenericDBCreate</servlet-class>
<!--load-on-startup>2</load-on-startup-->
</servlet>
<servlet>
<servlet-name>serveFiles</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.filestorage.serving.FileServingServlet</servlet-class>
@ -1140,21 +1004,6 @@
<url-pattern>/file/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>generic_editprep</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.GenericDBEditPrep</servlet-class>
</servlet>
<servlet>
<servlet-name>generic_update</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.GenericDBUpdate</servlet-class>
</servlet>
<servlet>
<servlet-name>generic_delete</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.GenericDBDelete</servlet-class>
</servlet>
<servlet>
<servlet-name>SparqlQuery</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.SparqlQueryServlet</servlet-class>
@ -1165,16 +1014,6 @@
<url-pattern>/admin/sparqlquery</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>VisualizationController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.visualization.VisualizationController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VisualizationController</servlet-name>
<url-pattern>/visualization</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>primitiveRdfEdit</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.edit.PrimitiveRdfEdit</servlet-class>
@ -1194,10 +1033,6 @@
</servlet-mapping>
<!-- ============================== servlet-mappings ======================== -->
<servlet-mapping>
<servlet-name>fetch</servlet-name>
<url-pattern>/fetch</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mailusers</servlet-name>
<url-pattern>/mailusers</url-pattern>
@ -1256,22 +1091,6 @@
<servlet-name>coauthors</servlet-name>
<url-pattern>/coauthors</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>generic_create</servlet-name>
<url-pattern>/generic_create</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>generic_editprep</servlet-name>
<url-pattern>/generic_editprep</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>generic_update</servlet-name>
<url-pattern>/generic_update</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>generic_delete</servlet-name>
<url-pattern>/generic_delete</url-pattern>
</servlet-mapping>
<!-- ==================== sparql query builder ==================== -->