OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@2952 ae02f08e...
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Tue, 10 Feb 2009 01:31:35 +0000 (01:31 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Tue, 10 Feb 2009 01:31:35 +0000 (01:31 +0000)
pom.xml
src/main/java/org/xerial/silk/SilkEnv.java
src/main/java/org/xerial/silk/SilkWalker.java
src/main/java/org/xerial/silk/package-info.java [new file with mode: 0644]
src/main/java/org/xerial/silk/plugin/Import.java
src/main/java/org/xerial/util/opt/package-info.java
src/main/java/org/xerial/util/tree/package-info.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 82e6a96..0157946 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
   <parent>
     <artifactId>xerial-project</artifactId>
     <groupId>org.xerial</groupId>
-    <version>1.1</version>
+    <version>1.1.1-SNAPSHOT</version>
   </parent>
 
   <modelVersion>4.0.0</modelVersion>
index 0425d61..5faa945 100644 (file)
@@ -24,6 +24,7 @@
 //--------------------------------------
 package org.xerial.silk;
 
+import org.xerial.silk.impl.SilkNode;
 import org.xerial.util.log.Logger;
 import org.xerial.util.tree.TreeVisitor;
 import org.xerial.util.tree.TreeWalker;
@@ -47,7 +48,7 @@ public interface SilkEnv
      * 
      * @return the baseline indentation
      */
-    public int getIndentationLevel();
+    public int getIndentationOffset();
 
     /**
      * Get the base path for finding resources, e.g. import files.
@@ -55,4 +56,6 @@ public interface SilkEnv
      * @return the resource base path
      */
     public String getResourceBasePath();
+
+    public SilkNode getContextNode();
 }
index 4279837..5fac631 100644 (file)
@@ -66,6 +66,10 @@ import org.xerial.util.tree.TreeWalker;
 /**
  * {@link TreeWalker} implementation of the Silk format.
  * 
+ * <pre>
+ * &#064;
+ * </pre>
+ * 
  * @author leo
  * 
  */
@@ -74,9 +78,64 @@ public class SilkWalker implements TreeWalker
     private static Logger _logger = Logger.getLogger(SilkWalker.class);
 
     private final SilkPullParser parser;
+
+    private int indentationOffset = 0;
     private String resourceBasePath = null;
     private final Deque<SilkNode> contextNodeStack = new ArrayDeque<SilkNode>();
 
+    private class SilkEnvImpl implements SilkEnv
+    {
+        int offset = indentationOffset;
+        TreeVisitor visitor;
+
+        private SilkEnvImpl(SilkFunction f, TreeVisitor visitor)
+        {
+            if (f.getIndentLevel() == SilkFunction.NO_INDENT)
+            {
+                SilkNode node = getContextNode();
+                if (node == null)
+                    this.offset = indentationOffset;
+                else
+                    this.offset = node.getIndentLevel();
+            }
+            else
+                this.offset = f.getIndentLevel();
+
+            this.visitor = visitor;
+        }
+
+        public int getIndentationOffset()
+        {
+            return offset;
+        }
+
+        public Logger getLogger()
+        {
+            return _logger;
+        }
+
+        public TreeWalker getTreeWalker()
+        {
+            return SilkWalker.this;
+        }
+
+        public TreeVisitor getTreeVisitor()
+        {
+            return visitor;
+        }
+
+        public String getResourceBasePath()
+        {
+            return resourceBasePath;
+        }
+
+        public SilkNode getContextNode()
+        {
+            return contextNodeStack.peekLast();
+        }
+
+    }
+
     /**
      * Creates a new SilkWalker with the specified input stream
      * 
@@ -121,12 +180,30 @@ public class SilkWalker implements TreeWalker
         init();
     }
 
+    public SilkWalker(URL resource, SilkEnv env) throws IOException
+    {
+        String path = resource.toExternalForm();
+        int fileNamePos = path.lastIndexOf("/");
+        this.resourceBasePath = fileNamePos > 0 ? path.substring(0, fileNamePos) : null;
+        this.parser = new SilkPullParser(new BufferedReader(new InputStreamReader(resource.openStream())));
+        init(env);
+    }
+
     public void init()
     {
         if (resourceBasePath == null)
             resourceBasePath = System.getProperty("user.dir", "");
     }
 
+    public void init(SilkEnv env)
+    {
+        resourceBasePath = env.getResourceBasePath();
+        indentationOffset = env.getIndentationOffset();
+        SilkNode contextNode = env.getContextNode();
+        if (contextNode != null)
+            contextNodeStack.addLast(contextNode);
+    }
+
     public TreeNode getSubTree() throws XerialException
     {
         // TODO Auto-generated method stub
@@ -146,7 +223,7 @@ public class SilkWalker implements TreeWalker
 
         while (!contextNodeStack.isEmpty())
         {
-            SilkNode node = contextNodeStack.peekLast();
+            SilkNode node = contextNodeStack.peek();
             if (node.getIndentLevel() >= newIndentLevel)
             {
                 contextNodeStack.removeLast();
@@ -172,7 +249,7 @@ public class SilkWalker implements TreeWalker
 
     private void openContext(SilkNode node, TreeVisitor visitor) throws XerialException
     {
-        int indentLevel = node.getIndentLevel();
+        int indentLevel = node.getIndentLevel() + indentationOffset;
 
         closeUpTo(indentLevel, visitor);
 
@@ -223,54 +300,6 @@ public class SilkWalker implements TreeWalker
         visitor.finish(this);
     }
 
-    private class SilkEnvImpl implements SilkEnv
-    {
-        int indentationLevel;
-        TreeVisitor visitor;
-
-        private SilkEnvImpl(SilkFunction f, TreeVisitor visitor)
-        {
-            if (f.getIndentLevel() == SilkFunction.NO_INDENT)
-            {
-                SilkNode node = getContextNode();
-                if (node == null)
-                    this.indentationLevel = 0;
-                else
-                    this.indentationLevel = node.getIndentLevel();
-            }
-            else
-                this.indentationLevel = f.getIndentLevel();
-
-            this.visitor = visitor;
-        }
-
-        public int getIndentationLevel()
-        {
-            return indentationLevel;
-        }
-
-        public Logger getLogger()
-        {
-            return _logger;
-        }
-
-        public TreeWalker getTreeWalker()
-        {
-            return SilkWalker.this;
-        }
-
-        public TreeVisitor getTreeVisitor()
-        {
-            return visitor;
-        }
-
-        public String getResourceBasePath()
-        {
-            return resourceBasePath;
-        }
-
-    }
-
     public void walkWithoutInitAndFinish(TreeVisitor visitor) throws XerialException
     {
         // depth first search 
@@ -359,7 +388,7 @@ public class SilkWalker implements TreeWalker
 
         }
 
-        closeUpTo(0, visitor);
+        closeUpTo(indentationOffset, visitor);
 
     }
 
diff --git a/src/main/java/org/xerial/silk/package-info.java b/src/main/java/org/xerial/silk/package-info.java
new file mode 100644 (file)
index 0000000..5e49841
--- /dev/null
@@ -0,0 +1,7 @@
+
+/**
+ * Provides Silk text format lexer/parser, TreeWalker implementation, etc.
+ * 
+ * 
+ */
+package org.xerial.silk;
\ No newline at end of file
index 154221b..139c711 100644 (file)
@@ -61,7 +61,7 @@ public class Import implements SilkFunctionPlugin
                 url += "/";
             url += filePath;
 
