[Jiemamy-notify:2193] commit [3257] リファクタリング

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2009年 4月 13日 (月) 19:05:21 JST


Revision: 3257
          http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=3257
Author:   j5ik2o
Date:     2009-04-13 19:05:21 +0900 (Mon, 13 Apr 2009)

Log Message:
-----------
リファクタリング

Added Paths:
-----------
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/DeleteEmptyFileWriter.java
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/EntitySrcFileGenerator.java
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/GeneratorContextImpl.java
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/IncludeDirective.java
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/OnDemandDateModel.java
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/ResourceTemplateLoader.java

Removed Paths:
-------------
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/DeleteEmptyFileWriter.java
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/EntitySrcFileGenerator.java
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/GeneratorContextImpl.java
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/IncludeDirective.java
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/OnDemandDateModel.java
    leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/ResourceTemplateLoader.java


-------------- next part --------------
Deleted: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/DeleteEmptyFileWriter.java
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/DeleteEmptyFileWriter.java	2009-04-13 10:04:14 UTC (rev 3256)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/DeleteEmptyFileWriter.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -1,118 +0,0 @@
-/*
- * Copyright 2007-2009 Jiemamy Project and the Others.
- * Created on Apr 7, 2009
- *
- * This file is part of Jiemamy.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific language
- * governing permissions and limitations under the License.
- */
-package org.jiemamy.entity.io.gen.generator;
-
-import java.io.File;
-import java.io.FilterWriter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.Writer;
-
-import org.jiemamy.utils.FileInputStreamUtil;
-
-/**
- * 空のファイルを削除する{@link Writer}の実装。
- * 
- * @author j5ik2o
- */
-public class DeleteEmptyFileWriter extends FilterWriter {
-	
-	/** 書き込みが行われた場合{@code true} */
-	protected boolean written;
-	
-	/** 削除された場合{@code true} */
-	protected boolean deleted;
-	
-	/** 書き込み先のファイル */
-	protected File file;
-	
-
-	/**
-	 * インスタンスを生成する。
-	 * 
-	 * @param writer ライタ
-	 * @param file 書き込み先のファイル
-	 */
-	public DeleteEmptyFileWriter(Writer writer, File file) {
-		super(writer);
-		if (file == null) {
-			throw new NullPointerException("file");
-		}
-		this.file = file;
-	}
-	
-	@Override
-	public void close() throws IOException {
-		super.close();
-		if (!written && file.exists() && isEmpty()) {
-			deleted = file.delete();
-		}
-	}
-	
-	/**
-	 * ファイルが削除された場合のフラグを取得する。
-	 * 
-	 * @return 削除された場合{@code true}、そうでない場合{@code false}
-	 */
-	public boolean isDeleted() {
-		return deleted;
-	}
-	
-	/**
-	 * ファイルが空の場合のフラグを取得する。
-	 * 
-	 * @return ファイルが空の場合は{@code true}、そうでない場合{@code false}
-	 * @throws IOException 入出力が失敗した場合
-	 */
-	protected boolean isEmpty() throws IOException {
-		InputStream is = FileInputStreamUtil.create(file);
-		try {
-			return is.read() == -1;
-		} finally {
-			if (is != null) {
-				is.close();
-			}
-		}
-	}
-	
-	@Override
-	public void write(char[] cbuf, int off, int len) throws IOException {
-		if (len <= 0) {
-			return;
-		}
-		written = true;
-		super.write(cbuf, off, len);
-	}
-	
-	@Override
-	public void write(int c) throws IOException {
-		written = true;
-		super.write(c);
-	}
-	
-	@Override
-	public void write(String str, int off, int len) throws IOException {
-		if (len <= 0) {
-			return;
-		}
-		written = true;
-		super.write(str, off, len);
-	}
-	
-}

