/**
* {@link TreeWalker} implementation of the Silk format.
*
+ * <pre>
+ * @
+ * </pre>
+ *
* @author leo
*
*/
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
*
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
while (!contextNodeStack.isEmpty())
{
- SilkNode node = contextNodeStack.peekLast();
+ SilkNode node = contextNodeStack.peek();
if (node.getIndentLevel() >= newIndentLevel)
{
contextNodeStack.removeLast();
private void openContext(SilkNode node, TreeVisitor visitor) throws XerialException
{
- int indentLevel = node.getIndentLevel();
+ int indentLevel = node.getIndentLevel() + indentationOffset;
closeUpTo(indentLevel, visitor);
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
}
- closeUpTo(0, visitor);
+ closeUpTo(indentationOffset, visitor);
}
* limitations under the License.
*--------------------------------------------------------------------------*/
/**
- * command-line argument parser
+ * Command-line argument parser.
+ *
+ *
+ * <p>Usage:</p>
+ *
+ * <pre>
+ * <code>
+ * class MyOption
+ * {
+ * @Option(symbol = "h", longName = "help", description = "display help message")
+ * private boolean displayHelp;
+ *
+ * @Argument(index = 0)
+ * private String subCommand;
+ *
+ * @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;
--- /dev/null
+/*--------------------------------------------------------------------------
+ * 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;
+