All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.hsac.fitnesse.slim.converter.NumberedListConverter Maven / Gradle / Ivy

There is a newer version: 5.3.17
Show newest version
package nl.hsac.fitnesse.slim.converter;

import fitnesse.slim.Converter;
import fitnesse.slim.converters.ConverterRegistry;
import fitnesse.slim.converters.ElementConverterHelper;
import fitnesse.slim.converters.GenericCollectionConverter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Slim Converter which works with HTML ordered lists (i.e. numbered lists) instead of the standard Java
 * toString() representation for lists.
 */
public class NumberedListConverter extends GenericCollectionConverter> {
    private static final Pattern LIST_PATTERN = Pattern.compile(
                                                            "\\s*((
  • \\s*.*?\\s*
  • \\s*)*)", Pattern.DOTALL); private static final Converter OBJ_CONVERTER = new ObjectConverter(); /** * Makes NumberedListConverter the Converter Slim will uses for (Array)Lists. */ public static void register() { try { Class> listObjectClass; listObjectClass = (Class>) NumberedListConverter.class .getMethod("getString", List.class).getParameterTypes()[0]; NumberedListConverter converter = new NumberedListConverter(); ConverterRegistry.addConverter(listObjectClass, converter); ConverterRegistry.addConverter(ArrayList.class, new NumberedArrayListConverter(converter)); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } /** * NumberedListConverter cannot implement Converter for List and ArrayList, therefore * this inner class ensures we have an implementation of ArrayList also. */ public static class NumberedArrayListConverter implements Converter { private final NumberedListConverter numberedListConverter; public NumberedArrayListConverter(NumberedListConverter aNumberedListConverter) { numberedListConverter = aNumberedListConverter; } @Override public String toString(ArrayList o) { return numberedListConverter.toString(o); } @Override public ArrayList fromString(String arg) { return (ArrayList) numberedListConverter.fromString(arg); } } public NumberedListConverter() { super(ArrayList.class, OBJ_CONVERTER); } @Override public String getString(List list) { StringBuilder messageList = new StringBuilder("
      "); for (Object element : list) { messageList.append("
    1. "); String formattedElement = ElementConverterHelper.elementToString(element); messageList.append(formattedElement); messageList.append("
    2. "); } messageList.append("
    "); return messageList.toString(); } @Override public ArrayList getObject(String arg) { ArrayList result; Matcher matcher = LIST_PATTERN.matcher(arg); if (matcher.matches()) { result = new ArrayList<>(); String items = matcher.group(2); if (!"".equals(items)) { items = items.replaceFirst("^\\s*
  • \\s*", ""); items = items.replaceFirst("\\s*
  • \\s*$", ""); String[] elements = items.split("\\s*\\s*
  • \\s*"); result.addAll(Arrays.asList(elements)); } } else { result = new ArrayList<>(super.getObject(arg)); } return result; } }