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 java.util.LinkedList;
20  import java.util.StringTokenizer;
21  
22  /**
23   * CategoryPath is a collection of CategoryItems which represent a
24   * path of categories.
25   *
26   * @author Michael J. Sikorsky
27   * @author Robert Shaw
28   */
29  
30  // Contributed by ThoughtWorks Inc.
31  
32  public class CategoryPath {
33    //--------------------------------------------------------------------------
34    //   Constants:
35    //--------------------------------------------------------------------------
36  
37    //--------------------------------------------------------------------------
38    //   Protected Variables:
39    //--------------------------------------------------------------------------
40    protected LinkedList _categoryElements = new LinkedList();
41  
42    //--------------------------------------------------------------------------
43    //   Private Variables:
44    //--------------------------------------------------------------------------
45  
46    //--------------------------------------------------------------------------
47    //   Constructors:
48    //--------------------------------------------------------------------------
49  
50    public CategoryPath() {
51      super();
52    }
53  
54    /**
55     * Construct a CategoryPath.  If the category is null, it defaults to "Debug".
56     */
57    public CategoryPath(String category) {
58      String processedCategory = category;
59  
60      if (processedCategory == null) {
61        processedCategory = "Debug";
62      }
63  
64      processedCategory = processedCategory.replace('/', '.');
65      processedCategory = processedCategory.replace('\\', '.');
66  
67      StringTokenizer st = new StringTokenizer(processedCategory, ".");
68      while (st.hasMoreTokens()) {
69        String element = st.nextToken();
70        addCategoryElement(new CategoryElement(element));
71      }
72    }
73  
74    //--------------------------------------------------------------------------
75    //   Public Methods:
76    //--------------------------------------------------------------------------
77  
78    /**
79     * returns the number of CategoryElements.
80     */
81    public int size() {
82      int count = _categoryElements.size();
83  
84      return (count);
85    }
86  
87    public boolean isEmpty() {
88      boolean empty = false;
89  
90      if (_categoryElements.size() == 0) {
91        empty = true;
92      }
93  
94      return (empty);
95    }
96  
97  
98    /**
99     * 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 }