List of usage examples for org.apache.commons.lang3 StringUtils countMatches
public static int countMatches(final CharSequence str, final char ch)
Counts how many times the char appears in the given string.
A null or empty ("") String input returns 0 .
StringUtils.countMatches(null, *) = 0 StringUtils.countMatches("", *) = 0 StringUtils.countMatches("abba", 0) = 0 StringUtils.countMatches("abba", 'a') = 2 StringUtils.countMatches("abba", 'b') = 2 StringUtils.countMatches("abba", 'x') = 0
From source file:org.kalypso.core.catalog.CatalogUtilities.java
/** * get max level of URN <br>//from w ww .j a v a 2 s .co m * "urn" of "urn:" is 1<br> * "urn:ogc" or "urn:ogc:" is 2 <br> * "urn:ogc:sld" or "urn:ogc:sld:" is 3<br> * * @return max level of URN */ public static int getMaxLevel(final String urn) { if (urn == null || "".equals(urn) || ":".equals(urn)) //$NON-NLS-1$ //$NON-NLS-2$ return 0; final int result = StringUtils.countMatches(urn, ":"); //$NON-NLS-1$ if (urn.endsWith(":")) //$NON-NLS-1$ return result; return result + 1; }
From source file:org.kalypso.model.wspm.tuhh.ui.utils.GuessStationPatternReplacer.java
public static String getSearchRegex(final String stationPattern) throws CoreException { final String token = String.format("<%s>", GuessStationPattern.TOKEN); //$NON-NLS-1$ final int countMatches = StringUtils.countMatches(stationPattern, token); if (countMatches != 1) { final String msg = String.format(Messages.getString("GuessStationPatternReplacer.2"), stationPattern); //$NON-NLS-1$ throw new CoreException(new Status(IStatus.ERROR, KalypsoModelWspmTuhhUIPlugin.getID(), msg)); }/* ww w. j a v a 2 s .c om*/ if (stationPattern.endsWith(token)) { // REMARK: in this case, we automatically extend the pattern with a '*', // as we assume that we at least have an extension return asRegex(stationPattern.substring(0, stationPattern.length() - token.length())) + token + ".*"; //$NON-NLS-1$ } if (stationPattern.startsWith(token)) return token + asRegex(stationPattern.substring(token.length())); final String[] split = stationPattern.split("\\Q" + token + "\\E"); //$NON-NLS-1$ //$NON-NLS-2$ return asRegex(split[0]) + token + asRegex(split[1]); }
From source file:org.kalypso.model.wspm.tuhh.ui.utils.GuessStationPatternValidator.java
@Override protected IStatus doValidate(final String pattern) throws CoreException { final int countMatches = StringUtils.countMatches(pattern, m_token); if (countMatches != 1) { final String containsMessage = String.format(Messages.getString("GuessStationPatternValidator.1"), //$NON-NLS-1$ m_token);/* ww w.j a v a 2 s .c om*/ fail(containsMessage); } return ValidationStatus.ok(); }
From source file:org.kawanfw.sql.api.util.PreparedStatementRunner.java
/** * Executes a SQL prepared statement for a query. * /*w w w . ja va2 s. c o m*/ * @return the result set of the prepared statement * * @throws SQLException * if a SQL Exception is raised */ public ResultSet executeQuery() throws SQLException { prepStatement = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); int numberOfIntMarks = StringUtils.countMatches(sql, "?"); int numberOfParams = params.size(); if (numberOfIntMarks != numberOfParams) { throw new SQLException("sql statement numbers of \"?\" do no match number of parameters: " + numberOfIntMarks + " and " + numberOfParams); } for (int i = 0; i < params.size(); i++) { int j = i + 1; prepStatement.setObject(j, params.get(i)); } rs = null; if (sql.toLowerCase().startsWith(SELECT)) { debug("sql query for prepStatement.executeQuery(): " + CR_LF + sql); rs = prepStatement.executeQuery(); } else { throw new SQLException("sql string is not a query: " + sql); } return rs; }
From source file:org.kawanfw.sql.api.util.PreparedStatementRunner.java
/** * Executes a SQL prepared statement for an update. * //from ww w . j a v a 2 s.c o m * @return the return code of the prepared statement * * @throws SQLException * if a SQL Exception is raised */ public int executeUpdate() throws SQLException { prepStatement = connection.prepareStatement(sql); int numberOfIntMarks = StringUtils.countMatches(sql, "?"); int numberOfParams = params.size(); if (numberOfIntMarks != numberOfParams) { throw new SQLException("sql statement numbers of \"?\" do no match number of parameters: " + numberOfIntMarks + " and " + numberOfParams); } for (int i = 0; i < params.size(); i++) { int j = i + 1; prepStatement.setObject(j, params.get(i)); } int rc = -1; String sqlLower = sql.toLowerCase(); if (sqlLower.startsWith(SELECT)) { throw new SQLException("sql string is not an update: " + sql); } if (sqlLower.startsWith(UPDATE) || sqlLower.startsWith(DELETE)) { if (sqlLower.indexOf(" " + WHERE + " ") == 0) { throw new SQLException("update and delete are not permitted without a WHERE clause: " + sql); } } if (sqlLower.startsWith(UPDATE) || sqlLower.startsWith(DELETE) || sqlLower.startsWith(INSERT)) { rc = prepStatement.executeUpdate(); } else { throw new SQLException("Statement is not INSERT / UPDATE / DELETE: " + sql); } debug(this.toString()); return rc; }
From source file:org.kawanfw.sql.jdbc.util.ParametersUtil.java
/** * Check that the number of parameters is correct in the statement hold * /*from www. j a v a2s.c om*/ * @param statementHolder * the statement holder * @throws SQLException * if the number of parameters is incorrect, with a clean * message */ public static void checkParameters(StatementHolder statementHolder) throws SQLException { if (statementHolder == null) { throw new SQLException(Tag.PRODUCT_PRODUCT_FAIL + "statementHolder can not be null"); } String sql = statementHolder.getSqlOrder(); int parameterNumbers = StringUtils.countMatches(sql, "?"); if (parameterNumbers == 0) { return; } Map<Integer, Integer> parameterTypes = statementHolder.getParameterTypes(); Set<Integer> set = parameterTypes.keySet(); if (set.size() > parameterNumbers) { // Nothing to do for this, already done in StatementHolder } for (int i = 1; i < parameterNumbers + 1; i++) { if (!set.contains(i)) { throw new SQLException("No value specified for parameter " + i); } } }
From source file:org.kawanfw.sql.jdbc.util.ParametersUtilCallable.java
/** * Check that the number of parameters is correct in the sql order * /*from ww w .j a v a 2 s. c om*/ * @param sql * the sql order * @param parameterTypes * the parameters types * @throws SQLException * if the number of parameters is incorrect, with a clean * message */ private static void checkParameters(String sql, Map<Integer, Integer> parameterTypes) throws SQLException { int parameterNumbers = StringUtils.countMatches(sql, "?"); if (parameterNumbers == 0) { return; } Set<Integer> set = parameterTypes.keySet(); if (set.size() > parameterNumbers) { // Nothing to do for this, already done in StatementHolder } for (int i = 1; i < parameterNumbers + 1; i++) { if (!set.contains(i)) { throw new SQLException("No value specified for parameter " + i); } } }
From source file:org.kawanfw.sql.json.no_obfuscation.CallableStatementHolder.java
/** * @param parameterIndex//www.ja va 2 s.co m * the first parameter is 1, the second is 2, ... * @param type * the out parameter data type */ public void setOutParameter(Integer parameterIndex, int type) throws SQLException { if (sql == null) { throw new SQLException(Tag.PRODUCT_PRODUCT_FAIL + "sql can not be null!"); } int parameterNumbers = StringUtils.countMatches(sql, "?"); if (parameterIndex <= 0 || parameterIndex > parameterNumbers) { throw new SQLException("Parameter index is out of bounds: " + parameterIndex + ". Number of Parameters: " + parameterNumbers); } parmsT.put(parameterIndex, type); outP.add(parameterIndex); }
From source file:org.kawanfw.sql.json.no_obfuscation.CallableStatementHolder.java
/** * @param parameterIndex//from w w w. ja v a 2 s . c om * the first parameter is 1, the second is 2, ... * @param x * the parameter value * @param info * more information if necessary for switch(example: for * setNString()) */ public void setParameter(Integer parameterIndex, Object x, String info) throws SQLException { if (sql == null) { throw new SQLException(Tag.PRODUCT_PRODUCT_FAIL + "sql can not be null!"); } int parameterNumbers = StringUtils.countMatches(sql, "?"); if (parameterIndex <= 0 || parameterIndex > parameterNumbers) { throw new SQLException("Parameter index is out of bounds: " + parameterIndex + ". Number of Parameters: " + parameterNumbers); } int type = -1; if (x instanceof TransportAsciiStream) { type = JavaTypes.ASCII_STREAM; } else if (x instanceof BigDecimal) { type = JavaTypes.BIG_DECIMAL; } else if (x instanceof Boolean) { type = JavaTypes.BOOLEAN; } else if (x instanceof Double) { type = JavaTypes.DOUBLE; } else if (x instanceof java.sql.Date) { type = JavaTypes.DATE; } else if (x instanceof InputStream) { type = JavaTypes.INPUT_STREAM; } else if (x instanceof Integer) { type = JavaTypes.INT; } else if (x instanceof Float) { type = JavaTypes.FLOAT; } else if (x instanceof Long) { type = JavaTypes.LONG; } else if (x instanceof Reader) { type = JavaTypes.READER; } else if (x instanceof Short) { type = JavaTypes.SHORT; } else if (x instanceof String) { if (info != null && info.equals(StatementHolder.SET_N_STRING)) { type = JavaTypes.NSTRING; } else { type = JavaTypes.STRING; } } else if (x instanceof Time) { type = JavaTypes.TIME; } else if (x instanceof Timestamp) { type = JavaTypes.TIMESTAMP; } else if (x instanceof URL) { type = JavaTypes.URL; } else if (x instanceof Array) { type = JavaTypes.ARRAY; } else if (x instanceof RowId) { type = JavaTypes.ROWID; } // For null values & unknown types: use Object for transportation // This must be done last (all classes are Objects!) else if (x == null || x instanceof Object) { type = JavaTypes.OBJECT; } else { throw new IllegalArgumentException("Unknown/Unauthorized type for: " + x + " Class: " + x.getClass()); } // 16/09/10 15:50 NDP - PreparedStatementHttp: Fix bug in // parametersToString(): value.toString() could be null! // Todo to avoid value.toString() because value maybe null String valueStr = null; if (x instanceof URL) { UrlTransporter urlTransporter = new UrlTransporter(); try { URL url = (URL) x; valueStr = urlTransporter.toBase64(url); } catch (IOException e) { throw new SQLException(e); } } else if (x instanceof Array) { // We don't send it on server, we us the server one. We just use the // Id for server location if (!(x instanceof ArrayHttp)) { throw new SQLException( Tag.PRODUCT + "Invalid Array, not created by a Connection.createArrayOf() call."); } valueStr = ((ArrayHttp) x).getArrayId(); } else if (x instanceof RowId) { if (!(x instanceof RowIdHttp)) { throw new SQLException(Tag.PRODUCT + "Invalid RowId, not created by a ResultSet.getRowId() call."); } RowIdTransporter rowIdTransporter = new RowIdTransporter(); try { RowId rowId = (RowId) x; valueStr = rowIdTransporter.toBase64(rowId); } catch (IOException e) { throw new SQLException(e); } } else { if (x != null) { valueStr = x.toString(); valueStr = HtmlConverter.toHtml(valueStr); } } // System.out.println(); // System.out.println("parameterIndex: " + parameterIndex); // System.out.println("type : " + type); // System.out.println("valueStr : " + valueStr); parmsT.put(parameterIndex, type); parmsV.put(parameterIndex, valueStr); }
From source file:org.kawanfw.sql.json.no_obfuscation.CallableStatementHolder.java
public void setOutParameterValue(Integer parameterIndex, Object x) throws SQLException { if (sql == null) { throw new SQLException(Tag.PRODUCT_PRODUCT_FAIL + "sql can not be null!"); }//www . j a v a 2s . c om int parameterNumbers = StringUtils.countMatches(sql, "?"); if (parameterIndex <= 0 || parameterIndex > parameterNumbers) { throw new SQLException("Parameter index is out of bounds: " + parameterIndex + ". Number of Parameters: " + parameterNumbers); } if (!outP.contains(parameterIndex)) { throw new SQLException("Parameter at index " + parameterIndex + " is not out parameter"); } String valueStr = null; if (x != null) { valueStr = x.toString(); valueStr = HtmlConverter.toHtml(valueStr); } parmsV.put(parameterIndex, valueStr); }