diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/StringUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/StringUtils.java index 28e3f2185..838931105 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/StringUtils.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/StringUtils.java @@ -1,9 +1,45 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + package edu.cornell.mannlib.vitro.webapp.utils; +import java.util.List; + public class StringUtils { public static String capitalize(String s) { return s.substring(0, 1).toUpperCase() + s.substring(1); } - + + public static String quote(String s) { + return s.isEmpty() ? "" : '"' + s + '"'; + } + + public static String join(List list, boolean quote, String glue) { + + if (glue == null) { + glue = ","; + } + String joinedList = ""; + + int count = 0; + for (Object o : list) { + String s = o.toString(); + if (count > 0) { + joinedList += glue; + } + count++; + joinedList += quote ? quote(s) : s; + } + + return joinedList; + } + + public static String join(List list) { + return join(list, false, ","); + } + + public static String quotedList(List list, String glue) { + + return join(list, true, glue); + } } diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/RdfLiteralHashTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/RdfLiteralHashTest.java index 547ba8f2c..943877539 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/RdfLiteralHashTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/RdfLiteralHashTest.java @@ -188,7 +188,7 @@ public class RdfLiteralHashTest { } @Test - public void testgetVitroNsPropertyStatement(){ + public void testGetVitroNsPropertyStatement(){ String n3 = "@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/StringUtilsTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/StringUtilsTest.java new file mode 100644 index 000000000..2df88f273 --- /dev/null +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/StringUtilsTest.java @@ -0,0 +1,74 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils; + +import java.util.List; +import java.util.ArrayList; + +import org.junit.Test; + +import junit.framework.Assert; + +import edu.cornell.mannlib.vitro.testing.AbstractTestClass; + +public class StringUtilsTest extends AbstractTestClass { + + protected static List stringList = new ArrayList(); + protected static List intList = new ArrayList(); + static { + stringList.add("apple"); + stringList.add("banana"); + stringList.add("orange"); + + intList.add(1); + intList.add(2); + intList.add(3); + } + + @Test + public void testCapitalize() { + String s1 = "cat"; + Assert.assertEquals("Cat", StringUtils.capitalize(s1)); + + String s2 = "Cat"; + Assert.assertEquals(s2, StringUtils.capitalize(s2)); + + String s3 = "CAT"; + Assert.assertEquals(s3, StringUtils.capitalize(s3)); + + } + + @Test + public void testQuote() { + String s1 = "cat"; + Assert.assertEquals("\"cat\"", StringUtils.quote(s1)); + + String s2 = ""; + Assert.assertEquals("", StringUtils.quote(s2)); + } + + @Test + public void testJoinNoArgs() { + + Assert.assertEquals("apple,banana,orange", StringUtils.join(stringList)); + Assert.assertEquals("1,2,3", StringUtils.join(intList)); + } + + @Test + public void testJoinArgs() { + + Assert.assertEquals("apple:banana:orange", StringUtils.join(stringList, false, ":")); + Assert.assertEquals("\"apple\"|\"banana\"|\"orange\"", StringUtils.join(stringList, true, "|")); + Assert.assertEquals("\"apple\",\"banana\",\"orange\"", StringUtils.join(stringList, true, null)); + Assert.assertEquals("apple,banana,orange", StringUtils.join(stringList, false, null)); + Assert.assertEquals("apple...banana...orange", StringUtils.join(stringList, false, "...")); + + } + + @Test + public void testQuotedList() { + + Assert.assertEquals("\"apple\"|\"banana\"|\"orange\"", StringUtils.quotedList(stringList, "|")); + Assert.assertEquals("\"apple\",\"banana\",\"orange\"", StringUtils.quotedList(stringList, null)); + } +} diff --git a/webapp/web/edit/forms/defaultVitroNsPropForm.jsp b/webapp/web/edit/forms/defaultVitroNsPropForm.jsp index 60b405647..7acc02a57 100644 --- a/webapp/web/edit/forms/defaultVitroNsPropForm.jsp +++ b/webapp/web/edit/forms/defaultVitroNsPropForm.jsp @@ -1,5 +1,8 @@ <%-- $This file is distributed under the terms of the license in /doc/license.txt$ --%> +<%@ page import="java.util.ArrayList"%> +<%@ page import="java.util.Arrays"%> + <%@ page import="com.hp.hpl.jena.rdf.model.Literal"%> <%@ page import="com.hp.hpl.jena.rdf.model.Model"%> @@ -11,6 +14,7 @@ <%@ page import="edu.cornell.mannlib.vitro.webapp.controller.VitroRequest"%> <%@ page import="edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement" %> <%@page import="edu.cornell.mannlib.vitro.webapp.web.MiscWebUtils"%> +<%@page import="edu.cornell.mannlib.vitro.webapp.utils.StringUtils" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> @@ -42,7 +46,21 @@ //String rangeDatatypeUri = vreq.getWebappDaoFactory().getDataPropertyDao().getRequiredDatatypeURI(subject, prop); //String rangeDatatypeUri = prop.getRangeDatatypeURI(); String rangeDatatypeUri = "http://www.w3.org/2001/XMLSchema#string"; - vreq.setAttribute("rangeDatatypeUriJson", MiscWebUtils.escape(rangeDatatypeUri)); + String rangeDatatypeUriJson = MiscWebUtils.escape(rangeDatatypeUri); + vreq.setAttribute("rangeDatatypeUriJson", rangeDatatypeUriJson); + + + ArrayList validatorList = new ArrayList(); + if (predicateUri.equals(VitroVocabulary.LABEL) || predicateUri.equals(VitroVocabulary.RDF_TYPE)) { + validatorList.add("nonempty"); + } + if (!rangeDatatypeUriJson.isEmpty()) { + validatorList.add("datatype:" + rangeDatatypeUriJson); + } + String validators = StringUtils.quotedList(validatorList, ","); + System.out.println("VALIDATORS: " + validators); + vreq.setAttribute("validators", StringUtils.quotedList(validatorList, ",")); + %> @@ -65,15 +83,12 @@ ?subject <${predicate}> ?${propertyName} . +<% - -<%-- RY Add other validation cases here. --%> - - - - - - +%> + + + { @@ -101,7 +116,7 @@ "fields" : { "${propertyName}" : { "newResource" : "false", - "validators" : [ "${validator}" ], + "validators" : [ ${validators} ], "optionsType" : "UNDEFINED", "literalOptions" : [ ], "predicateUri" : "", @@ -114,8 +129,12 @@ } -<% + +<% +System.out.println(request.getAttribute("editjson")); + if( log.isDebugEnabled()) log.debug(request.getAttribute("editjson")); + EditConfiguration editConfig = EditConfiguration.getConfigFromSession(session,request); if (editConfig == null) { log.debug("No editConfig in session. Making new editConfig.");