LogViewFormatter.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package de.tudarmstadt.informatik.hostage.format;
  2. import de.tudarmstadt.informatik.hostage.R;
  3. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  4. /**
  5. * Log view formatter used to format packet contents for the log view.
  6. * @author Wulf Pfeiffer
  7. */
  8. public class LogViewFormatter {
  9. /**
  10. * Formats the content of a packet giving it a specific format specified by a ProtocolFormatter.
  11. * @param protocol that is used for packet.
  12. * @param packet content.
  13. * @return formatted String of the packet content.
  14. */
  15. public static String format(String protocol, String packet) {
  16. return getFormatter(protocol).format(packet);
  17. }
  18. /**
  19. * Loads a ProtocolFormatter for a protocol.
  20. * If the protocol has its own formatter, with the name *protocol name*Formatter and it is located in .format package, it is loaded,
  21. * else a default formatter is loaded.
  22. * @param protocol that a formatter should be loaded for.
  23. * @return the loaded formatter.
  24. */
  25. private static ProtocolFormatter getFormatter(String protocol) {
  26. String[] protocols = MainActivity.getContext().getResources().getStringArray(R.array.protocols);
  27. String packageName = ProtocolFormatter.class.getPackage().getName();
  28. ProtocolFormatter formatter = new DefaultFormatter();
  29. for (String prot : protocols) {
  30. try {
  31. if(protocol.equals(prot)) formatter = (ProtocolFormatter) Class.forName(
  32. String.format("%s.%s", packageName, protocol+"Formatter"))
  33. .newInstance();
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. return formatter;
  39. }
  40. }