1 2 package org.jzonic.jlo; 3 4 import java.util.StringTokenizer ; 5 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 targetName; 21 22 25 protected Target() { 26 value = all.intValue(); 27 targetName = "all"; 28 } 29 30 31 33 protected Target(String name, int targets) { 34 value = targets; 35 targetName = name; 36 } 37 38 42 public int hashCode() { 43 return value; 44 } 45 49 public boolean equals(Object 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 64 public int intValue() { 65 return value; 66 } 67 68 69 71 public String getName() { 72 return targetName; 73 } 74 75 public static int parse(String line) { 76 if ( line == null ) { 78 return Target.all.intValue(); 79 } 80 line = line.toLowerCase(); 81 int value = 0; 82 StringTokenizer tokenizer = new StringTokenizer (line,","); 83 while ( tokenizer.hasMoreElements() ) { 84 String current = (String )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 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 144 public String toString() { 145 String retValue = "Target: name=" + targetName + " value=" + value; 146 return retValue; 147 } 148 } 149 | Popular Tags |