JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / org / apache / log4j / config / PropertyPrinter.java
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
18 package org.apache.log4j.config;
19
20 import org.apache.log4j.Appender;
21 import org.apache.log4j.Category;
22 import org.apache.log4j.Level;
23 import org.apache.log4j.LogManager;
24 import org.apache.log4j.Logger;
25
26 import java.io.PrintWriter;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
29
30 /**
31    Prints the configuration of the log4j default hierarchy
32    (which needs to be auto-initialized) as a propoperties file
33    on a {@link PrintWriter}.
34    
35    @author  Anders Kristensen
36  */
37 public class PropertyPrinter implements PropertyGetter.PropertyCallback {
38   protected int numAppenders = 0;
39   protected Hashtable appenderNames = new Hashtable();
40   protected Hashtable layoutNames   = new Hashtable();
41   protected PrintWriter out;
42   protected boolean doCapitalize;
43   
44   public
45   PropertyPrinter(PrintWriter out) {
46     this(out, false);
47   }
48   
49   public
50   PropertyPrinter(PrintWriter out, boolean doCapitalize) {
51     this.out = out;
52     this.doCapitalize = doCapitalize;
53     
54     print(out);
55     out.flush();
56   }
57   
58   protected
59   String genAppName() {
60     return "A" + numAppenders++;
61   }
62   
63   /**
64    * Returns true if the specified appender name is considered to have
65    * been generated, that is, if it is of the form A[0-9]+.
66   */
67   protected
68   boolean isGenAppName(String name) {
69     if (name.length() < 2 || name.charAt(0) != 'A') {
70         return false;
71     }
72     
73     for (int i = 0; i < name.length(); i++) {
74       if (name.charAt(i) < '0' || name.charAt(i) > '9') {
75         return false;
76     }
77     }
78     return true;
79   }
80   
81   /**
82    * Prints the configuration of the default log4j hierarchy as a Java
83    * properties file on the specified Writer.
84    * 
85    * <p>N.B. print() can be invoked only once!
86    */
87   public
88   void print(PrintWriter out) {
89     printOptions(out, Logger.getRootLogger());
90     
91     Enumeration cats = LogManager.getCurrentLoggers();
92     while (cats.hasMoreElements()) {
93       printOptions(out, (Logger) cats.nextElement());
94     }
95   }
96   
97   /**
98    * @since 1.2.15
99    */
100   protected
101   void printOptions(PrintWriter out, Category cat) {
102     Enumeration appenders = cat.getAllAppenders();
103     Level prio = cat.getLevel();
104     String appenderString = (prio == null ? "" : prio.toString());
105     
106     while (appenders.hasMoreElements()) {
107       Appender app = (Appender) appenders.nextElement();
108       String name;
109       
110       if ((name = (String) appenderNames.get(app)) == null) {
111       
112         // first assign name to the appender
113         if ((name = app.getName()) == null || isGenAppName(name)) {
114             name = genAppName();
115         }
116         appenderNames.put(app, name);
117         
118         printOptions(out, app, "log4j.appender."+name);
119         if (app.getLayout() != null) {
120           printOptions(out, app.getLayout(), "log4j.appender."+name+".layout");
121         }
122       }
123       appenderString += ", " + name;
124     }
125     String catKey = (cat == Logger.getRootLogger())
126         ? "log4j.rootLogger"
127         : "log4j.logger." + cat.getName();
128     if (appenderString != "") {
129       out.println(catKey + "=" + appenderString);
130     }
131     if (!cat.getAdditivity() && cat != Logger.getRootLogger()) {
132         out.println("log4j.additivity." + cat.getName() + "=false");    
133     }
134   }
135
136   protected void printOptions(PrintWriter out, Logger cat) {
137       printOptions(out, (Category) cat);
138   }
139   
140   protected
141   void printOptions(PrintWriter out, Object obj, String fullname) {
142     out.println(fullname + "=" + obj.getClass().getName());
143     PropertyGetter.getProperties(obj, this, fullname + ".");
144   }
145   
146   public void foundProperty(Object obj, String prefix, String name, Object value) {
147     // XXX: Properties encode value.toString()
148     if (obj instanceof Appender && "name".equals(name)) {
149       return;
150     }
151     if (doCapitalize) {
152       name = capitalize(name);
153     }
154     out.println(prefix + name + "=" + value.toString());
155   }
156   
157   public static String capitalize(String name) {
158     if (Character.isLowerCase(name.charAt(0))) {
159       if (name.length() == 1 || Character.isLowerCase(name.charAt(1))) {
160         StringBuffer newname = new StringBuffer(name);
161         newname.setCharAt(0, Character.toUpperCase(name.charAt(0)));
162         return newname.toString();
163       }
164     }
165     return name;
166   }
167   
168   // for testing
169   public static void main(String[] args) {
170     new PropertyPrinter(new PrintWriter(System.out));
171   }
172 }