Deleted: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/EntitySrcFileGenerator.java
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/EntitySrcFileGenerator.java	2009-04-13 10:04:14 UTC (rev 3256)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/EntitySrcFileGenerator.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -1,217 +0,0 @@
-/*
- * Copyright 2007-2009 Jiemamy Project and the Others.
- * Created on Apr 13, 2009
- *
- * This file is part of Jiemamy.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific language
- * governing permissions and limitations under the License.
- */
-package org.jiemamy.entity.io.gen.generator;
-
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
-import java.nio.charset.Charset;
-import java.util.Locale;
-
-import freemarker.cache.FileTemplateLoader;
-import freemarker.cache.MultiTemplateLoader;
-import freemarker.cache.TemplateLoader;
-import freemarker.template.Configuration;
-import freemarker.template.DefaultObjectWrapper;
-import freemarker.template.Template;
-import freemarker.template.TemplateException;
-
-import org.apache.commons.lang.Validate;
-
-import org.jiemamy.utils.FileOutputStreamUtil;
-
-/**
- * エンティティを生成するためのジェネレータクラス。
- * 
- * @author j5ik2o
- */
-public class EntitySrcFileGenerator implements Generator {
-	
-	private Configuration configuration;
-	
-	/** デフォルトのテンプレートディレクトリの名前 */
-	protected static final String DEFAULT_TEMPLATE_DIR_NAME = "org/jiemamy/entity/io/tempaltes";
-	
-
-	/**
-	 * インスタンスを生成する。
-	 * 
-	 * @param configuration FreeMarkerの設定
-	 */
-	public EntitySrcFileGenerator(Configuration configuration) {
-		Validate.notNull(configuration);
-		this.configuration = configuration;
-	}
-	
-	/**
-	* インスタンスを生成する。
-	* 
-	* @param templateFileEncoding テンプレートファイルのエンコーディング
-	* @param templateFilePrimaryDir テンプレートファイルを格納したプライマリディレクトリ、プライマリディレクトリを使用しない場合{@code null}
-	 * @throws IOException 入出力が失敗した場合
-	*/
-	public EntitySrcFileGenerator(String templateFileEncoding, File templateFilePrimaryDir) throws IOException {
-		Validate.notNull(templateFileEncoding);
-		configuration = new Configuration();
-		configuration.setObjectWrapper(new DefaultObjectWrapper());
-		configuration.setSharedVariable("include", new IncludeDirective());
-		configuration.setSharedVariable("currentDate", new OnDemandDateModel());
-		configuration.setEncoding(Locale.getDefault(), templateFileEncoding);
-		configuration.setNumberFormat("0.#####");
-		configuration.setTemplateLoader(createTemplateLoader(templateFilePrimaryDir));
-	}
-	
-	/**
-	 * {@link TemplateLoader}を作成する。
-	 * 
-	 * @param templateFilePrimaryDir テンプレートファイルを格納したプライマリディレクトリ、プライマリディレクトリを使用しない場合{@code null}
-	 * @return {@link TemplateLoader}
-	 * @throws IOException 入出力が失敗した場合
-	 */
-	protected TemplateLoader createTemplateLoader(File templateFilePrimaryDir) throws IOException {
-		Validate.notNull(templateFilePrimaryDir);
-		TemplateLoader primary = null;
-		if (templateFilePrimaryDir != null) {
-			primary = new FileTemplateLoader(templateFilePrimaryDir);
-		}
-		TemplateLoader secondary = new ResourceTemplateLoader(DEFAULT_TEMPLATE_DIR_NAME);
-		if (primary == null) {
-			return secondary;
-		}
-		return new MultiTemplateLoader(new TemplateLoader[] {
-			primary,
-			secondary
-		});
-	}
-	
-	/**
-	 * {@code file}が存在する場合に{@code true}を取得する。
-	 * 
-	 * @param file ファイル
-	 * @return {@code file}が存在する場合は{@code true}、そうでない場合は{@code false}
-	 */
-	protected boolean exists(File file) {
-		Validate.notNull(file);
-		return file.exists();
-	}
-	
-	public void generate(GeneratorContext context) throws GenerateException {
-		Validate.notNull(context);
-		boolean exists = exists(context.getFile());
-		if (!context.isOverwrite() && exists) {
-			return;
-		}
-		File dir = context.getFile().getParentFile();
-		if (dir != null) {
-			mkdirs(dir);
-		}
-		Writer writer = null;
-		try {
-			writer = openWriter(context);
-		} catch (FileNotFoundException e) {
-			throw new GenerateException(e);
-		}
-		try {
-			Template template = getTemplate(context.getTemplateName());
-			process(template, context.getModel(), writer);
-		} catch (TemplateException e) {
-			throw new GenerateException(e);
-		} catch (IOException e) {
-			throw new GenerateException(e);
-		} finally {
-			if (writer != null) {
-				try {
-					writer.close();
-				} catch (IOException e) {
-					throw new GenerateException(e);
-				}
-			}
-		}
-		if (writer instanceof DeleteEmptyFileWriter) {
-			if (((DeleteEmptyFileWriter) writer).isDeleted()) {
-				return;
-			}
-		}
-	}
-	
-	/**
-	* テンプレートを取得する。
-	* 
-	* @param name テンプレートの名前
-	* @return テンプレート
-	 * @throws IOException 入出力が失敗した場合
-	*/
-	protected Template getTemplate(String name) throws IOException {
-		Validate.notNull(name);
-		return configuration.getTemplate(name);
-	}
-	
-	/**
-	 * テンプレートを取得する。
-	 * 
-	 * @param name テンプレートの名前
-	 * @return テンプレート
-	 * @throws IOException 入出力が失敗した場合
-	 */
-	protected Template getTemplate1(String name) throws IOException {
-		Validate.notNull(name);
-		return configuration.getTemplate(name);
-	}
-	
-	/**
-	 * ディレクトリを生成します。
-	 * 
-	 * @param dir ディレクトリ
-	 */
-	protected void mkdirs(File dir) {
-		dir.mkdirs();
-	}
-	
-	/**
-	 * {@link Writer}を開きます。
-	 * 
-	 * @param context コンテキスト
-	 * @return {@link Writer}
-	 * @throws FileNotFoundException ファイルが見つからなかった場合
-	 */
-	protected Writer openWriter(GeneratorContext context) throws FileNotFoundException {
-		Charset charset = Charset.forName(context.getEncoding());
-		FileOutputStream fos = FileOutputStreamUtil.create(context.getFile());
-		OutputStreamWriter osw = new OutputStreamWriter(fos, charset);
-		BufferedWriter bw = new BufferedWriter(osw);
-		return new DeleteEmptyFileWriter(bw, context.getFile());
-	}
-	
-	/**
-	 * テンプレートを処理します。
-	 * 
-	 * @param template テンプレート
-	 * @param dataModel データモデル
-	 * @param writer ライタ
-	 * @throws IOException 入出力が失敗した場合
-	 * @throws TemplateException テンプレートの処理に失敗した場合
-	 */
-	protected void process(Template template, Object dataModel, Writer writer) throws TemplateException, IOException {
-		template.process(dataModel, writer);
-	}
-}

