ObjectをAbstractJaxbに変換したい

13 views
Skip to first unread message

ひろ

unread,
Apr 9, 2014, 1:08:04 AM4/9/14
to mixe...@googlegroups.com
お世話になります。

getContent()で取得したObjectをAbstractJaxbに変換したいのですが、どのようにすれば良いのでしょうか?
次のようなコードではうまくいきませんでした。
※Aタグの中にAタグが追加されてしまいます。

【変換メソッド】
protected AbstractJaxb cast(final java.lang.Object template) {
if (template.getClass().equals(A.class)) {
A obj = new A();
obj.getContent().add(template);
return obj;
}
                               :
         }

【呼び出し側】
Div tag = (Div) master;
List<Object> m =
tag.getContent();
for (Object content : m) {
AbstractJaxb a = cast(content);
if (a == null) {
continue;
}
logger.debug("master HTML =" +
mixer2Engine.saveToString(a));
}


nabedge

unread,
Apr 9, 2014, 9:48:20 AM4/9/14
to mixe...@googlegroups.com
渡辺です。

これをやるなら、instanceof を使った方がいいんじゃないでしょうか?

for (Object o: tag.getContent()) {
if (o instanceof AbstractJaxb) {
logger.debug("master HTML =" + mixer2Engine.saveToString(o));
} else {
continue;
}
}

--------
nab...@gmail.com


2014年4月9日 14:08 ひろ <hiro...@freegw.net>:
> --
> このメールは Google グループのグループ「mixer2-ja」に登録しているユーザーに送られています。
> このグループから退会し、グループからのメールの配信を停止するには mixer2-ja+...@googlegroups.com
> にメールを送信してください。
> その他のオプションについては https://groups.google.com/d/optout にアクセスしてください。

nabedge

unread,
Apr 9, 2014, 10:05:26 AM4/9/14
to mixe...@googlegroups.com
ああ、すいません、abstractJaxb型にキャストしたいんでしたね。
for (Object o: tag.getContent()) {
if (o instanceof AbstractJaxb) {
AbstractJaxb aj = (AbstractJaxb) o
logger.debug("master HTML =" + mixer2Engine.saveToString(aj));
} else {
continue;
}
}
--------
nab...@gmail.com


2014年4月9日 22:48 nabedge <nab...@gmail.com>:

ひろ

unread,
Apr 10, 2014, 12:41:31 AM4/10/14
to mixe...@googlegroups.com
ひろです。
単純なキャストで良かったんですね!
ありがとうございます。
うまくいきました。

一応自分ですべてのタグを変換できるように次のようなメソッドを作成したのですが、AbstractJaxbの派生クラスが増えた時の為に、本体にcast用のメソッドがあると嬉しいのですがサポートして頂けないでしょうか?

protected static AbstractJaxb cast(final java.lang.Object template) {

if (template instanceof A) {
return (A) template;
} else if (template instanceof Address) {
return (Address) template;
} else if (template instanceof Applet) {
return (Applet) template;
} else if (template instanceof Area) {
return (Area) template;
} else if (template instanceof Base) {
return (Base) template;
} else if (template instanceof Basefont) {
return (Basefont) template;
} else if (template instanceof Br) {
return (Br) template;
} else if (template instanceof ButtonContent) {
return (ButtonContent) template;
} else if (template instanceof Col) {
return (Col) template;
             (途中省略)
} else if (template instanceof H5) {
return (H5) template;
} else if (template instanceof H6) {
return (H6) template;
}

return null;
}


2014年4月9日水曜日 23時05分26秒 UTC+9 nabedge:
Reply all
Reply to author
Forward
0 new messages