List of usage examples for org.apache.commons.lang3 StringUtils endsWith
public static boolean endsWith(final CharSequence str, final CharSequence suffix)
Check if a CharSequence ends with a specified suffix.
null s are handled without exceptions.
From source file:com.joyent.manta.http.MantaHttpHeaders.java
/** * Translates the range request header into two values. The first value * is the starting bytes of the binary file to read and the second value * is the ending bytes of the file to read. If the range indicates the * end of a file (unlimited), then the end value will be set to null. * Likewise, if the start position is unknown, it will be set to null. * * This method may eventually be deprecated in favor of {@link HttpRange#parseRequestRange(String)} but * that refactoring is being deferred.// www . j a v a 2s . co m * * @return two value array containing the start and the end of a byte range as Long * * @see HttpRange#parseRequestRange(String) */ public Long[] getByteRange() { final String rangeString = getRange(); Validate.notNull(rangeString, "Range value must not be null"); String[] rangeValuesStrings = StringUtils.split(rangeString, "bytes="); Validate.isTrue(rangeValuesStrings.length == 1, "Range header value doesn't begin with string: bytes="); final String byteRange = rangeValuesStrings[0]; Validate.isTrue(StringUtils.split(byteRange, ",").length == 1, "Multi-range requests are not supported"); String[] rangeParts = StringUtils.split(byteRange, "-"); Validate.isTrue(StringUtils.countMatches(byteRange, "-") < 2, "Cannot end or start with a negative number"); Long startPos = null; Long endPos = null; if (StringUtils.startsWith(byteRange, "-")) { endPos = Long.parseLong(byteRange); } else if (StringUtils.endsWith(byteRange, "-")) { startPos = Long.parseLong(byteRange.split("-")[0]); } else if (rangeParts.length == 2) { startPos = Long.parseUnsignedLong(rangeParts[0]); endPos = Long.parseUnsignedLong(rangeParts[1]); } else { throw new IllegalArgumentException("range must exist with - separator"); } return new Long[] { startPos, endPos }; }
From source file:com.ottogroup.bi.streaming.operator.json.JsonProcessingUtils.java
/** * Attempts to read the value from the path element which expectedly points to an array (nested arrays are supported) * @param json/*from w ww . ja v a 2 s . c om*/ * {@link JSONObject} instance which is expected to hold a {@link JSONArray} at the provided path element * @param pathElement * Path element pointing to a {@link JSONArray} structure inside the provided {@link JSONObject} instance. Example: <code>value[1] or value[1][2]</code> * @return * Value inside the referenced {@link JSONArray} element * @throws JSONException * @throws NoSuchElementException */ protected Object getArrayElement(final JSONObject json, final String pathElement) throws JSONException, NoSuchElementException { ///////////////////////////////////////////////////////////////////////// // validate provided input and throw exceptions if required if (json == null) throw new IllegalArgumentException("Null is not permitted as input to json object parameter"); if (pathElement == null) throw new IllegalArgumentException("Null is not permitted as input to path parameter"); if (StringUtils.isBlank(pathElement)) return json; ///////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////// // extract and access referenced field to ensure that it holds an array String field = pathElement.substring(0, pathElement.indexOf('[')); if (!json.has(field)) throw new NoSuchElementException( "Path element '" + field + "' does not exist for provided JSON object"); Object fieldContent = json.get(field); if (!(fieldContent instanceof JSONArray)) throw new NoSuchElementException("Referenced element '" + field + "' is not an array"); ///////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////// // extract referenced positions - nested arrays supported String[] tempSplits = pathElement.substring(pathElement.indexOf('[') + 1).split("\\]\\["); int[] positions = new int[tempSplits.length]; // iterate through splits and extract positions -- remove trailing brackets which may // remain there as result of splitting for (int i = 0; i < tempSplits.length; i++) { String split = tempSplits[i]; if (StringUtils.endsWith(split, "]")) split = split.substring(0, split.length() - 1); positions[i] = Integer.valueOf(split); } ///////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////// // step through array positions and attempt to extract value Object posValue = fieldContent; for (int i = 0; i < positions.length; i++) { posValue = getArrayElement((JSONArray) posValue, positions[i]); if (i < positions.length - 1) { if (!(posValue instanceof JSONArray)) throw new NoSuchElementException( "Referenced array element does not hold a nested array as expected"); } } ///////////////////////////////////////////////////////////////////////// return posValue; }
From source file:com.nridge.core.base.field.data.DataTable.java
/** * Returns one or more field rows that match the search criteria of the * parameters provided. Each row of the table will be examined to determine * if a cell identified by name evaluates true when the operator and value * are applied to it.//from w ww .java 2 s. c o m * <p> * <b>Note:</b> This method supports text based logical operators only. * You should use other <code>findValue()</code> methods for different * data types. * </p> * * @param aName Column name. * @param anOperator Logical operator. * @param aValue Comparison value. * * @return Array list of matching field rows or <i>null</i> if none evaluate true. */ public ArrayList<FieldRow> findValue(String aName, Field.Operator anOperator, String aValue) { String valueString; FieldRow fieldRow; Matcher regexMatcher = null; Pattern regexPattern = null; ArrayList<FieldRow> matchingRows = new ArrayList<FieldRow>(); int rowCount = rowCount(); int colOffset = offsetByName(aName); if ((aValue != null) && (colOffset != -1) && (rowCount > 0)) { for (int row = 0; row < rowCount; row++) { fieldRow = getRow(row); valueString = fieldRow.getValue(colOffset); switch (anOperator) { case NOT_EMPTY: if (StringUtils.isNotEmpty(valueString)) matchingRows.add(fieldRow); break; case EQUAL: if (StringUtils.equals(valueString, aValue)) matchingRows.add(fieldRow); break; case NOT_EQUAL: if (!StringUtils.equals(valueString, aValue)) matchingRows.add(fieldRow); break; case CONTAINS: if (StringUtils.contains(valueString, aValue)) matchingRows.add(fieldRow); break; case STARTS_WITH: if (StringUtils.startsWith(valueString, aValue)) matchingRows.add(fieldRow); break; case ENDS_WITH: if (StringUtils.endsWith(valueString, aValue)) matchingRows.add(fieldRow); break; case EMPTY: if (StringUtils.isEmpty(valueString)) matchingRows.add(fieldRow); break; case REGEX: // http://www.regular-expressions.info/java.html if (regexPattern == null) regexPattern = Pattern.compile(aValue); if (regexMatcher == null) regexMatcher = regexPattern.matcher(valueString); else regexMatcher.reset(valueString); if (regexMatcher.find()) matchingRows.add(fieldRow); break; } } } return matchingRows; }
From source file:org.apache.commons.lang3.StringUtils.java
/** * <p>Check if a CharSequence ends with any of an array of specified strings.</p> * * <pre>/* w w w .ja v a 2 s. c o m*/ * StringUtils.endsWithAny(null, null) = false * StringUtils.endsWithAny(null, new String[] {"abc"}) = false * StringUtils.endsWithAny("abcxyz", null) = false * StringUtils.endsWithAny("abcxyz", new String[] {""}) = true * StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}) = true * StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true * </pre> * * @param string the CharSequence to check, may be null * @param searchStrings the CharSequences to find, may be null or empty * @return {@code true} if the CharSequence ends with any of the the prefixes, case insensitive, or * both {@code null} * @since 3.0 */ public static boolean endsWithAny(CharSequence string, CharSequence... searchStrings) { if (isEmpty(string) || ArrayUtils.isEmpty(searchStrings)) { return false; } for (CharSequence searchString : searchStrings) { if (StringUtils.endsWith(string, searchString)) { return true; } } return false; }
From source file:nl.sidn.dnslib.util.NameUtil.java
public static Domaininfo getDomainWithSuffixList(String name, List<DomainParent> parents) { if (name == null || name.length() == 0) { return new Domaininfo(null, 0); }//from www. ja v a 2s .c o m if (StringUtils.equals(name, ".")) { return new Domaininfo(name, 0); } String[] parts = StringUtils.split(name, "."); if (parts != null && parts.length > 0) { if (parts.length == 1) { //only 1 label present return new Domaininfo(parts[0], 1); } //try to find a matching tld suffix for (DomainParent parent : parents) { if (StringUtils.endsWith(name, parent.getMatch())) { //get the label before the tld which is the registred name. return new Domaininfo(parts[parts.length - (parent.getLabels() + 1)] + parent.getParent(), parts.length); } } //unknown tld, assume 2nd level tld return new Domaininfo(parts[parts.length - 2] + "." + parts[parts.length - 1], parts.length); } return new Domaininfo(name, 0); }
From source file:nl.sidn.pcap.util.Settings.java
public static void createTldSuffixes(String value) { if (StringUtils.isEmpty(value)) { //no value found, do nothing return;//from w w w .ja va2s .c o m } String[] tlds = StringUtils.split(value, ","); //create list of DomainParents for (int i = 0; i < tlds.length; i++) { String parent = tlds[i]; if (parent == null) { //skip nulls continue; } //start and end with a dot. if (!StringUtils.startsWith(parent, ".")) { parent = "." + parent; } int labelCount = StringUtils.split(parent, '.').length; if (StringUtils.endsWith(parent, ".")) { //remove last dot (will become the used tld suffix tldSuffixes.add(new DomainParent(parent, StringUtils.removeEnd(parent, "."), labelCount)); } else { tldSuffixes.add(new DomainParent(parent + ".", parent, labelCount)); } } }
From source file:nl.vpro.jcr.criteria.query.xpath.utils.XPathTextUtils.java
/** * @param path to encode eg //my//path/2009//* * @return String encoded path eg //my//path/_x0032_009//* *///from ww w .ja v a 2s . co m public static String encodeDigitsInPath(String path) { LOG.debug("path to encode is {}", path); if (StringUtils.isBlank(path)) { String msg = "path cannot be a null or empty string"; LOG.error(msg); throw new IllegalArgumentException(msg); } StringBuilder encodedPath = new StringBuilder(path.length()); int inXpathCondition = 0; boolean xpathWithFunction = false; // TODO maybe a more robust check is needed for (int i = 0; i < path.length(); ++i) { char ch = path.charAt(i); if (i > 0 && path.charAt(i - 1) == '/' && Character.isDigit(ch)) { encodedPath.append("_x").append(StringUtils.leftPad(Integer.toHexString(ch), 4, '0')).append("_"); } else if (i > 0 && path.charAt(i - 1) == '/' && ch == '-') { encodedPath.append("_x002d_"); } else if (inXpathCondition <= 0 && ch == ' ') { encodedPath.append("_x0020_"); } else if (inXpathCondition <= 0 && ch == ',') { encodedPath.append("_x002c_"); } else if (inXpathCondition <= 0 && ch == '\u00b0') { // CRIT-53 encodedPath.append("_x00b0_"); } else if (inXpathCondition <= 0 && ch == '$') { // CRIT-54 encodedPath.append("_x0024_"); } else { if (ch == '[') { inXpathCondition++; } else if (ch == '(') { // "(" is the beginning of an expression only when used with the element() function if (StringUtils.endsWith(StringUtils.substring(path, 0, i), "element")) { inXpathCondition++; xpathWithFunction = true; } else if (inXpathCondition == 0) { encodedPath.append("_x0028_"); continue; } } else if (inXpathCondition > 0 && ch == ']') { inXpathCondition--; } else if (ch == ')') { if (inXpathCondition > 0 && xpathWithFunction) { inXpathCondition--; xpathWithFunction = false; } else if (inXpathCondition == 0) { encodedPath.append("_x0029_"); continue; } } encodedPath.append(ch); } } LOG.debug("returning encoded path {}", encodedPath); return encodedPath.toString(); }
From source file:objective.taskboard.it.TemplateIT.java
private static File okTemplateWithoutSharedStrings() throws IOException, URISyntaxException { Path temp = Files.createTempFile("Followup", ".xlsm"); ZipUtils.zip(ZipUtils.stream(okTemplate()) .filter(ze -> !StringUtils.endsWith(ze.getName(), "sharedStrings.xml")), temp); return temp.toFile(); }
From source file:objective.taskboard.it.TemplateIT.java
private static File corruptedFile() throws IOException, URISyntaxException { Path temp = Files.createTempFile("Followup", ".xlsm"); ZipUtils.zip(ZipUtils.stream(okTemplate()).map(ze -> { if (StringUtils.endsWith(ze.getName(), "workbook.xml.rels")) ze.setInputStream(TemplateIT.class.getResourceAsStream("/objective/taskboard/utils/file.xml")); return ze; }), temp);/*from w w w . j a v a 2 s. c om*/ return temp.toFile(); }
From source file:org.alfresco.dataprep.CMISUtil.java
/** * Get the node ref for a item by path/* w ww. ja v a 2s .c om*/ * @param session the session * @param pathToContent String path to item (e.g. /Sites/siteId/documentLibrary/doc.txt) * @return String node ref of the item */ public String getNodeRefByPath(Session session, String pathToContent) { if (StringUtils.isEmpty(pathToContent)) { throw new CmisRuntimeException("Path to content is missing"); } try { if (!StringUtils.startsWith(pathToContent, "/")) { pathToContent = "/" + pathToContent; } if (StringUtils.endsWith(pathToContent, "/")) { pathToContent = StringUtils.removeEnd(pathToContent, "/"); } CmisObject content = session.getObjectByPath(pathToContent); return content.getId().split(";")[0]; } catch (CmisObjectNotFoundException nf) { return ""; } }