OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3496 ae02f08e...
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Wed, 29 Jul 2009 09:40:18 +0000 (09:40 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Wed, 29 Jul 2009 09:40:18 +0000 (09:40 +0000)
src/main/java/org/xerial/json/JSONWriter.java
src/main/java/org/xerial/lens/XMLSilkLens.java [new file with mode: 0644]
src/main/java/org/xerial/silk/SilkNodeWriter.java [new file with mode: 0644]
src/main/java/org/xerial/silk/SilkWriter.java
src/test/java/org/xerial/json/JSONWriterTest.java
src/test/java/org/xerial/lens/XMLSilkLensTest.java [new file with mode: 0644]
src/test/java/org/xerial/lens/track-config.xml [new file with mode: 0644]
src/test/java/org/xerial/silk/SilkWriterTest.java [new file with mode: 0644]
src/test/java/org/xerial/silk/TreeWalkLog.java

index 1d2adb1..7317e12 100644 (file)
@@ -40,8 +40,7 @@ import org.xerial.lens.ObjectLens;
  * @author leo
  * 
  */
-public class JSONWriter
-{
+public class JSONWriter {
     private final PrintWriter writer;
 
     enum JSONState {
@@ -51,56 +50,49 @@ public class JSONWriter
     private final LinkedList<JSONState> stateStack = new LinkedList<JSONState>();
     private final LinkedList<Integer> elementCountStack = new LinkedList<Integer>();
 
-    public JSONWriter(Writer writer)
-    {
+    public JSONWriter(Writer writer) {
         this.writer = new PrintWriter(writer);
         stateStack.add(JSONState.Root);
         elementCountStack.add(0);
     }
 
-    private void pushState(JSONState state)
-    {
+    private void pushState(JSONState state) {
         stateStack.add(state);
         elementCountStack.add(0);
     }
 
-    private void popState()
-    {
+    private void popState() {
         stateStack.removeLast();
         elementCountStack.removeLast();
     }
 
-    private JSONState getCurrentState()
-    {
+    private JSONState getCurrentState() {
         if (stateStack.isEmpty())
             return JSONState.Unknown;
         else
             return stateStack.getLast();
     }
 
-    private int getPreviousElementCount()
-    {
+    private int getPreviousElementCount() {
         return elementCountStack.isEmpty() ? 0 : elementCountStack.getLast();
     }
 
-    private void incrementElementCount()
-    {
+    private void incrementElementCount() {
         int count = elementCountStack.removeLast();
         elementCountStack.add(++count);
     }
 
-    public void startObject()
-    {
+    public void startObject() {
         if (getCurrentState() == JSONState.InArray)
             putComma();
         writer.print("{");
         pushState(JSONState.InObject);
     }
 
-    public void endObject()
-    {
+    public void endObject() {
         if (getCurrentState() != JSONState.InObject)
-            throw new XerialError(JSONErrorCode.NotInAJSONObject, "cannot end the object outside of the JSON object");
+            throw new XerialError(JSONErrorCode.NotInAJSONObject,
+                    "cannot end the object outside of the JSON object");
         writer.print("}");
         popState();
 
@@ -108,23 +100,21 @@ public class JSONWriter
             incrementElementCount();
     }
 
-    public void startArray()
-    {
+    public void startArray() {
         writer.print("[");
         pushState(JSONState.InArray);
     }
 
-    public void endArray()
-    {
+    public void endArray() {
         if (getCurrentState() != JSONState.InArray)
-            throw new XerialError(JSONErrorCode.NotInAJSONArray, "cannot end the arry outside of the JSON array");
+            throw new XerialError(JSONErrorCode.NotInAJSONArray,
+                    "cannot end the arry outside of the JSON array");
 
         writer.print("]");
         popState();
     }
 
-    public void startString()
-    {
+    public void startString() {
         if (getCurrentState() == JSONState.InArray)
             throw new XerialError(JSONErrorCode.NotInAJSONArray,
                     "cannot start a new string value outside of the JSON array in this method");
@@ -133,15 +123,13 @@ public class JSONWriter
         pushState(JSONState.InString);
     }
 
-    public void startString(String key)
-    {
-        outputKeyPart(key);
+    public void startString(String key) {
+        putKeyPart(key);
         writer.append("\"");
         pushState(JSONState.InString);
     }
 
-    public void append(String stringFragment)
-    {
+    public void append(String stringFragment) {
         if (getCurrentState() != JSONState.InString)
             throw new XerialError(JSONErrorCode.NotInAJSONString,
                     "cannot append any string before invoking startString() method");
@@ -149,8 +137,7 @@ public class JSONWriter
         writer.append(stringFragment);
     }
 
-    public void endString() throws IOException
-    {
+    public void endString() throws IOException {
         if (getCurrentState() != JSONState.InString)
             throw new XerialError(JSONErrorCode.NotInAJSONString,
                     "cannot end the string not beginning from startString() method.");
@@ -158,8 +145,7 @@ public class JSONWriter
         popState();
     }
 
-    public void startArray(String key)
-    {
+    public void startArray(String key) {
         if (getCurrentState() != JSONState.InObject)
             throw new XerialError(JSONErrorCode.NotInAJSONObject,
                     "cannot start a keyed array outside of the JSON object");
@@ -172,52 +158,43 @@ public class JSONWriter
         pushState(JSONState.InArray);
     }
 
-    public void add(String value)
-    {
+    public void add(String value) {
         addInternal(doubleQuote(value));
     }
 
-    public void add(int value)
-    {
+    public void add(int value) {
         addInternal(value);
     }
 
-    public void add(long value)
-    {
+    public void add(long value) {
         addInternal(value);
     }
 
-    public void add(double value)
-    {
+    public void add(double value) {
         addInternal(value);
     }
 
-    public void add(float value)
-    {
+    public void add(float value) {
         addInternal(value);
     }
 
-    public void add(boolean bool)
-    {
+    public void add(boolean bool) {
         if (bool)
             addInternal("true");
         else
             addInternal("false");
     }
 
-    public void addNull()
-    {
+    public void addNull() {
         addInternal("null");
     }
 
-    public void addObject(Object bean)
-    {
+    public void addObject(Object bean) {
         String jsonObject = ObjectLens.toJSON(bean);
         addInternal(jsonObject);
     }
 
-    private void addInternal(Object value)
-    {
+    private void addInternal(Object value) {
         putComma();
         if (value != null)
             writer.append(value.toString());
@@ -226,58 +203,48 @@ public class JSONWriter
         incrementElementCount();
     }
 
-    private void putComma()
-    {
+    private void putComma() {
         if (getPreviousElementCount() > 0)
             writer.print(",");
     }
 
-    public void put(String key, String value)
-    {
+    public void put(String key, String value) {
         putInternal(key, doubleQuote(value));
     }
 
-    public void put(String key, int value)
-    {
+    public void put(String key, int value) {
         putInternal(key, value);
     }
 
-    public void put(String key, float value)
-    {
+    public void put(String key, float value) {
         putInternal(key, value);
     }
 
-    public void put(String key, double value)
-    {
+    public void put(String key, double value) {
         putInternal(key, value);
     }
 
-    public void put(String key, long value)
-    {
+    public void put(String key, long value) {
         putInternal(key, value);
     }
 
-    public void put(String key, boolean value)
-    {
+    public void put(String key, boolean value) {
         if (value)
             putInternal(key, "true");
         else
             putInternal(key, "false");
     }
 
-    public void putObject(String key, Object obj)
-    {
+    public void putObject(String key, Object obj) {
         if (obj == null)
             return; // output nothing
-        else
-        {
+        else {
             putInternal(key, ObjectLens.toJSON(obj));
         }
 
     }
 
-    public void putNull(String key)
-    {
+    public void putNull(String key) {
         putInternal(key, "null");
     }
 
@@ -289,26 +256,22 @@ public class JSONWriter
      * @throws IOException
      * @throws IOException
      */
-    public void putString(String key, Reader input) throws IOException
-    {
-        outputKeyPart(key);
+    public void putString(String key, Reader input) throws IOException {
+        putKeyPart(key);
         writer.append("\"");
-        for (int ch; (ch = input.read()) != -1;)
-        {
+        for (int ch; (ch = input.read()) != -1;) {
             writer.append((char) ch);
         }
         writer.append("\"");
     }
 
-    private void putInternal(String key, Object value)
-    {
-        outputKeyPart(key);
+    private void putInternal(String key, Object value) {
+        putKeyPart(key);
         writer.append(value.toString());
         incrementElementCount();
     }
 
-    private void outputKeyPart(String key)
-    {
+    private void putKeyPart(String key) {
         if (getCurrentState() != JSONState.InObject)
             throw new XerialError(JSONErrorCode.NotInAJSONObject,
                     "cannot add key and value pair outside of the JSON object");
@@ -316,20 +279,23 @@ public class JSONWriter
             writer.append(",");
         writer.append(doubleQuote(key));
         writer.append(":");
+        incrementElementCount();
+    }
+
+    public void startObject(String key) {
+        putKeyPart(key);
+        startObject();
     }
 
-    public void flush()
-    {
+    public void flush() {
         writer.flush();
     }
 
-    public void endJSON()
-    {
-        for (ListIterator<JSONState> it = stateStack.listIterator(stateStack.size()); it.hasPrevious();)
-        {
+    public void endJSON() {
+        for (ListIterator<JSONState> it = stateStack.listIterator(stateStack.size()); it
+                .hasPrevious();) {
             JSONState state = it.previous();
-            switch (state)
-            {
+            switch (state) {
             case InObject:
                 writer.append("}");
                 break;
@@ -346,13 +312,11 @@ public class JSONWriter
         flush();
     }
 
-    private String doubleQuote(String value)
-    {
+    private String doubleQuote(String value) {
         return "\"" + value + "\"";
     }
 
-    public Writer getWriter()
-    {
+    public Writer getWriter() {
         return writer;
     }
 
diff --git a/src/main/java/org/xerial/lens/XMLSilkLens.java b/src/main/java/org/xerial/lens/XMLSilkLens.java
new file mode 100644 (file)
index 0000000..7d95716
--- /dev/null
@@ -0,0 +1,184 @@
+/*--------------------------------------------------------------------------\r
+ *  Copyright 2009 Taro L. Saito\r
+ *\r
+ *  Licensed under the Apache License, Version 2.0 (the "License");\r
+ *  you may not use this file except in compliance with the License.\r
+ *  You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// XMLSilkLens.java\r
+// Since: Jul 28, 2009 11:41:27 AM\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.lens;\r
+\r
+import java.io.Reader;\r
+import java.io.StringWriter;\r
+import java.io.Writer;\r
+\r
+import javax.xml.parsers.SAXParser;\r
+import javax.xml.parsers.SAXParserFactory;\r
+\r
+import org.xerial.core.XerialErrorCode;\r
+import org.xerial.core.XerialException;\r
+import org.xerial.silk.SilkWriter;\r
+import org.xerial.util.tree.TreeVisitor;\r
+import org.xerial.util.tree.TreeWalker;\r
+import org.xml.sax.Attributes;\r
+import org.xml.sax.ContentHandler;\r
+import org.xml.sax.InputSource;\r
+import org.xml.sax.Locator;\r
+import org.xml.sax.SAXException;\r
+import org.xml.sax.XMLReader;\r
+\r
+/**\r
+ * XML to Silk format converter\r
+ * \r
+ * @author leo\r
+ * \r
+ */\r
+public class XMLSilkLens {\r
+\r
+    private static class XMLToSilk implements TreeVisitor {\r
+\r
+        private final SilkWriter out;\r
+\r
+        public XMLToSilk(Writer out) {\r
+            this.out = new SilkWriter(out);\r
+\r
+        }\r
+\r
+        public void finish(TreeWalker walker) throws XerialException {\r
+\r
+            out.flush();\r
+        }\r
+\r
+        public void init(TreeWalker walker) throws XerialException {\r
+\r
+            out.preamble();\r
+        }\r
+\r
+        public void leaveNode(String nodeName, TreeWalker walker) throws XerialException {\r
+            out.leaveNode();\r
+        }\r
+\r
+        public void text(String nodeName, String textDataFragment, TreeWalker walker)\r
+                throws XerialException {\r
+\r
+        }\r
+\r
+        public void visitNode(String nodeName, String immediateNodeValue, TreeWalker walker)\r
+                throws XerialException {\r
+            if (immediateNodeValue == null)\r
+                out.startNode(nodeName);\r
+            else\r
+                out.startNode(nodeName, immediateNodeValue);\r
+\r
+        }\r
+\r
+    }\r
+\r
+    private static class XMLToSilkSAXHandler implements ContentHandler {\r
+        private final SilkWriter out;\r
+\r
+        public XMLToSilkSAXHandler(Writer out) {\r
+            this.out = new SilkWriter(out);\r
+        }\r
+\r
+        public void characters(char[] ch, int start, int length) throws SAXException {\r
+\r
+        }\r
+\r
+        public void endDocument() throws SAXException {\r
+            out.flush();\r
+        }\r
+\r
+        public void endElement(String uri, String localName, String name) throws SAXException {\r
+            out.leaveNode();\r
+        }\r
+\r
+        public void endPrefixMapping(String prefix) throws SAXException {\r
+\r
+        }\r
+\r
+        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {\r
+\r
+        }\r
+\r
+        public void processingInstruction(String target, String data) throws SAXException {\r
+\r
+        }\r
+\r
+        public void setDocumentLocator(Locator locator) {\r
+\r
+        }\r
+\r
+        public void skippedEntity(String name) throws SAXException {\r
+\r
+        }\r
+\r
+        public void startDocument() throws SAXException {\r
+            out.preamble();\r
+        }\r
+\r
+        public void startElement(String uri, String localName, String name, Attributes atts)\r
+                throws SAXException {\r
+            out.startNode(name);\r
+        }\r
+\r
+        public void startPrefixMapping(String prefix, String uri) throws SAXException {\r
+\r
+        }\r
+\r
+    }\r
+\r
+    public static String toSilk(Reader xml) throws XerialException {\r
+\r
+        StringWriter buf = new StringWriter();\r
+\r
+        try {\r
+            SAXParserFactory spf = SAXParserFactory.newInstance();\r
+\r
+            // Set namespaceAware to true to get a parser that corresponds to\r
+            // the default SAX2 namespace feature setting.  This is necessary\r
+            // because the default value from JAXP 1.0 was defined to be false.\r
+            spf.setNamespaceAware(true);\r
+\r
+            // Validation part 1: set whether validation is on\r
+            //spf.setValidating(dtdValidate || xsdValidate);\r
+\r
+            // Create a JAXP SAXParser\r
+            SAXParser saxParser = spf.newSAXParser();\r
+\r
+            // Get the encapsulated SAX XMLReader\r
+            XMLReader xmlReader = saxParser.getXMLReader();\r
+            // Set the ContentHandler of the XMLReader\r
+            xmlReader.setContentHandler(new XMLToSilkSAXHandler(buf));\r
+\r
+            xmlReader.parse(new InputSource(xml));\r
+\r
+        }\r
+        catch (Exception e) {\r
+            throw new XerialException(XerialErrorCode.INVALID_STATE,\r
+                    "failed to instantiate the XML parser: " + e);\r
+        }\r
+\r
+        //        XMLTreeWalker treeWalker = new XMLTreeWalker(xml);\r
+        //      treeWalker.walk(new XMLToSilk(buf));\r
+\r
+        return buf.toString();\r
+    }\r
+\r
+}\r
diff --git a/src/main/java/org/xerial/silk/SilkNodeWriter.java b/src/main/java/org/xerial/silk/SilkNodeWriter.java
new file mode 100644 (file)
index 0000000..c0c8ff5
--- /dev/null
@@ -0,0 +1,46 @@
+/*--------------------------------------------------------------------------\r
+ *  Copyright 2009 Taro L. Saito\r
+ *\r
+ *  Licensed under the Apache License, Version 2.0 (the "License");\r
+ *  you may not use this file except in compliance with the License.\r
+ *  You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// SilkNodeWriter.java\r
+// Since: Jul 29, 2009 6:32:16 PM\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.silk;\r
+\r
+public class SilkNodeWriter {\r
+\r
+    private final SilkWriter out;\r
+    private int nodeCount = 0;\r
+\r
+    public SilkNodeWriter(SilkWriter parent) {\r
+        this.out = parent;\r
+    }\r
+\r
+    public SilkNodeWriter node(String nodeName, String value) {\r
+\r
+        if (nodeCount > 0)\r
+            out.comma();\r
+\r
+        out.inlineNode(nodeName, value);\r
+        nodeCount++;\r
+        return this;\r
+    }\r
+\r
+}\r
index 3a0d9e9..066e320 100644 (file)
 //--------------------------------------
 package org.xerial.silk;
 
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Writer;
+
+import org.xerial.util.ArrayDeque;
+
 /**
- * Supporting class for generating Silk data
+ * Supporting class for generating Silk data. This class is not thread-safe,
+ * i.e, concurrent access to this class may cause invalid Silk format.
+ * 
  * 
  * @author leo
  * 
  */
-public class SilkWriter
-{
-    public SilkWriter dataLine(String dataLine)
-    {
+public class SilkWriter {
+    private final PrintWriter out;
+    private ArrayDeque<String> nodeStack = new ArrayDeque<String>();
+    private Config config = new Config();
+
+    public static class Config {
+        public int indentWidth = 2;
+
+        public boolean indentBeforeDataLine = false;
+    }
+
+    public SilkWriter(Writer out) {
+        this.out = new PrintWriter(out);
+    }
+
+    public SilkWriter(OutputStream out) {
+        this.out = new PrintWriter(new OutputStreamWriter(out));
+    }
+
+    public void flush() {
+        this.out.flush();
+    }
+
+    public void close() {
+        this.out.close();
+    }
+
+    public void resetContext() {
+        nodeStack.clear();
+    }
+
+    public SilkWriter preamble() {
+        out.println("%silk(version:1.0)");
+        return this;
+    }
+
+    private void printIndent() {
+        for (int i = 0; i < nodeStack.size(); ++i) {
+            for (int w = 0; w < config.indentWidth; ++w)
+                out.append(" ");
+        }
+    }
+
+    public SilkWriter startNode(String nodeName) {
+
+        printIndent();
+        out.print("-");
+        out.println(nodeName);
+
+        pushContext(nodeName);
+
+        return this;
+    }
+
+    public SilkWriter startNode(String nodeName, String nodeValue) {
+
+        printIndent();
+        out.print("-");
+        out.print(nodeName);
+        out.print(":");
+        out.println(nodeValue);
+
+        pushContext(nodeName);
+
+        return this;
+    }
+
+    SilkWriter startInLineNode(String nodeName) {
+        printIndent();
+        out.print("-");
+        out.print(nodeName);
+        out.print("(");
+        return this;
+    }
+
+    SilkWriter endInLineNode() {
+        out.println(")");
         return this;
     }
 
+    SilkWriter comma() {
+        out.print(",");
+        return this;
+    }
+
+    SilkWriter inlineNode(String nodeName, String value) {
+        out.print(nodeName);
+        if (value != null) {
+            out.print(":");
+            out.print(value);
+        }
+        return this;
+    }
+
+    private void pushContext(String nodeName) {
+        nodeStack.addLast(nodeName);
+    }
+
+    public SilkWriter leaveNode() {
+        nodeStack.removeLast();
+        return this;
+    }
+
+    public SilkWriter dataLine(String dataLine) {
+
+        if (config.indentBeforeDataLine)
+            printIndent();
+
+        out.println(escapeDataLine(dataLine));
+
+        return this;
+    }
+
+    private String escapeDataLine(String dataLine) {
+
+        if (dataLine.startsWith("-")) {
+            StringBuilder buf = new StringBuilder();
+            buf.append("\\");
+            buf.append(dataLine);
+            return buf.toString();
+        }
+
+        // no change
+        return dataLine;
+    }
+
 }
index 58d8312..4b6eeb0 100644 (file)
@@ -24,8 +24,7 @@
 //--------------------------------------
 package org.xerial.json;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
 
 import java.io.IOException;
 import java.io.StringReader;
@@ -33,17 +32,16 @@ import java.io.StringWriter;
 
 import org.junit.Test;
 import org.xerial.core.XerialError;
+import org.xerial.lens.Lens;
 import org.xerial.util.bean.BeanException;
 import org.xerial.util.bean.BeanUtil;
 import org.xerial.util.log.Logger;
 
-public class JSONWriterTest
-{
+public class JSONWriterTest {
     private static Logger _logger = Logger.getLogger(JSONWriterTest.class);
 
     @Test
-    public void test() throws IOException, JSONException, BeanException
-    {
+    public void test() throws IOException, JSONException, BeanException {
         StringWriter writer = new StringWriter();
         JSONWriter json = new JSONWriter(writer);
 
@@ -69,8 +67,7 @@ public class JSONWriterTest
     }
 
     @Test
-    public void test2() throws IOException, JSONException, BeanException
-    {
+    public void test2() throws IOException, JSONException, BeanException {
         StringWriter writer = new StringWriter();
         JSONWriter json = new JSONWriter(writer);
 
@@ -97,8 +94,7 @@ public class JSONWriterTest
     }
 
     @Test(expected = XerialError.class)
-    public void testInvalidJSONData() throws IOException, JSONException, BeanException
-    {
+    public void testInvalidJSONData() throws IOException, JSONException, BeanException {
         StringWriter writer = new StringWriter();
         JSONWriter json = new JSONWriter(writer);
 
@@ -116,8 +112,7 @@ public class JSONWriterTest
     }
 
     @Test
-    public void streamWrite() throws IOException, JSONException, BeanException
-    {
+    public void streamWrite() throws IOException, JSONException, BeanException {
         StringWriter writer = new StringWriter();
         JSONWriter json = new JSONWriter(writer);
 
@@ -136,8 +131,7 @@ public class JSONWriterTest
     }
 
     @Test
-    public void appendString() throws IOException, JSONException, BeanException
-    {
+    public void appendString() throws IOException, JSONException, BeanException {
         StringWriter writer = new StringWriter();
         JSONWriter json = new JSONWriter(writer);
 
@@ -159,8 +153,7 @@ public class JSONWriterTest
     }
 
     @Test
-    public void arrayOfObjects() throws JSONException, IOException
-    {
+    public void arrayOfObjects() throws JSONException, IOException {
         StringWriter writer = new StringWriter();
         JSONWriter json = new JSONWriter(writer);
 
@@ -187,4 +180,36 @@ public class JSONWriterTest
         assertEquals("yui", yui.getString("name"));
 
     }
+
+    public static class Seq {
+        public int start;
+        public int end;
+        public String sequence;
+    }
+
+    @Test
+    public void startKeyedObject() throws Exception {
+        StringWriter writer = new StringWriter();
+        JSONWriter json = new JSONWriter(writer);
+
+        json.startObject();
+        json.put("start", 1);
+        json.put("end", 2);
+        json.startString("sequence");
+        json.append("AAAA");
+        json.append("CCCC");
+        json.endString();
+        json.endObject();
+        json.endJSON();
+
+        String j = writer.toString();
+        _logger.info(j);
+
+        Seq s = Lens.loadJSON(Seq.class, new StringReader(j));
+        _logger.info(Lens.toJSON(s));
+
+        assertEquals(1, s.start);
+        assertEquals(2, s.end);
+        assertEquals("AAAACCCC", s.sequence);
+    }
 }
diff --git a/src/test/java/org/xerial/lens/XMLSilkLensTest.java b/src/test/java/org/xerial/lens/XMLSilkLensTest.java
new file mode 100644 (file)
index 0000000..39a58d2
--- /dev/null
@@ -0,0 +1,70 @@
+/*--------------------------------------------------------------------------\r
+ *  Copyright 2009 Taro L. Saito\r
+ *\r
+ *  Licensed under the Apache License, Version 2.0 (the "License");\r
+ *  you may not use this file except in compliance with the License.\r
+ *  You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// XMLSilkLensTest.java\r
+// Since: Jul 28, 2009 4:44:07 PM\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.lens;\r
+\r
+import static org.junit.Assert.*;\r
+\r
+import java.io.StringReader;\r
+\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.xerial.silk.SilkWalker;\r
+import org.xerial.silk.TreeWalkLog;\r
+import org.xerial.util.FileResource;\r
+import org.xerial.util.log.Logger;\r
+import org.xerial.util.xml.XMLTreeWalker;\r
+\r
+public class XMLSilkLensTest {\r
+\r
+    private static Logger _logger = Logger.getLogger(XMLSilkLensTest.class);\r
+\r
+    @Before\r
+    public void setUp() throws Exception {}\r
+\r
+    @After\r
+    public void tearDown() throws Exception {}\r
+\r
+    @Test\r
+    public void toSilk() throws Exception {\r
+        String silk = XMLSilkLens.toSilk(FileResource.open(XMLSilkLensTest.class,\r
+                "track-config.xml"));\r
+        _logger.info(silk);\r
+\r
+        TreeWalkLog l1 = new TreeWalkLog();\r
+        TreeWalkLog l2 = new TreeWalkLog();\r
+\r
+        XMLTreeWalker x = new XMLTreeWalker(FileResource.open(XMLSilkLensTest.class,\r
+                "track-config.xml"));\r
+        SilkWalker s = new SilkWalker(new StringReader(silk));\r
+\r
+        x.walk(l1);\r
+        s.walk(l2);\r
+\r
+        boolean doesMatch = TreeWalkLog.compare(l1, l2);\r
+        assertTrue(doesMatch);\r
+\r
+    }\r
+}\r
diff --git a/src/test/java/org/xerial/lens/track-config.xml b/src/test/java/org/xerial/lens/track-config.xml
new file mode 100644 (file)
index 0000000..ffbe766
--- /dev/null
@@ -0,0 +1,21 @@
+<config version="1.0">\r
+\r
+  <group>org.utgenome</group>\r
+  <projectName>utgb-core</projectName>\r
+  <package>org.utgenome.gwt.utgb.server</package>\r
+  \r
+  <import actionPackage="org.utgenome.gwt.utgb.server.app" alias="utgb-core" />\r
+\r
+  <!-- \r
+    database descriptors\r
+      database address for SQLite is relative to the track project folder \r
+  -->\r
+\r
+  <database id="legacy-track">\r
+    <connection dbms="sqlite">\r
+      <address>db/legacy-track.db</address>\r
+    </connection>\r
+  </database>\r
+\r
+\r
+</config>
\ No newline at end of file
diff --git a/src/test/java/org/xerial/silk/SilkWriterTest.java b/src/test/java/org/xerial/silk/SilkWriterTest.java
new file mode 100644 (file)
index 0000000..f0d0d87
--- /dev/null
@@ -0,0 +1,47 @@
+/*--------------------------------------------------------------------------\r
+ *  Copyright 2009 Taro L. Saito\r
+ *\r
+ *  Licensed under the Apache License, Version 2.0 (the "License");\r
+ *  you may not use this file except in compliance with the License.\r
+ *  You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// SilkWriterTest.java\r
+// Since: Jul 28, 2009 12:08:53 PM\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.silk;\r
+\r
+import java.io.StringWriter;\r
+\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+\r
+public class SilkWriterTest {\r
+\r
+    @Before\r
+    public void setUp() throws Exception {}\r
+\r
+    @After\r
+    public void tearDown() throws Exception {}\r
+\r
+    @Test\r
+    public void toSilk() throws Exception {\r
+        StringWriter buf = new StringWriter();\r
+        SilkWriter w = new SilkWriter(buf);\r
+\r
+    }\r
+}\r
index 39cf158..9a01802 100644 (file)
@@ -199,7 +199,7 @@ public class TreeWalkLog implements TreeVisitor
         Iterator<EventLog> i1 = l1.iterator();
         Iterator<EventLog> i2 = l2.iterator();
 
-        boolean compareFlag = true;
+        boolean doComparison = true;
 
         while (i1.hasNext() && i2.hasNext())
         {
@@ -207,15 +207,15 @@ public class TreeWalkLog implements TreeVisitor
             EventLog e2 = i2.next();
 
             _logger.debug(String.format("compare: %-20s %-20s", e1, e2));
-            if (!e1.equals(e2) && compareFlag)
+            if (!e1.equals(e2) && doComparison)
             {
                 _logger.warn("--- do not match");
-                compareFlag = false;
+                doComparison = false;
             }
         }
         if (l1.size() != l2.size())
-            compareFlag = false;
-        return compareFlag;
+            doComparison = false;
+        return doComparison;
     }
 
 }