001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.util;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import java.io.ByteArrayOutputStream;
026: import java.io.File;
027: import java.io.PrintStream;
028: import java.lang.reflect.Field;
029: import java.lang.reflect.Method;
030: import java.util.Properties;
031:
032: /**
033: * Tidies up the java source code using Jalopy.
034: * This is used by both ADB and Codegen hence needs to be in the
035: * commons rather than a specific module
036: */
037: public class PrettyPrinter {
038: private static final Log log = LogFactory
039: .getLog(PrettyPrinter.class);
040:
041: /**
042: * Pretty prints contents of the java source file.
043: *
044: * @param file
045: */
046: public static void prettify(File file) {
047: // If the user has set "axis2.jalopy=false" on the system property,
048: // then just return back to caller
049: String property = System.getProperty("axis2.jalopy");
050: if (property != null && JavaUtils.isFalseExplicitly(property)) {
051: return;
052: }
053: PrintStream backupOutputStream = System.out;
054: PrintStream backupErrorStream = System.err;
055: System.setOut(new PrintStream(new ByteArrayOutputStream()));
056: System.setErr(new PrintStream(new ByteArrayOutputStream()));
057: try {
058: Class clazzConfigurator = Loader
059: .loadClass("org.apache.log4j.PropertyConfigurator");
060: Method configure = clazzConfigurator.getMethod("configure",
061: new Class[] { Properties.class });
062: Properties properties = new Properties();
063: properties.setProperty(
064: "log4j.logger.de.hunsicker.jalopy.io",
065: System.getProperty(
066: "log4j.logger.de.hunsicker.jalopy.io",
067: "FATAL"));
068: configure.invoke(null, new Object[] { properties });
069:
070: // Create an instance of the Jalopy bean
071: Class clazz = Loader
072: .loadClass("de.hunsicker.jalopy.Jalopy");
073: Object prettifier = clazz.newInstance();
074:
075: // Set the input file
076: Method input = clazz.getMethod("setInput",
077: new Class[] { File.class });
078: input.invoke(prettifier, new Object[] { file });
079:
080: // Set the output file
081: Method output = clazz.getMethod("setOutput",
082: new Class[] { File.class });
083: output.invoke(prettifier, new Object[] { file });
084:
085: Class clazz2 = Loader
086: .loadClass("de.hunsicker.jalopy.storage.Convention");
087: Method instance = clazz2.getMethod("getInstance",
088: new Class[] {});
089: Object settings = instance.invoke(null, new Object[] {});
090:
091: Class clazz3 = Loader
092: .loadClass("de.hunsicker.jalopy.storage.ConventionKeys");
093: Field field = clazz3.getField("COMMENT_FORMAT_MULTI_LINE");
094: Object key = field.get(null);
095: Method put = clazz2.getMethod("put", new Class[] {
096: key.getClass(), String.class });
097: put.invoke(settings, new Object[] { key, "true" });
098:
099: // format and overwrite the given input file
100: Method format = clazz.getMethod("format", new Class[] {});
101: format.invoke(prettifier, new Object[] {});
102: log.debug("Pretty printed file : " + file);
103: } catch (ClassNotFoundException e) {
104: log
105: .debug("Jalopy/Log4j not found - unable to pretty print "
106: + file);
107: } catch (Exception e) {
108: log.warn(
109: "Exception occurred while trying to pretty print file "
110: + file, e);
111: } catch (Throwable t) {
112: log.debug(
113: "Exception occurred while trying to pretty print file "
114: + file, t);
115: } finally {
116: System.setOut(backupOutputStream);
117: System.setErr(backupErrorStream);
118: }
119: }
120: }
|