private static PatternConverter createConverter()

in src/main/java/org/apache/log4j/pattern/PatternParser.java [421:513]


  private static PatternConverter createConverter(
    final String converterId, final StringBuffer currentLiteral,
    final Map converterRegistry, final Map rules, final List options) {
    String converterName = converterId;
    Object converterObj = null;

    for (int i = converterId.length(); (i > 0) && (converterObj == null);
        i--) {
      converterName = converterName.substring(0, i);

      if (converterRegistry != null) {
        converterObj = converterRegistry.get(converterName);
      }

      if ((converterObj == null) && (rules != null)) {
        converterObj = rules.get(converterName);
      }
    }

    if (converterObj == null) {
        LogLog.error("Unrecognized format specifier [" + converterId + "]");

      return null;
    }

    Class converterClass = null;

    if (converterObj instanceof Class) {
      converterClass = (Class) converterObj;
    } else {
      if (converterObj instanceof String) {
        try {
          converterClass = Loader.loadClass((String) converterObj);
        } catch (ClassNotFoundException ex) {
            LogLog.warn(
              "Class for conversion pattern %" + converterName + " not found",
              ex);

          return null;
        }
      } else {
          LogLog.warn(
            "Bad map entry for conversion pattern %" +  converterName + ".");

        return null;
      }
    }

    try {
      Method factory =
        converterClass.getMethod(
          "newInstance",
          new Class[] {
            Class.forName("[Ljava.lang.String;")
          });
      String[] optionsArray = new String[options.size()];
      optionsArray = (String[]) options.toArray(optionsArray);

      Object newObj =
        factory.invoke(null, new Object[] { optionsArray });

      if (newObj instanceof PatternConverter) {
        currentLiteral.delete(
          0,
          currentLiteral.length()
          - (converterId.length() - converterName.length()));

        return (PatternConverter) newObj;
      } else {
          LogLog.warn(
            "Class " + converterClass.getName()
            + " does not extend PatternConverter.");
      }
    } catch (Exception ex) {
        LogLog.error("Error creating converter for " + converterId, ex);

      try {
        //
        //  try default constructor
        PatternConverter pc = (PatternConverter) converterClass.newInstance();
        currentLiteral.delete(
          0,
          currentLiteral.length()
          - (converterId.length() - converterName.length()));

        return pc;
      } catch (Exception ex2) {
          LogLog.error("Error creating converter for " + converterId, ex2);
      }
    }

    return null;
  }