/* * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.xwiki.rendering.internal.renderer; import org.apache.commons.httpclient.util.URIUtil; import org.apache.commons.lang.StringUtils; import org.xwiki.component.annotation.Component; import org.xwiki.rendering.listener.Link; import org.xwiki.rendering.renderer.URILabelGenerator; import java.util.Map; import java.util.HashMap; import java.util.Locale; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; /** * Generate link labels for MAGNET URIs. * * @version $Id: MailtoURILabelGenerator.java 26608 2010-02-02 16:20:17Z vmassol $ * @since 2.2RC1 */ @Component("magnet") public class MagnetURILabelGenerator implements URILabelGenerator { /** * The MAGNET URI prefix (the scheme followed by ":"). */ private static final String MAGNET = "magnet:"; private static Map getQueryMap(String query) { String[] params = query.split("&"); Map map = new HashMap(); for (String param : params) { String name = StringUtils.substringBefore(param, "="); String value = StringUtils.substringAfter(param, "="); map.put(name, value); } return map; } /** * {@inheritDoc} * @see org.xwiki.rendering.renderer.URILabelGenerator#generateLabel(org.xwiki.rendering.listener.Link) */ public String generateLabel(Link link) { // Remove the scheme prefix and the query string (if any). //return StringUtils.substringBefore(StringUtils.removeStart(link.getReference(), MAGNET), "?"); String result = "(magnet link)"; String magnet = StringUtils.removeStart(link.getReference(), MAGNET); int index = StringUtils.indexOf(magnet, '?'); if (index < 0) { return result; } String queryAndHash = StringUtils.substring(magnet, index + 1); // I haven't heard of magnet links with #s, but let's handle them // anyway String query = StringUtils.substringBeforeLast(queryAndHash, "#"); Map magnetMap = getQueryMap(magnet); if (magnetMap.containsKey("dn")) { String dnString = magnetMap.get("dn"); if (!StringUtils.isEmpty(dnString)) { try { dnString = URIUtil.decode(dnString); } catch (Exception e) { } result = dnString; } } if (magnetMap.containsKey("xl")) { String xlString = magnetMap.get("xl"); if (!StringUtils.isEmpty(xlString)) { try { long xlLong = Long.parseLong(xlString); String humanReadableSize = null; DecimalFormat xlFormat = new DecimalFormat("#0.##", new DecimalFormatSymbols(Locale.US)); if (xlLong < 1024L) { humanReadableSize = "" + xlLong + " bytes"; } else if (xlLong < 1048576L) { humanReadableSize = xlFormat.format(((double) xlLong) / 1024.0) + "kb"; } else if (xlLong < 1073741824L) { humanReadableSize = xlFormat.format(((double) xlLong) / 1048576.0) + "Mb"; } else if (xlLong < 1099511627776L) { humanReadableSize = xlFormat.format(((double) xlLong) / 1073741824.0) + "Gb"; } else if (xlLong < 1125899906842624L) { humanReadableSize = xlFormat.format(((double) xlLong) / 1099511627776.0) + "Tb"; } else if (xlLong < 1152921504606846976L) { humanReadableSize = xlFormat.format(((double) xlLong) / 1125899906842624.0) + "Pb"; } else { humanReadableSize = xlFormat.format(((double) xlLong) / 1152921504606846976.0) + "Eb"; } result = result + " (" + humanReadableSize + ")"; } catch (Exception e) { } } } return result; } }