List of usage examples for org.apache.commons.lang3 StringUtils left
public static String left(final String str, final int len)
Gets the leftmost len characters of a String.
If len characters are not available, or the String is null , the String will be returned without an exception.
From source file:info.magnolia.integrationtests.uitest.AbstractMagnoliaUITest.java
protected void takeScreenshot(String suffix) { if (driver instanceof TakesScreenshot) { final TakesScreenshot takesScreenshotDriver = (TakesScreenshot) driver; final File file = takesScreenshotDriver.getScreenshotAs(OutputType.FILE); // Replace non url-safe characters with underscores. // Safe: $-_.+!*'() See: http://perishablepress.com/stop-using-unsafe-characters-in-urls/ suffix = suffix.replace(" = ", "-eq-"); suffix = suffix.replaceAll("[\\W\\[\\]\\.\\$\\+!\\*'()]", "_"); suffix = reduceUnderscores(suffix); String dirName = SCREENSHOT_DIR + "/" + testName(); String fileName = String.format("%04d-%s", screenshotIndex++, suffix); // Java somehow thinks it needs to limit file name lengths !? fileName = StringUtils.left(fileName, 250); final File destinationFile = new File(dirName, fileName + ".png"); if (destinationFile.exists()) { // can be existing e.g. from previous test run destinationFile.delete();//from w w w . j av a2s . co m } try { FileUtils.moveFile(file, destinationFile); } catch (IOException e) { log.error(e.getMessage()); // error message might be overlooked so we explicitly fail here. Should assures ppl will immediately realize and fix asap. fail("IOException while moving screenshot " + file + " to " + fileName + " : " + e); } } }
From source file:edu.cmu.cs.lti.discoursedb.github.converter.GithubConverterService.java
/** * Maps a mailing list to DiscourseDB entities * // w w w . j a va 2 s .com * @param owner Github owner * @param project Github project (that this mailing list is sort of associated with) * @param forumName Google groups forum name * @param internal Does this mailing list really belong to the project? */ public void mapForumPost(MailingListComment posting, String dataSourceName) { // TODO: 2nd argument to findOneByDataSource should be a constant in an enum class // don't let it get added twice // but scanning through all these is inefficient, so, maybe, actually, don't prevent this /*if (keyIndex.containsKey(posting.getFullyQualifiedUniqueMessage())) { logger.error("Not re-adding post " + posting.getFullyQualifiedUniqueMessage()); return; } if (contributionService.findOneByDataSource(posting.getFullyQualifiedUniqueMessage(), "ggroups#unique_message", dataSourceName).isPresent()) { logger.error("Not re-adding post " + posting.getFullyQualifiedUniqueMessage()); return; }*/ Discourse curDiscourse = getDiscourse("Github"); DiscoursePart forumDP = getDiscoursePart(curDiscourse, posting.getFullForumName(), DiscoursePartTypes.FORUM); DiscoursePart threadDP = getDiscoursePartByDataSource(curDiscourse, posting.getForumThreadIdentifier(), "ggroups:forum/threadid", DataSourceTypes.GITHUB, dataSourceName, DiscoursePartTypes.THREAD); if (posting.getResponseTo() == "") { discoursePartService.createDiscoursePartRelation(forumDP, threadDP, DiscoursePartRelationTypes.SUBPART); } if (threadDP.getName() == null) { threadDP.setName("Thread: " + posting.getTitle()); threadDP.setStartTime(posting.getDate()); threadDP.setType("THREAD"); } threadDP.setEndTime(posting.getDate()); User actor = getUser(curDiscourse, posting.getAuthorNameAndEmail()); actor.setEmail(posting.getAuthorEmail()); actor.setRealname(posting.getAuthorName()); Content k = contentService.createContent(); k.setAuthor(actor); k.setStartTime(posting.getDate()); if (posting.getTitle() != null && posting.getTitle().length() > 255) { logger.info("Title too long " + posting.getFullyQualifiedUniqueMessage() + ": " + posting.getTitle()); k.setTitle(sanitizeUtf8mb4(posting.getTitle()).substring(0, 254)); } else { k.setTitle(sanitizeUtf8mb4(posting.getTitle())); } k.setText(sanitizeUtf8mb4(posting.getBody())); Contribution co = null; if (posting.getResponseTo() == "") { co = contributionService.createTypedContribution(ContributionTypes.THREAD_STARTER); } else { co = contributionService.createTypedContribution(ContributionTypes.POST); } co.setCurrentRevision(k); co.setFirstRevision(k); co.setStartTime(posting.getDate()); keyIndex.put(posting.getFullyQualifiedUniqueMessage(), co.getId()); dataSourceService.addSource(co, new DataSourceInstance(StringUtils.left(posting.getFullyQualifiedUniqueMessage(), 94), "ggroups#unique_message", DataSourceTypes.GITHUB, dataSourceName)); //Add contribution to DiscoursePart discoursePartService.addContributionToDiscoursePart(co, threadDP); }
From source file:com.quinsoft.zeidon.dbhandler.JdbcHandler.java
private String leftStr(String str) { if (str.length() <= 100) return str; return StringUtils.left(str, 100) + "<truncated>"; }
From source file:TestItem.java
public String pad() { return (level < 1) ? "" : StringUtils.left("-----------------", level) + " "; }
From source file:controllers.UserApp.java
public static void updatePreferredLanguage() { Http.Request request = Http.Context.current().request(); User user = UserApp.currentUser();/*from www .j a v a 2 s . com*/ if (user.isAnonymous()) { return; } if (request.acceptLanguages().isEmpty() && request.cookie(Play.langCookieName()) == null) { return; } String code = StringUtils.left(Http.Context.current().lang().code(), 255); if (!code.equals(user.lang)) { user.lang = code; user.update(); } }
From source file:com.moscona.dataSpace.persistence.DirectoryDataStore.java
private String getBackingArrayPathForSegment(int num, String suffix) { String str = Integer.toString(num); ArrayList<String> parts = new ArrayList<String>(); while (str.length() > 2) { String head = StringUtils.left(str, 2); str = StringUtils.removeStart(str, head); parts.add(head);//w w w. j ava2s . c o m } return StringUtils.removeEnd(segmentsDirName() + StringUtils.join(parts, "/"), "/") + "/" + Integer.toString(num) + "." + suffix.trim(); }
From source file:edu.cmu.cs.lti.discoursedb.github.converter.GithubConverterService.java
public void mapVersionInfo(String repo, String nameInRepo, String version, String packageFile, Date updated) { DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try {// w ww.ja v a 2 s.c om Discourse discourse = getDiscourse("Github"); DiscoursePart dps = getDiscoursePart(discourse, repo, DiscoursePartTypes.GITHUB_REPO); AnnotationInstance a = annotationService.createTypedAnnotation("REVISION"); annotationService.addAnnotation(dps, a); annotationService.addFeature(a, annotationService.createTypedFeature(version, "version")); annotationService.addFeature(a, annotationService.createTypedFeature(fmt.format(updated), "update_date")); annotationService.addFeature(a, annotationService.createTypedFeature(packageFile, "update_file")); dataSourceService.addSource(a, new DataSourceInstance(StringUtils.left("pypi_versions#" + packageFile, 94), "versionfile", DataSourceTypes.GITHUB, "GITHUB")); } catch (Exception e) { logger.trace("Error classifying project info for " + repo + ", " + e.getMessage()); } }
From source file:edu.cmu.cs.lti.discoursedb.github.converter.GithubConverterService.java
/** * Maps a post to DiscourseDB entities.//w w w. jav a 2s .c o m * * @param p the post object to map to DiscourseDB * @param dataSetName the name of the dataset the post was extracted from */ public long mapIssueEntities(GitHubIssueComment p) { Assert.notNull(p, "Cannot map relations for post. Post data was null."); //if (p.getText() == null || p.getText() == "") { // return 0L; //} Discourse curDiscourse = getDiscourse("Github"); DiscoursePart issueDP = getDiscoursePart(curDiscourse, p.getIssueIdentifier(), DiscoursePartTypes.GITHUB_ISSUE); String actorname = p.getActor(); if (actorname == null) { actorname = "unknown"; } User actor = getUser(curDiscourse, actorname); addCrossrefs(p.getIssues(), p.getIssueIdentifier(), curDiscourse, actor); switch (p.getRectype()) { case "pull_request_commit": case "commit_messages": { String dataSourceString = StringUtils.left(p.getProjectFullName() + "#" + p.getAction(), 94); Optional<Contribution> oc = contributionService.findOneByDataSource(dataSourceString, COMMIT_SHA, "GITHUB"); Contribution co = null; if (oc.isPresent()) { co = oc.get(); } else { Content k = contentService.createContent(); k.setAuthor(actor); k.setStartTime(p.getTime()); if (p.getTitle() != null && p.getTitle().length() > 255) { logger.info("Title too long " + p.getTitle()); k.setTitle(sanitizeUtf8mb4(p.getTitle()).substring(0, 254)); } else { k.setTitle(sanitizeUtf8mb4(p.getTitle())); } k.setText(sanitizeUtf8mb4(p.getText())); co = contributionService.createTypedContribution(ContributionTypes.GIT_COMMIT_MESSAGE); co.setCurrentRevision(k); co.setFirstRevision(k); co.setStartTime(p.getTime()); dataSourceService.addSource(co, new DataSourceInstance(dataSourceString, COMMIT_SHA, DataSourceTypes.GITHUB, "GITHUB")); } extendDiscoursePartDates(issueDP, p.getTime()); discoursePartService.addContributionToDiscoursePart(co, issueDP); return co.getId(); } case "issue_title": { Content k = contentService.createContent(); k.setAuthor(actor); k.setStartTime(p.getTime()); if (p.getTitle() != null && p.getTitle().length() > 255) { logger.info("Title too long " + p.getTitle()); k.setTitle(sanitizeUtf8mb4(p.getTitle()).substring(0, 254)); } else { k.setTitle(sanitizeUtf8mb4(p.getTitle())); } k.setText(sanitizeUtf8mb4(p.getText())); Contribution co = contributionService.createTypedContribution(ContributionTypes.THREAD_STARTER); co.setCurrentRevision(k); co.setFirstRevision(k); co.setStartTime(p.getTime()); extendDiscoursePartDates(issueDP, p.getTime()); dataSourceService.addSource(co, new DataSourceInstance(p.getIssueIdentifier(), "github#issue", DataSourceTypes.GITHUB, "GITHUB")); //Add contribution to DiscoursePart discoursePartService.addContributionToDiscoursePart(co, issueDP); return co.getId(); } case "issue_closed": { DiscoursePartInteraction dpi = userService.createDiscoursePartInteraction(actor, issueDP, DiscoursePartInteractionTypes.GITHUB_ISSUE_CLOSE); extendDiscoursePartDates(issueDP, p.getTime()); dpi.setStartTime(p.getTime()); return 0L; } case "pull_request_merged": { DiscoursePartInteraction dpi = userService.createDiscoursePartInteraction(actor, issueDP, DiscoursePartInteractionTypes.GIT_PULL_REQUEST_MERGE); extendDiscoursePartDates(issueDP, p.getTime()); dpi.setStartTime(p.getTime()); return 0L; } case "issue_comment": { Content k = contentService.createContent(); k.setAuthor(actor); k.setStartTime(p.getTime()); k.setText(sanitizeUtf8mb4(p.getText())); k.setTitle(p.getTitle()); Contribution co = contributionService.createTypedContribution(ContributionTypes.POST); co.setCurrentRevision(k); co.setFirstRevision(k); co.setStartTime(p.getTime()); extendDiscoursePartDates(issueDP, p.getTime()); /*Optional<Contribution> parent = contributionService.findOneByDataSource(p.getIssueIdentifier(), "github#issue", "GITHUB"); if (!parent.isPresent()) { logger.error("cannot link to issue " + p.getIssueIdentifier()); } contributionService.createDiscourseRelation(parent.get(), co, DiscourseRelationTypes.DESCENDANT); */ //Add contribution to DiscoursePart discoursePartService.addContributionToDiscoursePart(co, issueDP); return co.getId(); } /*//pull_request_commit_comment, pull_request_history, commit_messages, readme, issue_event case "pull_request_commit_comment": { User actor = userService.createOrGetUser(curDiscourse, p.getActor()); Content k = contentService.createContent(); k.setAuthor(actor); k.setStartTime(p.getTime()); k.setText(p.getText()); Contribution co = contributionService.createTypedContribution(ContributionTypes.GITHUB_COMMIT_COMMENT); co.setCurrentRevision(k); co.setFirstRevision(k); co.setStartTime(p.getTime()); Optional<Contribution> parent = contributionService.findOneByDataSource(p.getIssueIdentifier(), "github#issue", "GITHUB"); if (!parent.isPresent()) { logger.error("cannot link to issue " + p.getIssueIdentifier()); } dataSourceService.addSource(co, new DataSourceInstance(p.getProjectFullName() + "#" + p.getProvenance(), COMMIT_SHA, DataSourceTypes.GITHUB, "GITHUB")); contributionService.createDiscourseRelation(parent.get(), co, DiscourseRelationTypes.DESCENDANT); //Add contribution to DiscoursePart discoursePartService.addContributionToDiscoursePart(co, issueDP); }*/ } logger.trace("Post mapping completed."); return 0L; }
From source file:net.sourceforge.seqware.pipeline.workflowV2.engine.oozie.object.StringTruncator.java
/** * Translates a long name to a shortname that is compatible with Oozie, yet still unique. * /*from w w w . j a v a2 s . c o m*/ * @param longName * @return */ public String translateName(String longName) { if (longName.length() < ROOT_CHAR_COUNT) { return longName; } String root = StringUtils.left(longName, ROOT_CHAR_COUNT); int count; if (rootUsed.containsKey(root)) { // already used, need to count up count = rootUsed.get(root) + 1; } else { count = 1; } rootUsed.put(root, count); if (count > DUPLICATION_LIMIT) { throw new RuntimeException( "string truncation only supports up to " + DUPLICATION_LIMIT + " duplicates"); } return root + "-" + count; }
From source file:org.apache.james.backends.es.ElasticSearchIndexer.java
public IndexResponse index(String id, String content) { checkArgument(content);/*from w w w . j a v a 2s . com*/ if (LOGGER.isDebugEnabled()) { LOGGER.debug("Indexing {}: {}", id, StringUtils.left(content, DEBUG_MAX_LENGTH_CONTENT)); } return client.prepareIndex(aliasName.getValue(), typeName.getValue(), id).setSource(content).get(); }