ProtocolFormatter.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package de.tudarmstadt.informatik.hostage.logging.formatter.protocol;
  2. /**
  3. * Protocol formatter. This class provides functionality to format the packet as
  4. * a human-readable string of a specific protocol. Custom formatters should
  5. * inherit from this class.
  6. *
  7. * @author Wulf Pfeiffer
  8. * @author Mihai Plasoianu
  9. */
  10. public class ProtocolFormatter {
  11. /**
  12. * Loads a protocol formatter for a protocol. Load a class matching
  13. * {protocol} located in logging.formatter.protocol, default formatter
  14. * otherwise.
  15. *
  16. * @param protocolName
  17. * String representation of a protocol.
  18. * @return The protocol formatter.
  19. */
  20. public static ProtocolFormatter getFormatter(String protocolName) {
  21. String packageName = ProtocolFormatter.class.getPackage().getName();
  22. String className = String.format("%s.%s", packageName, protocolName);
  23. try {
  24. return (ProtocolFormatter) Class.forName(className).newInstance();
  25. } catch (InstantiationException e) {
  26. return new ProtocolFormatter();
  27. } catch (IllegalAccessException e) {
  28. return new ProtocolFormatter();
  29. } catch (ClassNotFoundException e) {
  30. return new ProtocolFormatter();
  31. }
  32. }
  33. /**
  34. * Formats the content of packet. The packet content is represented as
  35. * string or hex, depending on the protocol.
  36. *
  37. * @param packet
  38. * Packet to format.
  39. * @return Formatted string.
  40. */
  41. public String format(String packet) {
  42. return String.format("%s\n", packet);
  43. }
  44. }