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. * Formats the content of packet. The packet content is represented as
  13. * string or hex, depending on the protocol.
  14. *
  15. * @param packet
  16. * Packet to format.
  17. * @return Formatted string.
  18. */
  19. public String format(String packet) {
  20. return packet;
  21. }
  22. /**
  23. * Loads a protocol formatter for a protocol. Load a class matching
  24. * {protocol} located in logging.formatter.protocol, default formatter
  25. * otherwise.
  26. *
  27. * @param protocolName
  28. * String representation of a protocol.
  29. * @return The protocol formatter.
  30. */
  31. public static ProtocolFormatter getFormatter(String protocolName) {
  32. String packageName = ProtocolFormatter.class.getPackage().getName();
  33. String className = String.format("%s.%s", packageName, protocolName);
  34. // TODO Auf Singletons umstellen und newInstance() sparen.
  35. try {
  36. return (ProtocolFormatter) Class.forName(className).newInstance();
  37. } catch (ReflectiveOperationException e) {
  38. return new ProtocolFormatter();
  39. }
  40. }
  41. }