[Jiemamy-notify] commit [2024] ModelをInputStreamするModelInputStreamを作成しました.かなり実装はいいかげんなんで,適宜,改善を行っていきましょう.

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2008年 10月 23日 (木) 02:16:39 JST


Revision: 2024
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=jiemamy&view=rev&rev=2024
Author:   j5ik2o
Date:     2008-10-23 02:16:39 +0900 (Thu, 23 Oct 2008)

Log Message:
-----------
ModelをInputStreamするModelInputStreamを作成しました.かなり実装はいいかげんなんで,適宜,改善を行っていきましょう.

Added Paths:
-----------
    artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelInputStream.java
    artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/ModelInputStreamTest.java


-------------- next part --------------
Added: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelInputStream.java
===================================================================
--- artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelInputStream.java	                        (rev 0)
+++ artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelInputStream.java	2008-10-22 17:16:39 UTC (rev 2024)
@@ -0,0 +1,184 @@
+/*
+ * Copyright 2007-2008 MIYAMOTO Daisuke, jiemamy.org and the Others.
+ * Created on 2008/10/22
+ *
+ * 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.serializer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Queue;
+import java.util.Stack;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import com.megginson.sax.DataWriter;
+
+import org.xml.sax.SAXException;
+
+import org.jiemamy.spec.model.JiemamyModel;
+import org.jiemamy.spec.model.RootModel;
+
+/**
+ * TODO for junichi
+ * @author junichi
+ */
+public class ModelInputStream extends InputStream {
+	
+	private Queue<Byte> resourceQueues = new LinkedBlockingQueue<Byte>();
+	
+	private DataWriter dataWriter;
+	
+	private StringWriter stringWriter;
+	
+	private Stack<JiemamyModel> nextWriteModelStacks = new Stack<JiemamyModel>();
+	
+
+	/**
+	 * コンストラクタ。
+	 * @param rootModel
+	 * @category instance creation
+	 */
+	public ModelInputStream(RootModel rootModel) {
+		stringWriter = (new StringWriter());
+		dataWriter = (new DataWriter(stringWriter));
+		nextWriteModelStacks.push(rootModel);
+	}
+	
+	/**
+	 * 要求された1モデルだけを読み込み,Byteのキューに追加していく
+	 * @return 読み込むデータがない場合false, ある場合true
+	 * @throws InvocationTargetException 
+	 * @throws IllegalAccessException 
+	 * @throws IllegalArgumentException 
+	 */
+	private boolean loadFromModel() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
+		try {
+			if (nextWriteModelStacks.empty()) {
+				return false;
+			}
+			dataWriter.setIndentStep(2);
+			dataWriter.startDocument();
+			List<JiemamyModel> nextObjects = writeModel(nextWriteModelStacks.pop());
+			for (JiemamyModel next : nextObjects) {
+				nextWriteModelStacks.push(next);
+			}
+			dataWriter.endDocument();
+			for (byte b : stringWriter.toString().getBytes()) {
+				resourceQueues.add(b);
+			}
+			dataWriter = (new DataWriter(stringWriter));
+		} catch (SAXException e) {
+			e.printStackTrace();
+		}
+		return resourceQueues.size() > 0 || !nextWriteModelStacks.empty();
+	}
+	
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public int read() throws IOException {
+		if (resourceQueues.size() == 0) {
+			try {
+				if (!loadFromModel()) {
+					return -1;
+				}
+			} catch (IllegalArgumentException e) {
+				throw new IOException();
+			} catch (IllegalAccessException e) {
+				throw new IOException();
+			} catch (InvocationTargetException e) {
+				throw new IOException();
+			}
+		}
+		return resourceQueues.poll().intValue();
+	}
+	
+	/**
+	 * モデルをDataWriterに書きこむ.
+	 * @param jiemamyModel
+	 * @return 次にwriteしないといけないモデルのリスト
+	 * @throws SAXException 
+	 * @throws InvocationTargetException 
+	 * @throws IllegalAccessException 
+	 * @throws IllegalArgumentException 
+	 */
+	private List<JiemamyModel> writeModel(JiemamyModel jiemamyModel) throws SAXException, IllegalArgumentException,
+			IllegalAccessException, InvocationTargetException {
+		List<JiemamyModel> nextWriteModelList = new ArrayList<JiemamyModel>();
+		Class<? extends Object> clazz = jiemamyModel.getClass();
+		
+		String className = clazz.getName();
+		dataWriter.startElement(className);
+		dataWriter.dataElement("id", jiemamyModel.getId().toString());
+		while (clazz != Object.class) {
+			Method[] methods = clazz.getMethods();
+			for (Method method : methods) {
+				if ((method.getParameterTypes().length > 0) || (!method.getName().startsWith("get"))
+						|| (method.getModifiers() == Modifier.STATIC) || (!method.getName().startsWith("get")) || (method.getModifiers() == Modifier.STATIC)) {
+					continue;
+				}
+				Object value = method.invoke(jiemamyModel, new Object[0]);
+				if (value != null) {
+					if (value instanceof String) {
+						dataWriter.startElement(method.getName());
+						dataWriter.dataElement(value.getClass().getName(), (String) value);
+						dataWriter.endElement(method.getName());
+					} else if (value instanceof Integer) {
+						dataWriter.startElement(method.getName());
+						dataWriter.dataElement(value.getClass().getName(), ((Integer) value).toString());
+						dataWriter.endElement(method.getName());
+					} else if (value instanceof Long) {
+						dataWriter.startElement(method.getName());
+						dataWriter.dataElement(value.getClass().getName(), ((Long) value).toString());
+						dataWriter.endElement(method.getName());
+					} else if (value instanceof Float) {
+						dataWriter.startElement(method.getName());
+						dataWriter.dataElement(value.getClass().getName(), ((Float) value).toString());
+						dataWriter.endElement(method.getName());
+					} else if (value instanceof JiemamyModel) {
+						String refClassName = ((JiemamyModel) value).getClass().getName();
+						dataWriter.startElement(refClassName);
+						dataWriter.dataElement("refid", ((JiemamyModel) value).getId().toString());
+						dataWriter.endElement(refClassName);
+						nextWriteModelList.add(((JiemamyModel) value));
+					} else if (value instanceof Collection) {
+						for (Object e : ((Collection<?>) value)) {
+							if (e instanceof JiemamyModel) {
+								String refClassName = ((JiemamyModel) e).getClass().getName();
+								dataWriter.startElement(refClassName);
+								dataWriter.dataElement("refid", ((JiemamyModel) e).getId().toString());
+								dataWriter.endElement(refClassName);
+								nextWriteModelList.add((JiemamyModel) e);
+							}
+						}
+					}
+				}
+			}
+			clazz = clazz.getSuperclass();
+		}
+		dataWriter.endElement(className);
+		
+		return nextWriteModelList;
+	}
+}


