Example usage for org.apache.commons.lang3 StringUtils chomp

List of usage examples for org.apache.commons.lang3 StringUtils chomp

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils chomp.

Prototype

@Deprecated
public static String chomp(final String str, final String separator) 

Source Link

Document

Removes separator from the end of str if it's there, otherwise leave it alone.

NOTE: This method changed in version 2.0.

Usage

From source file:by.lang.StringUtilsTest.java

public static void main(String[] args) {
    System.out.println("?,....");
    System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 10));

    System.out.println(".");
    System.out.println(StringUtils.chomp("1,2,3,", ","));

    System.out.println("???.");
    System.out.println(StringUtils.contains("defg", "ef"));
    System.out.println(StringUtils.contains("ef", "defg"));

    System.out.println("??nulltoString().");
    System.out.println(StringUtils.defaultString(null, "defaultIfEmpty"));
    System.out.println(".");
    System.out.println(StringUtils.deleteWhitespace("aa  bb  cc"));

    System.out.println("??.");
    System.out.println(StringUtils.isAlpha("ab"));
    System.out.println(StringUtils.isAlphanumeric("12"));
    System.out.println(StringUtils.isBlank(""));
    System.out.println(StringUtils.isNumeric("123"));

    System.out.println("?Null  \"\"");
    System.out.println(StringUtils.isEmpty(null));
    System.out.println(StringUtils.isNotEmpty(null));
    System.out.println("?null  \"\" ");
    System.out.println(StringUtils.isBlank("  "));
    System.out.println(StringUtils.isNotBlank(null));
    System.out.println(".Nullnull");
    System.out.println(StringUtils.trim(null));
    System.out.println("Null\"\" ?Null");
    System.out.println(StringUtils.trimToNull(""));
    // NULL  "" ?""
    // System.out.println(StringUtils.trimToEmpty(null));
    // ??/* w w w. ja v  a  2s  .com*/
    // System.out.println(StringUtils.strip("  \t"));
    // ?""null?Null
    // System.out.println(StringUtils.stripToNull(" \t"));
    // ?""null?""
    // System.out.println(StringUtils.stripToEmpty(null));
    // ""Null ? ""
    // System.out.println(StringUtils.defaultString(null));
    // Null ?(?)
    // System.out.println(StringUtils.defaultString("", "df"));
    // null""?(?)
    // System.out.println(StringUtils.defaultIfEmpty(null, "sos"));
    // .~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // ?null(?2?)
    // System.out.println(StringUtils.strip("fsfsdf", "f"));
    // ?null???(????)
    // System.out.println(StringUtils.stripStart("ddsuuu ", "d"));
    // ?null???(????)
    // System.out.println(StringUtils.stripEnd("dabads", "das"));
    // 
    // ArrayToList(StringUtils.stripAll(new String[]{" ? ", "  ",
    // " "}));
    // ?null.?(??)
    // ArrayToList(StringUtils.stripAll(new String[]{" ? ", " ", ""},
    // ""));
    // ,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // 2?,Null
    // System.out.println(StringUtils.equals(null, null));
    // ??
    // System.out.println(StringUtils.equalsIgnoreCase("abc", "ABc"));
    // ????~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // ?null""-1
    // System.out.println(StringUtils.indexOf(null, "a"));
    // ?(?)2k
    // System.out.println(StringUtils.indexOf("akfekcd?", "k", 2));
    // ???
    // System.out.println(StringUtils.ordinalIndexOf("akfekcd?", "k", 2));
    // ,??
    // System.out.println(StringUtils.indexOfIgnoreCase("adfs", "D"));
    // ?(?),??
    // System.out.println(StringUtils.indexOfIgnoreCase("adfs", "a", 3));
    // ??
    // System.out.println(StringUtils.lastIndexOf("adfas", "a"));
    // ?,2
    // System.out.println(StringUtils.lastIndexOf("dabasdafs", "a", 3));
    // ,-1
    // System.out.println(StringUtils.lastOrdinalIndexOf("yksdfdht", "f",
    // 2));
    // ????
    // System.out.println(StringUtils.lastIndexOfIgnoreCase("sdffet", "E"));
    // ,1
    // System.out.println(StringUtils.lastIndexOfIgnoreCase("efefrfs", "F"
    // , 2));
    // ?boolean,null?
    // System.out.println(StringUtils.contains("sdf", "dg"));
    // ?boolean,null?,??
    // System.out.println(StringUtils.containsIgnoreCase("sdf", "D"));
    // ??,boolean
    // System.out.println(StringUtils.containsWhitespace(" d"));
    // ???
    // System.out.println(StringUtils.indexOfAny("absfekf", new
    // String[]{"f", "b"}));
    // (?)
    // System.out.println(StringUtils.indexOfAny("afefes", "e"));
    // ??boolean
    // System.out.println(StringUtils.containsAny("asfsd", new char[]{'k',
    // 'e', 's'}));
    // ?lastIndexOf???boolean
    // System.out.println(StringUtils.containsAny("f", ""));
    // 
    // System.out.println(StringUtils.indexOfAnyBut("seefaff", "af"));
    // ?
    // System.out.println(StringUtils.containsOnly("??", "?"));
    // ?
    // System.out.println(StringUtils.containsOnly("?", new char[]{'',
    // '?'}));
    // ??
    // System.out.println(StringUtils.containsNone("??", ""));
    // ??
    // System.out.println(StringUtils.containsNone("?", new char[]{'',
    // ''}));
    // ????4
    // System.out.println(StringUtils.lastIndexOfAny("", new
    // String[]{"", ""}));
    // ?indexOfAny?? (?)
    // System.out.println(StringUtils.countMatches("", ""));
    // ?CharSequence??Unicode?falseCharSequence=
    // 0true
    // System.out.println(StringUtils.isAlpha("2"));
    // ???UnicodeCharSequence?''CharSequence?=
    // 0true
    // System.out.println(StringUtils.isAlphaSpace("NBA "));
    // ???UnicodeCharSequence?falseCharSequence=
    // 0true
    // System.out.println(StringUtils.isAlphanumeric("NBA"));
    // Unicode
    // CharSequence???''falseCharSequence=
    // 0true
    // System.out.println(StringUtils.isAlphanumericSpace("NBA"));
    // ???ASCII?CharSequencefalseCharSequence=
    // 0true
    // System.out.println(StringUtils.isAsciiPrintable("NBA"));
    // ???
    // System.out.println(StringUtils.isNumeric("NBA"));
    // ???
    // System.out.println(StringUtils.isNumericSpace("33 545"));
    // ??""
    // System.out.println(StringUtils.isWhitespace(" "));
    // ??
    // System.out.println(StringUtils.isAllLowerCase("kjk33"));
    // ?
    // System.out.println(StringUtils.isAllUpperCase("KJKJ"));
    // ?~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // ?2?:
    // System.out.println(StringUtils.difference("", ""));
    // 2
    // System.out.println(StringUtils.indexOfDifference("ww.taobao",
    // "www.taobao.com"));
    // ?
    // System.out.println(StringUtils.indexOfDifference(new String[]
    // {"", "", ""}));
    // ???
    // System.out.println(StringUtils.getCommonPrefix(new String[] {"",
    // "", ""}));
    // ??????
    // System.out.println(StringUtils.getLevenshteinDistance("?",
    // ""));
    // ???
    // System.out.println(StringUtils.startsWith("", ""));
    // ?????
    // System.out.println(StringUtils.startsWithIgnoreCase("",
    // ""));
    // ???
    // System.out.println(StringUtils.startsWithAny("abef", new
    // String[]{"ge", "af", "ab"}));
    // ??
    // System.out.println(StringUtils.endsWith("abcdef", "def"));
    // ????
    // System.out.println(StringUtils.endsWithIgnoreCase("abcdef", "Def"));
    // ?~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // ??nullnull.""""
    // System.out.println(StringUtils.substring("", 2));
    // ?
    // System.out.println(StringUtils.substring("", 2, 4));
    // ?
    // System.out.println(StringUtils.left("", 3));
    // ??
    // System.out.println(StringUtils.right("", 3));
    // ???
    // System.out.println(StringUtils.mid("", 3, 2));
    // ??
    // System.out.println(StringUtils.substringBefore("", ""));
    // ?????
    // System.out.println(StringUtils.substringAfter("", ""));
    // ??.
    // System.out.println(StringUtils.substringBeforeLast("", ""));
    // ?????
    // System.out.println(StringUtils.substringAfterLast("", ""));
    // ???null:2010?
    // System.out.println(StringUtils.substringBetween("??2010?????",
    // "??"));
    // ???
    // ArrayToList(StringUtils.substringsBetween("[a][b][c]", "[", "]"));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // ?nullnull
    // ArrayToList(StringUtils.split("?  "));
    // ?
    // ArrayToList(StringUtils.split("? ,,", ","));
    // ???0
    // ArrayToList(StringUtils.split("? ", "", 2));
    // ???,?
    // ArrayToList(StringUtils.splitByWholeSeparator("ab-!-cd-!-ef",
    // "-!-"));
    // ???,???
    // ArrayToList(StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-",
    // 2));
    // " "?,?null
    // ArrayToList(StringUtils.splitByWholeSeparatorPreserveAllTokens(" ab
    // de fg ",
    // null));
    // ?," "??
    // ArrayToList(StringUtils.splitByWholeSeparatorPreserveAllTokens("ab de
    // fg",
    // null, 3));
    // ???,
    // ArrayToList(StringUtils.splitPreserveAllTokens(" ab de fg "));
    // ???,?
    // ArrayToList(StringUtils.splitPreserveAllTokens(" ab de fg ",
    // null));
    // ???,???
    // ArrayToList(StringUtils.splitPreserveAllTokens(" ab de fg ", null,
    // 2));
    // ??
    // ArrayToList(StringUtils.splitByCharacterType("AEkjKr i39:"));
    // 
    // ArrayToList(StringUtils.splitByCharacterTypeCamelCase("ASFSRules234"));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // ??
    // System.out.println(StringUtils.concat(getArrayData()));
    // ?.?null
    // System.out.println(StringUtils.concatWith(",", getArrayData()));
    // ?
    // System.out.println(StringUtils.join(getArrayData()));
    // ?
    // System.out.println(StringUtils.join(getArrayData(), ":"));
    // (?)?(?,??)
    // System.out.println(StringUtils.join(getArrayData(), ":", 1, 3));
    // ?.?
    // System.out.println(StringUtils.join(getListData(), ":"));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // 
    // System.out.println(StringUtils.deleteWhitespace(" s   4j"));
    // ?
    // System.out.println(StringUtils.removeStart("www.baidu.com", "www."));
    // ?,??
    // System.out.println(StringUtils.removeStartIgnoreCase("www.baidu.com",
    // "WWW"));
    // ???
    // System.out.println(StringUtils.removeEnd("www.baidu.com", ".com"));
    // ?????
    // System.out.println(StringUtils.removeEndIgnoreCase("www.baidu.com",
    // ".COM"));
    // ?
    // System.out.println(StringUtils.remove("www.baidu.com/baidu", "bai"));
    // "\n", "\r",  "\r\n".
    // System.out.println(StringUtils.chomp("abcrabc\r"));
    // ?
    // System.out.println(StringUtils.chomp("baidu.com", "com"));
    // ?."\n", "\r",  "\r\n"
    // System.out.println(StringUtils.chop("wwe.baidu"));
    // ?~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // ???
    // System.out.println(StringUtils.replaceOnce("www.baidu.com/baidu",
    // "baidu", "hao123"));
    // ?
    // System.out.println(StringUtils.replace("www.baidu.com/baidu",
    // "baidu", "hao123"));
    // ????
    // System.out.println(StringUtils.replace("www.baidu.com/baidu",
    // "baidu", "hao123", 1));
    // ?????:baidu?taobaocom?net
    // System.out.println(StringUtils.replaceEach("www.baidu.com/baidu", new
    // String[]{"baidu", "com"}, new String[]{"taobao", "net"}));
    // ????
    // System.out.println(StringUtils.replaceEachRepeatedly("www.baidu.com/baidu",
    // new String[]{"baidu", "com"}, new String[]{"taobao", "net"}));
    // ????.(????)
    // System.out.println(StringUtils.replaceChars("www.baidu.com", "bdm",
    // "qo"));
    // ?(?)?(?)
    // System.out.println(StringUtils.overlay("www.baidu.com", "hao123", 4,
    // 9));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // ????
    // System.out.println(StringUtils.repeat("ba", 3));
    // ??????
    // System.out.println(StringUtils.repeat("ab", "ou", 3));
    // ??(???)
    // System.out.println(StringUtils.rightPad("?", 4));
    // ????(?)
    // System.out.println(StringUtils.rightPad("?", 4, "?"));
    // ???
    // System.out.println(StringUtils.leftPad("?", 4));
    // ??????(?)
    // System.out.println(StringUtils.leftPad("?", 4, ""));
    // ?????
    // System.out.println(StringUtils.center("?", 3));
    // ??????
    // System.out.println(StringUtils.center("?", 5, "?"));
    // ??(?),??(??+=?)
    // System.out.println(StringUtils.abbreviate("?", 5));
    // 2: ...ijklmno
    // System.out.println(StringUtils.abbreviate("abcdefghijklmno", 12,
    // 10));
    // ???.: ab.f
    // System.out.println(StringUtils.abbreviateMiddle("abcdef", ".", 4));
    // ?,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // ?.
    // System.out.println(StringUtils.capitalize("Ddf"));
    // ?.
    // System.out.println(StringUtils.uncapitalize("DTf"));
    // ????????
    // System.out.println(StringUtils.swapCase("I am Jiang, Hello"));
    // ?
    // System.out.println(StringUtils.reverse(""));
    // ?(?)??
    // System.out.println(StringUtils.reverseDelimited("::", ':'));
    // isEmpty
    System.out.println(StringUtils.isEmpty(null)); // true
    System.out.println(StringUtils.isEmpty("")); // true
    System.out.println(StringUtils.isEmpty(" ")); // false
    System.out.println(StringUtils.isEmpty("bob")); // false
    System.out.println(StringUtils.isEmpty("  bob  ")); // false

    // isBlank
    System.out.println(StringUtils.isBlank(null)); // true
    System.out.println(StringUtils.isBlank("")); // true
    System.out.println(StringUtils.isBlank(" ")); // true
    System.out.println(StringUtils.isBlank("bob")); // false
    System.out.println(StringUtils.isBlank("  bob  ")); // false

    // trim
    System.out.println(StringUtils.trim(null)); // null
    System.out.println(StringUtils.trim("")); // ""
    System.out.println(StringUtils.trim("     ")); // ""
    System.out.println(StringUtils.trim("abc")); // "abc"
    System.out.println(StringUtils.trim("    abc")); // "abc"
    System.out.println(StringUtils.trim("    abc  ")); // "abc"
    System.out.println(StringUtils.trim("    ab c  ")); // "ab c"

    // strip
    System.out.println(StringUtils.strip(null)); // null
    System.out.println(StringUtils.strip("")); // ""
    System.out.println(StringUtils.strip("   ")); // ""
    System.out.println(StringUtils.strip("abc")); // "abc"
    System.out.println(StringUtils.strip("  abc")); // "abc"
    System.out.println(StringUtils.strip("abc  ")); // "abc"
    System.out.println(StringUtils.strip(" abc ")); // "abc"
    System.out.println(StringUtils.strip(" ab c ")); // "ab c"
    System.out.println(StringUtils.strip("  abcyx", "xyz")); // " abc"
    System.out.println(StringUtils.stripStart("yxabcxyz  ", "xyz")); // "abcxyz
    // "
    System.out.println(StringUtils.stripEnd("  xyzabcyx", "xyz")); // "
    // xyzabc"
    // ?
    String str1 = "aaa bbb ccc";
    String[] dim1 = StringUtils.split(str1); // => ["aaa", "bbb", "ccc"]

    System.out.println(dim1.length);// 3
    System.out.println(dim1[0]);// "aaa"
    System.out.println(dim1[1]);// "bbb"
    System.out.println(dim1[2]);// "ccc"

    // 
    String str2 = "aaa,bbb,ccc";
    String[] dim2 = StringUtils.split(str2, ","); // => ["aaa", "bbb",
    // "ccc"]

    System.out.println(dim2.length);// 3
    System.out.println(dim2[0]);// "aaa"
    System.out.println(dim2[1]);// "bbb"
    System.out.println(dim2[2]);// "ccc"

    // 
    String str3 = "aaa,,bbb";
    String[] dim3 = StringUtils.split(str3, ","); // => ["aaa", "bbb"]

    System.out.println(dim3.length);// 2
    System.out.println(dim3[0]);// "aaa"
    System.out.println(dim3[1]);// "bbb"

    // ?
    String str4 = "aaa,,bbb";
    String[] dim4 = StringUtils.splitPreserveAllTokens(str4, ","); // =>
    // ["aaa",
    // "",
    // "bbb"]

    System.out.println(dim4.length);// 3
    System.out.println(dim4[0]);// "aaa"
    System.out.println(dim4[1]);// ""
    System.out.println(dim4[2]);// "bbb"

    // ??
    String str5 = "aaa,bbb,ccc";
    String[] dim5 = StringUtils.split(str5, ",", 2); // => ["aaa",
    // "bbb,ccc"]

    System.out.println(dim5.length);// 2
    System.out.println(dim5[0]);// "aaa"
    System.out.println(dim5[1]);// "bbb,ccc"

    // 
    String[] array = { "aaa", "bbb", "ccc" };
    String result1 = StringUtils.join(array, ",");

    System.out.println(result1);// "aaa,bbb,ccc"

    // ?
    List<String> list = new ArrayList<String>();
    list.add("aaa");
    list.add("bbb");
    list.add("ccc");
    String result2 = StringUtils.join(list, ",");
    System.out.println(result2);// "aaa,bbb,ccc"

}

