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:org.esa.cci.lc.conversion.RemapGraphCreatorTest.java
@Test public void testCreateGraph() throws Exception { StringWriter writer = new StringWriter(); RemapGraphCreator.GraphWriter graphWriter = new RemapGraphCreator.GraphWriter(writer, "any_lut.csv"); graphWriter.init(new String[] { "source_band", "chl", "sst", "tsm" }); graphWriter.writeHeader();//w w w .j a va 2 s .c om graphWriter.extendExpression("source_band", new String[] { "5", "3", "9", "2" }); graphWriter.extendExpression("source_band", new String[] { "15", "0", "", "37" }); graphWriter.finishExpressions(); graphWriter.writeTargetBands(); graphWriter.writeFooter("schlumpf"); String result = removeSpaces(writer.toString()); assertTrue(result.startsWith(removeSpaces(RemapGraphCreator.GraphWriter.GRAPH_HEAD))); assertTrue( result.endsWith(removeSpaces(String.format(RemapGraphCreator.GraphWriter.GRAPH_FOOT, "schlumpf")))); assertTrue(result.contains(removeSpaces("<name>chl</name>\n" + " <expression>\n" + " source_band == 5 ? 3 : source_band == 15 ? 0 : 0\n" + " </expression>\n" + " <description>chl as defined in any_lut.csv</description>"))); assertTrue(result.contains( removeSpaces("<name>sst</name>\n" + " <expression>\n" + " source_band == 5 ? 9 : 0\n" + " </expression>\n" + " <description>sst as defined in any_lut.csv</description>"))); assertTrue(result.contains(removeSpaces("<name>tsm</name>\n" + " <expression>\n" + " source_band == 5 ? 2 : source_band == 15 ? 37 : 0\n" + " </expression>\n" + " <description>tsm as defined in any_lut.csv</description>"))); assertEquals(3, StringUtils.countMatches(result, "<scalingFactor>0.01</scalingFactor>")); }
From source file:org.esa.s2tbx.dataio.spot.dimap.SpotTake5Metadata.java
@Override public int getNumBands() { int numBands = -1; MetadataElement currentElement;/* www . j a v a2 s.c o m*/ if (((currentElement = rootElement.getElement(SpotConstants.SPOT4_TAKE5_TAG_RADIOMETRY)) != null)) { String descBands = currentElement.getAttributeString(SpotConstants.SPOT4_TAKE5_TAG_BANDS); numBands = StringUtils.countMatches(descBands, SpotConstants.SPOT4_TAKE5_VALUES_SEPARATOR) + 1; } return numBands; }
From source file:org.exoplatform.services.cms.impl.CmsServiceImpl.java
/** * Process to update or add property for current node and all its property * Properties of node is given in current NodeType * To add/update property of all node, need check that property is created automatically or protected * When property is not created automatically and not protected then update for all child of node * @param create create = true: process adding, create = false, process updating * @param itemPath used with property name as key to get value of one property * @param currentNode Node is updated * @param currentNodeType Node type//from w w w . java2 s .co m * @param jcrVariables Mapping key = property name and value * @throws Exception */ @SuppressWarnings("unchecked") private void processNodeRecursively(boolean create, String itemPath, Node currentNode, NodeType currentNodeType, Map jcrVariables) throws Exception { if (create) { processAddEditProperty(true, currentNode, itemPath, currentNodeType, jcrVariables); } else { List<String> keyList = new ArrayList<String>(); for (Object key : jcrVariables.keySet()) { keyList.add(key.toString().substring(key.toString().lastIndexOf("/") + 1)); } List<String> currentListPropertyName = new ArrayList<String>(); for (PropertyIterator pi = currentNode.getProperties(); pi.hasNext();) { Property property = pi.nextProperty(); currentListPropertyName.add(property.getName()); } Set keys = jcrVariables.keySet(); String nodePath = extractNodeName(keys); JcrInputProperty relRootProp = (JcrInputProperty) jcrVariables.get(nodePath); String[] mixinTypes = {}; String mixintypeName = relRootProp.getMixintype(); if (mixintypeName != null && mixintypeName.trim().length() > 0) { if (mixintypeName.indexOf(",") > -1) { mixinTypes = mixintypeName.split(","); } else { mixinTypes = new String[] { mixintypeName }; } } for (String mixinType : mixinTypes) { if (!currentNode.isNodeType(mixinType)) { if (currentNode.canAddMixin(mixinType)) { currentNode.addMixin(mixinType); } } } PropertyDefinition[] propertyDefs = currentNodeType.getPropertyDefinitions(); List<PropertyDefinition> lstPropertyDefinition = Arrays.asList(propertyDefs); List<PropertyDefinition> lstPropertyDefinitionAll = new ArrayList<PropertyDefinition>(); NodeType[] mixinNodeTypes = currentNode.getMixinNodeTypes(); lstPropertyDefinitionAll.addAll(lstPropertyDefinition); for (NodeType mixinNodeType : mixinNodeTypes) { Collections.addAll(lstPropertyDefinitionAll, mixinNodeType.getPropertyDefinitions()); } // process property for (PropertyDefinition propertyDef : lstPropertyDefinitionAll) { String propertyName = propertyDef.getName(); Object value = null; String currentPath = itemPath + "/" + propertyName; JcrInputProperty inputVariable = (JcrInputProperty) jcrVariables.get(currentPath); if (inputVariable != null) { value = inputVariable.getValue(); } if (currentListPropertyName.contains(propertyName) && currentNode.hasProperty(propertyName)) { Property property = currentNode.getProperty(propertyName); int requiredtype = property.getType(); if (keyList.contains(propertyName)) { if (!propertyDef.isProtected()) { processProperty(property, currentNode, requiredtype, value, propertyDef.isMultiple()); } } } else { if (!propertyDef.isProtected()) { int requiredtype = propertyDef.getRequiredType(); if (value != null || propertyDef.isMandatory()) { processProperty(propertyName, currentNode, requiredtype, value, propertyDef.isMultiple()); } } } } //process multiple binary data for (Object key : jcrVariables.keySet()) { Object value = jcrVariables.get(key); if (((JcrInputProperty) value).getValue() instanceof Map) { processProperty(key.toString().substring(itemPath.length() + 1), currentNode, PropertyType.BINARY, ((JcrInputProperty) value).getValue(), true); } } } //process child nodes int itemLevel = StringUtils.countMatches(itemPath, "/"); List<JcrInputProperty> childNodeInputs = extractNodeInputs(jcrVariables, itemLevel + 1); NodeTypeManager nodeTypeManger = currentNode.getSession().getWorkspace().getNodeTypeManager(); List<Object> childs = new ArrayList<Object>(); if (currentNodeType.isMixin()) { if (create) { for (NodeDefinition childNodeDef : currentNodeType.getChildNodeDefinitions()) { childs.add(childNodeDef); } } else { for (NodeIterator iterator = currentNode.getNodes(); iterator.hasNext();) { childs.add(iterator.nextNode()); } } } else { Set<String> childNames = new HashSet<String>(); for (NodeDefinition childNodeDef : currentNodeType.getChildNodeDefinitions()) { childs.add(childNodeDef); NodeType declaringNodeType = childNodeDef.getDeclaringNodeType(); NodeType defaultPrimaryType = childNodeDef.getDefaultPrimaryType(); childNames.add( childNodeDef.getName() + (declaringNodeType == null ? null : declaringNodeType.getName()) + (defaultPrimaryType == null ? null : defaultPrimaryType.getName())); } if (currentNode != null) { for (NodeIterator iterator = currentNode.getNodes(); iterator.hasNext();) { NodeDefinition childNodeDef = iterator.nextNode().getDefinition(); NodeType declaringNodeType = childNodeDef.getDeclaringNodeType(); NodeType defaultPrimaryType = childNodeDef.getDefaultPrimaryType(); if (!childNames.contains(childNodeDef.getName() + (declaringNodeType == null ? null : declaringNodeType.getName()) + (defaultPrimaryType == null ? null : defaultPrimaryType.getName()))) { childs.add(childNodeDef); } } } } Set<String> childItemPaths = new HashSet<String>(); for (Object obj : childs) { NodeDefinition nodeDef; if (obj instanceof Node) { nodeDef = ((Node) obj).getDefinition(); } else { nodeDef = (NodeDefinition) obj; } if (nodeDef.isAutoCreated() || nodeDef.isProtected()) { continue; } if (((ItemDefinitionImpl) nodeDef).isResidualSet()) { for (JcrInputProperty input : childNodeInputs) { String childItemPath = itemPath + "/" + input.getValue(); //Only child node input has dependent path of current node is added as child node if (!childItemPath.equals(input.getJcrPath())) continue; String primaryNodeType = input.getNodetype(); NodeType nodeType = nodeTypeManger.getNodeType(primaryNodeType); if (!canAddNode(nodeDef, nodeType)) { continue; } String[] mixinTypes = null; if (input.getMixintype() != null) { mixinTypes = input.getMixintype().split(","); } Node childNode = doAddNode(currentNode, (String) input.getValue(), nodeType.getName(), mixinTypes); if (childNode != null && !childItemPaths.contains(childItemPath)) processNodeRecursively(create, childItemPath, childNode, childNode.getPrimaryNodeType(), jcrVariables); childItemPaths.add(childItemPath); } } else { String childNodeName = null; if (obj instanceof Node) { childNodeName = ((Node) obj).getName(); } else { childNodeName = ((NodeDefinition) obj).getName(); } String newItemPath = itemPath + "/" + childNodeName; JcrInputProperty jcrInputVariable = (JcrInputProperty) jcrVariables.get(newItemPath); if (jcrInputVariable == null) { continue; } String nodeTypeName = jcrInputVariable.getNodetype(); String[] mixinTypes = null; if (jcrInputVariable.getMixintype() != null) { mixinTypes = jcrInputVariable.getMixintype().split(","); } NodeType nodeType = null; if (obj instanceof Node) { nodeType = ((Node) obj).getPrimaryNodeType(); } else if (nodeTypeName == null || nodeTypeName.length() == 0) { nodeType = nodeDef.getRequiredPrimaryTypes()[0]; } else { nodeType = nodeTypeManger.getNodeType(nodeTypeName); } Node childNode = doAddNode(currentNode, childNodeName, nodeType.getName(), mixinTypes); if (!childItemPaths.contains(newItemPath)) processNodeRecursively(create, newItemPath, childNode, childNode.getPrimaryNodeType(), jcrVariables); childItemPaths.add(newItemPath); } } }
From source file:org.exoplatform.services.cms.impl.CmsServiceImpl.java
/** * Get all value in Map.//from w ww.ja va 2 s . c om * Base on key, iterate each key to get value in map * @param map Map of key and value of property * @param itemLevel level of child of specific node * @return * @see {@link #processNodeRecursively(boolean, String, Node, NodeType, Map)} */ private List<JcrInputProperty> extractNodeInputs(Map<String, JcrInputProperty> map, int itemLevel) { List<JcrInputProperty> list = new ArrayList<JcrInputProperty>(); for (Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext();) { String jcrPath = iterator.next(); if (itemLevel == StringUtils.countMatches(jcrPath, "/")) { JcrInputProperty input = map.get(jcrPath); if (input.getType() == JcrInputProperty.NODE) { list.add(input); } } } return list; }
From source file:org.exoplatform.wiki.rendering.impl.TestMacroRendering.java
public void testIncludePageMacro() throws Exception { Model model = mowService.getModel(); WikiStoreImpl wStore = (WikiStoreImpl) model.getWikiStore(); WikiContainer<PortalWiki> portalWikiContainer = wStore.getWikiContainer(WikiType.PORTAL); PortalWiki wiki = portalWikiContainer.addWiki("classic"); WikiHome home = wiki.getWikiHome();// ww w . jav a 2 s. c om String content = "Test include contents of a page"; home.getContent().setText(content); String expectedHtml = "<div class=\"IncludePage \" ><p>" + content + "</p></div>"; model.save(); assertEquals(expectedHtml, renderingService.render("{{includepage page=\"Wiki Home\"/}}", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false)); // Test recursive inclusion String content2 = "{includepage:page=\"Wiki Home\"}"; home.getContent().setText(content2); model.save(); String renderedHTML = renderingService.render("{includepage:page=\"Wiki Home\"}", Syntax.CONFLUENCE_1_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false); assertEquals(1, (StringUtils.countMatches(renderedHTML, "<div class=\"IncludePage \" >"))); }
From source file:org.genemania.util.GeneManiaStringUtils.java
public static String extractSeparator(String text) { String ret = " "; Hashtable<String, Integer> separatorsMap = new Hashtable<String, Integer>(); separatorsMap.put(SPACE, Integer.valueOf(StringUtils.countMatches(text, " "))); separatorsMap.put(TAB, Integer.valueOf(StringUtils.countMatches(text, "\t"))); separatorsMap.put(CR, Integer.valueOf(StringUtils.countMatches(text, "\n"))); separatorsMap.put(COMMA, Integer.valueOf(StringUtils.countMatches(text, ","))); separatorsMap.put(SEMICOLON, Integer.valueOf(StringUtils.countMatches(text, ";"))); Enumeration<String> separators = separatorsMap.keys(); int maxCount = 0; while (separators.hasMoreElements()) { String nextSeparator = separators.nextElement(); int nextSeparatorCount = ((Integer) separatorsMap.get(nextSeparator)).intValue(); if (nextSeparatorCount > maxCount) { maxCount = nextSeparatorCount; ret = nextSeparator;//w w w . j a v a2 s .co m } } return ret; }
From source file:org.glite.authz.pap.common.utils.PathNamingScheme.java
public static String getParentGroupName(String groupName) { checkSyntax(groupName);// www. ja v a2 s. c o m if (StringUtils.countMatches(groupName, "/") == 1) return groupName; else return StringUtils.substringBeforeLast(groupName, "/"); }
From source file:org.gradle.api.changedetection.state.DirectoryStateBuilder.java
public DirectoryState getDirectoryState() { if (directory == null) throw new IllegalStateException("directory is null!"); final String relativePath = stateFileUtil.getRelativePathToDirectoryToProcess(directory); final String relativePathDigest = stateFileUtil.getStringDigest(relativePath); final int level = StringUtils.countMatches(relativePath, System.getProperty("file.separator")); final DirectoryState directoryState = new DirectoryState(directory, relativePath, relativePathDigest, level);// w w w . j ava2 s . com directory = null; return directoryState; }
From source file:org.gradle.api.changedetection.state.DirectoryStateDigestCalculator.java
public List<DirectoryState> getNewSubDirectoryStates(String relativeDirectoryPath) { final List<DirectoryState> subDirectoryStates = new ArrayList<DirectoryState>(); for (final String currentStateItemKey : previousLevelDirectoryStates.keySet()) { if (currentStateItemKey.startsWith(relativeDirectoryPath)) { String belowPath = currentStateItemKey.replaceAll(relativeDirectoryPath, ""); if ("".equals(belowPath) || StringUtils.countMatches(belowPath, System.getProperty("file.separator")) == 1) subDirectoryStates.add(previousLevelDirectoryStates.get(currentStateItemKey)); }/*from w w w . j a v a 2s .c o m*/ } return subDirectoryStates; }
From source file:org.gradle.api.internal.plugins.AbstractTemplateBasedStartScriptGenerator.java
String createJoinedAppHomeRelativePath(String scriptRelPath) { int depth = StringUtils.countMatches(scriptRelPath, "/"); if (depth == 0) { return ""; }//from w w w . j a va2 s. c o m List<String> appHomeRelativePath = new ArrayList<String>(); for (int i = 0; i < depth; i++) { appHomeRelativePath.add(".."); } Joiner slashJoiner = Joiner.on("/"); return slashJoiner.join(appHomeRelativePath); }