Deleted: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/GeneratorContextImpl.java
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/GeneratorContextImpl.java	2009-04-13 10:04:14 UTC (rev 3256)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/GeneratorContextImpl.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -1,107 +0,0 @@
-/*
- * Copyright 2007-2009 Jiemamy Project and the Others.
- * Created on Apr 13, 2009
- *
- * This file is part of Jiemamy.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific language
- * governing permissions and limitations under the License.
- */
-package org.jiemamy.entity.io.gen.generator;
-
-import java.io.File;
-
-
-/**
- * {@link GeneratorContext}の実装クラス。
- * 
- * @author j5ik2o
- */
-public class GeneratorContextImpl implements GeneratorContext {
-	
-	private String encoding;
-	
-	private File file;
-	
-	private Object model;
-	
-	private String templateName;
-	
-	private boolean overwrite;
-	
-
-	public String getEncoding() {
-		return encoding;
-	}
-	
-	public File getFile() {
-		return file;
-	}
-	
-	public Object getModel() {
-		return model;
-	}
-	
-	public String getTemplateName() {
-		return templateName;
-	}
-	
-	public boolean isOverwrite() {
-		return overwrite;
-	}
-	
-	/**
-	 * エンコーディングを設定する。
-	 * 
-	 * @param encoding エンコーディング
-	 */
-	public void setEncoding(String encoding) {
-		this.encoding = encoding;
-	}
-	
-	/**
-	 * 生成するファイルを設定する。
-	 * 
-	 * @param file 生成するファイル
-	 */
-	public void setFile(File file) {
-		this.file = file;
-	}
-	
-	/**
-	 * データモデルを設定する。
-	 * 
-	 * @param model データモデル
-	 */
-	public void setModel(Object model) {
-		this.model = model;
-	}
-	
-	/**
-	 * 上書きフラグを設定する。
-	 * 
-	 * @param overwrite 上書きフラグ
-	 */
-	public void setOverwrite(boolean overwrite) {
-		this.overwrite = overwrite;
-	}
-	
-	/**
-	 * テンプレート名を設定する。
-	 * 
-	 * @param templateName テンプレート名
-	 */
-	public void setTemplateName(String templateName) {
-		this.templateName = templateName;
-	}
-	
-}

