2010年6月3日木曜日

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.msoShapeDoubleBracket,
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("test62.pptx").canonicalPath)
ppa.Presentations(1).close()
ppa.quit()
}


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


出力プレゼンテーション(test62.pptx)
ScriptomとPower Pointで描画した括弧

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

groovyとApache Commons VFSでRAMファイルシステム上のファイル/フォルダを判別する

groovyとApache Commons VFSでRAMファイルシステム上のファイル/フォルダを判別するには、以下のコードを実行します。

import org.apache.commons.vfs.*

fsm = VFS.getManager()

// RAM file systemにファイルをコピー
fsm.resolveFile( "ram://folder1/test.txt").copyFrom(
fsm.resolveFile("C:\\share\\commons-vfs\\test.txt"),
new AllFileSelector())

// ファイルタイプ表示:フォルダ
println fsm.resolveFile( "ram://folder1/").getType().getName()

// ファイルタイプ表示:ファイル
println fsm.resolveFile( "ram://folder1/test.txt").getType().getName()


動作環境
groovy 1.7.1, JDK6 Update19, apache commons vfs 1.0, apache commons logging 1.1.1

2010年6月2日水曜日

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.msoShapeDoubleBrace,
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("test61.pptx").canonicalPath)
ppa.Presentations(1).close()
ppa.quit()
}


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


出力プレゼンテーション(test61.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")

// ファイルのユーザ(ユーザID)・グループ(グループID)を表示
files = fc.listFiles(".")
for( file in files ){
if( !file.isDirectory() ){
println "${file.name}:${file.user}:${file.group}"
}
}

// ログアウト
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年6月1日火曜日

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 Entry
{
@Key
String title
@Key
String id
}

// フィード
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()
request.url = GoogleCalendar.ROOT_URL +"default/allcalendars/full"
feed = request.execute().parseAs(Feed.class)
println "feed title:" + feed.title
target = "GDataで作成したカレンダ"
for(item in feed.entry){
if( item.title.equals(target) ){
// 指定した名前のカレンダーを削除する
dr = transport.buildDeleteRequest()
id = item.id.substring(item.id.lastIndexOf('/'))
dr.url = "http://www.google.com/calendar/feeds/default/owncalendars/full${id}"
dr.execute().parseAsString()
}
}


動作環境
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.msoShapeDonut,
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("test60.pptx").canonicalPath)
ppa.Presentations(1).close()
ppa.quit()
}


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


出力プレゼンテーション(test60.pptx)
ScriptomとPower Pointで描画したドーナツ形

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

groovyとApache Commons NetでFTPサーバ上のファイルのタイムスタンプを取得する

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

import java.text.*
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")

// ファイルのタイムスタンプを表示
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
files = fc.listFiles(".")
for( file in files ){
if( !file.isDirectory() ){
println "${file.name}:" + sdf.format(file.getTimestamp().getTime())
}
}

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


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