View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.log4j.lf5.viewer.categoryexplorer;
    18
    19  import org.apache.log4j.lf5.LogRecord;
    20  import org.apache.log4j.lf5.LogRecordFilter;
    21
    22  import java.util.Enumeration;
    23
    24  /***
    25   * An implementation of LogRecordFilter based on a CategoryExplorerModel
    26   *
    27   * @author Richard Wan
    28   */
    29
    30  // Contributed by ThoughtWorks Inc.
    31
    32  public class CategoryExplorerLogRecordFilter implements LogRecordFilter {
    33    //--------------------------------------------------------------------------
    34    //   Constants:
    35    //--------------------------------------------------------------------------
    36
    37    //--------------------------------------------------------------------------
    38    //   Protected Variables:
    39    //--------------------------------------------------------------------------
    40
    41    protected CategoryExplorerModel _model;
    42
    43    //--------------------------------------------------------------------------
    44    //   Private Variables:
    45    //--------------------------------------------------------------------------
    46
    47    //--------------------------------------------------------------------------
    48    //   Constructors:
    49    //--------------------------------------------------------------------------
    50
    51    public CategoryExplorerLogRecordFilter(CategoryExplorerModel model) {
    52      _model = model;
    53    }
    54
    55    //--------------------------------------------------------------------------
    56    //   Public Methods:
    57    //--------------------------------------------------------------------------
    58
    59    /***
    60     * @return true if the CategoryExplorer model specified at construction
    61     * is accepting the category of the specified LogRecord.  Has a side-effect
    62     * of adding the CategoryPath of the LogRecord to the explorer model
    63     * if the CategoryPath is new.
    64     */
    65    public boolean passes(LogRecord record) {
    66      CategoryPath path = new CategoryPath(record.getCategory());
    67      return _model.isCategoryPathActive(path);
    68    }
    69
    70    /***
    71     * Resets the counters for the contained CategoryNodes to zero.
    72     */
    73    public void reset() {
    74      resetAllNodes();
    75    }
    76
    77    //--------------------------------------------------------------------------
    78    //   Protected Methods:
    79    //--------------------------------------------------------------------------
    80
    81    protected void resetAllNodes() {
    82      Enumeration nodes = _model.getRootCategoryNode().depthFirstEnumeration();
    83      CategoryNode current;
    84      while (nodes.hasMoreElements()) {
    85        current = (CategoryNode) nodes.nextElement();
    86        current.resetNumberOfContainedRecords();
    87        _model.nodeChanged(current);
    88      }
    89    }
    90    //--------------------------------------------------------------------------
    91    //   Private Methods:
    92    //--------------------------------------------------------------------------
    93
    94    //--------------------------------------------------------------------------
    95    //   Nested Top-Level Classes or Interfaces
    96    //--------------------------------------------------------------------------
    97  }
    98