001: /**
002: * Copyright (C) 2001-2003 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.util.monolog.wrapper.common;
018:
019: import org.objectweb.util.monolog.api.Level;
020: import org.objectweb.util.monolog.api.LevelFactory;
021:
022: import java.util.StringTokenizer;
023: import java.io.Serializable;
024:
025: /**
026: * This class is the basic implementation of the Level interface. It proviedes
027: * also a static method 'evaluate' which permits to obtain the integer value
028: * of a level expression.
029: *
030: * @author Sebastien Chassande-Barrioz
031: */
032: public class LevelImpl implements Level, Serializable {
033: int value = 0;
034: String name = null;;
035: String stringValue = null;
036:
037: public LevelImpl(String n, int val) {
038: value = val;
039: name = n;
040: stringValue = String.valueOf(val);
041: }
042:
043: public LevelImpl(String n, String val, LevelFactory lf) {
044: //System.out.println("LevelImpl("+n+", "+val+", lf)");
045: stringValue = val;
046: value = evaluate(val, lf);
047: name = n;
048: }
049:
050: /**
051: * It retrieves the integer value of the level.
052: */
053: public int hashCode() {
054: return value;
055: }
056:
057: /**
058: * It retrieves the string expression of the level. ex: 'DEBUG + 1'
059: */
060: public String getStringValue() {
061: return stringValue;
062: }
063:
064: /**
065: * It analyzes a string expression to obtain its integer value. The allowed
066: * expression type are the following:
067: * <ul>
068: * <li>an integer value</li>
069: * <li>another level name</li>
070: * <li>levelName + integerValue</li>
071: * <li>levelName - integerValue</li>
072: * </ul>
073: * @param expr is the string expression which must be evaluated.
074: * @param lf is the LevelFactory which permits to obtain the referenced level.
075: * @return an integer value or 0 if it is impossible to evaluate the
076: * expression.
077: */
078: public static int evaluate(String expr, LevelFactory lf) {
079: int firstOperande = 0;
080: int secondOperande = 0;
081: byte operator = 0; // 1=>+ 2=>-
082: for (StringTokenizer st = new StringTokenizer(expr, " +-", true); st
083: .hasMoreTokens();) {
084:
085: String elem = st.nextToken();
086: if (Character.isDigit(elem.charAt(0))) {
087: try {
088: secondOperande = Integer.parseInt(elem);
089: } catch (NumberFormatException e) {
090: continue;
091: }
092: } else if (Character.isLetter(elem.charAt(0))) {
093: // The token is another intermediate level
094: Level l = lf.getLevel(elem);
095: if (l == null) {
096: l = lf.getLevel(elem.toUpperCase());
097: if (l == null) {
098: return 0;
099: }
100: }
101: firstOperande = l.getIntValue();
102: } else if (elem.charAt(0) == '+') {
103: operator = 1;
104: } else if (elem.charAt(0) == '-') {
105: operator = 2;
106: } else if (Character.isSpaceChar(elem.charAt(0))) {
107: continue;
108: }
109: }
110: if (firstOperande != 0) {
111: if (secondOperande != 0 && operator != 0) {
112: switch (operator) {
113: case 1:
114: return firstOperande + secondOperande;
115: case 2:
116: return firstOperande - secondOperande;
117: }
118: } else {
119: return firstOperande;
120: }
121: } else if (secondOperande != 0) {
122: return secondOperande;
123: }
124: return 0;
125: }
126:
127: public String toString() {
128: return "(name=" + name + ", val=" + value + ", sval="
129: + stringValue + ")";
130: }
131:
132: // IMPLEMENTATION OF THE Level INTERFACE //
133: //---------------------------------------//
134:
135: public boolean isComparableWith(Level o) {
136: return o instanceof LevelImpl;
137: }
138:
139: public int compareTo(Level o) {
140: return value - o.getIntValue();
141: }
142:
143: public int getIntValue() {
144: return value;
145: }
146:
147: public String getName() {
148: return name;
149: }
150:
151: public void setName(String n) {
152: name = n;
153: }
154: }
|