List of usage examples for org.apache.commons.lang StringUtils abbreviate
public static String abbreviate(String str, int maxWidth)
Abbreviates a String using ellipses.
From source file:org.openengsb.connector.facebook.internal.FacebookNotifier.java
@Override public void notify(Notification notification) { LOGGER.info("Message: {}", StringUtils.abbreviate(notification.getMessage(), 200)); send(notification.getMessage());// w ww. j a v a 2 s.com LOGGER.info("facebook message has been sent"); }
From source file:org.openengsb.connector.notificationsample.internal.FileNotifier.java
private void createMessage(Notification notification, String finalFile, StringBuilder builder) { LOGGER.info("notifying {} via file {}...", notification.getRecipient(), finalFile); builder.append("notifying ").append(notification.getRecipient()).append(" by writing in this file") .append("\n"); LOGGER.info("Subject: {}", notification.getSubject()); builder.append("Subject: ").append(notification.getSubject()).append("\n"); LOGGER.info("Message: {}", StringUtils.abbreviate(notification.getMessage(), 200)); builder.append("Message: ").append(notification.getMessage()).append("\n"); }
From source file:org.openhab.action.twitter.internal.Twitter.java
/** * Sends a Tweet via Twitter//from w w w . j ava2 s . c om * * @param tweetTxt the Tweet to send * * @return <code>true</code>, if sending the tweet has been successful and * <code>false</code> in all other cases. */ @ActionDoc(text = "Sends a Tweet via Twitter", returns = "<code>true</code>, if sending the tweet has been successful and <code>false</code> in all other cases.") public static boolean sendTweet(@ParamDoc(name = "tweetTxt", text = "the Tweet to send") String tweetTxt) { if (!TwitterActionService.isProperlyConfigured) { logger.debug("Twitter client is not yet configured > execution aborted!"); return false; } if (!isEnabled) { logger.debug("Twitter client is disabled > execution aborted!"); return false; } try { // abbreviate the Tweet to meet the 140 character limit ... tweetTxt = StringUtils.abbreviate(tweetTxt, CHARACTER_LIMIT); // send the Tweet Status status = client.updateStatus(tweetTxt); logger.debug("Successfully sent Tweet '{}'", status.getText()); return true; } catch (TwitterException e) { logger.error("Failed to send Tweet '" + tweetTxt + "' because of: " + e.getLocalizedMessage()); return false; } }
From source file:org.openhab.action.twitter.internal.Twitter.java
/** * Sends a direct message via Twitter/*from w w w . j a va2s. c o m*/ * * @param recipientId the receiver of this direct message * @param messageTxt the direct message to send * * @return <code>true</code>, if sending the direct message has been successful and * <code>false</code> in all other cases. */ @ActionDoc(text = "Sends a direct message via Twitter", returns = "<code>true</code>, if sending the direct message has been successful and <code>false</code> in all other cases.") public static boolean sendDirectMessage( @ParamDoc(name = "recipientId", text = "the receiver of this direct message") String recipientId, @ParamDoc(name = "messageTxt", text = "the direct message to send") String messageTxt) { if (!isEnabled) { logger.debug("Twitter client is disabled > execution aborted!"); return false; } try { // abbreviate the Tweet to meet the 140 character limit ... messageTxt = StringUtils.abbreviate(messageTxt, CHARACTER_LIMIT); // send the direct message DirectMessage message = client.sendDirectMessage(recipientId, messageTxt); logger.debug("Successfully sent direct message '{}' to @", message.getText(), message.getRecipientScreenName()); return true; } catch (TwitterException e) { logger.error("Failed to send Tweet '" + messageTxt + "' because of: " + e.getLocalizedMessage()); return false; } }
From source file:org.openhab.binding.max.internal.command.MCommand.java
@Override public String getCommandString() { int deviceCount = 0; int roomCount = 0; ByteArrayOutputStream message = new ByteArrayOutputStream(); try {/* w w w . j a v a2 s.c o m*/ byte[] header = { MAGIC_NR, M_VERSION, (byte) rooms.size() }; message.write(header); Set<Integer> sortedRooms = new TreeSet<>(); for (RoomInformation room : rooms) { sortedRooms.add(room.getPosition()); } for (Integer roomPos : sortedRooms) { for (RoomInformation room : rooms) { if (room.getPosition() == roomPos) { if (roomCount < MAX_GROUP_COUNT) { byte[] roomName = StringUtils.abbreviate(room.getName(), MAX_NAME_LENGTH) .getBytes(StandardCharsets.UTF_8); byte[] nameLength = new byte[] { (byte) roomName.length }; byte[] rfAddress = Utils.hexStringToByteArray(room.getRFAddress()); message.write(roomPos.byteValue()); message.write(nameLength); message.write(roomName); message.write(rfAddress); } else { logger.warn("{} exceeds max number of rooms ({}). Ignored", room, MAX_GROUP_COUNT); } roomCount++; } } } for (Device di : devices) { if (deviceCount < MAX_DEVICES_COUNT) { deviceCount++; } else { logger.warn("{} exceeds max number of devices ({}). Ignored", di, MAX_DEVICES_COUNT); } } message.write((byte) deviceCount); for (Device di : devices) { if (deviceCount > 0) { byte[] deviceType = { (byte) di.getType().getValue() }; byte[] rfAddress = Utils.hexStringToByteArray(di.getRFAddress()); byte[] deviceName = StringUtils.abbreviate(di.getName(), MAX_NAME_LENGTH) .getBytes(StandardCharsets.UTF_8); byte[] nameLength = { (byte) deviceName.length }; byte[] serialNumber = di.getSerialNumber().getBytes(StandardCharsets.UTF_8); byte[] roomId = { (byte) di.getRoomId() }; message.write(deviceType); message.write(rfAddress); message.write(serialNumber); message.write(nameLength); message.write(deviceName); message.write(roomId); } else { logger.warn("{} exceeds max number of devices ({}). Ignored", di, MAX_DEVICES_COUNT); } deviceCount--; } byte[] dst = { 0x01 }; message.write(dst); } catch (IOException e) { logger.debug("Error while generating m: command: {}", e.getMessage(), e); } final String encodedString = Base64.encodeBase64StringUnChunked(message.toByteArray()); final StringBuilder commandStringBuilder = new StringBuilder(); int parts = (int) Math.round(encodedString.length() / MAX_MSG_LENGTH + 0.5); for (int i = 0; i < parts; i++) { String partString = StringUtils.abbreviate(encodedString.substring((i) * MAX_MSG_LENGTH), MAX_MSG_LENGTH); commandStringBuilder.append("m:").append(String.format("%02d", i)).append(",").append(partString) .append('\r').append('\n'); } return commandStringBuilder.toString(); }
From source file:org.openhab.binding.max.internal.command.M_Command.java
@Override public String getCommandString() { int deviceCount = 0; int roomCount = 0; ByteArrayOutputStream message = new ByteArrayOutputStream(); try {//w w w . ja v a2 s .com byte[] header = { MAGIC_NR, M_VERSION, (byte) rooms.size() }; message.write(header); TreeSet<Integer> sortedRooms = new TreeSet<Integer>(); for (RoomInformation room : rooms) { sortedRooms.add(room.getPosition()); } for (Integer roomPos : sortedRooms) { for (RoomInformation room : rooms) { if (room.getPosition() == roomPos) { if (roomCount < MAX_GROUP_COUNT) { byte[] roomName = StringUtils.abbreviate(room.getName(), MAX_NAME_LENGTH) .getBytes("UTF-8"); byte[] nameLength = new byte[] { (byte) roomName.length }; byte[] rfAddress = Utils.hexStringToByteArray(room.getRFAddress()); message.write(roomPos.byteValue()); message.write(nameLength); message.write(roomName); message.write(rfAddress); } else { logger.warn("{} exceeds max number of rooms ({}). Ignored", room.toString(), MAX_GROUP_COUNT); } roomCount++; } } } for (Device di : devices) { if (deviceCount < MAX_DEVICES_COUNT) { deviceCount++; } else { logger.warn("{} exceeds max number of devices ({}). Ignored", di.toString(), MAX_DEVICES_COUNT); } } message.write((byte) deviceCount); for (Device di : devices) { if (deviceCount > 0) { byte[] deviceType = { (byte) di.getType().getValue() }; byte[] rfAddress = Utils.hexStringToByteArray(di.getRFAddress()); byte[] deviceName = StringUtils.abbreviate(di.getName(), MAX_NAME_LENGTH).getBytes("UTF-8"); byte[] nameLength = { (byte) deviceName.length }; byte[] serialNumber = di.getSerialNumber().getBytes(); byte[] roomId = { (byte) di.getRoomId() }; message.write(deviceType); message.write(rfAddress); message.write(serialNumber); message.write(nameLength); message.write(deviceName); message.write(roomId); } else { logger.warn("{} exceeds max number of devices ({}). Ignored", di.toString(), MAX_DEVICES_COUNT); } deviceCount--; } byte[] dst = { 0x01 }; message.write(dst); } catch (IOException e) { logger.debug("Error while generating m: command: {}", e.getMessage(), e); } String encodedString = Base64.encodeBase64StringUnChunked(message.toByteArray()); String commandString = ""; int parts = (int) Math.round(encodedString.length() / MAX_MSG_LENGTH + 0.5); for (int i = 0; i < parts; i++) { String partString = StringUtils.abbreviate(encodedString.substring((i) * MAX_MSG_LENGTH), MAX_MSG_LENGTH); commandString = commandString + "m:" + String.format("%02d", i) + "," + partString + '\r' + '\n'; } return commandString; }
From source file:org.openspotlight.graph.query.console.command.dynamic.QueryCommand.java
/** * Generate output based on result nodes. * //from w w w .j a va 2 s. com * @param nodes the nodes * @param additionalProperties the additional properties * @return the string */ protected String generateOutput(final Collection<Node> nodes, final Collection<String> additionalProperties) { final StringBuilder buffer = new StringBuilder(); // Header StringBuilderUtil.append(buffer, StringUtils.repeat("-", ((3 + additionalProperties.size()) * (COLUMN_SIZE + 3)) + 1), "\n"); StringBuilderUtil.append(buffer, "|", StringUtils.center("type name", COLUMN_SIZE + 2), "|"); StringBuilderUtil.append(buffer, StringUtils.center("name", COLUMN_SIZE + 2), "|"); StringBuilderUtil.append(buffer, StringUtils.center("parent name", COLUMN_SIZE + 2), "|"); for (final String property : additionalProperties) { StringBuilderUtil.append(buffer, StringUtils.center(property, COLUMN_SIZE + 2), "|"); } StringBuilderUtil.append(buffer, "\n"); StringBuilderUtil.append(buffer, StringUtils.repeat("-", ((3 + additionalProperties.size()) * (COLUMN_SIZE + 3)) + 1), "\n"); if (!nodes.isEmpty()) { for (final Node node : nodes) { final List<String> output = new LinkedList<String>(); output.add("| "); output.add( StringUtils.rightPad(StringUtils.abbreviate(node.getTypeName(), COLUMN_SIZE), COLUMN_SIZE)); output.add(" | "); output.add(StringUtils.rightPad(StringUtils.abbreviate(node.getName(), COLUMN_SIZE), COLUMN_SIZE)); output.add(" | "); output.add(StringUtils.rightPad( StringUtils.abbreviate(StringKeysSupport.getNodeType(node.getParentId()), COLUMN_SIZE), COLUMN_SIZE)); output.add(" | "); for (final String propertyName : additionalProperties) { String propertyValue = ""; try { propertyValue = node.getPropertyValueAsString(propertyName); } catch (final Exception e) { } output.add( StringUtils.rightPad(StringUtils.abbreviate(propertyValue, COLUMN_SIZE), COLUMN_SIZE)); output.add(" | "); } StringBuilderUtil.appendLine(buffer, output); } StringBuilderUtil.append(buffer, "\n", nodes.size(), " nodes affected.", "\n"); } else { StringBuilderUtil.append(buffer, "\n", "0 nodes affected.", "\n"); } return buffer.toString(); }
From source file:org.orcid.api.common.writer.citeproc.WorkToCiteprocTranslator.java
/** * Extract the bibtex and turn into CSL Adds in DOI and URL from metadata if * missing Horrible use of reflection to shorten hyperauthorship. It will * strip anything above 20 authors down to the primary author and 'et all'. * /*w w w.j a v a2 s. co m*/ * @param work * @param abreviate * @return */ private CSLItemData translateFromBibtexCitation(Work work, boolean abreviate) { try { System.out.println("----"); System.out.println(work.getWorkCitation().getCitation()); System.out.println("----"); BibTeXConverter conv = new BibTeXConverter(); BibTeXDatabase db = conv.loadDatabase(IOUtils.toInputStream(work.getWorkCitation().getCitation())); Map<String, CSLItemData> cids = conv.toItemData(db); if (cids.size() == 1) { CSLItemData item = cids.values().iterator().next(); // FOR REASONS UNKNOWN, CITEPROC WILL SOMETIMES generate // multiple authors not a literal. if (abreviate) { if (item.getAuthor().length > 20) { CSLName[] abrev = Arrays.copyOf(item.getAuthor(), 1); abrev[0] = new CSLNameBuilder() .literal(abrev[0].getGiven() + " " + abrev[0].getFamily() + " " + "et all.") .build(); ReflectionUtils.makeAccessible(authorField); ReflectionUtils.setField(authorField, item, abrev); } for (int i = 0; i < item.getAuthor().length; i++) { if (item.getAuthor()[i].getLiteral() != null && item.getAuthor()[i].getLiteral().length() > 200) { ReflectionUtils.makeAccessible(literalField); ReflectionUtils.setField(literalField, item.getAuthor()[i], StringUtils.abbreviate(item.getAuthor()[i].getLiteral(), 200)); } } } if (item.getDOI() == null) { String doi = extractID(work, WorkExternalIdentifierType.DOI); if (doi != null) { ReflectionUtils.makeAccessible(doiField); ReflectionUtils.setField(doiField, item, doi); } } if (item.getURL() == null) { if (extractID(work, WorkExternalIdentifierType.URI) != null) { ReflectionUtils.makeAccessible(urlField); ReflectionUtils.setField(urlField, item, extractID(work, WorkExternalIdentifierType.URI)); } else if (item.getDOI() != null) { ReflectionUtils.makeAccessible(urlField); ReflectionUtils.setField(urlField, item, item.getDOI()); } else if (extractID(work, WorkExternalIdentifierType.HANDLE) != null) { ReflectionUtils.makeAccessible(urlField); ReflectionUtils.setField(urlField, item, extractID(work, WorkExternalIdentifierType.HANDLE)); } } return item; } else throw new ParseException("Invalid Citation count"); } catch (IOException | ParseException e) { return null; } }
From source file:org.owasp.jbrofuzz.core.Database.java
/** * <p>// w w w . jav a 2 s .co m * Method responsible for creating a Fuzzer, based on an existing * prototypeId and length specified. * </p> * * @param prototypeId * prototypeId e.g 001-HTT-MTH or 032-SQL-INJ * @param len * The length of the fuzzer, used for recursive fuzzers * @return org.owasp.jbrofuzz.core#Fuzzer() * @throws NoSuchFuzzerException * * @see #containsPrototype(String) * * @author subere@uncon.org * @version 1.8 * @since 1.2 */ public Fuzzer createFuzzer(final String prototypeId, final int len) throws NoSuchFuzzerException { if (!containsPrototype(prototypeId)) { throw new NoSuchFuzzerException( StringUtils.abbreviate(prototypeId, 10) + " : No Such Fuzzer Found in the Database "); } return new Fuzzer(getPrototype(prototypeId), len); }
From source file:org.owasp.jbrofuzz.core.Database.java
public PowerFuzzer createPowerFuzzer(final String prototypeId, final int len, final int power) throws NoSuchFuzzerException { if (!containsPrototype(prototypeId)) { throw new NoSuchFuzzerException( StringUtils.abbreviate(prototypeId, 10) + "No Such Fuzzer Found in the Database "); }/* www . ja v a 2 s . c o m*/ return new PowerFuzzer(getPrototype(prototypeId), len, power); }