From source file:com.jaspersoft.jasperserver.jrsh.common.ZipUtil.java

public static File pack(String directory) {
    File dir = new File(directory);
    if (!dir.isDirectory()) {
        throw new DirectoryDoesNotExistException(directory);
    }/*from w ww  .ja v  a 2 s. co  m*/
    directory = StringUtils.chomp(directory, separator);
    String outputFileName = directory.concat(".zip");
    try {
        File arch = new File(outputFileName);
        FileOutputStream fos = null;
        fos = new FileOutputStream(arch);
        ZipOutputStream zos = new ZipOutputStream(fos);
        addFiles(zos, directory, directory);
        zos.close();
        return arch;
    } catch (Exception unimportant) {
        throw new CannotPackDirectoryException();
    }
}

From source file:com.xpn.xwiki.objects.ListProperty.java

/**
 * {@inheritDoc}//from  w  w  w  .  j  a v a2  s . com
 * 
 * @see com.xpn.xwiki.objects.BaseProperty#toFormString()
 */
@Override
public String toFormString() {
    List<String> list = getList();
    StringBuilder result = new StringBuilder();
    for (String item : list) {
        result.append(XMLUtils.escape(item).replace(this.formStringSeparator, "\\" + this.formStringSeparator));
        result.append(this.formStringSeparator);
    }

    return StringUtils.chomp(result.toString(), this.formStringSeparator);
}

