12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package de.tudarmstadt.informatik.hostage.format;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.ui.MainActivity;
- /**
- * Log view formatter used to format packet contents for the log view.
- * @author Wulf Pfeiffer
- */
- public class LogViewFormatter {
-
- /**
- * Formats the content of a packet giving it a specific format specified by a ProtocolFormatter.
- * @param protocol that is used for packet.
- * @param packet content.
- * @return formatted String of the packet content.
- */
- public static String format(String protocol, String packet) {
- return getFormatter(protocol).format(packet);
- }
-
- /**
- * Loads a ProtocolFormatter for a protocol.
- * If the protocol has its own formatter, with the name *protocol name*Formatter and it is located in .format package, it is loaded,
- * else a default formatter is loaded.
- * @param protocol that a formatter should be loaded for.
- * @return the loaded formatter.
- */
- private static ProtocolFormatter getFormatter(String protocol) {
- String[] protocols = MainActivity.getContext().getResources().getStringArray(R.array.protocols);
- String packageName = ProtocolFormatter.class.getPackage().getName();
- ProtocolFormatter formatter = new DefaultFormatter();
- for (String prot : protocols) {
- try {
- if(protocol.equals(prot)) formatter = (ProtocolFormatter) Class.forName(
- String.format("%s.%s", packageName, protocol+"Formatter"))
- .newInstance();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return formatter;
- }
- }
|