List of usage examples for org.apache.commons.lang StringUtils countMatches
public static int countMatches(String str, String sub)
Counts how many times the substring appears in the larger String.
From source file:de.awtools.basic.file.AWToolsFileUtils.java
/** * Berechnet die Anzahl der Verzeichnisstufen bis * zum gesuchten Dateinamen. Die Angabe kann fiktiv sein, d.h. * mussen nicht notwendiger Weise im Dateisystem vorliegen. * Z.B. Der Parameter "./www/dir/dir1/index.html" liefert 3 zurck. * * @param fileName Die gesuchte Datei./* w ww. j a v a 2 s. c o m*/ * @return Anzahl der Verzeichnistiefen. */ public static int countDirLevel(final String fileName) { // Normalisieren und ... String newFileName = AWToolsFileUtils.normalizePath(fileName); /* // ... nur noch die '/' zhlen und fertig (ohne Root!) int correction = 0; if (fileName.startsWith(WINFILE_SEPERATOR)) { correction = 1; } else if (WINDOWS_ROOT_PATTERN.matcher(newFileName).find()) { correction = 1; } */ //return (StringUtils.countMatches(newFileName, "/") - correction); return (StringUtils.countMatches(newFileName, "/") - 1); }
From source file:loaders.LoadQueryTransformerTest.java
@Test public void testParamReordering2() throws Exception { String query = "select \n" + "u.id as initiatorId,\n" + "u.name as initiator,\n" + "count(*) as tasks,\n" + "count(case when c.state = ',InWork,' or c.state = ',Assigned,' then 1 end) as tasksInWork,\n" + "count(case when c.state = ',InControl,' or c.state = ',Completed,' then 1 end) as tasksOnControl,\n" + "count(case when ((c.state = ',InControl,' or c.state = ',InWork,' or c.state = ',Assigned,' or c.state = ',Completed,') \n" + " and t.finish_date_plan <= ${date}) then 1 end) as overdue\n" + "\n" + "from (select distinct r.card_id from wf_card_role r where r.delete_ts is null \n" + "and (r.code='10-Initiator' or r.code='20-Executor' or r.code='30-Controller' or r.code='90-Observer')\n" + "and r.user_id = ${runs}) as r\n" + "join tm_task t on t.card_id = r.card_id \n" + "join wf_card c on t.card_id = c.id\n" + "join (select card_id, user_id from wf_card_role where delete_ts is null and code='20-Executor') as execut on execut.card_id = t.card_id\n" + "join df_employee e on execut.user_id = e.user_id\n" + "join sec_user u on t.initiator_id = u.id\n" + "\n" + "where c.delete_ts is null\n" + "and (c.state = ',InControl,' or c.state = ',InWork,' or c.state = ',Completed,' or c.state = ',Assigned,')\n" + "and t.create_date <= ${date}\n" + "and e.user_id = ${executorId}\n" + "group by u.id, u.name\n" + "order by u.name"; HashMap<String, Object> params = new HashMap<String, Object>(); AbstractDbDataLoader.QueryPack queryPack = prepareQuery(query, new BandData(""), params); System.out.println(queryPack.getQuery()); Assert.assertFalse(queryPack.getQuery().contains("${")); writeParams(queryPack);/*w w w. jav a 2 s . co m*/ params.put("date", "param1"); params.put("executorId", "param2"); params.put("runs", null); queryPack = prepareQuery(query, new BandData(""), params); System.out.println(queryPack.getQuery()); Assert.assertEquals(3, StringUtils.countMatches(queryPack.getQuery(), "?")); writeParams(queryPack); Assert.assertEquals("param1", queryPack.getParams()[0].getValue()); Assert.assertEquals("param1", queryPack.getParams()[1].getValue()); Assert.assertEquals("param2", queryPack.getParams()[2].getValue()); }
From source file:de.tudarmstadt.ukp.clarin.webanno.tsv.WebannoCustomTsvReader.java
/** * Iterate through lines and create span annotations accordingly. For multiple span annotation, * based on the position of the annotation in the line, update only the end position of the * annotation//www . j a va 2 s. c om */ private void setAnnotations(JCas aJcas, InputStream aIs, String aEncoding, StringBuilder text) throws IOException { // getting header information LineIterator lineIterator = IOUtils.lineIterator(aIs, aEncoding); int columns = 1;// token number + token columns (minimum required) int tokenStart = 0, sentenceStart = 0; Map<Type, Set<Feature>> spanLayers = new LinkedHashMap<Type, Set<Feature>>(); Map<Type, Type> relationayers = new LinkedHashMap<Type, Type>(); // an annotation for every feature in a layer Map<Type, Map<Integer, AnnotationFS>> annotations = new LinkedHashMap<Type, Map<Integer, AnnotationFS>>(); // store if this is a Begin/Intermediate/End of an annotation Map<Type, Map<Integer, String>> beginEndAnno = new LinkedHashMap<Type, Map<Integer, String>>(); // Store annotations of tokens so that it can be used later for relation annotations Map<Type, Map<String, List<AnnotationFS>>> tokenAnnotations = new LinkedHashMap<Type, Map<String, List<AnnotationFS>>>(); // store target token ids used for a relation Map<Type, Map<String, List<String>>> relationTargets = new LinkedHashMap<Type, Map<String, List<String>>>(); // store tokens indexing with the concat of itsbegin-end so that lemma and pos annotation // can be attached, if exists, later indexedTokens = new HashMap<String, Token>(); while (lineIterator.hasNext()) { String line = lineIterator.next().trim(); if (line.trim().equals("") && sentenceStart == tokenStart) { continue; } if (line.trim().equals("")) { text.replace(tokenStart - 1, tokenStart, ""); tokenStart = tokenStart - 1; Sentence sentence = new Sentence(aJcas, sentenceStart, tokenStart); sentence.addToIndexes(); tokenStart++; sentenceStart = tokenStart; text.append("\n"); continue; } // sentence if (line.startsWith("#text=")) { continue; } if (line.startsWith("#id=")) { continue;// it is a comment line } if (line.startsWith("#")) { columns = getLayerAndFeature(aJcas, columns, spanLayers, relationayers, line); continue; } // some times, the sentence in #text= might have a new line which break this reader, // so skip such lines if (!Character.isDigit(line.split(" ")[0].charAt(0))) { continue; } // If we are still unlucky, the line starts with a number from the sentence but not // a token number, check if it didn't in the format NUM-NUM if (!Character.isDigit(line.split("-")[1].charAt(0))) { continue; } int count = StringUtils.countMatches(line, "\t"); if (columns != count) { throw new IOException(fileName + " This is not a valid TSV File. check this line: " + line); } // adding tokens and sentence StringTokenizer lineTk = new StringTokenizer(line, "\t"); String tokenNumberColumn = lineTk.nextToken(); String tokenColumn = lineTk.nextToken(); Token token = new Token(aJcas, tokenStart, tokenStart + tokenColumn.length()); token.addToIndexes(); Type posType = JCasUtil.getType(aJcas, POS.class); Type lemmaType = JCasUtil.getType(aJcas, Lemma.class); if (spanLayers.containsKey(posType) || spanLayers.containsKey(lemmaType)) { indexedTokens.put(tokenStart + "-" + tokenStart + tokenColumn.length(), token); } // adding the annotations createSpanAnnotation(aJcas, tokenStart, spanLayers, relationayers, annotations, beginEndAnno, tokenAnnotations, relationTargets, lineTk, tokenColumn, tokenNumberColumn); tokenStart = tokenStart + tokenColumn.length() + 1; text.append(tokenColumn + " "); } if (tokenStart > sentenceStart) { Sentence sentence = new Sentence(aJcas, sentenceStart, tokenStart); sentence.addToIndexes(); text.append("\n"); } createRelationLayer(aJcas, relationayers, tokenAnnotations, relationTargets); }
From source file:de.tudarmstadt.ukp.clarin.webanno.tsv.WebannoTsv2Reader.java
/** * Iterate through lines and create span annotations accordingly. For * multiple span annotation, based on the position of the annotation in the * line, update only the end position of the annotation *//*from w ww .j av a 2 s .c o m*/ private void setAnnotations(JCas aJcas, InputStream aIs, String aEncoding, StringBuilder text) throws IOException { // getting header information LineIterator lineIterator = IOUtils.lineIterator(aIs, aEncoding); int columns = 1;// token number + token columns (minimum required) int tokenStart = 0, sentenceStart = 0; Map<Type, Set<Feature>> spanLayers = new LinkedHashMap<Type, Set<Feature>>(); Map<Type, Type> relationayers = new LinkedHashMap<Type, Type>(); // an annotation for every feature in a layer Map<Type, Map<Integer, AnnotationFS>> annotations = new LinkedHashMap<Type, Map<Integer, AnnotationFS>>(); // store if this is a Begin/Intermediate/End of an annotation Map<Type, Map<Integer, String>> beginEndAnno = new LinkedHashMap<Type, Map<Integer, String>>(); // Store annotations of tokens so that it can be used later for relation // annotations Map<Type, Map<String, List<AnnotationFS>>> tokenAnnotations = new LinkedHashMap<Type, Map<String, List<AnnotationFS>>>(); // store target token ids used for a relation Map<Type, Map<String, List<String>>> relationTargets = new LinkedHashMap<Type, Map<String, List<String>>>(); // store tokens indexing with the concat of itsbegin-end so that lemma // and pos annotation // can be attached, if exists, later indexedTokens = new HashMap<String, Token>(); while (lineIterator.hasNext()) { String line = lineIterator.next().trim(); if (line.trim().equals("") && sentenceStart == tokenStart) { continue; } if (line.trim().equals("")) { text.replace(tokenStart - 1, tokenStart, ""); tokenStart = tokenStart - 1; Sentence sentence = new Sentence(aJcas, sentenceStart, tokenStart); sentence.addToIndexes(); tokenStart++; sentenceStart = tokenStart; text.append("\n"); continue; } // sentence if (line.startsWith("#text=")) { continue; } if (line.startsWith("#id=")) { continue;// it is a comment line } if (line.startsWith("#")) { columns = getLayerAndFeature(aJcas, columns, spanLayers, relationayers, line); continue; } // some times, the sentence in #text= might have a new line which // break this reader, // so skip such lines if (!Character.isDigit(line.split(" ")[0].charAt(0))) { continue; } // If we are still unlucky, the line starts with a number from the // sentence but not // a token number, check if it didn't in the format NUM-NUM if (!Character.isDigit(line.split("-")[1].charAt(0))) { continue; } int count = StringUtils.countMatches(line, "\t"); if (columns != count) { throw new IOException(fileName + " This is not a valid TSV File. check this line: " + line); } // adding tokens and sentence StringTokenizer lineTk = new StringTokenizer(line, "\t"); String tokenNumberColumn = lineTk.nextToken(); String tokenColumn = lineTk.nextToken(); Token token = new Token(aJcas, tokenStart, tokenStart + tokenColumn.length()); token.addToIndexes(); Type posType = JCasUtil.getType(aJcas, POS.class); Type lemmaType = JCasUtil.getType(aJcas, Lemma.class); if (spanLayers.containsKey(posType) || spanLayers.containsKey(lemmaType)) { indexedTokens.put(tokenStart + "-" + tokenStart + tokenColumn.length(), token); } // adding the annotations createSpanAnnotation(aJcas, tokenStart, spanLayers, relationayers, annotations, beginEndAnno, tokenAnnotations, relationTargets, lineTk, tokenColumn, tokenNumberColumn); tokenStart = tokenStart + tokenColumn.length() + 1; text.append(tokenColumn + " "); } if (tokenStart > sentenceStart) { Sentence sentence = new Sentence(aJcas, sentenceStart, tokenStart); sentence.addToIndexes(); text.append("\n"); } createRelationLayer(aJcas, relationayers, tokenAnnotations, relationTargets); }
From source file:com.googlecode.androidcartridge.metafacades.db.AndroidEntityFacadeLogicImpl.java
@Override protected Integer handleGetIdentifierPositionInContentURI(final String identifier) { String contentPath = getContentPath(true, true); int position = contentPath.lastIndexOf(identifier); Integer result;//from ww w. ja va2 s .c o m if (position > 0) { String trimmedPath = contentPath.substring(0, position); result = StringUtils.countMatches(trimmedPath, "/"); } else { result = -1; } return result; }
From source file:com.kalessil.phpStorm.yii2inspections.inspectors.MissingPropertyAnnotationsInspector.java
@Override @NotNull// w w w. ja v a 2 s .c om public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { return new PhpElementVisitor() { @Override public void visitPhpClass(PhpClass clazz) { /* check only regular named classes */ final PsiElement nameNode = NamedElementUtil.getNameIdentifier(clazz); if (null == nameNode) { return; } /* check if the class inherited from yii\base\Object */ boolean supportsPropertyFeature = false; final Set<PhpClass> parents = InheritanceChainExtractUtil.collect(clazz); if (!parents.isEmpty()) { for (final PhpClass parent : parents) { if (baseObjectClasses.contains(parent.getFQN())) { supportsPropertyFeature = true; break; } } parents.clear(); } if (!supportsPropertyFeature) { return; } /* iterate get methods, find matching set methods */ final Map<String, String> props = this.findPropertyCandidates(clazz); if (props.size() > 0) { List<String> names = new ArrayList<>(props.keySet()); Collections.sort(names); final String message = messagePattern.replace("%p%", String.join("', '", names)); holder.registerProblem(nameNode, message, ProblemHighlightType.WEAK_WARNING, new TheLocalFix(props)); } } @NotNull private Map<String, String> findPropertyCandidates(@NotNull PhpClass clazz) { final Map<String, String> properties = new HashMap<>(); /* extract methods and operate on name-methods relations */ final Method[] methods = clazz.getOwnMethods(); if (null == methods || 0 == methods.length) { return properties; } final Map<String, Method> mappedMethods = new HashMap<>(); for (Method method : methods) { mappedMethods.put(method.getName(), method); } /* process extracted methods*/ for (String candidate : mappedMethods.keySet()) { Method getterMethod = null; Method setterMethod = null; /* extract methods: get (looks up and extracts set), set (looks up get and skipped if found) */ if (candidate.startsWith("get")) { getterMethod = mappedMethods.get(candidate); if (getterMethod.isStatic() || 0 != getterMethod.getParameters().length) { getterMethod = null; } final String complimentarySetter = candidate.replaceAll("^get", "set"); if (mappedMethods.containsKey(complimentarySetter)) { setterMethod = mappedMethods.get(complimentarySetter); if (setterMethod.isStatic() || 0 == setterMethod.getParameters().length) { setterMethod = null; } } } if (candidate.startsWith("set")) { setterMethod = mappedMethods.get(candidate); if (setterMethod.isStatic() || setterMethod.getParameters().length != 1) { setterMethod = null; } final String complimentaryGetter = candidate.replaceAll("^set", "get"); if (mappedMethods.containsKey(complimentaryGetter)) { continue; } } /* ensure that strategies are reachable */ if ((null == getterMethod && null == setterMethod) || (REQUIRE_BOTH_GETTER_SETTER && (null == getterMethod || null == setterMethod))) { continue; } /* store property and it's types */ final Set<String> propertyTypesFqns = new HashSet<>(); if (null != getterMethod) { propertyTypesFqns.addAll(getterMethod.getType().filterUnknown().getTypes()); } if (null != setterMethod) { final Parameter[] setterParams = setterMethod.getParameters(); if (setterParams.length > 0) { propertyTypesFqns.addAll(setterParams[0].getType().filterUnknown().getTypes()); } } /* drop preceding \ in core types */ final Set<String> propertyTypes = new HashSet<>(); for (String type : propertyTypesFqns) { if (type.length() > 0) { if ('\\' == type.charAt(0) && 1 == StringUtils.countMatches(type, "\\")) { type = type.replace("\\", ""); } propertyTypes.add(type); } } propertyTypesFqns.clear(); final String typesAsString = propertyTypes.isEmpty() ? "mixed" : String.join("|", propertyTypes); properties.put(StringUtils.uncapitalize(candidate.replaceAll("^(get|set)", "")), typesAsString); } /* exclude annotated properties: lazy bulk operation */ if (properties.size() > 0) { final Collection<Field> fields = clazz.getFields(); for (Field candidate : fields) { /* do not process constants and static fields */ if (candidate.isConstant() || candidate.getModifier().isStatic()) { continue; } properties.remove(candidate.getName()); } fields.clear(); } return properties; } }; }
From source file:com.temenos.interaction.core.rim.TestHeaderHelper.java
@Test public void testEncodeQueryParametersWithHttpEntities() { MultivaluedMap<String, String> values = new MultivaluedMapImpl<String>(); values.add("customerNam=", "J&ck"); values.add("trans&ction", "!0!"); String queryParam = HeaderHelper.encodeMultivalueQueryParameters(values); assertThat(queryParam,//from ww w . jav a 2s.co m allOf(containsString("customerNam%3D=J%26ck"), containsString("trans%26ction=%210%21"))); assertThat(StringUtils.countMatches(queryParam, "&"), equalTo(1)); }
From source file:de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.RemoteApiController.java
/** * Create a new project./*from www . j av a 2 s . c om*/ * * To test, use the Linux "curl" command. * * curl -v -F 'file=@test.zip' -F 'name=Test' -F 'filetype=tcf' * 'http://USERNAME:PASSWORD@localhost:8080/de.tudarmstadt.ukp.clarin.webanno.webapp/api/project * ' * * @param aName * the name of the project to create. * @param aFileType * the type of the files contained in the ZIP. The possible file types are configured * in the formats.properties configuration file of WebAnno. * @param aFile * a ZIP file containing the project data. * @throws Exception if there was en error. */ @RequestMapping(value = "/project", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public @ResponseStatus(HttpStatus.NO_CONTENT) void createProject(@RequestParam("file") MultipartFile aFile, @RequestParam("name") String aName, @RequestParam("filetype") String aFileType) throws Exception { LOG.info("Creating project [" + aName + "]"); if (!ZipUtils.isZipStream(aFile.getInputStream())) { throw new InvalidFileNameException("", "is an invalid Zip file"); } // Get current user String username = SecurityContextHolder.getContext().getAuthentication().getName(); User user = userRepository.get(username); Project project = null; // Configure project if (!projectRepository.existsProject(aName)) { project = new Project(); project.setName(aName); // Create the project and initialize tags projectRepository.createProject(project, user); annotationService.initializeTypesForProject(project, user, new String[] {}, new String[] {}, new String[] {}, new String[] {}, new String[] {}, new String[] {}, new String[] {}, new String[] {}); // Create permission for this user ProjectPermission permission = new ProjectPermission(); permission.setLevel(PermissionLevel.ADMIN); permission.setProject(project); permission.setUser(username); projectRepository.createProjectPermission(permission); permission = new ProjectPermission(); permission.setLevel(PermissionLevel.USER); permission.setProject(project); permission.setUser(username); projectRepository.createProjectPermission(permission); } // Existing project else { throw new IOException("The project with name [" + aName + "] exists"); } // Iterate through all the files in the ZIP // If the current filename does not start with "." and is in the root folder of the ZIP, // import it as a source document File zimpFile = File.createTempFile(aFile.getOriginalFilename(), ".zip"); aFile.transferTo(zimpFile); ZipFile zip = new ZipFile(zimpFile); for (Enumeration<?> zipEnumerate = zip.entries(); zipEnumerate.hasMoreElements();) { // // Get ZipEntry which is a file or a directory // ZipEntry entry = (ZipEntry) zipEnumerate.nextElement(); // If it is the zip name, ignore it if ((FilenameUtils.removeExtension(aFile.getOriginalFilename()) + "/").equals(entry.toString())) { continue; } // IF the current filename is META-INF/webanno/source-meta-data.properties store it // as // project meta data else if (entry.toString().replace("/", "") .equals((META_INF + "webanno/source-meta-data.properties").replace("/", ""))) { InputStream zipStream = zip.getInputStream(entry); projectRepository.savePropertiesFile(project, zipStream, entry.toString()); } // File not in the Zip's root folder OR not // META-INF/webanno/source-meta-data.properties else if (StringUtils.countMatches(entry.toString(), "/") > 1) { continue; } // If the current filename does not start with "." and is in the root folder of the // ZIP, import it as a source document else if (!FilenameUtils.getExtension(entry.toString()).equals("") && !FilenameUtils.getName(entry.toString()).equals(".")) { uploadSourceDocument(zip, entry, project, user, aFileType); } } LOG.info("Successfully created project [" + aName + "] for user [" + username + "]"); }
From source file:com.impetus.client.mongodb.MongoDBClientFactory.java
@Override protected void onValidation(final String contactNode, final String defaultPort) { if (contactNode != null) { // allow configuration as comma-separated list of host:port // addresses without the default port boolean allAddressesHaveHostAndPort = true; for (String node : contactNode.split(Constants.COMMA)) { if (StringUtils.countMatches(node, Constants.COLON) == 1) { // node is given with hostname and port // count == 1 is to exclude IPv6 addresses if (StringUtils.isNumeric(node.split(Constants.COLON)[1])) { continue; }//from w ww. j av a2 s. co m } allAddressesHaveHostAndPort = false; break; } if (allAddressesHaveHostAndPort) { return; } } // fall back to the generic validation which requires the default port // to be set super.onValidation(contactNode, defaultPort); }
From source file:de.tudarmstadt.ukp.dkpro.wsd.io.reader.AidaReader.java
private void addAnnotations(JCas jCas, StringBuffer documentText, String namedEntity, String aidaResult) { int offset = documentText.lastIndexOf(namedEntity); int length = namedEntity.length(); if (offset == -1) { offset = documentText.lastIndexOf(namedEntity.replaceAll("'", " '")); length += StringUtils.countMatches(namedEntity, "'"); }/*from w ww . j ava 2 s .com*/ // add WSDItem WSDItem wsdItem = getWsdItem(jCas, offset, length, namedEntity); // Get sense tags FSArray senseArray = new FSArray(jCas, 1); Sense sense = new Sense(jCas); sense.setId(StringEscapeUtils.unescapeJava(aidaResult)); sense.setConfidence(1.0); sense.addToIndexes(); senseArray.set(0, sense); WSDResult wsdResult = new WSDResult(jCas); wsdResult.setWsdItem(wsdItem); wsdResult.setSenses(senseArray); wsdResult.setSenseInventory(senseInventory); wsdResult.setDisambiguationMethod(DISAMBIGUATION_METHOD_NAME); wsdResult.addToIndexes(); }