Property changes on: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelInputStream.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/ModelInputStreamTest.java
===================================================================
--- artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/ModelInputStreamTest.java	                        (rev 0)
+++ artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/ModelInputStreamTest.java	2008-10-22 17:16:39 UTC (rev 2024)
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2007-2008 MIYAMOTO Daisuke, jiemamy.org and the Others.
+ * Created on 2008/10/22
+ *
+ * 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.serializer;
+
+import org.junit.Test;
+
+import org.jiemamy.core.S2FactoryStrategy;
+import org.jiemamy.creator.JiemamyModelFactory;
+import org.jiemamy.spec.model.RootModel;
+import org.jiemamy.spec.model.node.TableModel;
+
+/**
+ * TODO for junichi
+ * @author junichi
+ */
+public class ModelInputStreamTest {
+	
+	/**
+	 * モデルをInputStreamに変えるテスト
+	 * @throws Exception
+	 */
+	@Test
+	public void testRead() throws Exception {
+		JiemamyModelFactory.init(new S2FactoryStrategy("jiemamy-view.dicon"));
+		
+		RootModel rootModel = JiemamyModelFactory.createRoot().init();
+		rootModel.setDialectClassName("org.jiemamy.dialect.mysql.MySqlDialect"); // MySQLDialect.class.getName()	
+		rootModel.setBeginScript("BEGIN;");
+		rootModel.setEndScript("COMMIT;");
+		rootModel.setDescription("シリアライゼーションイメージ");
+		rootModel.setSchemaName("FOO");
+		
+		TableModel t = rootModel.createJiemamyModel(TableModel.class).init(rootModel).init("T_DEPT");
+		t.setDescription("HOGEHOGE");
+		rootModel.appendModel(t);
+		
+		ModelInputStream mis = null;
+		try {
+			mis = new ModelInputStream(rootModel);
+			byte[] buffer = new byte[50];
+			while (-1 != mis.read(buffer)) {
+				System.out.print(new String(buffer));
+				buffer = new byte[255];
+			}
+		} finally {
+			mis.close();
+		}
+	}
+}


Property changes on: artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/ModelInputStreamTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain


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