Deleted: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/IncludeDirective.java
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/IncludeDirective.java	2009-04-13 10:04:14 UTC (rev 3256)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/IncludeDirective.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -1,68 +0,0 @@
-/*
- * Copyright 2007-2009 Jiemamy Project and the Others.
- * Created on Apr 7, 2009
- *
- * This file is part of Jiemamy.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific language
- * governing permissions and limitations under the License.
- */
-package org.jiemamy.entity.io.gen.generator;
-
-import java.io.IOException;
-import java.util.Map;
-
-import freemarker.core.Environment;
-import freemarker.template.SimpleScalar;
-import freemarker.template.Template;
-import freemarker.template.TemplateDirectiveBody;
-import freemarker.template.TemplateDirectiveModel;
-import freemarker.template.TemplateException;
-import freemarker.template.TemplateModel;
-
-/**
- * インクルードのディレクティブ。
- * <p>
- * インクルード先のテンプレートで任意のオブジェクトをルートのデータモデルに指定できます。
- * </p>
- * 
- * @author j5ik2o
- */
-public class IncludeDirective implements TemplateDirectiveModel {
-	
-	/** インクルードするテンプレート名のパラメータ名 */
-	protected static final String PARAM_NAME = "name";
-	
-	/** ルートモデルのパラメータ名 */
-	protected static final String PARAM_ROOT_MODEL = "rootModel";
-	
-
-	public void execute(Environment env, @SuppressWarnings("unchecked") Map params, TemplateModel[] loopVars,
-			TemplateDirectiveBody body) throws TemplateException, IOException {
-		Object name = params.get(PARAM_NAME);
-		if (name == null) {
-			throw new IllegalArgumentException("params[" + PARAM_NAME + "]");
-		}
-		if (!SimpleScalar.class.isInstance(name)) {
-			throw new IllegalArgumentException("params[" + PARAM_NAME + "]");
-		}
-		Object rootModel = params.get(PARAM_ROOT_MODEL);
-		if (rootModel == null) {
-			throw new IllegalArgumentException("params[" + PARAM_ROOT_MODEL + "]");
-		}
-		Template template = env.getTemplateForInclusion(((SimpleScalar) name).getAsString(), null, true);
-		template.process(rootModel, env.getOut());
-		if (body != null) {
-			body.render(env.getOut());
-		}
-	}
-}

Deleted: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/OnDemandDateModel.java
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/OnDemandDateModel.java	2009-04-13 10:04:14 UTC (rev 3256)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/OnDemandDateModel.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -1,40 +0,0 @@
-/*
- * Copyright 2007-2009 Jiemamy Project and the Others.
- * Created on Apr 7, 2009
- *
- * This file is part of Jiemamy.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific language
- * governing permissions and limitations under the License.
- */
-package org.jiemamy.entity.io.gen.generator;
-
-import java.util.Date;
-
-import freemarker.template.TemplateDateModel;
-import freemarker.template.TemplateModelException;
-
-/**
- * 必要とされるたびに{@link Date}を生成する{@link TemplateDateModel}の実装。
- * 
- * @author j5ik2o
- */
-public class OnDemandDateModel implements TemplateDateModel {
-	
-	public Date getAsDate() throws TemplateModelException {
-		return new Date();
-	}
-	
-	public int getDateType() {
-		return UNKNOWN;
-	}
-}

Deleted: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/ResourceTemplateLoader.java
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/ResourceTemplateLoader.java	2009-04-13 10:04:14 UTC (rev 3256)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/ResourceTemplateLoader.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -1,61 +0,0 @@
-/*
- * Copyright 2007-2009 Jiemamy Project and the Others.
- * Created on Apr 7, 2009
- *
- * This file is part of Jiemamy.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific language
- * governing permissions and limitations under the License.
- */
-package org.jiemamy.entity.io.gen.generator;
-
-import java.net.URL;
-
-import freemarker.cache.TemplateLoader;
-import freemarker.cache.URLTemplateLoader;
-
-import org.apache.commons.lang.Validate;
-
-import org.jiemamy.utils.ResourceUtil;
-
-/**
- * リソースを扱う{@link TemplateLoader}の実装クラス。
- * <p>
- * JARファイルに含まれたリソースを扱う。
- * </p>
- * 
- * @author j5ik2o
- */
-public class ResourceTemplateLoader extends URLTemplateLoader {
-	
-	/** ベースとなるパス */
-	protected String basePath;
-	
-
-	/**
-	 * インスタンスを生成する。
-	 * 
-	 * @param basePath ベースとなるパス
-	 */
-	public ResourceTemplateLoader(String basePath) {
-		Validate.notNull(basePath);
-		this.basePath = basePath;
-	}
-	
-	@Override
-	protected URL getURL(String name) {
-		Validate.notNull(name);
-		String path = basePath + "/" + name;
-		return ResourceUtil.getResourceNoException(path);
-	}
-	
-}

