001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.log4j.lf5.viewer.categoryexplorer;
018
019import org.apache.log4j.lf5.LogRecord;
020import org.apache.log4j.lf5.LogRecordFilter;
021
022import java.util.Enumeration;
023
024/**
025 * An implementation of LogRecordFilter based on a CategoryExplorerModel
026 *
027 * @author Richard Wan
028 */
029
030// Contributed by ThoughtWorks Inc.
031
032public class CategoryExplorerLogRecordFilter implements LogRecordFilter {
033  //--------------------------------------------------------------------------
034  //   Constants:
035  //--------------------------------------------------------------------------
036
037  //--------------------------------------------------------------------------
038  //   Protected Variables:
039  //--------------------------------------------------------------------------
040
041  protected CategoryExplorerModel _model;
042
043  //--------------------------------------------------------------------------
044  //   Private Variables:
045  //--------------------------------------------------------------------------
046
047  //--------------------------------------------------------------------------
048  //   Constructors:
049  //--------------------------------------------------------------------------
050
051  public CategoryExplorerLogRecordFilter(CategoryExplorerModel model) {
052    _model = model;
053  }
054
055  //--------------------------------------------------------------------------
056  //   Public Methods:
057  //--------------------------------------------------------------------------
058
059  /**
060   * @return true if the CategoryExplorer model specified at construction
061   * is accepting the category of the specified LogRecord.  Has a side-effect
062   * of adding the CategoryPath of the LogRecord to the explorer model
063   * if the CategoryPath is new.
064   */
065  public boolean passes(LogRecord record) {
066    CategoryPath path = new CategoryPath(record.getCategory());
067    return _model.isCategoryPathActive(path);
068  }
069
070  /**
071   * Resets the counters for the contained CategoryNodes to zero.
072   */
073  public void reset() {
074    resetAllNodes();
075  }
076
077  //--------------------------------------------------------------------------
078  //   Protected Methods:
079  //--------------------------------------------------------------------------
080
081  protected void resetAllNodes() {
082    Enumeration nodes = _model.getRootCategoryNode().depthFirstEnumeration();
083    CategoryNode current;
084    while (nodes.hasMoreElements()) {
085      current = (CategoryNode) nodes.nextElement();
086      current.resetNumberOfContainedRecords();
087      _model.nodeChanged(current);
088    }
089  }
090  //--------------------------------------------------------------------------
091  //   Private Methods:
092  //--------------------------------------------------------------------------
093
094  //--------------------------------------------------------------------------
095  //   Nested Top-Level Classes or Interfaces
096  //--------------------------------------------------------------------------
097}
098