List of usage examples for org.apache.commons.lang3 StringUtils substringBeforeLast
public static String substringBeforeLast(final String str, final String separator)
Gets the substring before the last occurrence of a separator.
From source file:gov.nih.nci.firebird.FirebirdModule.java
private void addEmailMapping(Map<String, String> emailMapping, String emailMappingString) { if (emailMappingString.contains(":")) { String sponsorExternalId = StringUtils.substringBeforeLast(emailMappingString, ":"); String sponsorEmail = StringUtils.substringAfterLast(emailMappingString, ":"); emailMapping.put(sponsorExternalId, sponsorEmail); }// w ww . ja va 2s.c om }
From source file:de.blizzy.documentr.access.UserStore.java
private List<String> listUsers(ILockedRepository repo) { File workingDir = RepositoryUtil.getWorkingDir(repo.r()); FileFilter filter = new FileFilter() { @Override/* w ww .j a va 2s. c om*/ public boolean accept(File file) { return file.isFile() && file.getName().endsWith(USER_SUFFIX); } }; List<File> files = Lists.newArrayList(workingDir.listFiles(filter)); Function<File, String> function = new Function<File, String>() { @Override public String apply(File file) { return StringUtils.substringBeforeLast(file.getName(), USER_SUFFIX); } }; List<String> users = Lists.newArrayList(Lists.transform(files, function)); Collections.sort(users); return users; }
From source file:com.soolr.core.web.Servlets.java
public static String getBasePath(HttpServletRequest request) { String requestURL = request.getRequestURL().toString(); String requestURI = request.getRequestURI(); String host = StringUtils.substringBeforeLast(requestURL, requestURI); String contextPath = request.getContextPath(); return StringUtils.removeEnd(host + contextPath, "/"); }
From source file:info.magnolia.ui.contentapp.setup.AbstractDataTypeMigrationTask.java
private String getParentPath(String path) { int lastIndexOfSlash = path.lastIndexOf("/"); if (lastIndexOfSlash > 0) { return StringUtils.substringBeforeLast(path, "/"); }/*from ww w . j av a 2 s .c om*/ return "/"; }
From source file:com.xpn.xwiki.plugin.image.ImagePlugin.java
/** * Transforms the given image (i.e. shrinks the image and changes its quality) before it is downloaded. * /*from ww w. j av a2 s .c o m*/ * @param image the image to be downloaded * @param width the desired image width; this value is taken into account only if it is greater than zero and less * than the current image width * @param height the desired image height; this value is taken into account only if it is greater than zero and less * than the current image height * @param quality the desired compression quality * @param context the XWiki context * @return the transformed image * @throws Exception if transforming the image fails */ private XWikiAttachment downloadImage(XWikiAttachment image, int width, int height, float quality, XWikiContext context) throws Exception { if (this.imageCache == null) { initCache(context); } boolean keepAspectRatio = Boolean.valueOf(context.getRequest().getParameter("keepAspectRatio")); XWikiAttachment thumbnail = this.imageCache == null ? shrinkImage(image, width, height, keepAspectRatio, quality, context) : downloadImageFromCache(image, width, height, keepAspectRatio, quality, context); // If the image has been transformed, update the file name extension to match the image format. String fileName = thumbnail.getFilename(); String extension = StringUtils.lowerCase(StringUtils.substringAfterLast(fileName, String.valueOf('.'))); if (thumbnail != image && !Arrays.asList("jpeg", "jpg", "png").contains(extension)) { // The scaled image is PNG, so correct the extension in order to output the correct MIME type. thumbnail.setFilename(StringUtils.substringBeforeLast(fileName, ".") + ".png"); } return thumbnail; }
From source file:com.google.code.rees.scope.struts2.StrutsActionProvider.java
/** * Note that we can't include the test for {@link #actionSuffix} here * because a class is included if its name ends in {@link #actionSuffix} OR * it implements {@link com.opensymphony.xwork2.Action}. Since the whole * goal is to avoid loading the class if we don't have to, the (actionSuffix * || implements Action) test will have to remain until later. See * {@link #getActionClassTest()} for the test performed on the loaded * {@link ClassInfo} structure./*w w w.j a v a2 s . com*/ * * @param className * the name of the class to test * @return true if the specified class should be included in the * package-based action scan */ protected boolean includeClassNameInActionScan(String className) { String classPackageName = StringUtils.substringBeforeLast(className, "."); if (actionPackages != null) { for (String packageName : actionPackages) { String strictPackageName = packageName + "."; if (classPackageName.equals(packageName) || classPackageName.startsWith(strictPackageName)) return true; } } if (packageLocators != null && !disablePackageLocatorsScanning) { for (String packageLocator : packageLocators) { if (classPackageName.length() > 0 && (packageLocatorsBasePackage == null || classPackageName.startsWith(packageLocatorsBasePackage))) { String[] splitted = classPackageName.split("\\."); if (contains(splitted, packageLocator, false)) return true; } } } return false; }
From source file:com.netflix.spinnaker.clouddriver.ecs.provider.view.EcsServerClusterProvider.java
private EcsServerCluster buildSpinnakerServerCluster(AmazonCredentials credentials, Set<LoadBalancer> loadBalancers, EcsServerGroup ecsServerGroup) { return new EcsServerCluster().setAccountName(credentials.getName()) .setName(StringUtils.substringBeforeLast(ecsServerGroup.getName(), "-")) .setLoadBalancers(loadBalancers).setServerGroups(Sets.newHashSet(ecsServerGroup)); }
From source file:com.gargoylesoftware.htmlunit.html.XmlSerializer.java
/** * Computes the best file to save the response to the given URL. * @param url the requested URL//from w w w . j a v a2 s . c om * @param extension the preferred extension * @return the file to create * @throws IOException if a problem occurs creating the file */ private File createFile(final String url, final String extension) throws IOException { String name = url.replaceFirst("/$", ""); name = CREATE_FILE_PATTERN.matcher(name).replaceAll(""); name = StringUtils.substringBefore(name, "?"); // remove query name = StringUtils.substringBefore(name, ";"); // remove additional info name = StringUtils.substring(name, 0, 30); // many file systems have a limit at 255, let's limit it name = com.gargoylesoftware.htmlunit.util.StringUtils.sanitizeForFileName(name); if (!name.endsWith(extension)) { name += extension; } int counter = 0; while (true) { final String fileName; if (counter != 0) { fileName = StringUtils.substringBeforeLast(name, ".") + "_" + counter + "." + StringUtils.substringAfterLast(name, "."); } else { fileName = name; } outputDir_.mkdirs(); final File f = new File(outputDir_, fileName); if (f.createNewFile()) { return f; } counter++; } }
From source file:kenh.expl.impl.ExpLParser.java
/** * Invoke functioin, support name space. For example, expl:contains(...). * @param function//w ww . j a v a 2 s. com * @return Return string or non-string object. Return empty string instead of null. */ private Object executeFunction(String function) throws UnsupportedExpressionException { // function should return a string object; if (StringUtils.isBlank(function)) { UnsupportedExpressionException e = new UnsupportedExpressionException("Function is null."); throw e; } logger.trace("Func: " + function); String parameter = StringUtils.substringBeforeLast(StringUtils.substringAfter(function, "("), ")"); String[] parts = splitParameter(parameter); String funcName = StringUtils.substringBetween(function, "#", "("); String nameSpace = null; if (StringUtils.contains(funcName, ":")) { nameSpace = StringUtils.substringBefore(funcName, ":"); funcName = StringUtils.substringAfter(funcName, ":"); } if (StringUtils.isBlank(funcName)) { UnsupportedExpressionException e = new UnsupportedExpressionException("Failure to get function name"); e.push(function); throw e; } Object[] objs = new Object[parts.length]; int i = 0; for (String part : parts) { if (part.indexOf('{') != -1) { Object obj = this.parseExpress(part); objs[i++] = obj; } else { objs[i++] = part; } } try { // instantiate it, and create the parameters Function func = this.getEnvironment().getFunction(nameSpace, funcName); if (func == null) throw new UnsupportedExpressionException("Can't find the function. [" + (StringUtils.isBlank(nameSpace) ? funcName : nameSpace + ":" + funcName) + "]"); func.setEnvironment(this.getEnvironment()); // invoke return func.invoke(objs); } catch (UnsupportedExpressionException e) { e.push(function); throw e; } catch (Exception ex) { UnsupportedExpressionException e = new UnsupportedExpressionException(ex); e.push(function); throw e; } }
From source file:de.blizzy.backup.database.Database.java
public void initialize() { try {/* www.ja v a 2s. c om*/ int sha256Length = DigestUtils.sha256Hex(StringUtils.EMPTY).length(); factory.query("CREATE TABLE IF NOT EXISTS backups (" + //$NON-NLS-1$ "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " + //$NON-NLS-1$ "run_time DATETIME NOT NULL, " + //$NON-NLS-1$ "num_entries INT NULL" + //$NON-NLS-1$ ")") //$NON-NLS-1$ .execute(); int sampleBackupPathLength = Utils.createSampleBackupFilePath().length(); factory.query("CREATE TABLE IF NOT EXISTS files (" + //$NON-NLS-1$ "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " + //$NON-NLS-1$ "backup_path VARCHAR(" + sampleBackupPathLength + ") NOT NULL, " + //$NON-NLS-1$ //$NON-NLS-2$ "checksum VARCHAR(" + sha256Length + ") NOT NULL, " + //$NON-NLS-1$ //$NON-NLS-2$ "length BIGINT NOT NULL, " + //$NON-NLS-1$ "compression TINYINT NOT NULL" + //$NON-NLS-1$ ")") //$NON-NLS-1$ .execute(); factory.query("CREATE INDEX IF NOT EXISTS idx_old_files ON files " + //$NON-NLS-1$ "(checksum, length)") //$NON-NLS-1$ .execute(); factory.query("CREATE TABLE IF NOT EXISTS entries (" + //$NON-NLS-1$ "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " + //$NON-NLS-1$ "parent_id INT NULL, " + //$NON-NLS-1$ "backup_id INT NOT NULL, " + //$NON-NLS-1$ "type TINYINT NOT NULL, " + //$NON-NLS-1$ "creation_time DATETIME NULL, " + //$NON-NLS-1$ "modification_time DATETIME NULL, " + //$NON-NLS-1$ "hidden BOOLEAN NOT NULL, " + //$NON-NLS-1$ "name VARCHAR(1024) NOT NULL, " + //$NON-NLS-1$ "name_lower VARCHAR(1024) NOT NULL, " + //$NON-NLS-1$ "file_id INT NULL" + //$NON-NLS-1$ ")") //$NON-NLS-1$ .execute(); factory.query("CREATE INDEX IF NOT EXISTS idx_entries_files ON entries " + //$NON-NLS-1$ "(file_id)") //$NON-NLS-1$ .execute(); factory.query("CREATE INDEX IF NOT EXISTS idx_folder_entries ON entries " + //$NON-NLS-1$ "(backup_id, parent_id)") //$NON-NLS-1$ .execute(); factory.query("DROP INDEX IF EXISTS idx_entries_names") //$NON-NLS-1$ .execute(); factory.query("CREATE INDEX IF NOT EXISTS idx_entries_names2 ON entries " + //$NON-NLS-1$ "(name, backup_id, parent_id)") //$NON-NLS-1$ .execute(); if (!isTableColumnExistent("FILES", "COMPRESSION")) { //$NON-NLS-1$ //$NON-NLS-2$ factory.query( "ALTER TABLE files ADD compression TINYINT NULL DEFAULT " + Compression.GZIP.getValue()) //$NON-NLS-1$ .execute(); factory.update(Tables.FILES) .set(Tables.FILES.COMPRESSION, Byte.valueOf((byte) Compression.GZIP.getValue())).execute(); factory.query("ALTER TABLE files ALTER COLUMN compression TINYINT NOT NULL") //$NON-NLS-1$ .execute(); } if (getTableColumnSize("FILES", "CHECKSUM") != sha256Length) { //$NON-NLS-1$ //$NON-NLS-2$ factory.query("ALTER TABLE files ALTER COLUMN " + //$NON-NLS-1$ "checksum VARCHAR(" + sha256Length + ") NOT NULL") //$NON-NLS-1$ //$NON-NLS-2$ .execute(); } if (!isTableColumnExistent("ENTRIES", "NAME_LOWER")) { //$NON-NLS-1$ //$NON-NLS-2$ factory.query("ALTER TABLE entries ADD name_lower VARCHAR(1024) NULL") //$NON-NLS-1$ .execute(); factory.update(Tables.ENTRIES).set(Tables.ENTRIES.NAME_LOWER, Tables.ENTRIES.NAME.lower()) .execute(); factory.query("ALTER TABLE entries ALTER COLUMN name_lower VARCHAR(1024) NOT NULL") //$NON-NLS-1$ .execute(); } factory.query("CREATE INDEX IF NOT EXISTS idx_entries_search ON entries " + //$NON-NLS-1$ "(backup_id, name_lower)") //$NON-NLS-1$ .execute(); if (getTableColumnSize("FILES", "BACKUP_PATH") != sampleBackupPathLength) { //$NON-NLS-1$ //$NON-NLS-2$ Cursor<Record> cursor = null; try { cursor = factory.select(Tables.FILES.ID, Tables.FILES.BACKUP_PATH).from(Tables.FILES) .fetchLazy(); while (cursor.hasNext()) { Record record = cursor.fetchOne(); String backupPath = record.getValue(Tables.FILES.BACKUP_PATH); String backupFileName = StringUtils.substringAfterLast(backupPath, "/"); //$NON-NLS-1$ if (backupFileName.indexOf('-') > 0) { Integer id = record.getValue(Tables.FILES.ID); File backupFile = Utils.toBackupFile(backupPath, outputFolder); File folder = backupFile.getParentFile(); int maxIdx = Utils.getMaxBackupFileIndex(folder); int newIdx = maxIdx + 1; String newBackupFileName = Utils.toBackupFileName(newIdx); File newBackupFile = new File(folder, newBackupFileName); FileUtils.moveFile(backupFile, newBackupFile); String newBackupPath = StringUtils.substringBeforeLast(backupPath, "/") + "/" //$NON-NLS-1$//$NON-NLS-2$ + newBackupFileName; factory.update(Tables.FILES).set(Tables.FILES.BACKUP_PATH, newBackupPath) .where(Tables.FILES.ID.equal(id)).execute(); } } factory.query("ALTER TABLE files ALTER COLUMN backup_path VARCHAR(" + sampleBackupPathLength //$NON-NLS-1$ + ") NOT NULL") //$NON-NLS-1$ .execute(); } finally { closeQuietly(cursor); } } factory.query("ANALYZE") //$NON-NLS-1$ .execute(); } catch (SQLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }