2010年5月31日月曜日

groovyとgdata-java-clientでGoogle Calendarの指定時間内のイベントを一覧表示する

groovyとgdata-java-clientでGoogle Calendarの指定時間内のイベントを一覧表示するには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.calendar.v2.*
import com.google.api.data.calendar.v2.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*

// 日時
public class When
{
@Key("@startTime")
DateTime startTime
@Key("@endTime")
DateTime endTime
}

// エントリ
public class Entry
{
@Key
String title
@Key
String content
@Key("gd:when")
When when
}

// フィード
public class Feed
{
@Key
String title
@Key
List<Entry> entry
}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleCalendar.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleCalendarAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

username = "youraccount@gmail.com"
password = 'yourpassword'

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleCalendar.AUTH_TOKEN_TYPE
cl.username = username
cl.password = password
cl.authenticate().setAuthorizationHeader(transport)

// デフォルトカレンダーから指定時間内のイベントを取得
request = transport.buildGetRequest();
st = new DateTime(new GregorianCalendar(2010, 6-1, 3, 10, 0, 0).getTime(),
TimeZone.getTimeZone("GMT+9")).toStringRfc3339()
et = new DateTime(new GregorianCalendar(2010, 6-1, 3, 13, 0, 0).getTime(),
TimeZone.getTimeZone("GMT+9")).toStringRfc3339()
request.url =
"http://www.google.com/calendar/feeds/default/private/full" +
"?start-min=${URLEncoder.encode(st, "UTF-8")}" +
"&start-max=${URLEncoder.encode(et, "UTF-8")}"

feed = request.execute().parseAs( Feed.class)
println "feed title:" + feed.title
for(item in feed.entry){
println "title:" + item.title
println "content:" + item.content
if( item.when != null ){
println "開始日時:" + item.when?.startTime
println "終了日時:" + item.when?.endTime
}
}


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/

ScriptomとPower Pointでダイヤモンド形を描画する

ScriptomとPower Pointでダイヤモンド形を描画するには、以下のコードを実行します。

import org.codehaus.groovy.scriptom.*;
import org.codehaus.groovy.scriptom.tlb.office.*;
import org.codehaus.groovy.scriptom.tlb.office.powerpoint.*;

Scriptom.inApartment
{
ppa = new ActiveXObject("PowerPoint.Application")
ppa.Presentations.Open(new File("test1.pptx").canonicalPath,
Scriptom.MISSING, Scriptom.MISSING, MsoTriState.msoFalse)
// ダイヤモンドを描画する
slide = ppa.Presentations(1).slides(1)
shape = slide.Shapes.AddShape(
MsoAutoShapeType.msoShapeDiamond,
100/* =left */, 100/* =top*/, 200/* =width*/, 200/* height */)

// 線の色
shape.Line.ForeColor.RGB = 0xffffff /* BGR*/
// 塗りつぶし色
shape.Fill.ForeColor.RGB = 0xffddbb /* BGR*/

ppa.Presentations(1).saveAs(new File("test59.pptx").canonicalPath)
ppa.Presentations(1).close()
ppa.quit()
}


元プレゼンテーション(test1.pptx)


出力プレゼンテーション(test59.pptx)
ScriptomとPower Pointでダイヤモンド形を描画したスライド

動作環境
groovy1.7.0, JDK6 Update18, PowerPoint 2007

groovyとApache Commons NetでFTPサーバ上のファイルのサイズを取得する

groovyとApache Commons NetでFTPサーバ上のファイルのサイズを取得するには、以下のコードを実行します。

import org.apache.commons.net.ftp.*

fos = null
fc = new FTPClient();
try
{
// 接続
fc.connect("192.168.1.1", 21)
if( !FTPReply.isPositiveCompletion(fc.getReplyCode()) ){
throw new Exception("failed to connect.")
}
// ログイン
if( fc.login("user", "password") == false ){
throw new Exception("failed to login.")
}
// エンコーディングの設定
fc.setControlEncoding("UTF-8")

// ファイルのサイズを表示
files = fc.listFiles(".")
for( file in files ){
if( !file.isDirectory() ){
println "${file.name}:${file.size}"
}
}

// ログアウト
fc.logout()
}
finally
{
if( fc.isConnected() ){
fc.disconnect()
}
if( fos != null )fos.close()
}


動作環境
groovy 1.7.1, JDK6 Update19, Apache Commons Net 2.0

2010年5月30日日曜日

groovyとgdata-java-clientでGoogle Calendarのカレンダーを登録する

groovyとgdata-java-clientでGoogle Calendarのカレンダーを登録するには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.calendar.v2.*
import com.google.api.data.calendar.v2.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*

// タイムゾーン
public class Timezone
{
@Key("@value")
String value
}

// 色
public class GCalColor
{
@Key("@value")
String value
}

// エントリ
public class Entry
{
@Key
String title
@Key
String summary
@Key("gCal:timezone")
Timezone timezone
@Key("gCal:color")
GCalColor color

public Entry()
{
}
public Entry(String title, String sumary,
String tz, String color)
{
this.title = title
this.summary = summary
def tzi = new Timezone()
tzi.value = tz
this.timezone = tzi
def colori = new GCalColor()
colori.value = color
this.color = colori
}
}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleCalendar.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleCalendarAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

username = "youraccount@gmail.com"
password = 'yourpassword'

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleCalendar.AUTH_TOKEN_TYPE
cl.username = username
cl.password = password
cl.authenticate().setAuthorizationHeader(transport)

entry = new Entry("GDataで作成したカレンダ",
"テスト用です。", "Asia/Tokyo", "#705770")

// カレンダーを登録
request = transport.buildPostRequest()
request.url = "http://www.google.com/calendar/feeds/default/owncalendars/full"
content = new AtomContent()
content.entry = entry
content.namespaceDictionary = GoogleCalendarAtom.NAMESPACE_DICTIONARY
request.content = content;
request.execute().parseAs(Entry.class)


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/

groovyとgdata-java-clientでGoogle Calendarのカレンダーにイベントを登録する

groovyとgdata-java-clientでGoogle Calendarのカレンダーにイベントを登録するには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.calendar.v2.*
import com.google.api.data.calendar.v2.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*

// 日時
public class When
{
@Key("@startTime")
DateTime startTime
@Key("@endTime")
DateTime endTime

public When()
{
}
public When(GregorianCalendar st, GregorianCalendar et)
{
startTime = new DateTime(st.getTime(), TimeZone.getTimeZone("GMT+9"))
endTime = new DateTime(et.getTime(), TimeZone.getTimeZone("GMT+9"))
}
}

// 場所
public class Where
{
@Key("@valueString")
String valueString

public Where()
{
}
public Where(String vs)
{
valueString = vs
}
}

// カテゴリ
public class Category
{
@Key("@scheme")
String scheme = "http://schemas.google.com/g/2005#kind"
@Key("@term")
String term = "http://schemas.google.com/g/2005#event"
}

// エントリ
public class Entry
{
@Key
Category category
@Key
String title
@Key
String content
@Key("gd:when")
When when
@Key("gd:where")
Where where

public Entry()
{
}
public Entry(String title, String content,
GregorianCalendar st, GregorianCalendar et,
String where)
{
this.category = new Category()
this.title = title
this.content = content
this.when = new When(st, et)
this.where = new Where(where)
}

}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleCalendar.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleCalendarAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

username = "youraccount@gmail.com"
password = 'yourpassword'

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleCalendar.AUTH_TOKEN_TYPE
cl.username = username
cl.password = password
cl.authenticate().setAuthorizationHeader(transport)

entry = new Entry("イベントのテスト",
"イベントのテストのコンテント",
new GregorianCalendar(2010, 6-1, 3, 10, 0, 0),
new GregorianCalendar(2010, 6-1, 3, 12, 0, 0),
"オフィス"
)

// デフォルトカレンダーにイベントを登録
request = transport.buildPostRequest()
request.url = "http://www.google.com/calendar/feeds/default/private/full"
content = new AtomContent()
content.entry = entry
content.namespaceDictionary = GoogleCalendarAtom.NAMESPACE_DICTIONARY
request.content = content;
request.execute().parseAs(Entry.class)


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/

ScriptomとPower Pointで立体的に突き出す様に折れ曲がったリボンを描画する

ScriptomとPower Pointで立体的に突き出す様に折れ曲がったリボンを描画するには、以下のコードを実行します。

import org.codehaus.groovy.scriptom.*;
import org.codehaus.groovy.scriptom.tlb.office.*;
import org.codehaus.groovy.scriptom.tlb.office.powerpoint.*;

Scriptom.inApartment
{
ppa = new ActiveXObject("PowerPoint.Application")
ppa.Presentations.Open(new File("test1.pptx").canonicalPath,
Scriptom.MISSING, Scriptom.MISSING, MsoTriState.msoFalse)
// 立体的に突き出す様に折れ曲がったリボンを描画する
slide = ppa.Presentations(1).slides(1)
shape = slide.Shapes.AddShape(
MsoAutoShapeType.msoShapeCurvedUpRibbon,
100/* =left */, 100/* =top*/, 500/* =width*/, 200/* height */)

// 線の色
shape.Line.ForeColor.RGB = 0xffffff /* BGR*/
// 塗りつぶし色
shape.Fill.ForeColor.RGB = 0xffddbb /* BGR*/

ppa.Presentations(1).saveAs(new File("test58.pptx").canonicalPath)
ppa.Presentations(1).close()
ppa.quit()
}


元プレゼンテーション(test1.pptx)


出力プレゼンテーション(test58.pptx)
ScriptomとPower Pointで立体的に突き出す様に折れ曲がったリボンを描画したスライド

動作環境
groovy1.7.0, JDK6 Update18, PowerPoint 2007

groovyとApache Commons NetでFTPサーバ上のディレクトリを列挙する

groovyとApache Commons NetでFTPサーバ上のディレクトリを列挙するには、以下のコードを実行します。

import org.apache.commons.net.ftp.*

fos = null
fc = new FTPClient();
try
{
// 接続
fc.connect("192.168.1.1", 21)
if( !FTPReply.isPositiveCompletion(fc.getReplyCode()) ){
throw new Exception("failed to connect.")
}
// ログイン
if( fc.login("user", "password") == false ){
throw new Exception("failed to login.")
}
// エンコーディングの設定
fc.setControlEncoding("UTF-8")

// ディレクトリだけを列挙
files = fc.listFiles(".")
for( file in files ){
if( file.isDirectory() ){
println file.name
}
}

// ログアウト
fc.logout()
}
finally
{
if( fc.isConnected() ){
fc.disconnect()
}
if( fos != null )fos.close()
}


動作環境
groovy 1.7.1, JDK6 Update19, Apache Commons Net 2.0

groovyとgdata-java-clientとiCal4jでGoogle Calendarのイベントを一覧表示する

groovyとgdata-java-clientとiCal4jでGoogle Calendarのイベントを一覧表示するには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.calendar.v2.*
import com.google.api.data.calendar.v2.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*
import net.fortuna.ical4j.data.*
import net.fortuna.ical4j.model.property.*

// 日時
public class When
{
@Key("@startTime")
DateTime startTime
@Key("@endTime")
DateTime endTime
}

// エントリ
public class Entry
{
@Key
String title
@Key("gd:when")
When when
@Key("gd:recurrence")
String recurrence

def getRecurrenceInfo()
{
String result = ""
if( recurrence != null ){
def sr = new StringReader(
"BEGIN:VCALENDAR\n" + recurrence + "END:VCALENDAR")
def cb = new CalendarBuilder()
def calendar = cb.build(sr)
for( property in calendar.getProperties() ){
if( property.name.equals("DTSTART") ){
result += "開始日:" + property.value + "\n"
}
if( property instanceof RRule ){
result += "周期:" + property.recur.getFrequency() + "\n"
}
}
}
return result
}
}

// フィード
public class Feed
{
@Key
String title
@Key
List<Entry> entry
}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleCalendar.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleCalendarAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

