Index: org/dspace/app/mets/METSExport.java =================================================================== --- org/dspace/app/mets/METSExport.java (revision 1940) +++ org/dspace/app/mets/METSExport.java (working copy) @@ -70,7 +70,7 @@ import org.dspace.core.Context; import org.dspace.core.Utils; import org.dspace.handle.HandleManager; -import org.dspace.app.webui.util.UIUtil; +import org.dspace.app.util.Util; import edu.harvard.hul.ois.mets.Agent; import edu.harvard.hul.ois.mets.AmdSec; @@ -436,7 +436,7 @@ + "/" + bitstreams[bits].getSequenceID() + "/" - + UIUtil.encodeBitstreamName(bitstreams[bits].getName(), + + Util.encodeBitstreamName(bitstreams[bits].getName(), "UTF-8"); edu.harvard.hul.ois.mets.File file = new edu.harvard.hul.ois.mets.File(); Index: org/dspace/app/webui/util/UIUtil.java =================================================================== --- org/dspace/app/webui/util/UIUtil.java (revision 1940) +++ org/dspace/app/webui/util/UIUtil.java (working copy) @@ -53,6 +53,7 @@ import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; +import org.dspace.app.util.Util; import org.dspace.content.Collection; import org.dspace.content.Community; import org.dspace.content.DCDate; @@ -69,10 +70,10 @@ * @author Robert Tansley * @version $Revision$ */ -public class UIUtil +public class UIUtil extends Util { /** log4j category */ - private static Logger log = Logger.getLogger(UIUtil.class); + static Logger log = Logger.getLogger(UIUtil.class); /** * Obtain a new context object. If a context object has already been created @@ -225,129 +226,6 @@ } /** - * Utility method to convert spaces in a string to HTML non-break space - * elements. - * - * @param s - * string to change spaces in - * @return the string passed in with spaces converted to HTML non-break - * spaces - */ - public static String nonBreakSpace(String s) - { - StringBuffer newString = new StringBuffer(); - - for (int i = 0; i < s.length(); i++) - { - char ch = s.charAt(i); - - if (ch == ' ') - { - newString.append(" "); - } - else - { - newString.append(ch); - } - } - - return newString.toString(); - } - - /** - * Write a human-readable version of a DCDate. - * - * @param d - * the date - * @param time - * if true, display the time with the date - * @param localTime - * if true, adjust for local timezone, otherwise GMT - * - * @return the date in a human-readable form. - */ - public static String displayDate(DCDate d, boolean time, boolean localTime) - { - StringBuffer sb = new StringBuffer(); - - if (d != null) - { - int year; - int month; - int day; - int hour; - int minute; - int second; - - if (localTime) - { - year = d.getYear(); - month = d.getMonth(); - day = d.getDay(); - hour = d.getHour(); - minute = d.getMinute(); - second = d.getSecond(); - } - else - { - year = d.getYearGMT(); - month = d.getMonthGMT(); - day = d.getDayGMT(); - hour = d.getHourGMT(); - minute = d.getMinuteGMT(); - second = d.getSecondGMT(); - } - - if (year > -1) - { - if (month > -1) - { - if (day > -1) - { - sb.append(day + "-"); - } - - sb.append(DCDate.getMonthName(month).substring(0, 3) + "-"); - } - - sb.append(year + " "); - } - - if (time && (hour > -1)) - { - String hr = String.valueOf(hour); - - while (hr.length() < 2) - { - hr = "0" + hr; - } - - String mn = String.valueOf(minute); - - while (mn.length() < 2) - { - mn = "0" + mn; - } - - String sc = String.valueOf(second); - - while (sc.length() < 2) - { - sc = "0" + sc; - } - - sb.append(hr + ":" + mn + ":" + sc + " "); - } - } - else - { - sb.append("Unset"); - } - - return (sb.toString()); - } - - /** * Return a string for logging, containing useful information about the * current request - the URL, the method and parameters. * @@ -564,143 +442,5 @@ // Not much we can do here! log.warn("Unable to send email alert", e); } - } - - /** - * Encode a bitstream name for inclusion in a URL in an HTML document. This - * differs from the usual URL-encoding, since we want pathname separators to - * be passed through verbatim; this is required so that relative paths in - * bitstream names and HTML references work correctly. - *
- * If the link to a bitstream is generated with the pathname separators - * escaped (e.g. "%2F" instead of "/") then the Web user agent perceives it - * to be one pathname element, and relative URI paths within that document - * containing ".." elements will be handled incorrectly. - *
- * - * @param stringIn - * input string to encode - * @param encoding - * character encoding, e.g. UTF-8 - * @return the encoded string - */ - public static String encodeBitstreamName(String stringIn, String encoding) - throws java.io.UnsupportedEncodingException - { - // FIXME: This should be moved elsewhere, as it is used outside the UI - StringBuffer out = new StringBuffer(); - - final String[] pctEncoding = { "%00", "%01", "%02", "%03", "%04", - "%05", "%06", "%07", "%08", "%09", "%0a", "%0b", "%0c", "%0d", - "%0e", "%0f", "%10", "%11", "%12", "%13", "%14", "%15", "%16", - "%17", "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f", - "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", "%28", - "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f", "%30", "%31", - "%32", "%33", "%34", "%35", "%36", "%37", "%38", "%39", "%3a", - "%3b", "%3c", "%3d", "%3e", "%3f", "%40", "%41", "%42", "%43", - "%44", "%45", "%46", "%47", "%48", "%49", "%4a", "%4b", "%4c", - "%4d", "%4e", "%4f", "%50", "%51", "%52", "%53", "%54", "%55", - "%56", "%57", "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", - "%5f", "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67", - "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f", "%70", - "%71", "%72", "%73", "%74", "%75", "%76", "%77", "%78", "%79", - "%7a", "%7b", "%7c", "%7d", "%7e", "%7f", "%80", "%81", "%82", - "%83", "%84", "%85", "%86", "%87", "%88", "%89", "%8a", "%8b", - "%8c", "%8d", "%8e", "%8f", "%90", "%91", "%92", "%93", "%94", - "%95", "%96", "%97", "%98", "%99", "%9a", "%9b", "%9c", "%9d", - "%9e", "%9f", "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", - "%a7", "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af", - "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7", "%b8", - "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf", "%c0", "%c1", - "%c2", "%c3", "%c4", "%c5", "%c6", "%c7", "%c8", "%c9", "%ca", - "%cb", "%cc", "%cd", "%ce", "%cf", "%d0", "%d1", "%d2", "%d3", - "%d4", "%d5", "%d6", "%d7", "%d8", "%d9", "%da", "%db", "%dc", - "%dd", "%de", "%df", "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", - "%e6", "%e7", "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", - "%ef", "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7", - "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff" }; - - byte[] bytes = stringIn.getBytes(encoding); - - for (int i = 0; i < bytes.length; i++) - { - // Any unreserved char or "/" goes through unencoded - if ((bytes[i] >= 'A' && bytes[i] <= 'Z') - || (bytes[i] >= 'a' && bytes[i] <= 'z') - || (bytes[i] >= '0' && bytes[i] <= '9') || bytes[i] == '-' - || bytes[i] == '.' || bytes[i] == '_' || bytes[i] == '~' - || bytes[i] == '/') - { - out.append((char) bytes[i]); - } - else if (bytes[i] >= 0) - { - // encode other chars (byte code < 128) - out.append(pctEncoding[bytes[i]]); - } - else - { - // encode other chars (byte code > 127, so it appears as - // negative in Java signed byte data type) - out.append(pctEncoding[256 + bytes[i]]); - } - } - log.debug("encoded \"" + stringIn + "\" to \"" + out.toString() + "\""); - - return out.toString(); } - - /** Version of encodeBitstreamName with one parameter, uses default encoding - *
- * @param stringIn - * input string to encode - * @return the encoded string - */ - public static String encodeBitstreamName(String stringIn) - throws java.io.UnsupportedEncodingException - { - return encodeBitstreamName(stringIn, Constants.DEFAULT_ENCODING); - } - - /** - * Formats the file size. Examples: - * - * - 50 = 50B - * - 1024 = 1KB - * - 1,024,000 = 1MB etc - * - * The numbers are formatted using java Locales - * - * @param in The number to covnert - * @return the file size as a String - */ - public static String formatFileSize(double in) - { - // Work out the size of the file, and format appropriatly - // FIXME: When full i18n support is available, use the user's Locale - // rather than the default Locale. - NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault()); - DecimalFormat df = (DecimalFormat)nf; - df.applyPattern("###,###.##"); - if (in < 1024) - { - df.applyPattern("0"); - return df.format(in) + " " + "B"; - } - else if (in < 1024000) - { - in = in / 1024; - return df.format(in) + " " + "kB"; - } - else if (in < 1024000000) - { - in = in / 1024000; - return df.format(in) + " " + "MB"; - } - else - { - in = in / 1024000000; - return df.format(in) + " " + "GB"; - } - } } Index: org/dspace/app/util/Util.java =================================================================== --- org/dspace/app/util/Util.java +++ org/dspace/app/util/Util.java @@ -0,0 +1,274 @@ +package org.dspace.app.util; + +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; + +import org.apache.log4j.Logger; +import org.dspace.content.DCDate; +import org.dspace.core.Constants; + +public class Util { + + /** log4j category */ + static Logger log = Logger.getLogger(Util.class); + + /** + * Utility method to convert spaces in a string to HTML non-break space + * elements. + * + * @param s + * string to change spaces in + * @return the string passed in with spaces converted to HTML non-break + * spaces + */ + public static String nonBreakSpace(String s) { + StringBuffer newString = new StringBuffer(); + + for (int i = 0; i < s.length(); i++) + { + char ch = s.charAt(i); + + if (ch == ' ') + { + newString.append(" "); + } + else + { + newString.append(ch); + } + } + + return newString.toString(); + } + + /** + * Write a human-readable version of a DCDate. + * + * @param d + * the date + * @param time + * if true, display the time with the date + * @param localTime + * if true, adjust for local timezone, otherwise GMT + * + * @return the date in a human-readable form. + */ + public static String displayDate(DCDate d, boolean time, boolean localTime) { + StringBuffer sb = new StringBuffer(); + + if (d != null) + { + int year; + int month; + int day; + int hour; + int minute; + int second; + + if (localTime) + { + year = d.getYear(); + month = d.getMonth(); + day = d.getDay(); + hour = d.getHour(); + minute = d.getMinute(); + second = d.getSecond(); + } + else + { + year = d.getYearGMT(); + month = d.getMonthGMT(); + day = d.getDayGMT(); + hour = d.getHourGMT(); + minute = d.getMinuteGMT(); + second = d.getSecondGMT(); + } + + if (year > -1) + { + if (month > -1) + { + if (day > -1) + { + sb.append(day + "-"); + } + + sb.append(DCDate.getMonthName(month).substring(0, 3) + "-"); + } + + sb.append(year + " "); + } + + if (time && (hour > -1)) + { + String hr = String.valueOf(hour); + + while (hr.length() < 2) + { + hr = "0" + hr; + } + + String mn = String.valueOf(minute); + + while (mn.length() < 2) + { + mn = "0" + mn; + } + + String sc = String.valueOf(second); + + while (sc.length() < 2) + { + sc = "0" + sc; + } + + sb.append(hr + ":" + mn + ":" + sc + " "); + } + } + else + { + sb.append("Unset"); + } + + return (sb.toString()); + } + + /** + * Encode a bitstream name for inclusion in a URL in an HTML document. This + * differs from the usual URL-encoding, since we want pathname separators to + * be passed through verbatim; this is required so that relative paths in + * bitstream names and HTML references work correctly. + *
+ * If the link to a bitstream is generated with the pathname separators + * escaped (e.g. "%2F" instead of "/") then the Web user agent perceives it + * to be one pathname element, and relative URI paths within that document + * containing ".." elements will be handled incorrectly. + *
+ * + * @param stringIn + * input string to encode + * @param encoding + * character encoding, e.g. UTF-8 + * @return the encoded string + */ + public static String encodeBitstreamName(String stringIn, String encoding) throws java.io.UnsupportedEncodingException { + // FIXME: This should be moved elsewhere, as it is used outside the UI + StringBuffer out = new StringBuffer(); + + final String[] pctEncoding = { "%00", "%01", "%02", "%03", "%04", + "%05", "%06", "%07", "%08", "%09", "%0a", "%0b", "%0c", "%0d", + "%0e", "%0f", "%10", "%11", "%12", "%13", "%14", "%15", "%16", + "%17", "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f", + "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", "%28", + "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f", "%30", "%31", + "%32", "%33", "%34", "%35", "%36", "%37", "%38", "%39", "%3a", + "%3b", "%3c", "%3d", "%3e", "%3f", "%40", "%41", "%42", "%43", + "%44", "%45", "%46", "%47", "%48", "%49", "%4a", "%4b", "%4c", + "%4d", "%4e", "%4f", "%50", "%51", "%52", "%53", "%54", "%55", + "%56", "%57", "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", + "%5f", "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67", + "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f", "%70", + "%71", "%72", "%73", "%74", "%75", "%76", "%77", "%78", "%79", + "%7a", "%7b", "%7c", "%7d", "%7e", "%7f", "%80", "%81", "%82", + "%83", "%84", "%85", "%86", "%87", "%88", "%89", "%8a", "%8b", + "%8c", "%8d", "%8e", "%8f", "%90", "%91", "%92", "%93", "%94", + "%95", "%96", "%97", "%98", "%99", "%9a", "%9b", "%9c", "%9d", + "%9e", "%9f", "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", + "%a7", "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af", + "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7", "%b8", + "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf", "%c0", "%c1", + "%c2", "%c3", "%c4", "%c5", "%c6", "%c7", "%c8", "%c9", "%ca", + "%cb", "%cc", "%cd", "%ce", "%cf", "%d0", "%d1", "%d2", "%d3", + "%d4", "%d5", "%d6", "%d7", "%d8", "%d9", "%da", "%db", "%dc", + "%dd", "%de", "%df", "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", + "%e6", "%e7", "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", + "%ef", "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7", + "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff" }; + + byte[] bytes = stringIn.getBytes(encoding); + + for (int i = 0; i < bytes.length; i++) + { + // Any unreserved char or "/" goes through unencoded + if ((bytes[i] >= 'A' && bytes[i] <= 'Z') + || (bytes[i] >= 'a' && bytes[i] <= 'z') + || (bytes[i] >= '0' && bytes[i] <= '9') || bytes[i] == '-' + || bytes[i] == '.' || bytes[i] == '_' || bytes[i] == '~' + || bytes[i] == '/') + { + out.append((char) bytes[i]); + } + else if (bytes[i] >= 0) + { + // encode other chars (byte code < 128) + out.append(pctEncoding[bytes[i]]); + } + else + { + // encode other chars (byte code > 127, so it appears as + // negative in Java signed byte data type) + out.append(pctEncoding[256 + bytes[i]]); + } + } + log.debug("encoded \"" + stringIn + "\" to \"" + out.toString() + "\""); + + return out.toString(); + } + + /** Version of encodeBitstreamName with one parameter, uses default encoding + *
+ * @param stringIn + * input string to encode + * @return the encoded string + */ + public static String encodeBitstreamName(String stringIn) throws java.io.UnsupportedEncodingException { + return encodeBitstreamName(stringIn, Constants.DEFAULT_ENCODING); + } + + /** + * Formats the file size. Examples: + * + * - 50 = 50B + * - 1024 = 1KB + * - 1,024,000 = 1MB etc + * + * The numbers are formatted using java Locales + * + * @param in The number to covnert + * @return the file size as a String + */ + public static String formatFileSize(double in) { + // Work out the size of the file, and format appropriatly + // FIXME: When full i18n support is available, use the user's Locale + // rather than the default Locale. + NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault()); + DecimalFormat df = (DecimalFormat)nf; + df.applyPattern("###,###.##"); + if (in < 1024) + { + df.applyPattern("0"); + return df.format(in) + " " + "B"; + } + else if (in < 1024000) + { + in = in / 1024; + return df.format(in) + " " + "kB"; + } + else if (in < 1024000000) + { + in = in / 1024000; + return df.format(in) + " " + "MB"; + } + else + { + in = in / 1024000000; + return df.format(in) + " " + "GB"; + } + } + + public Util() { + super(); + } + +} \ No newline at end of file