ProtocolFormatter.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. // TODO Auf Singletons umstellen und newInstance() sparen.
  24. try {
  25. return (ProtocolFormatter) Class.forName(className).newInstance();
  26. } catch (ReflectiveOperationException e) {
  27. return new ProtocolFormatter();
  28. }
  29. }
  30. /**
  31. * Formats the content of packet. The packet content is represented as
  32. * string or hex, depending on the protocol.
  33. *
  34. * @param packet
  35. * Packet to format.
  36. * @return Formatted string.
  37. */
  38. public String format(String packet) {
  39. return packet;
  40. }
  41. }