username = "youraccount@gmail.com"
password = 'yourpassword'

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleCalendar.AUTH_TOKEN_TYPE
cl.username = username
cl.password = password
cl.authenticate().setAuthorizationHeader(transport)

// リクエスト
request = transport.buildGetRequest()
// カレンダのURLは以下の形式。google calendarの設定の限定公開のXMLから
// userIDとmagicCookieが取れる
// http://www.google.com/calendar/feeds/userID/private-magicCookie/full
request.url = "http://www.google.com/calendar/feeds/xxxxgroup.calendar.google.com/private-xxxx/full"


feed = request.execute().parseAs( Feed.class)
println "feed title:" + feed.title
for(item in feed.entry){
println "title:" + item.title
if( item.when != null ){
println "開始日時:" + item.when?.startTime
println "終了日時:" + item.when?.endTime
}
if( item.recurrence != null ){
// 繰り返しイベント
println item.getRecurrenceInfo()
}
}


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha, iCal4j-1.0-rc3

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/

iCal4jのホームページ
http://wiki.modularity.net.au/ical4j/index.php?title=Main_Page

2010年5月29日土曜日

groovyとVI Java APIで仮想マシンの注釈を一覧表示する

groovyとVI Java APIで仮想マシンの注釈を一覧表示するには、以下のコードを実行します。

import com.vmware.vim25.*
import com.vmware.vim25.mo.*

// VMWare ESXiに接続
host = "https://192.168.1.161/sdk"
user = "root"
password = "password"
si = new ServiceInstance(new URL(host), user, password, true)
rf = si.getRootFolder()

// 仮想マシンの注釈を一覧表示
sc = [["VirtualMachine", "name"]] as String[][]
vms = new InventoryNavigator(rf).searchManagedEntities(sc, true)
for( vm in vms ){
println "${vm.name}:${vm.config?.annotation}"
}
si.getServerConnection().logout()


動作環境
groovy 1.7.1, JDK6 Update19, VMware VI (vSphere) Java API vijava2u120091204,
VMWare ESXi 4.0 Update1

関連情報
groovyとVI Java APIのまとめ
VMware VI (vSphere) Java APIのホームページ
http://vijava.sourceforge.net/

groovyとiCal4jでiCalendarの情報をダンプする

groovyとiCal4jでiCalendarの情報をダンプするには、以下のコードを実行します。

import net.fortuna.ical4j.data.*

// iCalendarを読み込んで情報をダンプする
cb = new CalendarBuilder()
calendar = cb.build(new FileInputStream("basic.ics"))
// プロパティの列挙
for( property in calendar.getProperties() ){
println "${property.name}=${property.value}"
for( parameter in property.getParameters() ){
println " ${parameter.name}=${parameter.value}"
}
}
println "----"
// コンポーネントの列挙
for( component in calendar.getComponents() ){
println "${component.name}"
for( property in component.getProperties() ){
println " ${property.name}=${property.value}"
for( parameter in property.getParameters() ){
println " ${parameter.name}=${parameter.value}"
}
}
}


動作環境
groovy 1.7.2, JDK6 Update20, iCal4j-1.0-rc3

関連情報
iCal4jのホームページ
http://wiki.modularity.net.au/ical4j/index.php?title=Main_Page

groovyとJCIFSでWindows共有上のファイルに書き込む

groovyとJCIFSでWindows共有上のファイルに書き込むには、以下のコードを実行します。

import jcifs.smb.*

domain = "workgroup"
user = "testuser"
password = "password"
server = "win01"
path = "sharedir"
file = "test.txt"

wr = new BufferedWriter(new OutputStreamWriter(new SmbFileOutputStream(
"smb://${domain};${user}:${password}@${server}/${path}/${file}"
)))

// Windows共有上にファイルを書き込む
wr.write("Windows共有上にファイル書き込みのテスト\r\n")
wr.write("write test.\r\n")
wr.flush()
wr.close()


動作環境
groovy 1.7.1, JDK6 Update19, JCIFS 1.3.14

関連情報
JCIFSまとめ