From source file:com.xpn.xwiki.web.XWikiServletURLFactory.java

/**
 * {@inheritDoc}/*from   ww  w  .  ja v  a2  s  . c o m*/
 * 
 * @see com.xpn.xwiki.web.XWikiURLFactory#createURL(java.lang.String, java.lang.String, java.lang.String,
 *      java.lang.String, java.lang.String, java.lang.String, com.xpn.xwiki.XWikiContext)
 */
public URL createURL(String web, String name, String action, String querystring, String anchor, String xwikidb,
        XWikiContext context) {
    // Action and Query String transformers
    if (("view".equals(action)) && (context.getLinksAction() != null)) {
        action = context.getLinksAction();
    }
    if (context.getLinksQueryString() != null) {
        if (querystring == null) {
            querystring = context.getLinksQueryString();
        } else {
            querystring = querystring + "&" + context.getLinksQueryString();
        }
    }

    StringBuffer newpath = new StringBuffer(this.contextPath);
    addServletPath(newpath, xwikidb, context);
    addAction(newpath, action, context);
    addSpace(newpath, web, action, context);
    addName(newpath, name, action, context);

    if (!StringUtils.isEmpty(querystring)) {
        newpath.append("?");
        newpath.append(StringUtils.chomp(StringUtils.chomp(querystring, "&"), "&amp;"));
        // newpath.append(querystring.replaceAll("&","&amp;"));
    }

    if (!StringUtils.isEmpty(anchor)) {
        newpath.append("#");
        newpath.append(encode(anchor, context));
    }

    try {
        return new URL(getServerURL(xwikidb, context), newpath.toString());
    } catch (MalformedURLException e) {
        // This should not happen
        return null;
    }
}

