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 java.util.LinkedList;
020import java.util.StringTokenizer;
021
022/**
023 * CategoryPath is a collection of CategoryItems which represent a
024 * path of categories.
025 *
026 * @author Michael J. Sikorsky
027 * @author Robert Shaw
028 */
029
030// Contributed by ThoughtWorks Inc.
031
032public class CategoryPath {
033  //--------------------------------------------------------------------------
034  //   Constants:
035  //--------------------------------------------------------------------------
036
037  //--------------------------------------------------------------------------
038  //   Protected Variables:
039  //--------------------------------------------------------------------------
040  protected LinkedList _categoryElements = new LinkedList();
041
042  //--------------------------------------------------------------------------
043  //   Private Variables:
044  //--------------------------------------------------------------------------
045
046  //--------------------------------------------------------------------------
047  //   Constructors:
048  //--------------------------------------------------------------------------
049
050  public CategoryPath() {
051    super();
052  }
053
054  /**
055   * Construct a CategoryPath.  If the category is null, it defaults to "Debug".
056   */
057  public CategoryPath(String category) {
058    String processedCategory = category;
059
060    if (processedCategory == null) {
061      processedCategory = "Debug";
062    }
063
064    processedCategory = processedCategory.replace('/', '.');
065    processedCategory = processedCategory.replace('\\', '.');
066
067    StringTokenizer st = new StringTokenizer(processedCategory, ".");
068    while (st.hasMoreTokens()) {
069      String element = st.nextToken();
070      addCategoryElement(new CategoryElement(element));
071    }
072  }
073
074  //--------------------------------------------------------------------------
075  //   Public Methods:
076  //--------------------------------------------------------------------------
077
078  /**
079   * returns the number of CategoryElements.
080   */
081  public int size() {
082    int count = _categoryElements.size();
083
084    return (count);
085  }
086
087  public boolean isEmpty() {
088    boolean empty = false;
089
090    if (_categoryElements.size() == 0) {
091      empty = true;
092    }
093
094    return (empty);
095  }
096
097
098  /**
099   * Removes all categoryElements.
100   */
101  public void removeAllCategoryElements() {
102    _categoryElements.clear();
103  }
104
105  /**
106   * Adds the specified categoryElement to the end of the categoryElement set.
107   */
108  public void addCategoryElement(CategoryElement categoryElement) {
109    _categoryElements.addLast(categoryElement);
110  }
111
112  /**
113   * Returns the CategoryElement at the specified index.
114   */
115  public CategoryElement categoryElementAt(int index) {
116    return ((CategoryElement) _categoryElements.get(index));
117  }
118
119
120  public String toString() {
121    StringBuffer out = new StringBuffer(100);
122
123    out.append("\n");
124    out.append("===========================\n");
125    out.append("CategoryPath:                   \n");
126    out.append("---------------------------\n");
127
128    out.append("\nCategoryPath:\n\t");
129
130    if (this.size() > 0) {
131      for (int i = 0; i < this.size(); i++) {
132        out.append(this.categoryElementAt(i).toString());
133        out.append("\n\t");
134      }
135    } else {
136      out.append("<<NONE>>");
137    }
138
139    out.append("\n");
140    out.append("===========================\n");
141
142    return (out.toString());
143  }
144
145  //--------------------------------------------------------------------------
146  //   Protected Methods:
147  //--------------------------------------------------------------------------
148
149  //--------------------------------------------------------------------------
150  //   Private Methods:
151  //--------------------------------------------------------------------------
152
153  //--------------------------------------------------------------------------
154  //   Nested Top-Level Classes or Interfaces:
155  //--------------------------------------------------------------------------
156
157}