protected void initialize()

in src/main/java/org/apache/log4j/varia/LogFilePatternReceiver.java [660:793]


    protected void initialize() {
        if (host == null && path == null) {
            try {
                URL url = new URL(fileURL);
                host = url.getHost();
                path = url.getPath();
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        if (host == null || host.trim().equals("")) {
            host = DEFAULT_HOST;
        }
        if (path == null || path.trim().equals("")) {
            path = fileURL;
        }

        currentMap = new HashMap();
        additionalLines = new ArrayList<>();
        matchingKeywords = new ArrayList<>();

        if (timestampFormat != null) {
            dateFormat = new SimpleDateFormat(quoteTimeStampChars(timestampFormat));
            timestampPatternText = convertTimestamp();
        }
        //if custom level definitions exist, parse them
        updateCustomLevelDefinitionMap();
        try {
            if (filterExpression != null) {
                expressionRule = ExpressionRule.getRule(filterExpression);
            }
        } catch (Exception e) {
            logger.warn("Invalid filter expression: " + filterExpression, e);
        }

        List<String> buildingKeywords = new ArrayList<>();

        String newPattern = logFormat;

        //process newlines - (NL) - in the logFormat - before processing properties
        int index = 0;
        while (index > -1) {
            index = newPattern.indexOf(NEWLINE);
            if (index > -1) {
                //keep track of number of expected newlines in the format, so the lines can be concatenated prior to matching
                lineCount++;
                newPattern = singleReplace(newPattern, NEWLINE, NEWLINE_REGEXP);
            }
        }

        String current = newPattern;
        //build a list of property names and temporarily replace the property with an empty string,
        //we'll rebuild the pattern later
        List<String> propertyNames = new ArrayList<>();
        index = 0;
        while (index > -1) {
            if (current.contains(PROP_START) && current.contains(PROP_END)) {
                index = current.indexOf(PROP_START);
                String longPropertyName = current.substring(current.indexOf(PROP_START), current.indexOf(PROP_END) + 1);
                String shortProp = getShortPropertyName(longPropertyName);
                buildingKeywords.add(shortProp);
                propertyNames.add(longPropertyName);
                current = current.substring(longPropertyName.length() + 1 + index);
                newPattern = singleReplace(newPattern, longPropertyName, Integer.toString(buildingKeywords.size() - 1));
            } else {
                //no properties
                index = -1;
            }
        }

        /*
         * we're using a treemap, so the index will be used as the key to ensure
         * keywords are ordered correctly
         *
         * examine pattern, adding keywords to an index-based map patterns can
         * contain only one of these per entry...properties are the only 'keyword'
         * that can occur multiple times in an entry
         */
        for (Object keyword1 : keywords) {
            String keyword = (String) keyword1;
            int index2 = newPattern.indexOf(keyword);
            if (index2 > -1) {
                buildingKeywords.add(keyword);
                newPattern = singleReplace(newPattern, keyword, Integer.toString(buildingKeywords.size() - 1));
            }
        }

        StringBuilder buildingInt = new StringBuilder();

        for (int i = 0; i < newPattern.length(); i++) {
            String thisValue = String.valueOf(newPattern.substring(i, i + 1));
            if (isInteger(thisValue)) {
                buildingInt.append(thisValue);
            } else {
                String stringInt = buildingInt.toString();
                if (isInteger(stringInt)) {
                    matchingKeywords.add(buildingKeywords.get(Integer.parseInt(stringInt)));
                }
                //reset
                buildingInt.setLength(0);
            }
        }

        //if the very last value is an int, make sure to add it
        String stringInt = buildingInt.toString();
        if (isInteger(stringInt)) {
            matchingKeywords.add(buildingKeywords.get(Integer.parseInt(stringInt)));
        }

        newPattern = replaceMetaChars(newPattern);

        //compress one or more spaces in the pattern into the [ ]+ regexp
        //(supports padding of level in log files)
        newPattern = newPattern.replaceAll(MULTIPLE_SPACES_REGEXP, MULTIPLE_SPACES_REGEXP);
        newPattern = newPattern.replaceAll(Pattern.quote(PATTERN_WILDCARD), REGEXP_DEFAULT_WILDCARD);
        //use buildingKeywords here to ensure correct order
        for (int i = 0; i < buildingKeywords.size(); i++) {
            String keyword = buildingKeywords.get(i);
            //make the final keyword greedy (we're assuming it's the message)
            if (i == (buildingKeywords.size() - 1)) {
                newPattern = singleReplace(newPattern, String.valueOf(i), GREEDY_GROUP);
            } else if (TIMESTAMP.equals(keyword)) {
                newPattern = singleReplace(newPattern, String.valueOf(i), "(" + timestampPatternText + ")");
            } else if (LOGGER.equals(keyword) || LEVEL.equals(keyword)) {
                newPattern = singleReplace(newPattern, String.valueOf(i), NOSPACE_GROUP);
            } else {
                newPattern = singleReplace(newPattern, String.valueOf(i), DEFAULT_GROUP);
            }
        }

        regexp = newPattern;
        logger.debug("regexp is " + regexp);
    }