svnno****@sourc*****
svnno****@sourc*****
2008年 10月 29日 (水) 00:51:46 JST
Revision: 2059 http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=jiemamy&view=rev&rev=2059 Author: daisuke_m Date: 2008-10-29 00:51:46 +0900 (Wed, 29 Oct 2008) Log Message: ----------- ModelWriter、なんとなく形になってきた。まだ動きは完全じゃない。 Modified Paths: -------------- artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelWriter.java artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/XmlElement.java artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/ModelInputStreamTest.java -------------- next part -------------- Modified: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelWriter.java =================================================================== --- artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelWriter.java 2008-10-28 15:49:30 UTC (rev 2058) +++ artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelWriter.java 2008-10-28 15:51:46 UTC (rev 2059) @@ -21,12 +21,13 @@ import java.io.StringWriter; 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.serializer.XmlElement.ElementState; +import org.jiemamy.spec.model.JiemamyModel; import org.jiemamy.spec.model.RootModel; /** @@ -36,13 +37,11 @@ */ public class ModelWriter { + private static final String NAMESPACE = "http://jiemamy.org/ns/model/v1"; + /** 処理中のモデル構造スタック */ - private Stack<Queue<XmlElement>> nextWriteModelStack = new Stack<Queue<XmlElement>>(); + private Stack<XmlElement> xmlStack = new Stack<XmlElement>(); - private Queue<XmlElement> currentQueue; - - private XmlElement currentElement; - private DataWriter dataWriter = new DataWriter(); private Queue<Byte> resourceQueue; @@ -56,16 +55,20 @@ */ public ModelWriter(RootModel rootModel, Queue<Byte> resourceQueue) { this.resourceQueue = resourceQueue; + dataWriter.setPrefix(NAMESPACE, ""); dataWriter.setIndentStep(2); + + StringWriter sw = new StringWriter(); + dataWriter.setOutput(sw); try { dataWriter.startDocument(); } catch (SAXException e) { e.printStackTrace(); } - Queue<XmlElement> rootElement = new LinkedBlockingQueue<XmlElement>(); - rootElement.add(new XmlElement("rootModel", rootModel)); - nextWriteModelStack.push(rootElement); + addToQueue(sw.toString().getBytes()); + + xmlStack.push(new XmlElement(rootModel)); } /** @@ -94,84 +97,65 @@ * @throws SAXException */ public boolean write() throws SAXException { + if (xmlStack.isEmpty()) { + return false; + } StringWriter sw = new StringWriter(); dataWriter.setOutput(sw); // 毎回出力先を更新する -// // 次に出力すべきエレメントの特定 -// boolean opened = true; -// while (currentElement == null) { -// if (currentQueue == null) { -// if (nextWriteModelStack.empty()) { -// return false; -// } -// currentQueue = nextWriteModelStack.peek(); -// } -// -// if (currentQueue.isEmpty()) { -// dataWriter.endElement(currentElement.name); -// nextWriteModelStack.pop(); -// currentQueue = null; -// currentElement = null; -// } else { -// currentElement = currentQueue.poll(); -// opened = false; -// } -// } -// -// // モデルの出力 -// if (currentElement.content instanceof JiemamyModel) { -// if (opened == false) { -// dataWriter.startElement(currentElement.name); -// } -// writeJiemamyModel(currentElement); -// } else if (currentElement.content instanceof Collection) { -// Collection<?> collection = (Collection<?>) currentElement.content; -// Iterator<?> iterator = collection.iterator(); -// Queue<XmlElement> queue = new LinkedBlockingQueue<XmlElement>(); -// while (iterator.hasNext()) { -// queue.add(new XmlElement("TODO", iterator.next())); -// } -// nextWriteModelStack.push(queue); -// } else if (currentElement.content instanceof Map) { -// Map map = (Map) currentElement.content; -// // TODO Mapの時の処理 -// } else { -// write(currentElement); -// } + XmlElement currentElement = xmlStack.peek(); + write(currentElement); - // 今回出力された内容をキューに突っ込む - for (byte b : sw.toString().getBytes()) { - resourceQueue.add(b); - } + addToQueue(sw.toString().getBytes()); return resourceQueue.size() > 0; - } /** - * TODO for daisuke - * @param element - * @throws SAXException + * 今回出力された内容をキューに突っ込む。 + * @param bytes */ - private void write(XmlElement element) throws SAXException { - if (element.content == null) { - dataWriter.emptyElement(element.name); - } else { - dataWriter.dataElement(element.name, element.content.toString()); + private void addToQueue(byte[] bytes) { + for (byte b : bytes) { + resourceQueue.add(b); } } - /** - * TODO for daisuke - * @param currentElement - */ - private void writeJiemamyModel(XmlElement currentElement) { - // TODO Auto-generated method stub -// if(currentElement.content instanceof TableModel) { - Queue<XmlElement> queue = new LinkedBlockingQueue<XmlElement>(); - queue.add(new XmlElement("a", "b")); - nextWriteModelStack.push(queue); -// } + private void write(XmlElement element) throws SAXException { + if (element.getState() == ElementState.INIT) { + if (element.getContent() instanceof JiemamyModel) { + if (element.hasNext()) { + dataWriter.startElement(element.getName()); + element.setState(ElementState.PROGRESS); + xmlStack.push(element.next()); + } else { + dataWriter.emptyElement(element.getName()); + element.setState(ElementState.FINISHED); + xmlStack.pop(); + } + } else { + if (element.getContent() == null) { + dataWriter.emptyElement(element.getName()); + element.setState(ElementState.FINISHED); + xmlStack.pop(); + } else { + dataWriter.dataElement(element.getName(), element.getContent().toString()); + element.setState(ElementState.FINISHED); + xmlStack.pop(); + } + } + } else if (element.getState() == ElementState.PROGRESS) { + if (element.getContent() instanceof JiemamyModel) { + if (element.hasNext()) { + xmlStack.push(element.next()); + } else { + dataWriter.endElement(element.getName()); + element.setState(ElementState.FINISHED); + xmlStack.pop(); + } + } else { + throw new IllegalStateException(); + } + } } - } Modified: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/XmlElement.java =================================================================== --- artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/XmlElement.java 2008-10-28 15:49:30 UTC (rev 2058) +++ artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/XmlElement.java 2008-10-28 15:51:46 UTC (rev 2059) @@ -18,30 +18,153 @@ */ package org.jiemamy.serializer; -public class XmlElement { +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Queue; + +import org.apache.commons.lang.ClassUtils; + +import org.jiemamy.spec.model.RootModel; +import org.jiemamy.spec.model.node.TableModel; + +class XmlElement implements Iterator<XmlElement> { - public String name; + private String name; - public Object content; + private Object content; + private ElementState state = ElementState.INIT; + + /** 未出力エレメントのキュー */ + private Queue<XmlElement> queue = new LinkedList<XmlElement>(); + /** * コンストラクタ。 + * @param content + * @category instance creation + */ + XmlElement(Object content) { + this(null, content); + } + + /** + * コンストラクタ。 * @param name * @param content * @category instance creation */ - public XmlElement(String name, Object content) { - this.name = name; + XmlElement(String name, Object content) { this.content = content; + + if (name == null) { + if (content instanceof RootModel) { + this.name = "rootModel"; + } else if (content instanceof TableModel) { + this.name = "table"; + } else { + this.name = ClassUtils.getShortClassName(content, "null"); + } + } else { + this.name = name; + } + + if (content instanceof Collection) { + Collection<?> collection = (Collection<?>) content; + Iterator<?> ite = collection.iterator(); + while (ite.hasNext()) { + queue.add(new XmlElement(ite.next())); + } + } else if (content instanceof Map) { + // TODO + } else if (content instanceof RootModel) { + RootModel rootModel = (RootModel) content; + queue.add(new XmlElement("dialect", rootModel.getDialectClassName())); + queue.add(new XmlElement("domains", rootModel.getDomains())); + queue.add(new XmlElement("nodes", rootModel.getNodes())); + } } /** * {@inheritDoc} */ + public boolean hasNext() { + return queue.isEmpty() == false; + } + + /** + * {@inheritDoc} + */ + public XmlElement next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + return queue.poll(); + } + + /** + * {@inheritDoc} + */ + public void remove() { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ @Override public String toString() { - return "<" + name + ">" + content + "</" + name + ">"; + return name + ":" + content; } + Object getContent() { + return content; + } + +// Queue<XmlElement> getContentAsQueue() { +// Queue<XmlElement> result = new LinkedList<XmlElement>(); +// if (content instanceof Collection) { +// Collection<?> items = (Collection<?>) content; +// for (Object item : items) { +// result.offer(new XmlElement("collectionItem", item)); +// } +// } else if (content instanceof Map) { +// Map<?, ?> items = (Map<?, ?>) content; +// for (Map.Entry<?, ?> item : items.entrySet()) { +// result.offer(new XmlElement("mapItem", item.getKey() + ":" + item.getValue())); +// } +// } else if (content instanceof JiemamyModel) { +// result.add(new XmlElement(content.getClass().getName(), content.toString())); +// } +// return result; +// } + + String getName() { + return name; + } + + ElementState getState() { + return state; + } + + void setContent(Object content) { + this.content = content; + } + + void setName(String name) { + this.name = name; + } + + void setState(ElementState state) { + this.state = state; + } + + + enum ElementState { + INIT, PROGRESS, FINISHED + } + } Modified: 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 2008-10-28 15:49:30 UTC (rev 2058) +++ artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/ModelInputStreamTest.java 2008-10-28 15:51:46 UTC (rev 2059) @@ -153,7 +153,7 @@ sbExpected.appendln("</rootModel>"); sbExpected.appendln(""); - System.out.println(sbExpected.toString()); +// System.out.println(sbExpected.toString()); assertEquals(sbExpected.toString(), sbActual.toString()); } }