List of usage examples for org.apache.commons.lang StringUtils chomp
public static String chomp(String str)
Removes one newline from end of a String if it's there, otherwise leave it alone.
From source file:org.mule.devkit.apt.model.AnnotationProcessorIdentifiable.java
@Override public String getJavaDocSummary() { String comment = elements.getDocComment(innerElement); if (comment == null || StringUtils.isBlank(comment)) { return null; }/*from ww w .j a va 2 s. com*/ comment = comment.trim(); String parsedComment = ""; boolean tagsBegan = false; StringTokenizer st = new StringTokenizer(comment, "\n\r"); while (st.hasMoreTokens()) { String nextToken = st.nextToken().trim(); if (nextToken.startsWith("@")) { tagsBegan = true; } if (!tagsBegan) { parsedComment = parsedComment + nextToken + "\n"; } } String strippedComments = ""; boolean insideTag = false; for (int i = 0; i < parsedComment.length(); i++) { if (parsedComment.charAt(i) == '{' && parsedComment.charAt(i + 1) == '@') { insideTag = true; } else if (parsedComment.charAt(i) == '}') { insideTag = false; } else { if (!insideTag) { strippedComments += parsedComment.charAt(i); } } } strippedComments = strippedComments.trim(); while (strippedComments.length() > 0 && strippedComments.charAt(strippedComments.length() - 1) == '\n') { strippedComments = StringUtils.chomp(strippedComments); } return strippedComments; }
From source file:org.mule.devkit.apt.model.AnnotationProcessorIdentifiable.java
@Override public String getJavaDocParameterSummary(String paramName) { String comment = elements.getDocComment(innerElement); if (StringUtils.isBlank(comment)) { return null; }//www. j a v a 2 s . c o m comment = comment.trim(); StringBuilder parameterCommentBuilder = new StringBuilder(); boolean insideParameter = false; StringTokenizer st = new StringTokenizer(comment, "\n\r"); while (st.hasMoreTokens()) { String nextToken = st.nextToken().trim(); if (nextToken.startsWith("@param " + paramName + " ") || nextToken.equals("@param " + paramName)) { insideParameter = true; } else if (nextToken.startsWith("@")) { insideParameter = false; } if (insideParameter) { parameterCommentBuilder.append(nextToken).append(" "); } } int startIndex = 7 + paramName.length() + 1; if (parameterCommentBuilder.length() < startIndex) { return null; } String parameterComment = parameterCommentBuilder.substring(startIndex); StringBuilder strippedCommentBuilder = new StringBuilder(); boolean insideTag = false; for (int i = 0; i < parameterComment.length(); i++) { if (parameterComment.charAt(i) == '{' && parameterComment.charAt(i + 1) == '@') { insideTag = true; } else if (parameterComment.charAt(i) == '}') { insideTag = false; } else { if (!insideTag) { strippedCommentBuilder.append(parameterComment.charAt(i)); } } } String strippedComment = strippedCommentBuilder.toString().trim(); while (strippedComment.length() > 0 && strippedComment.charAt(strippedComment.length() - 1) == '\n') { strippedComment = StringUtils.chomp(strippedComment); } return strippedComment; }
From source file:org.mule.devkit.utils.JavaDocUtils.java
public String getSummary(Element element) { if (element instanceof DevKitTypeElement) { element = ((DevKitTypeElement) element).getInnerTypeElement(); }//from w ww .ja v a 2 s. co m String comment = elements.getDocComment(element); if (comment == null || StringUtils.isBlank(comment)) { return null; } comment = comment.trim(); String parsedComment = ""; boolean tagsBegan = false; StringTokenizer st = new StringTokenizer(comment, "\n\r"); while (st.hasMoreTokens()) { String nextToken = st.nextToken().trim(); if (nextToken.startsWith("@")) { tagsBegan = true; } if (!tagsBegan) { parsedComment = parsedComment + nextToken + "\n"; } } String strippedComments = ""; boolean insideTag = false; for (int i = 0; i < parsedComment.length(); i++) { if (parsedComment.charAt(i) == '{' && parsedComment.charAt(i + 1) == '@') { insideTag = true; } else if (parsedComment.charAt(i) == '}') { insideTag = false; } else { if (!insideTag) { strippedComments += parsedComment.charAt(i); } } } strippedComments = strippedComments.trim(); while (strippedComments.length() > 0 && strippedComments.charAt(strippedComments.length() - 1) == '\n') { strippedComments = StringUtils.chomp(strippedComments); } return strippedComments; }
From source file:org.mule.devkit.utils.JavaDocUtils.java
public String getParameterSummary(String paramName, Element element) { String comment = elements.getDocComment(element); if (StringUtils.isBlank(comment)) { return null; }// ww w. j a v a2s. c o m comment = comment.trim(); StringBuilder parameterCommentBuilder = new StringBuilder(); boolean insideParameter = false; StringTokenizer st = new StringTokenizer(comment, "\n\r"); while (st.hasMoreTokens()) { String nextToken = st.nextToken().trim(); if (nextToken.startsWith("@param " + paramName + " ") || nextToken.equals("@param " + paramName)) { insideParameter = true; } else if (nextToken.startsWith("@")) { insideParameter = false; } if (insideParameter) { parameterCommentBuilder.append(nextToken).append(" "); } } int startIndex = 7 + paramName.length() + 1; if (parameterCommentBuilder.length() < startIndex) { return null; } String parameterComment = parameterCommentBuilder.substring(startIndex); StringBuilder strippedCommentBuilder = new StringBuilder(); boolean insideTag = false; for (int i = 0; i < parameterComment.length(); i++) { if (parameterComment.charAt(i) == '{' && parameterComment.charAt(i + 1) == '@') { insideTag = true; } else if (parameterComment.charAt(i) == '}') { insideTag = false; } else { if (!insideTag) { strippedCommentBuilder.append(parameterComment.charAt(i)); } } } String strippedComment = strippedCommentBuilder.toString().trim(); while (strippedComment.length() > 0 && strippedComment.charAt(strippedComment.length() - 1) == '\n') { strippedComment = StringUtils.chomp(strippedComment); } return strippedComment; }
From source file:org.mule.module.extension.internal.capability.xml.schema.AnnotationProcessorUtils.java
private static String stripTags(String comment) { StringBuilder builder = new StringBuilder(); boolean insideTag = false; comment = comment.trim();//from w ww . j av a 2 s . co m final int length = comment.length(); for (int i = 0; i < length; i++) { if (comment.charAt(i) == '{') { int nextCharIndex = i + 1; if (nextCharIndex < length && comment.charAt(nextCharIndex) == AT_CHAR) { insideTag = true; i = nextCharIndex; continue; } } else if (comment.charAt(i) == '}' && insideTag) { insideTag = false; continue; } builder.append(comment.charAt(i)); } String strippedComments = builder.toString().trim(); while (strippedComments.length() > 0 && strippedComments.charAt(strippedComments.length() - 1) == NEW_LINE_CHAR) { strippedComments = StringUtils.chomp(strippedComments); } return strippedComments; }
From source file:org.openhab.binding.exec.internal.ExecBinding.java
/** * <p>Executes <code>commandLine</code>. Sometimes (especially observed on * MacOS) the commandLine isn't executed properly. In that cases another * exec-method is to be used. To accomplish this please use the special * delimiter '<code>@@</code>'. If <code>commandLine</code> contains this * delimiter it is split into a String[] array and the special exec-method * is used.</p>/*from w w w.j av a 2 s.com*/ * <p>A possible {@link IOException} gets logged but no further processing is * done.</p> * * @param commandLine the command line to execute * @return response data from executed command line */ private String executeCommandAndWaitResponse(String commandLine) { String retval = null; CommandLine cmdLine = null; if (commandLine.contains(CMD_LINE_DELIMITER)) { String[] cmdArray = commandLine.split(CMD_LINE_DELIMITER); cmdLine = new CommandLine(cmdArray[0]); for (int i = 1; i < cmdArray.length; i++) { cmdLine.addArgument(cmdArray[i], false); } } else { cmdLine = CommandLine.parse(commandLine); } DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); Executor executor = new DefaultExecutor(); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(stdout); executor.setExitValue(1); executor.setStreamHandler(streamHandler); executor.setWatchdog(watchdog); try { executor.execute(cmdLine, resultHandler); logger.debug("executed commandLine '{}'", commandLine); } catch (ExecuteException e) { logger.error("couldn't execute commandLine '" + commandLine + "'", e); } catch (IOException e) { logger.error("couldn't execute commandLine '" + commandLine + "'", e); } // some time later the result handler callback was invoked so we // can safely request the exit code try { resultHandler.waitFor(); int exitCode = resultHandler.getExitValue(); retval = StringUtils.chomp(stdout.toString()); logger.debug("exit code '{}', result '{}'", exitCode, retval); } catch (InterruptedException e) { logger.error("Timeout occured when executing commandLine '" + commandLine + "'", e); } return retval; }
From source file:org.openhab.binding.irtrans.handler.EthernetBridgeHandler.java
protected int getByteCount(ByteBuffer byteBuffer) { String response = new String(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); response = StringUtils.chomp(response); Matcher matcher = RESPONSE_PATTERN.matcher(response); if (matcher.matches()) { return Integer.parseInt(matcher.group(1)); }/*from w w w. j a v a 2 s . c om*/ return 0; }
From source file:org.openhab.binding.irtrans.handler.EthernetBridgeHandler.java
protected String stripByteCount(ByteBuffer byteBuffer) { String message = null;/* ww w. ja v a 2 s . c o m*/ String response = new String(byteBuffer.array(), 0, byteBuffer.limit()); response = StringUtils.chomp(response); Matcher matcher = RESPONSE_PATTERN.matcher(response); if (matcher.matches()) { String byteCountAsString = matcher.group(1); message = matcher.group(2); } return message; }
From source file:org.openhab.binding.irtrans.internal.IRtransBinding.java
/** * Strip byte count from the bytebuffer. IRTrans devices include the number of bytes sent * in each response it sends back to the connected host. This is a simple error checking * mechanism - we do need that information, and so, we strip it * /*from w w w .ja v a2 s . c o m*/ * @param byteBuffer * the byte buffer * @return the string */ protected String stripByteCount(ByteBuffer byteBuffer) { /** {@link Pattern} which matches a binding configuration part */ Pattern RESPONSE_PATTERN = Pattern.compile("..(\\d{5}) (.*)"); String message = null; String response = new String(byteBuffer.array(), 0, byteBuffer.limit()); response = StringUtils.chomp(response); Matcher matcher = RESPONSE_PATTERN.matcher(response); if (matcher.matches()) { String byteCountAsString = matcher.group(1); int byteCount = Integer.parseInt(byteCountAsString); message = matcher.group(2); } return message; }
From source file:org.openhab.binding.keba.handler.KeContactHandler.java
protected void onRead(ByteBuffer byteBuffer) { String response = new String(byteBuffer.array(), 0, byteBuffer.limit()); response = StringUtils.chomp(response); if (response.contains("TCH-OK")) { // ignore confirmation messages which are not JSON return;//from w w w .j a v a2 s .c o m } try { JsonObject readObject = parser.parse(response).getAsJsonObject(); for (Entry<String, JsonElement> entry : readObject.entrySet()) { switch (entry.getKey()) { case "Product": { Map<String, String> properties = editProperties(); String product = entry.getValue().getAsString().trim(); properties.put(CHANNEL_MODEL, product); updateProperties(properties); if (product.contains("P20")) { type = KebaType.P20; } else if (product.contains("P30")) { type = KebaType.P30; } series = KebaSeries.getSeries(product.substring(13, 14).charAt(0)); break; } case "Serial": { Map<String, String> properties = editProperties(); properties.put(CHANNEL_SERIAL, entry.getValue().getAsString()); updateProperties(properties); break; } case "Firmware": { Map<String, String> properties = editProperties(); properties.put(CHANNEL_FIRMWARE, entry.getValue().getAsString()); updateProperties(properties); firmware = KebaFirmware.getFirmware(entry.getValue().getAsString()); break; } case "Plug": { int state = entry.getValue().getAsInt(); switch (state) { case 0: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_WALLBOX), OnOffType.OFF); updateState(new ChannelUID(getThing().getUID(), CHANNEL_VEHICLE), OnOffType.OFF); updateState(new ChannelUID(getThing().getUID(), CHANNEL_PLUG_LOCKED), OnOffType.OFF); break; } case 1: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_WALLBOX), OnOffType.ON); updateState(new ChannelUID(getThing().getUID(), CHANNEL_VEHICLE), OnOffType.OFF); updateState(new ChannelUID(getThing().getUID(), CHANNEL_PLUG_LOCKED), OnOffType.OFF); break; } case 3: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_WALLBOX), OnOffType.ON); updateState(new ChannelUID(getThing().getUID(), CHANNEL_VEHICLE), OnOffType.OFF); updateState(new ChannelUID(getThing().getUID(), CHANNEL_PLUG_LOCKED), OnOffType.ON); break; } case 5: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_WALLBOX), OnOffType.ON); updateState(new ChannelUID(getThing().getUID(), CHANNEL_VEHICLE), OnOffType.ON); updateState(new ChannelUID(getThing().getUID(), CHANNEL_PLUG_LOCKED), OnOffType.OFF); break; } case 7: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_WALLBOX), OnOffType.ON); updateState(new ChannelUID(getThing().getUID(), CHANNEL_VEHICLE), OnOffType.ON); updateState(new ChannelUID(getThing().getUID(), CHANNEL_PLUG_LOCKED), OnOffType.ON); break; } } break; } case "State": { State newState = new DecimalType(entry.getValue().getAsInt()); updateState(new ChannelUID(getThing().getUID(), CHANNEL_STATE), newState); break; } case "Enable sys": { int state = entry.getValue().getAsInt(); switch (state) { case 1: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_ENABLED), OnOffType.ON); break; } default: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_ENABLED), OnOffType.OFF); break; } } break; } case "Curr HW": { int state = entry.getValue().getAsInt(); maxSystemCurrent = state; State newState = new DecimalType(state); updateState(new ChannelUID(getThing().getUID(), CHANNEL_MAX_SYSTEM_CURRENT), newState); if (maxSystemCurrent < maxPresetCurrent) { sendCommand("curr " + String.valueOf(maxSystemCurrent)); updateState(new ChannelUID(getThing().getUID(), CHANNEL_MAX_PRESET_CURRENT), new DecimalType(maxSystemCurrent)); updateState(new ChannelUID(getThing().getUID(), CHANNEL_MAX_PRESET_CURRENT_RANGE), new PercentType((maxSystemCurrent - 6000) * 100 / (maxSystemCurrent - 6000))); } break; } case "Curr user": { int state = entry.getValue().getAsInt(); maxPresetCurrent = state; updateState(new ChannelUID(getThing().getUID(), CHANNEL_MAX_PRESET_CURRENT), new DecimalType(state)); updateState(new ChannelUID(getThing().getUID(), CHANNEL_MAX_PRESET_CURRENT_RANGE), new PercentType((state - 6000) * 100 / (maxSystemCurrent - 6000))); break; } case "Curr FS": { int state = entry.getValue().getAsInt(); State newState = new DecimalType(state); updateState(new ChannelUID(getThing().getUID(), CHANNEL_FAILSAFE_CURRENT), newState); break; } case "Output": { int state = entry.getValue().getAsInt(); switch (state) { case 1: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_OUTPUT), OnOffType.ON); break; } default: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_OUTPUT), OnOffType.OFF); break; } } break; } case "Input": { int state = entry.getValue().getAsInt(); switch (state) { case 1: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_INPUT), OnOffType.ON); break; } default: { updateState(new ChannelUID(getThing().getUID(), CHANNEL_INPUT), OnOffType.OFF); break; } } break; } case "Sec": { long state = entry.getValue().getAsLong(); Calendar uptime = Calendar.getInstance(); uptime.setTimeZone(TimeZone.getTimeZone("GMT")); uptime.setTimeInMillis(state * 1000); SimpleDateFormat pFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); pFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); updateState(new ChannelUID(getThing().getUID(), CHANNEL_UPTIME), new DateTimeType(pFormatter.format(uptime.getTime()))); break; } case "U1": { int state = entry.getValue().getAsInt(); State newState = new DecimalType(state); updateState(new ChannelUID(getThing().getUID(), CHANNEL_U1), newState); break; } case "U2": { int state = entry.getValue().getAsInt(); State newState = new DecimalType(state); updateState(new ChannelUID(getThing().getUID(), CHANNEL_U2), newState); break; } case "U3": { int state = entry.getValue().getAsInt(); State newState = new DecimalType(state); updateState(new ChannelUID(getThing().getUID(), CHANNEL_U3), newState); break; } case "I1": { int state = entry.getValue().getAsInt(); State newState = new DecimalType(state); updateState(new ChannelUID(getThing().getUID(), CHANNEL_I1), newState); break; } case "I2": { int state = entry.getValue().getAsInt(); State newState = new DecimalType(state); updateState(new ChannelUID(getThing().getUID(), CHANNEL_I2), newState); break; } case "I3": { int state = entry.getValue().getAsInt(); State newState = new DecimalType(state); updateState(new ChannelUID(getThing().getUID(), CHANNEL_I3), newState); break; } case "P": { long state = entry.getValue().getAsLong(); State newState = new DecimalType(state / 1000); updateState(new ChannelUID(getThing().getUID(), CHANNEL_POWER), newState); break; } case "PF": { int state = entry.getValue().getAsInt(); State newState = new PercentType(state / 10); updateState(new ChannelUID(getThing().getUID(), CHANNEL_POWER_FACTOR), newState); break; } case "E pres": { long state = entry.getValue().getAsLong(); State newState = new DecimalType(state / 10); updateState(new ChannelUID(getThing().getUID(), CHANNEL_SESSION_CONSUMPTION), newState); break; } case "E total": { long state = entry.getValue().getAsLong(); State newState = new DecimalType(state / 10); updateState(new ChannelUID(getThing().getUID(), CHANNEL_TOTAL_CONSUMPTION), newState); break; } } } } catch (JsonParseException e) { logger.debug("Invalid JSON data will be ignored: '{}'", response); } }