z
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class EvalSample 
{
	public static void main(String[] args) 
	throws Exception
	{
    
    	String expression = "function f(param) { return (param > 10); } f(ext_param_1);";
        
        expression = expression.replace("ext_param_1","12");
        
		ScriptEngine e = new ScriptEngineManager().getEngineByName("js");
		
		Object res = e.eval(expression);

		System.out.println(res); //true
	}
}

 

AND


// Dynamic Proxy나 AOP 대체로 가능할 듯.


(annotation 정의)
import java.lang.annotation.*;

@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface MyAnnotation{
	String value() default "MyAnnotation";
}

(annotation 적용확인)

import java.lang.reflect.Method;
import MyAnnotation;

public class UseAnnotation{

	@MyAnnotation(value="Annotation Test")
	public void annotationTest(){
		System.out.println("Annotation Method");
	}

	public static void main(String[] args){
	
		Method[] ms = UseAnnotation.class.getMethods();

		for(Method m: methods){
			MyAnnotation an = m.getAnnotation(MyAnnotation.class);
			
			if(an != null){
				System.out.println("ANNOTATION : "+an.value());
				m.invoke(new UseAnnotation(), null);
			}
		}
	}

}

AND


public static String toFullChar(String src)
    {
        // 입력된 스트링이 null 이면 null 을 리턴
        if (src == null)
            return null;
        // 변환된 문자들을 쌓아놓을 StringBuffer 를 마련한다
        StringBuffer strBuf = new StringBuffer();
        char c = 0;
        int nSrcLength = src.length();
        for (int i = 0; i < nSrcLength; i++)
        {
            c = src.charAt(i);
            //영문이거나 특수 문자 일경우.
            if (c >= 0x21 && c <= 0x7e)
            {
                c += 0xfee0;
            }
            //공백일경우
            else if (c == 0x20)
            {
                c = 0x3000;
            }
            // 문자열 버퍼에 변환된 문자를 쌓는다
            strBuf.append(c);
        }
        return strBuf.toString();
    }
    public static String toHalfChar(String src)
    {
        StringBuffer strBuf = new StringBuffer();
        char c = 0;
        int nSrcLength = src.length();
        for (int i = 0; i < nSrcLength; i++)
        {
            c = src.charAt(i);
            //영문이거나 특수 문자 일경우.
            if (c >= '!' && c <= '~')
            {
                c -= 0xfee0;
            }
            else if (c == ' ')
            {
                c = 0x20;
            }
            // 문자열 버퍼에 변환된 문자를 쌓는다
            strBuf.append(c);
        }
        return strBuf.toString();
    } 


반각문자열로 부터 전각 데이터를 얻을 떄:

String str = toFullChar("홍0A 길 ");

byte[] data = str.getBytes("KSC5601");


전각배열로 부터 반각 데이터를 얻을 때:

String strHalf= toHalfChar(new String(binary,offset,length,"KSC5601"));

byte[] data = strHalf.getBytes("KSC5601");
 

출처: 
http://www.javaservice.net/~java/bbs/read.cgi?m=resource&b=qna2&c=r_p&n=1069148174&k=%EC%A0%84%EA%B0%81&d=tb#1069148174   

 
AND


JAVA에서 BufferedImage 등의 Image 관련 함수 이용 시 "Can't connect to X11 window server..." 라고 에러 뜰때.. 
원인은 Graphics의 createGraphics에서 X의 자원을 이용하지 않음에도 X로 연결하려고 지롤을 떨어서 그런거 같음. 
VM 실행 시 파라미터로 -Djava.awt.headless=true 옵션으로 처리하면 됨.

그리고, 아래는 리눅스에서 윈도우 폰트 이용하는 방법 퍼온 것.

출처는 요기:http://mindconverter.tistory.com/18

윈도우에 있는 폰트를 Linux Java에서 사용하기 위한 내용에 대해서 정리하였다.

우선 윈도우 폰트를 찾아보자
 c:\windows\fonts
속에는 많은 폰트가 들어있다...

우선 윈도우에 있는 이 많은 폰트들 중 필요한 것을 골라서
linux의
 $JAVA_HOME/jre/lib/fonts
로 복사한다.

필자는 gulim.ttc, batang.ttc 를 복사하였다.

그 후에
 $JAVA_HOME/jre/lib/fonts
폴더에 보면
 fonts.dir
이란 파일이 있다.

요 파일을 수정이 가능하게 모드를 변경후에
 chmod 664 fonts.dir - 자신과 그룹만 쓰기 가능하게..

수정을 한다.

파일의 상단에 있는 숫자는 폰트의 개수를 나타낸다.
따라서 +2를 해줘야겠지????

이후 맨 아래다가 2줄 추가한다.
batang.ttc -ms-batang-medium-r-normal--0-0-0-0-c-0-ksc5601.1987-0
gulim.ttc -ms-gulim-medium-r-normal--0-0-0-0-c-0-ksc5601.1987-0

요렇게...

이럼 폰트를 java에서 불러쓸 수 있더라...
당근 fonts.dir은 기존 대로 돌려놓자
 chmod 444 fonts.dir
읽기만 가능하게 끄름..

불러오는 소스는 간단하게...


BufferedImage bi = new BufferedImage( 200, 12, BufferedImage.TYPE_INT_RGB);
g2 = bi.createGraphics();
g2.setFont( new Font( "gulim", Font.PLAIN, 12) );
g2.drawString( "굴림체닷!!!', 0 , 0 );
g2.setFont( new Font( "batang", Font.PLAIN, 12) );
g2.drawString( "바탕체닷!!!', 100 , 0 );
g2.dispose();

//서블릿이라면 이미지를 화면에 보여줄 수 있겠지... JAI를 이용해서...
ImageIO.write(bi, "jpg", response.getOutputStream() );


요럼 대겠다...

이건... 특정일자가 지난 파일을 삭제하는 커맨드 +30은 경과일자. 즉, 30일 지난 파일 삭제
find ./ -mtime +30 -print -exec rm -f {} \;


AND

System.out Redirect

개발/Java 2010. 1. 25. 18:00

1. System.out -> 특정 Output Stream 전달.

   PrintStream ps = new PrintStream(new FileOutputStream("d:\\result.txt", true));
   System.setOut(ps);
   System.out.println("test string");  
   ps.close();
   
2. 파이프 이용

   PipedOutputStream pout = new PipedOutputStream();
   PipedInputStream pin = new PipedInputStream(pout);
   
   PrintStream ps1 = new PrintStream(pout);

   System.setOut(ps1);

   System.out.println("test string");

   byte[] data = new byte[pin.available()];
   
   pin.read(data, 0, data.length);
   
   //System.out 쓴 녀석이 pipe out으로 나가고 그 녀석을 pin에서 읽어와서 파일에 기록.
   PrintStream ps2 = new PrintStream(new FileOutputStream("d:\\log.txt", true));
   
   ps2.write(data, 0, data.length);

AND