From source file:com.xpn.xwiki.web.XWikiServletURLFactory.java

/**
 * {@inheritDoc}/*from  w w  w . j  a v a 2 s.c o m*/
 * 
 * @see com.xpn.xwiki.web.XWikiURLFactory#createAttachmentURL(java.lang.String, java.lang.String, java.lang.String,
 *      java.lang.String, java.lang.String, java.lang.String, com.xpn.xwiki.XWikiContext)
 */
public URL createAttachmentURL(String filename, String web, String name, String action, String querystring,
        String xwikidb, XWikiContext context) {
    if ((context != null) && "viewrev".equals(context.getAction()) && context.get("rev") != null
            && isContextDoc(xwikidb, web, name, context)) {
        try {
            String docRevision = context.get("rev").toString();
            XWikiAttachment attachment = findAttachmentForDocRevision(context.getDoc(), docRevision, filename,
                    context);
            if (attachment == null) {
                action = "viewattachrev";
            } else {
                long arbId = findDeletedAttachmentForDocRevision(context.getDoc(), docRevision, filename,
                        context);
                return createAttachmentRevisionURL(filename, web, name, attachment.getVersion(), arbId,
                        querystring, xwikidb, context);
            }
        } catch (XWikiException e) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("Exception while trying to get attachment version !", e);
            }
        }
    }

    StringBuffer newpath = new StringBuffer(this.contextPath);
    addServletPath(newpath, xwikidb, context);
    addAction(newpath, action, context);
    addSpace(newpath, web, action, context);
    addName(newpath, name, action, context);
    addFileName(newpath, filename, context);

    if (!StringUtils.isEmpty(querystring)) {
        newpath.append("?");
        newpath.append(StringUtils.chomp(StringUtils.chomp(querystring, "&"), "&amp;"));
    }

    try {
        return new URL(getServerURL(xwikidb, context), newpath.toString());
    } catch (Exception e) {
        return null;
    }
}

From source file:com.xpn.xwiki.web.XWikiServletURLFactory.java

public URL createAttachmentRevisionURL(String filename, String web, String name, String revision,
        long recycleId, String querystring, String xwikidb, XWikiContext context) {
    String action = "downloadrev";
    StringBuffer newpath = new StringBuffer(this.contextPath);
    addServletPath(newpath, xwikidb, context);
    addAction(newpath, action, context);
    addSpace(newpath, web, action, context);
    addName(newpath, name, action, context);
    addFileName(newpath, filename, context);

    String qstring = "rev=" + revision;
    if (recycleId >= 0) {
        qstring += "&rid=" + recycleId;
    }/* w ww.j  a v  a2s  .  co m*/
    if (!StringUtils.isEmpty(querystring)) {
        qstring += "&" + querystring;
    }
    newpath.append("?");
    newpath.append(StringUtils.chomp(StringUtils.chomp(qstring, "&"), "&amp;"));

    try {
        return new URL(getServerURL(xwikidb, context), newpath.toString());
    } catch (MalformedURLException e) {
        // This should not happen
        return null;
    }
}

From source file:com.xpn.xwiki.web.XWikiServletURLFactory.java

/**
 * Converts a URL to a relative URL if it's a XWiki URL (keeping only the path + query string + anchor) and leave
 * the URL unchanged if it's an external URL.
 * <p>/* ww  w. j  a v  a  2  s  .  c o  m*/
 * An URL is considered to be external if its server component doesn't match the server of the current request URL.
 * This means that URLs are made relative with respect to the current request URL rather than the current wiki set
 * on the XWiki context. Let's take an example:
 * 
 * <pre>
 * {@code
 * request URL: http://playground.xwiki.org/xwiki/bin/view/Sandbox/TestURL
 * current wiki: code (code.xwiki.org)
 * URL (1): http://code.xwiki.org/xwiki/bin/view/Main/WebHome
 * URL (2): http://playground.xwiki.org/xwiki/bin/view/Spage/Page
 * 
 * The result will be:
 * (1) http://code.xwiki.org/xwiki/bin/view/Main/WebHome
 * (2) /xwiki/bin/view/Spage/Page
 * }
 * </pre>
 * 
 * @param url the URL to convert
 * @return the converted URL as a string
 * @see com.xpn.xwiki.web.XWikiDefaultURLFactory#getURL(java.net.URL, com.xpn.xwiki.XWikiContext)
 */
