プログラマメモ2 - programmer no memo2

org.apache.commons.configuration でXpathを使用して値のだしいれをしたい 2007/03/30

org.apache.commons.configuration でXpathを使用して値のだしいれをしたい


XPathExpressionEngine engine = new XPathExpressionEngine();
XMLConfiguration configuration = new XMLConfiguration();
configuration.setExpressionEngine(engine);

いまさらながらmaven1 2007/03/29

Index of /maven

maven1で作成したプロジェクトを動作させる必要があり、maven1の環境をつくりなおしていたときのメモです。

自分がはまったところ
(1)ユーザのホームディレクトリ直下に、build.propertiesをおく。
(2)build.propertiesにプロクシの設定を行う。

maven.proxy.host=アドレス
maven.proxy.port=8080
maven.repo.remote = http://repo1.maven.org/maven/
maven.proxy.username=bar
maven.proxy.password=bar


重要なのは、
http://www.ibiblio.org/maven/
が使えつかえなくなっている点です。

http://repo1.maven.org/maven/
を使います。

知らずに、ibiblioを指定していると下記のようなエラーがでてきます。


「commons-jelly-tags-interaction-20030211.143817.jar」のダウンロードを試みています。
Error retrieving artifact from [http://www.ibiblio.org/maven2/commons-jelly/jars
/commons-jelly-tags-interaction-20030211.143817.jar]: java.io.IOException: Unknown error downloading; status code was: 301


ステータス301はHTTPのステータスコードのようです。

swing スクロールペイン上の隠れたコンポーネントにフォーカスがあたったときに自動で表示するようにする 2007/03/23

スクロールペイン上の隠れたコンポーネントにフォーカスがあたったときに自動で表示するようにしたい。


解決方法として、フォーカス移動した場合、表示したいコンポーネントにフォーカスリスナーをつけます。
フォーカスを取得した場合に、親のコンポーネントののscrollRectToVisibleメソッドを利用します。
その際の引数には、対象コンポーネントのgetBoundsの値を利用するとうまくいきました。


class FocusListnerForScrollRectToVisible implements FocusListener {
public void focusGained(FocusEvent e) {

Component component = e.getComponent();
if (!(component instanceof JComponent))
return;
JComponent component2 = (JComponent) e.getComponent();
Rectangle r = component2.getBounds();

if (component2.getParent() instanceof JComponent)
((JComponent) (component2.getParent())).scrollRectToVisible(r);
}
public void focusLost(FocusEvent e) {
}
}

swing テキストコンポーネントで入力制限させてついでに点滅 2007/03/22

シナリオ

swingを使用してテキスト入力欄で、半角英数字のみ入力を許可して、関係ない文字を入力したら点滅させて入力させないようにする。


リスナーです。
初期化にテキストコンポーネント自身を使用してます。
背景が白い以外で設定されていることを想定してますので、もともと白ですと点滅しているようにみえないです。

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

public class InputLimitationDocument implements UndoableEditListener {

private static final long serialVersionUID = 1L;
private JTextComponent textComponent;

public InputLimitationDocument(JTextComponent textComponent) {
this.textComponent = textComponent;
}

/* 実行中かどうかのフラグ */
boolean isDoing = false;

public void undoableEditHappened(final UndoableEditEvent e) {
if (e.getSource() instanceof Document) {
Document document = (Document) e.getSource();
try {
String s = document.getText(0, document.getLength());
// check
if (!s.matches("[[A-Za-z0-9]\\s]*")) {
e.getEdit().undo();
if (isDoing)
return;
isDoing = true;
final Color orgColor = textComponent.getBackground();
textComponent.setBackground(Color.WHITE);

final Timer timer = new Timer(0, null);

ActionListener actionListener = new ActionListener() {
int count = 0;

public void actionPerformed(ActionEvent e) {
textComponent.setBackground(textComponent
.getBackground() == orgColor ? Color.WHITE
: orgColor);
count++;
// 点滅2回
if (count <= 5)
return;
textComponent.setBackground(orgColor);
isDoing = false;
timer.setRepeats(false);
}
};
timer.setDelay(200);
timer.addActionListener(actionListener);
timer.start();

}

} catch (BadLocationException e1) {
e1.printStackTrace();
}
}

}

}

JXPathを利用してオブジェクトから値を取得。 2007/03/22

JXPathがおもしろいです。
XPathを使用してオブジェクトグラフにアクセスできます。

簡単なサンプルです。


import java.util.HashMap;
import java.util.Map;

public class A {

private Map map = new HashMap();
private Map my = new HashMap();

public Map getMap() {
return map;
}

public void setMap(Map map) {
this.map = map;
}

public Map getMy() {
return my;
}

public void setMy(Map my) {
this.my = my;
}

}


JXPathを使用して値を取得するクラスです。


import org.apache.commons.jxpath.JXPathContext;
import a.a.A;

public class TestAccessA {

/**
* @param args
*/
public static void main(String[] args) {

a();
}

public static void a(){
A a = new A();
A a2 = new A();
a2.getMap().put("x", "OK");
a2.getMap().put("x2", "OK o_o");
a.getMap().put("aobj", a2);
a.getMy().put("aobj", a2);

JXPathContext context = JXPathContext.newContext(a);

Object object= context.getValue(
"/my['aobj']//map//x2");
System.out.println(object);

}
}



入りくんだオブジェクトグラフからXPathで値が取得できるのはおもしろいです。

半角英数字と半角スペースのチェック 2007/03/22

半角英数字と半角スペースのチェック

[[A-Za-z0-9]\\s]*



public static void a(String s) {

if(s.matches("[[A-Za-z0-9]\\s]*")){
System.out.println("BINGO!!");
return;
}

System.out.println("out!!");
}

ant task jarjar 2007/03/20

Why Tonic? - Quality, Features and Price

googleのguiceのbuild.xmlを眺めていて、jarjarというタスクをみつけた。

<jarjar jarfile="${build.dir}/dist/guice-${version}.jar">
<fileset dir="${build.dir}/classes"/>
<zipfileset src="lib/build/cglib-nodep-2.2_beta1.jar"/>
<zipfileset src="lib/build/asm-3.0.jar"/>
<rule pattern="net.sf.cglib.**" result="com.google.inject.cglib.@1"/>
<rule pattern="org.objectweb.asm.**" result="com.google.inject.asm.@1"/>
</jarjar>s

ここで、ruleというのがあり奇妙で

はてこれはなんだろうなと思い調べてみたら便利そうなので、メモ。

参考:
http://rektunpe.sakura.ne.jp/diary/?date=20060127

なにやら依存パッケージをjarにとりこめることができるらしい。
ここまでなら、他にもツールがあるので、特筆するほどでもないなぁと思ったのですが、
さらにその先にいっていて、依存パッケージの名前を変更して、jarにとりこめる!!

このant taskは
ここのWhy Tonic? - Quality, Features and Priceようです。

実験してたしかにとりこまれているのを確認しました。

jakarta commonsのlangを利用するソースを書いて、build.xmlを用意して、できたjarを解体して、jadでデコンパイルしてみたらものの見事にかわっていました。

参考に作成したbuild.xmlファイル


<?xml version="1.0" encoding="UTF-8"?>
<project name="research" default="compile" basedir=".">
<property name="version" value="0.0.3"/>
<property name="build.dir" value="build"/>
<property name="lib.dir" value="lib"/>
<property name="src.dir" value="src-string"/>

<path id="compile.classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>

<target name="jar" depends="compile" description="Build jar!!">
<taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask"
classpath="lib/jarjar-0.9.jar"/>
<mkdir dir="${build.dir}/dist"/>
<jarjar jarfile="${build.dir}/dist/myteset-${version}.jar">
<fileset dir="${build.dir}/classes"/>
<zipfileset src="lib/commons-lang-2.3.jar"/>
<rule pattern="org.apache.commons.lang.**" result="newpackage.@1"/>
</jarjar>
</target>

<target name="compile" description="Compile Java source!!">
<mkdir dir="${build.dir}/classes"/>
<javac srcdir="${src.dir}" debug="on" destdir="${build.dir}/classes">
<classpath refid="compile.classpath"/>
</javac>
<copy toDir="${build.dir}/classes">
<fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
</target>
</project>

Apache Xindice XMLデータベース 2007/03/13

Apache Xindice
java製のXMLデータベース。