Copied: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/DeleteEmptyFileWriter.java (from rev 3255, leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/DeleteEmptyFileWriter.java)
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/DeleteEmptyFileWriter.java	                        (rev 0)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/DeleteEmptyFileWriter.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2007-2009 Jiemamy Project and the Others.
+ * Created on Apr 7, 2009
+ *
+ * This file is part of Jiemamy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.jiemamy.entity.io.gen.generator.impl;
+
+import java.io.File;
+import java.io.FilterWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+
+import org.jiemamy.utils.FileInputStreamUtil;
+
+/**
+ * 空のファイルを削除する{@link Writer}の実装。
+ * 
+ * @author j5ik2o
+ */
+public class DeleteEmptyFileWriter extends FilterWriter {
+	
+	/** 書き込みが行われた場合{@code true} */
+	protected boolean written;
+	
+	/** 削除された場合{@code true} */
+	protected boolean deleted;
+	
+	/** 書き込み先のファイル */
+	protected File file;
+	
+
+	/**
+	 * インスタンスを生成する。
+	 * 
+	 * @param writer ライタ
+	 * @param file 書き込み先のファイル
+	 */
+	public DeleteEmptyFileWriter(Writer writer, File file) {
+		super(writer);
+		if (file == null) {
+			throw new NullPointerException("file");
+		}
+		this.file = file;
+	}
+	
+	@Override
+	public void close() throws IOException {
+		super.close();
+		if (!written && file.exists() && isEmpty()) {
+			deleted = file.delete();
+		}
+	}
+	
+	/**
+	 * ファイルが削除された場合のフラグを取得する。
+	 * 
+	 * @return 削除された場合{@code true}、そうでない場合{@code false}
+	 */
+	public boolean isDeleted() {
+		return deleted;
+	}
+	
+	/**
+	 * ファイルが空の場合のフラグを取得する。
+	 * 
+	 * @return ファイルが空の場合は{@code true}、そうでない場合{@code false}
+	 * @throws IOException 入出力が失敗した場合
+	 */
+	protected boolean isEmpty() throws IOException {
+		InputStream is = FileInputStreamUtil.create(file);
+		try {
+			return is.read() == -1;
+		} finally {
+			if (is != null) {
+				is.close();
+			}
+		}
+	}
+	
+	@Override
+	public void write(char[] cbuf, int off, int len) throws IOException {
+		if (len <= 0) {
+			return;
+		}
+		written = true;
+		super.write(cbuf, off, len);
+	}
+	
+	@Override
+	public void write(int c) throws IOException {
+		written = true;
+		super.write(c);
+	}
+	
+	@Override
+	public void write(String str, int off, int len) throws IOException {
+		if (len <= 0) {
+			return;
+		}
+		written = true;
+		super.write(str, off, len);
+	}
+	
+}


