KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > Target


1
2 package org.jzonic.jlo;
3
4 import java.util.StringTokenizer JavaDoc;
5 /**
6  */

7 public class Target {
8
9     public static final Target off = new Target ("off",0 );
10     public static final Target trace = new Target ("trace",1 );
11     public static final Target info = new Target ("info",2 );
12     public static final Target debug = new Target ("debug",4 );
13     public static final Target warn = new Target ("warn",8 );
14     public static final Target error = new Target ("error",16 );
15     public static final Target fatal = new Target ("fatal",32 );
16     public static final Target all = new Target("all",255);
17
18     private Target target;
19     private int value;
20     private String JavaDoc targetName;
21     
22     /**
23      * Constructor for the Target object
24      */

25     protected Target() {
26         value = all.intValue();
27         targetName = "all";
28     }
29
30
31     /**
32      */

33     protected Target(String JavaDoc name, int targets) {
34         value = targets;
35         targetName = name;
36     }
37         
38     /**
39      * Generate the hashcode
40      *
41      */

42     public int hashCode() {
43         return value;
44     }
45     /**
46      * Compare two objects for value equality.
47      *
48      */

49     public boolean equals(Object JavaDoc ox) {
50         if (!(ox instanceof Target)) {
51             return false;
52         }
53
54         Target mytarget = (Target) ox;
55
56         if (mytarget.intValue() == value && (mytarget.getName().equals(targetName))) {
57             return true;
58         } else {
59             return false;
60         }
61     }
62     /**
63      */

64     public int intValue() {
65         return value;
66     }
67
68
69     /**
70      */

71     public String JavaDoc getName() {
72         return targetName;
73     }
74     
75     public static int parse(String JavaDoc line) {
76         // if nothing found then set ALL
77
if ( line == null ) {
78             return Target.all.intValue();
79         }
80         line = line.toLowerCase();
81         int value = 0;
82         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(line,",");
83         while ( tokenizer.hasMoreElements() ) {
84             String JavaDoc current = (String JavaDoc)tokenizer.nextElement();
85             if ( current.equals("all") ) {
86                 value = Target.all.intValue();
87             }
88             else if ( current.equals("off") ) {
89                 return 0;
90             }
91             else if ( current.startsWith("!")) {
92                 int targetValue = getTargetByName(current);
93                 if ( targetValue != -1) {
94                     if ( (value & targetValue) == targetValue) {
95                         value -= targetValue;
96                     }
97                 }
98             }
99             else {
100                 int targetValue = getTargetByName(current);
101                 if ( targetValue != -1) {
102                     if ( (value & targetValue) == 0 ) {
103                         value += targetValue;
104                     }
105                 }
106             }
107         }
108         return value;
109     }
110         
111     private static int getTargetByName(String JavaDoc name) {
112         if ( name.startsWith("!") ) {
113             name = name.substring(1);
114         }
115             
116         if ( name.equals("trace") ) {
117             return Target.trace.intValue();
118         }
119     
120         if ( name.equals("info") ) {
121             return Target.info.intValue();
122         }
123     
124         if ( name.equals("debug") ) {
125             return Target.debug.intValue();
126         }
127     
128         if ( name.equals("warn") ) {
129             return Target.warn.intValue();
130         }
131     
132         if ( name.equals("error") ) {
133             return Target.error.intValue();
134         }
135         
136         if ( name.equals("fatal") ) {
137             return Target.fatal.intValue();
138         }
139         
140         return -1;
141     }
142     /**
143      */

144     public String JavaDoc toString() {
145         String JavaDoc retValue = "Target: name=" + targetName + " value=" + value;
146         return retValue;
147     }
148 }
149   
Popular Tags