⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formattedprintstream.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
字号:
/*
 *  YALE - Yet Another Learning Environment
 *  Copyright (C) 2001-2004
 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, 
 *          Katharina Morik, Oliver Ritthoff
 *      Artificial Intelligence Unit
 *      Computer Science Department
 *      University of Dortmund
 *      44221 Dortmund,  Germany
 *  email: yale-team@lists.sourceforge.net
 *  web:   http://yale.cs.uni-dortmund.de/
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as 
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version. 
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA.
 */
package edu.udo.cs.yale.tools;

import java.io.PrintStream;
import java.io.OutputStream;

/** A <tt>PrintStream</tt> which formats each String/Object before it is printed according
 *  to the following simple rules: 
 *  <ul>
 *    <li><tt>$c</tt> starts a tag, <tt>^c</tt> ends a tag where <tt>c</tt> is one of
 *    <ul>
 *      <li><tt>u</tt> uppercase
 *      <li><tt>b</tt> bold type
 *      <li><tt>s</tt> letterspace
 *    </ul>
 *    <li>everything in between the start and end tag is replaced by a formatted String
 *    <li>tags can be nested, although a bold-tag appearing within another bold tag
 *        is useless and will result in unpredictable behaviour
 *    <li>It is useful if the bold tag is the innermost one, because it adds two additional
 *        characters for each readable character
 *    <li>All except the first line are indented, whitespace and certain special characters
 *        produce a newline if the linewidth is exceeded
 *  </ul>
 *  @author Simon
 *  @version $Id: FormattedPrintStream.java,v 2.4 2004/08/27 11:57:45 ingomierswa Exp $
 *  <h4>Bugs</h4>
 *  <tt>$</tt> and <tt>^</tt> can't be used as normal cahracters anymore.
 */
public class FormattedPrintStream extends PrintStream {

    private static final char[] SEPARATORS = {'.', ',', '-', '!', '?', ' '};
   
    private static final String F = "\10";
    private static final String FORMAT_TAG = "$";
    private static final String FORMAT_END = "^";

    private boolean ignore, indented;
    private int indent, linewidth;

    public FormattedPrintStream(OutputStream o) {
	this(o, false, 0, -1);
    }

    public FormattedPrintStream(OutputStream o, boolean format, int indent, int linewidth) {
	super(o, true);
	this.ignore = !format;
	this.indent = indent;
	this.linewidth = linewidth;
    }

    public void print(String string) {
	printIndented(format(string));
    }

    public void print(Object o) {
	printIndented(format(o.toString()));
    }

    public void print(char[] string) {
  	print(new String(string));
    }

    
    public final String format(String formatMe) {
	if (formatMe == null) return null;
	StringBuffer result = new StringBuffer();
	try {
	    int tag = 0;
	    int endtag = 0;
	    while ((tag = formatMe.indexOf(FORMAT_TAG, endtag)) != -1) {
		result.append(formatMe.substring(endtag, tag));
		char format = formatMe.charAt(tag+1);
		endtag = formatMe.indexOf(FORMAT_END+format, tag);
		if (endtag != -1) {
		    String substr = format(formatMe.substring(tag+2, endtag));
		    if (!ignore) {
			switch (format) {
			case 'b': result.append(fett(substr)); break;
			case 's': result.append(sperren(substr)); break;
			case 'u': result.append(uppercase(substr)); break;
			default:  result.append(substr); break;
			}
		    } else {
			result.append(substr);
		    }
		} else {
		    result.append(formatMe.substring(tag));
		    return result.toString();
		}
		endtag += FORMAT_END.length()+1;
	    }
	    result.append(formatMe.substring(endtag));
	} catch (Exception e) {
	    e.printStackTrace();
	    return formatMe;
	}
	return result.toString();
    }

    public static final String fett(String string) {
	char[] c = string.toCharArray();
	String result = "";
	for (int i=0; i < c.length; i++) {
	    result += c[i];
	    if (c[i] > ' ') {
		result += F;
		result += c[i];
	    }
	}
	return result;
    }


    public static final String sperren(String string) {
	char[] c = string.toCharArray();
	String result = "";
	for (int i=0; i < c.length; i++)
	    result += c[i] + (i < c.length-1 ? " " : "");
	return result;
    }

    public static final String uppercase(String string) {
	return string.toUpperCase();
    }

    private void printIndented(String str) {
	boolean first = true && !indented;
	int nl = -1;
	int s = 0;
	while ((nl = str.indexOf("\n", s)) >= 0) {
	    if (!first) super.print(indent(indent));
	    super.print(str.substring(s, nl));
	    super.println();
	    s = nl+1;
	    first = false;
	}
	if (!first) super.print(indent(indent));
	super.print(str.substring(s));
    }

    /** Returns a whitespace length indent. */
    public static String indent(int indent) {
	String s;
	for (s = ""; s.length() < indent; s += " ");
	return s;
    }

    public void setIndented(boolean indented) {
	this.indented = indented;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -