@blog.justoneplanet.info

日々勉強

PHPで不正な閉じタグを見つける

ループは1回にできると思うけどまぁとりあえず。

<?php
$str = <<<EOD
<html>
<body>
<center>
<form action="confirm.php" method="get">
<input type="text" name="name"><br>
<input type="submit" value="submit">
</center>
</form>
</body>
</html>
EOD;

preg_match_all("/<.*?>/", $str, $matches);
//var_dump($matches);
$ary = array();
$stack = array();

foreach ($matches[0] as $key => $value) {
    array_push($ary, $value);
}

foreach ($ary as $key => $value) {
    if (preg_match("/<\/([a-zA-Z]+)/", $value, $tag)) {
#        echo "$value\n";
#        var_dump($tag[1]);
        $isFoundClose = false;
        foreach ($ary as $k => $v) {
            if (preg_match("/<{$tag[1]}/", $v)) {
#                echo "match\n";
                $isFoundClose = true;
                array_splice($ary, $k, $key - $k);
                break;
            }
        }
        if (!$isFoundClose) {
            echo "error:$value\n";
        }
    }
    else {

    }
}

UITextViewの高さを可変にする

編集可能なUITextViewを使用するときに入力内容にあわせてUITextViewの高さを変えたい時がある。

■コード

以下のようにUITextViewを定義する。

textview = [[UITextView alloc] init];
[textview setFrame:CGRectMake(0, 0, 100, 50)];
[textview setEditable:YES];
[textview setDelegate:self];

以下のようにcontentSizeを使用する。

-(void)textViewDidChange:(UITextView *)textView {
    CGRect f = textview.frame;
    f.size.height = textview.contentSize.height;
    textview.frame = f;
}

android-support-v4.jarを使ったプロジェクトのテストをする

テスト対象のプロジェクトにおいて以下の操作をする。

propaties > Java Build Path > Order and Export > android-support-v4.jarにチェックを入れる。

.classpath

以下のようにexported属性が付加されている。

<classpathentry exported="true" kind="lib" path="libs/android-support-v4.jar"/>

悪い例

テストプロジェクトにandroid-support-v4.jarをインポートすると以下のようなエラーがでる。

java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:263)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:185)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:373)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3285)
at android.app.ActivityThread.access$2200(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:987)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3728)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)

参考

androidでレンダリング後のViewのサイズを取得する

onClickなどのユーザイベント実行時に取得するならば以下の方法で取得ができる。

mView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        int width = v.getWidth();
        int height = v.getHeight();
    }
});

これを利用して以下のようにすると正しく取得できない。

@Override
public void onCreate(Bundle savedInstanceState) {
    // ...
    int width = mView.getWidth();// 0
    int height = mView.getHeight();// 0
}

onCreate, onStart, onResumeではレンダリングが終了していないためサイズが0となる。

■解決策

以下のようにViewTreeObserverを利用することでレンダリング後のサイズを取得することができる。

@Override
public void onCreate(Bundle savedInstanceState) {
    // ...
    ViewTreeObserver viewTreeObserver = mView.getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int width = mView.getWidth();
            int height = mView.getHeight();
        }
    });
}

CentOSにHaskellを入れる

めも。

■ghc

wget http://www.haskell.org/ghc/dist/7.2.2/ghc-7.2.2-x86_64-unknown-linux.tar.bz2
tar xvfj ghc-7.2.2-x86_64-unknown-linux.tar.bz2
cd ghc-7.2.2
./configure --prefix=/usr/local/haskell/ghc/7.2.2
make install
export PATH=/usr/local/haskell/ghc/7.2.2/bin:$PATH

コーディング

vim hello.hs
main = putStrLn "Hello World"

実行

以下のコマンドでコンパイルと実行が同時にできる。

ghc -e main hello.hs

もしくは以下のコマンドを実行する。

ghc --make hello.hs
./hello

インタプリタ

以下のコマンドで起動する。

ghci

以下のようにしてインストールできる。

Prelude> putStrLn "Hello World"
Hello World

以下のようにしてインタプリタを終了できる。

<ctrl>+D

エラー

<command line>: can't load .so/.DLL for: libgmp.so (libgmp.so: cannot open shared object file: No such file or directory)

以下のコマンドで解決できる。

cd /usr/lib64
ln -s libgmp.so.3 libgmp.so

参考

■hugs

インタプリタ。ghciもあるので必要がないと思われる。

wget http://cvs.haskell.org/Hugs/downloads/2006-09/hugs98-Sep2006.tar.gz
tar xvzf hugs98-Sep2006.tar.gz
cd hugs98-Sep2006
./configure
make
make install

開始

hugs

終了

<ctrl>+D

Hello World

Hugs> putStrLn "Hello World"
Hello World

androidでActivityが切り替わるときのアニメーションを変える

activityが切り替わるときにアニメーションをさせたくなくなった。

■実装

res/values/style.xml

以下のようにテーマを定義する。

<resource>
    <style name="NoAnimationTheme" parent="android:Theme">
        <item name="android:windowAnimationStyle">@style/Animation.Activity</item>
    </style>
    <style name="Animation.Activity" parent="android:Animation.Activity">
        <item name="android:activityOpenEnterAnimation">@anim/activity_open_enter</item>
        <item name="android:activityOpenExitAnimation">@anim/activity_open_exit</item>
        <item name="android:activityCloseEnterAnimation">@anim/activity_close_enter</item>
        <item name="android:activityCloseExitAnimation">@anim/activity_close_exit</item>
    </style>
</resource>

anim/activity_open_enter.xml

startActivityで開くActivityのanimation。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:fromXDelta="100%"
        android:toXDelta="0%"
        android:duration="0"
        android:fillAfter="true"
        android:fillEnabled="true"/>
</set>

anim/activity_open_exit.xml

startActivityで閉じるActivityのanimation。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:fromXDelta="0%"
        android:toXDelta="-100%"
        android:duration="0"
        android:fillAfter="true"
        android:fillEnabled="true"/>
</set>

anim/activity_close_exit.xml

finishで閉じるActivityのanimation。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:duration="0"
        android:fillAfter="true"
        android:fillEnabled="true"/>
</set>

anim/activity_close_open.xml

finishで開くActivityのanimation。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:fromXDelta="-100%"
        android:toXDelta="0%"
        android:duration="0"
        android:fillAfter="true"
        android:fillEnabled="true"/>
</set>
参考

上述の内容などは以下のような参考サイトに情報がある。

AndroidManifest.xml

アプリケーション全体に適用する

以下のようにapplicationタグに記述することでアプリケーション全体に変更したアニメーションを適用できる。

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/NoAnimationTheme">
アクティビティに適用する

以下のようにactivityタグに記述することで単一のアクティビティにのみ変更したアニメーションを適用できる。

<activity
    android:label="@string/app_name"
    android:name=".MainActivity">
</activity>

■応用

上述の方法だと特定の条件下でのみアニメーションを変えるといったことができない。条件によってanimationを変えたい場合は、以下のようにすることで対応できる。

public class MainActivity extends Activity {
    /**
     * 起動時に実行される
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.NoAnimationTheme);// super.onCreateの前に実行する必要がある
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

androidでギャラリーと連携する

■実装

ギャラリーの起動

以下のようにすることで(画像データに選択肢を絞って)ギャラリーアプリを起動することができる。

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, REQUESTCODE);

ギャラリーからデータを受け取る

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUESTCODE && resultCode == RESULT_OK) {
        //data.getData();//ギャラリーで選択したファイルのuriが格納されている
    }
}

uriからpathの文字列を取得

以下のようにすることでuriからpathの文字列を取得することができる。

private final String getPathFromUri(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
参考

androidで顔検出する

int maxFaces = 10;// 検出する顔の最大数
FaceDetector detector = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), maxFaces);
FaceDetector.Face[] faces = new FaceDetector.Face[maxFaces];
ArrayList<Bitmap> recognized = new ArrayList<Bitmap>();

// bitmapの中から顔を検出してfacesに格納する
int num = detector.findFaces(bitmap, faces);

// facesに格納されたデータから座標情報を取り出してbitmapを切り出す
for (int i = 0; i < num; i++) {
    FaceDetector.Face face = faces[i];
    PointF point = new PointF(0.0f, 0.0f);
    face.getMidPoint(point);

    int x = (int) (point.x - 1.5f * face.eyesDistance());
    int y = (int) (point.y - 2 * face.eyesDistance());
    int width  = (int) (face.eyesDistance() * 3);
    int height = (int) (face.eyesDistance() * 4);
    x = (x < 0)? 0 : x;
    y = (y < 0)? 0 : y;
    width = (width > bitmap.getWidth())? bitmap.getWidth() : width;
    height = (height > bitmap.getHeight())? bitmap.getHeight() : height;
    recognized.add(Bitmap.createBitmap(bitmap, x, y, width, height));
}
return recognized;

androidでprivateメソッドのテストをする

そういえば書いてなかったのでメモ。

    /**
     * Hogeクラスのprivateメソッドadd(int a, int b)をテストする
     * @throws SecurityException
     * @throws IllegalArgumentException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public void testAdd() throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Hoge hoge = new Hoge();
        Class<Hoge> clz = Hoge.class;
        Method method = clz.getDeclaredMethod("add", Integer.class, Integer.class);
        method.setAccessible(true);
        int result = (int) method.invoke(hoge, 5, 3);
        asserTrue(result == 8);
    }

staticメソッドの場合は以下のようになる。

    /**
     * Hogeクラスのprivate staticメソッドadd(int a, int b)をテストする
     * @throws SecurityException
     * @throws IllegalArgumentException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public void testAdd() throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Class<Hoge> clz = Hoge.class;
        Method method = clz.getDeclaredMethod("add", Integer.class, Integer.class);
        method.setAccessible(true);
        int result = (int) method.invoke(clz, 5, 3);
        asserTrue(result == 8);
    }

androidでArrayAdapterを使う

■実装

mListView = (ListView) findViewById(R.id.list);
mListView.setAdapter(mOriginalArrayAdapter);

mOriginalArrayAdapterを生成するとする。

public class OriginalArrayAdapter extends ArrayAdapter<OriginalDataRow> {
    private LayoutInflater inflater;

    /**
     * セル内における可変要素
     */
    public static class ViewHolder {
        public TextView text;
    }

    public OriginalArrayAdapter(Context context, int textViewResourceId, ArrayList<OriginalDataRow> data) {
        super(context, textViewResourceId, data);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final OriginalDataRow data = getItem(position);// フィールドなどから取得してはいけない...(a)

        // viewのセットアップ
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.cell_original_data, null);
            holder.text = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        // dataをviewに書き出す
        holder.text.setText(data.text);
        return convertView;
    }
}

filterを使用する

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) {
        String query = mEditText.getText().toString();
        if (query.equals("")) {
            mListView.clearTextFilter();
        }
        else {
            mListView.setFilterText(query);
        }
    }

(a)の部分でsuperクラスのgetItemを使用しないとフィルタリングが上手く適用されない。また、filterを使用する場合は、リストアイテムのtoString値が使用されるため、以下のように必要に応じてOverrideする必要がある。

public class OriginalDataRow {
    public String name;
    @Override
    public String toString() {
        return name;// filterに使用する文字列
    }
}