Property changes on: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/DeleteEmptyFileWriter.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Copied: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/EntitySrcFileGenerator.java (from rev 3252, leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/EntitySrcFileGenerator.java)
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/EntitySrcFileGenerator.java	                        (rev 0)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/EntitySrcFileGenerator.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -0,0 +1,220 @@
+/*
+ * Copyright 2007-2009 Jiemamy Project and the Others.
+ * Created on Apr 13, 2009
+ *
+ * This file is part of Jiemamy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.jiemamy.entity.io.gen.generator.impl;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.nio.charset.Charset;
+import java.util.Locale;
+
+import freemarker.cache.FileTemplateLoader;
+import freemarker.cache.MultiTemplateLoader;
+import freemarker.cache.TemplateLoader;
+import freemarker.template.Configuration;
+import freemarker.template.DefaultObjectWrapper;
+import freemarker.template.Template;
+import freemarker.template.TemplateException;
+
+import org.apache.commons.lang.Validate;
+
+import org.jiemamy.entity.io.gen.generator.GenerateException;
+import org.jiemamy.entity.io.gen.generator.Generator;
+import org.jiemamy.entity.io.gen.generator.GeneratorContext;
+import org.jiemamy.utils.FileOutputStreamUtil;
+
+/**
+ * エンティティを生成するためのジェネレータクラス。
+ * 
+ * @author j5ik2o
+ */
+public class EntitySrcFileGenerator implements Generator {
+	
+	private Configuration configuration;
+	
+	/** デフォルトのテンプレートディレクトリの名前 */
+	protected static final String DEFAULT_TEMPLATE_DIR_NAME = "org/jiemamy/entity/io/tempaltes";
+	
+
+	/**
+	 * インスタンスを生成する。
+	 * 
+	 * @param configuration FreeMarkerの設定
+	 */
+	public EntitySrcFileGenerator(Configuration configuration) {
+		Validate.notNull(configuration);
+		this.configuration = configuration;
+	}
+	
+	/**
+	* インスタンスを生成する。
+	* 
+	* @param templateFileEncoding テンプレートファイルのエンコーディング
+	* @param templateFilePrimaryDir テンプレートファイルを格納したプライマリディレクトリ、プライマリディレクトリを使用しない場合{@code null}
+	 * @throws IOException 入出力が失敗した場合
+	*/
+	public EntitySrcFileGenerator(String templateFileEncoding, File templateFilePrimaryDir) throws IOException {
+		Validate.notNull(templateFileEncoding);
+		configuration = new Configuration();
+		configuration.setObjectWrapper(new DefaultObjectWrapper());
+		configuration.setSharedVariable("include", new IncludeDirective());
+		configuration.setSharedVariable("currentDate", new OnDemandDateModel());
+		configuration.setEncoding(Locale.getDefault(), templateFileEncoding);
+		configuration.setNumberFormat("0.#####");
+		configuration.setTemplateLoader(createTemplateLoader(templateFilePrimaryDir));
+	}
+	
+	/**
+	 * {@link TemplateLoader}を作成する。
+	 * 
+	 * @param templateFilePrimaryDir テンプレートファイルを格納したプライマリディレクトリ、プライマリディレクトリを使用しない場合{@code null}
+	 * @return {@link TemplateLoader}
+	 * @throws IOException 入出力が失敗した場合
+	 */
+	protected TemplateLoader createTemplateLoader(File templateFilePrimaryDir) throws IOException {
+		Validate.notNull(templateFilePrimaryDir);
+		TemplateLoader primary = null;
+		if (templateFilePrimaryDir != null) {
+			primary = new FileTemplateLoader(templateFilePrimaryDir);
+		}
+		TemplateLoader secondary = new ResourceTemplateLoader(DEFAULT_TEMPLATE_DIR_NAME);
+		if (primary == null) {
+			return secondary;
+		}
+		return new MultiTemplateLoader(new TemplateLoader[] {
+			primary,
+			secondary
+		});
+	}
+	
+	/**
+	 * {@code file}が存在する場合に{@code true}を取得する。
+	 * 
+	 * @param file ファイル
+	 * @return {@code file}が存在する場合は{@code true}、そうでない場合は{@code false}
+	 */
+	protected boolean exists(File file) {
+		Validate.notNull(file);
+		return file.exists();
+	}
+	
+	public void generate(GeneratorContext context) throws GenerateException {
+		Validate.notNull(context);
+		boolean exists = exists(context.getFile());
+		if (!context.isOverwrite() && exists) {
+			return;
+		}
+		File dir = context.getFile().getParentFile();
+		if (dir != null) {
+			mkdirs(dir);
+		}
+		Writer writer = null;
+		try {
+			writer = openWriter(context);
+		} catch (FileNotFoundException e) {
+			throw new GenerateException(e);
+		}
+		try {
+			Template template = getTemplate(context.getTemplateName());
+			process(template, context.getModel(), writer);
+		} catch (TemplateException e) {
+			throw new GenerateException(e);
+		} catch (IOException e) {
+			throw new GenerateException(e);
+		} finally {
+			if (writer != null) {
+				try {
+					writer.close();
+				} catch (IOException e) {
+					throw new GenerateException(e);
+				}
+			}
+		}
+		if (writer instanceof DeleteEmptyFileWriter) {
+			if (((DeleteEmptyFileWriter) writer).isDeleted()) {
+				return;
+			}
+		}
+	}
+	
+	/**
+	* テンプレートを取得する。
+	* 
+	* @param name テンプレートの名前
+	* @return テンプレート
+	 * @throws IOException 入出力が失敗した場合
+	*/
+	protected Template getTemplate(String name) throws IOException {
+		Validate.notNull(name);
+		return configuration.getTemplate(name);
+	}
+	
+	/**
+	 * テンプレートを取得する。
+	 * 
+	 * @param name テンプレートの名前
+	 * @return テンプレート
+	 * @throws IOException 入出力が失敗した場合
+	 */
+	protected Template getTemplate1(String name) throws IOException {
+		Validate.notNull(name);
+		return configuration.getTemplate(name);
+	}
+	
+	/**
+	 * ディレクトリを生成します。
+	 * 
+	 * @param dir ディレクトリ
+	 */
+	protected void mkdirs(File dir) {
+		dir.mkdirs();
+	}
+	
+	/**
+	 * {@link Writer}を開きます。
+	 * 
+	 * @param context コンテキスト
+	 * @return {@link Writer}
+	 * @throws FileNotFoundException ファイルが見つからなかった場合
+	 */
+	protected Writer openWriter(GeneratorContext context) throws FileNotFoundException {
+		Charset charset = Charset.forName(context.getEncoding());
+		FileOutputStream fos = FileOutputStreamUtil.create(context.getFile());
+		OutputStreamWriter osw = new OutputStreamWriter(fos, charset);
+		BufferedWriter bw = new BufferedWriter(osw);
+		return new DeleteEmptyFileWriter(bw, context.getFile());
+	}
+	
+	/**
+	 * テンプレートを処理します。
+	 * 
+	 * @param template テンプレート
+	 * @param dataModel データモデル
+	 * @param writer ライタ
+	 * @throws IOException 入出力が失敗した場合
+	 * @throws TemplateException テンプレートの処理に失敗した場合
+	 */
+	protected void process(Template template, Object dataModel, Writer writer) throws TemplateException, IOException {
+		template.process(dataModel, writer);
+	}
+}

Copied: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/GeneratorContextImpl.java (from rev 3252, leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/GeneratorContextImpl.java)
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/GeneratorContextImpl.java	                        (rev 0)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/GeneratorContextImpl.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2007-2009 Jiemamy Project and the Others.
+ * Created on Apr 13, 2009
+ *
+ * This file is part of Jiemamy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.jiemamy.entity.io.gen.generator.impl;
+
+import java.io.File;
+
+import org.jiemamy.entity.io.gen.generator.GeneratorContext;
+
+
+/**
+ * {@link GeneratorContext}の実装クラス。
+ * 
+ * @author j5ik2o
+ */
+public class GeneratorContextImpl implements GeneratorContext {
+	
+	private String encoding;
+	
+	private File file;
+	
+	private Object model;
+	
+	private String templateName;
+	
+	private boolean overwrite;
+	
+
+	public String getEncoding() {
+		return encoding;
+	}
+	
+	public File getFile() {
+		return file;
+	}
+	
+	public Object getModel() {
+		return model;
+	}
+	
+	public String getTemplateName() {
+		return templateName;
+	}
+	
+	public boolean isOverwrite() {
+		return overwrite;
+	}
+	
+	/**
+	 * エンコーディングを設定する。
+	 * 
+	 * @param encoding エンコーディング
+	 */
+	public void setEncoding(String encoding) {
+		this.encoding = encoding;
+	}
+	
+	/**
+	 * 生成するファイルを設定する。
+	 * 
+	 * @param file 生成するファイル
+	 */
+	public void setFile(File file) {
+		this.file = file;
+	}
+	
+	/**
+	 * データモデルを設定する。
+	 * 
+	 * @param model データモデル
+	 */
+	public void setModel(Object model) {
+		this.model = model;
+	}
+	
+	/**
+	 * 上書きフラグを設定する。
+	 * 
+	 * @param overwrite 上書きフラグ
+	 */
+	public void setOverwrite(boolean overwrite) {
+		this.overwrite = overwrite;
+	}
+	
+	/**
+	 * テンプレート名を設定する。
+	 * 
+	 * @param templateName テンプレート名
+	 */
+	public void setTemplateName(String templateName) {
+		this.templateName = templateName;
+	}
+	
+}