@Override
public String getURL(URL url, XWikiContext context) {
    try {
        if (url == null) {
            return "";
        }

        String surl = url.toString();
        if (!surl.startsWith(serverURL.toString())) {
            // External URL: leave it as is.
            return surl;
        } else {
            // Internal XWiki URL: convert to relative.
            StringBuffer sbuf = new StringBuffer(url.getPath());
            String querystring = url.getQuery();
            if (!StringUtils.isEmpty(querystring)) {
                sbuf.append("?");
                sbuf.append(StringUtils.chomp(StringUtils.chomp(querystring, "&"), "&amp;"));
                // sbuf.append(querystring.replaceAll("&","&amp;"));
            }

            String anchor = url.getRef();
            if (!StringUtils.isEmpty(anchor)) {
                sbuf.append("#");
                sbuf.append(anchor);
            }
            return Util.escapeURL(sbuf.toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:com.opensymphony.xwork2.util.finder.ResourceFinder.java

private Set<String> convertPathsToPackages(Set<String> resources) {
    Set<String> packageNames = new HashSet<String>(resources.size());
    for (String resource : resources) {
        packageNames.add(StringUtils.chomp(StringUtils.replace(resource, "/", "."), "."));
    }//from  www.  j a  v a 2  s .  co m

    return packageNames;
}

From source file:com.opensymphony.xwork2.util.finder.ResourceFinder.java

/**
 * Reads subdirectories of a file. The output is a list of subdirectories, relative to the basepath
 *///from w ww.ja v  a  2 s  .  c o  m
private static void readSubDirectories(File dir, String basePath, Set<String> resources)
        throws MalformedURLException {
    if (dir.isDirectory()) {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                String name = file.getName();
                String subName = StringUtils.chomp(basePath, "/") + "/" + name;
                resources.add(subName);
                readSubDirectories(file, subName, resources);
            }
        }
    }
}

From source file:org.apache.struts2.osgi.FelixOsgiHost.java

/**
 * Return a list of directories under a directory whose name is a number
 *//*from   w ww .  j a  v  a 2  s  .c o  m*/
protected Map<String, String> getRunLevelDirs(String dir) {
    Map<String, String> dirs = new HashMap<String, String>();
    try {
        ResourceFinder finder = new ResourceFinder();
        URL url = finder.find("bundles");
        if (url != null) {
            if ("file".equals(url.getProtocol())) {
                File bundlesDir = new File(url.toURI());
                String[] runLevelDirs = bundlesDir.list(new FilenameFilter() {
                    public boolean accept(File file, String name) {
                        try {
                            return file.isDirectory() && Integer.valueOf(name) > 0;
                        } catch (NumberFormatException ex) {
                            //the name is not a number
                            return false;
                        }
                    }
                });

                if (runLevelDirs != null && runLevelDirs.length > 0) {
                    //add all the dirs to the list
                    for (String runLevel : runLevelDirs)
                        dirs.put(runLevel, StringUtils.chomp(dir, "/") + "/" + runLevel);

                } else if (LOG.isDebugEnabled()) {
                    LOG.debug("No run level directories found under the [#0] directory", dir);
                }
            } else if (LOG.isWarnEnabled())
                LOG.warn("Unable to read [#0] directory", dir);
        } else if (LOG.isWarnEnabled())
            LOG.warn("The [#0] directory was not found", dir);
    } catch (Exception e) {
        if (LOG.isWarnEnabled())
            LOG.warn("Unable load bundles from the [#0] directory", e, dir);
    }
    return dirs;
}