ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Zhou <...@alibaba.com>
Subject A logger issue
Date Fri, 31 May 2002 08:12:40 GMT
Hi,

I tried to echo some Non-English message in my build files.
For example:

  <echo message="some Chinese characters here!"/>

which replaces all Chinese character with a "?":

  [echo] ??????

I'm aware that it is PrintStream converts the Strings to bytes with the
system default character encoding (ISO-8859-1).  

Well, I decided to write my own logger instead of using the
ant's DefaultLogger.  The code comes here:

// ========================
// DefaultLogger.java
// ========================
package com.alibaba.toolkit.ant.loggers;

import org.apache.tools.ant.BuildEvent;
import java.io.PrintStream;

public class DefaultLogger extends org.apache.tools.ant.DefaultLogger {

    public void setErrorPrintStream(PrintStream err) {
        this.err = CharsetPrintStream.createWrapper(err);
    }

    public void setOutputPrintStream(PrintStream out) {
        this.out = CharsetPrintStream.createWrapper(out);
    }
}

// ========================
// CharsetPrintStream.java
// ========================
package com.alibaba.toolkit.ant.loggers;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

import org.apache.tools.ant.BuildEvent;

public class CharsetPrintStream extends PrintStream {
    private final static String SYSTEM_CHARSET  = new OutputStreamWriter(System.out).getEncoding();
    private String charset                      = SYSTEM_CHARSET;

    public static CharsetPrintStream createWrapper(OutputStream stream) {
        if (stream == null || stream instanceof CharsetPrintStream) {
            return (CharsetPrintStream) stream;
        }
        try {
            return new CharsetPrintStream(stream, System.getProperty("ant.logger.charset"));
        } catch (UnsupportedEncodingException e) {
            try {
                return new CharsetPrintStream(stream, SYSTEM_CHARSET);
            } catch (UnsupportedEncodingException e2) {
                return null;// should not occur
            }
        }
    }

    public CharsetPrintStream(OutputStream out, String charset)
             throws UnsupportedEncodingException {
        this(out, charset, false);
    }

    public CharsetPrintStream(OutputStream out, String charset, boolean autoFlush)
             throws UnsupportedEncodingException {
        super(out, autoFlush);

        if (charset != null && !charset.trim().equals("")) {
            this.charset = new OutputStreamWriter(out, charset).getEncoding();
        }
    }

    public void print(char c) {
        super.print(correctCharset(String.valueOf(c)));
    }

    public void print(char[] s) {
        super.print(correctCharset(String.valueOf(s)));
    }

    public void print(String s) {
        super.print(correctCharset(String.valueOf(s)));
    }

    public void print(Object obj) {
        super.print(correctCharset(String.valueOf(obj)));
    }

    private String correctCharset(String s) {
        if (s == null) {
            return null;
        }
        try {
            if (SYSTEM_CHARSET.equals(charset)) {
                return s;
            } else {
                return new String(s.getBytes(charset), SYSTEM_CHARSET);
            }
        } catch (UnsupportedEncodingException e) {
            return "should not occur!";
        }
    }
}

Now the echo task do the work:

# export ant.logger.charset=GBK
# ant -logger com.alibaba.toolkit.ant.loggers.DefaultLogger

But there's another problem! When I tried to describe my ant target in
Chinese, like this:

<target name="test" description="Chinese Description">
    ...
</target>

# ant -projecthelp
The console output is:
  
Buildfile: build.xml
Main targets:

test  ???????

Default target: test

That is because these messages are output by calling System.out.println,
and in turn captured and forwared by ant. The problem is that the
System.out/System.err converts the Non-English characters to "?" BEFORE
forward these messages to logger handler!

It's a big problem to me!

Why not use PrintWriter instead of PrintStream?  It's more natural to
Unicode and Java.


-- 
Michael Zhou <zyh@alibaba.com>



--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message