-            SilkWalker walker = new SilkWalker(new URL(url));
+            SilkWalker walker = new SilkWalker(new URL(url), env);
             walker.walkWithoutInitAndFinish(env.getTreeVisitor());
         }
         catch (IOException e)
index 3d0875c..74a47c5 100644 (file)
  *  limitations under the License.
  *--------------------------------------------------------------------------*/
 /**
- * command-line argument parser
+ * Command-line argument parser.
+ * 
+ * 
+ * <p>Usage:</p>
+ * 
+ * <pre>
+ * <code>
+ *    class MyOption
+ *    {
+ *     &#064;Option(symbol = "h", longName = "help", description = "display help message")
+ *     private boolean      displayHelp;
+ *  
+ *     &#064;Argument(index = 0)
+ *     private String       subCommand;
+ *  
+ *     &#064;Argument(name = "input_file", index = 1, required = false)
+ *     private List<String> fileList;
+ *   }
+ *  
+ *   public void main(String[] args)
+ *   {
+ *     MyOption myOption = new MyOption();
+ *     OptionParser parser = new OptionParser(myOption);
+ *  
+ *     // parse the command line arguments e.g. "add -h 1.txt 2.txt"
+ *     try
+ *     {
+ *        parser.parse(args);
+ *        
+ *        // myOption instance's field values are initialized with command line argument
+ *        if(displayHelp)
+ *        {
+ *           parser.printUsage();   // display usage
+ *           return;
+ *        }
+ *  
+ *        // array argument is also initialized 
+ *        for(String each : fileList)
+ *        {
+ *            System.out.println(String.format("input file: %s", each));
+ *        }
+ *     }
+ *     catch(OptionParserException e)
+ *     {
+ *        System.err.println(e.getMessage()):
+ *        parser.printUsage();  // display usage
+ *     } 
+ *  
+ *   }
+ * </code>
+ * </pre>
+ * 
  */
 package org.xerial.util.opt;
 
diff --git a/src/main/java/org/xerial/util/tree/package-info.java b/src/main/java/org/xerial/util/tree/package-info.java
new file mode 100644 (file)
index 0000000..ff25975
--- /dev/null
@@ -0,0 +1,21 @@
+/*--------------------------------------------------------------------------
+ *  Copyright 2009 Taro L. Saito
+ *
+ *  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.
+ *--------------------------------------------------------------------------*/
+/**
+ * Contains TreeVisitor, TreeWalker interfaces for manipulating tree structured data.
+ * @author leo
+ */
+package org.xerial.util.tree;
+