Copied: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/IncludeDirective.java (from rev 3255, leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/IncludeDirective.java)
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/IncludeDirective.java	                        (rev 0)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/IncludeDirective.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2007-2009 Jiemamy Project and the Others.
+ * Created on Apr 7, 2009
+ *
+ * This file is part of Jiemamy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.jiemamy.entity.io.gen.generator.impl;
+
+import java.io.IOException;
+import java.util.Map;
+
+import freemarker.core.Environment;
+import freemarker.template.SimpleScalar;
+import freemarker.template.Template;
+import freemarker.template.TemplateDirectiveBody;
+import freemarker.template.TemplateDirectiveModel;
+import freemarker.template.TemplateException;
+import freemarker.template.TemplateModel;
+
+/**
+ * インクルードのディレクティブ。
+ * <p>
+ * インクルード先のテンプレートで任意のオブジェクトをルートのデータモデルに指定できます。
+ * </p>
+ * 
+ * @author j5ik2o
+ */
+public class IncludeDirective implements TemplateDirectiveModel {
+	
+	/** インクルードするテンプレート名のパラメータ名 */
+	protected static final String PARAM_NAME = "name";
+	
+	/** ルートモデルのパラメータ名 */
+	protected static final String PARAM_ROOT_MODEL = "rootModel";
+	
+
+	public void execute(Environment env, @SuppressWarnings("unchecked") Map params, TemplateModel[] loopVars,
+			TemplateDirectiveBody body) throws TemplateException, IOException {
+		Object name = params.get(PARAM_NAME);
+		if (name == null) {
+			throw new IllegalArgumentException("params[" + PARAM_NAME + "]");
+		}
+		if (!SimpleScalar.class.isInstance(name)) {
+			throw new IllegalArgumentException("params[" + PARAM_NAME + "]");
+		}
+		Object rootModel = params.get(PARAM_ROOT_MODEL);
+		if (rootModel == null) {
+			throw new IllegalArgumentException("params[" + PARAM_ROOT_MODEL + "]");
+		}
+		Template template = env.getTemplateForInclusion(((SimpleScalar) name).getAsString(), null, true);
+		template.process(rootModel, env.getOut());
+		if (body != null) {
+			body.render(env.getOut());
+		}
+	}
+}


Property changes on: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/IncludeDirective.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Copied: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/OnDemandDateModel.java (from rev 3255, leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/OnDemandDateModel.java)
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/OnDemandDateModel.java	                        (rev 0)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/OnDemandDateModel.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2007-2009 Jiemamy Project and the Others.
+ * Created on Apr 7, 2009
+ *
+ * This file is part of Jiemamy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.jiemamy.entity.io.gen.generator.impl;
+
+import java.util.Date;
+
+import freemarker.template.TemplateDateModel;
+import freemarker.template.TemplateModelException;
+
+/**
+ * 必要とされるたびに{@link Date}を生成する{@link TemplateDateModel}の実装。
+ * 
+ * @author j5ik2o
+ */
+public class OnDemandDateModel implements TemplateDateModel {
+	
+	public Date getAsDate() throws TemplateModelException {
+		return new Date();
+	}
+	
+	public int getDateType() {
+		return UNKNOWN;
+	}
+}


Property changes on: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/OnDemandDateModel.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Copied: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/ResourceTemplateLoader.java (from rev 3255, leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/ResourceTemplateLoader.java)
===================================================================
--- leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/ResourceTemplateLoader.java	                        (rev 0)
+++ leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/ResourceTemplateLoader.java	2009-04-13 10:05:21 UTC (rev 3257)
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2007-2009 Jiemamy Project and the Others.
+ * Created on Apr 7, 2009
+ *
+ * This file is part of Jiemamy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.jiemamy.entity.io.gen.generator.impl;
+
+import java.net.URL;
+
+import freemarker.cache.TemplateLoader;
+import freemarker.cache.URLTemplateLoader;
+
+import org.apache.commons.lang.Validate;
+
+import org.jiemamy.utils.ResourceUtil;
+
+/**
+ * リソースを扱う{@link TemplateLoader}の実装クラス。
+ * <p>
+ * JARファイルに含まれたリソースを扱う。
+ * </p>
+ * 
+ * @author j5ik2o
+ */
+public class ResourceTemplateLoader extends URLTemplateLoader {
+	
+	/** ベースとなるパス */
+	protected String basePath;
+	
+
+	/**
+	 * インスタンスを生成する。
+	 * 
+	 * @param basePath ベースとなるパス
+	 */
+	public ResourceTemplateLoader(String basePath) {
+		Validate.notNull(basePath);
+		this.basePath = basePath;
+	}
+	
+	@Override
+	protected URL getURL(String name) {
+		Validate.notNull(name);
+		String path = basePath + "/" + name;
+		return ResourceUtil.getResourceNoException(path);
+	}
+	
+}


Property changes on: leto/jiemamy-entity-io/trunk/src/main/java/org/jiemamy/entity/io/gen/generator/impl/ResourceTemplateLoader.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain



Jiemamy-notify メーリングリストの案内
Back to archive index