+++ /dev/null
-/*--------------------------------------------------------------------------\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
-// SilkEnv.java\r
-// Since: Feb 9, 2009 5:43:18 PM\r
-//\r
-// $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/silk/SilkEnv.java $\r
-// $Author: leo $\r
-//--------------------------------------\r
-package org.xerial.silk;\r
-\r
-import java.io.File;\r
-import java.net.MalformedURLException;\r
-\r
-import org.xerial.core.XerialError;\r
-import org.xerial.core.XerialErrorCode;\r
-import org.xerial.silk.impl.SilkFunction;\r
-import org.xerial.silk.impl.SilkNode;\r
-import org.xerial.util.ArrayDeque;\r
-import org.xerial.util.Deque;\r
-import org.xerial.util.log.Logger;\r
-\r
-/**\r
- * Environment variable holder for evaluating Silk functions.\r
- * \r
- * @author leo\r
- * \r
- */\r
-public class SilkEnv\r
-{\r
- private static Logger _logger = Logger.getLogger(SilkEnv.class);\r
-\r
- private int indentationOffset = 0;\r
- private String resourceBasePath = null;\r
- private Deque<SilkContext> contextNodeStack = new ArrayDeque<SilkContext>();\r
- public int contextNodeAttributeCursor = 0;\r
- public boolean isAttributeOpen = false;\r
-\r
- private SilkEnv(SilkEnv parent, int indentationOffset)\r
- {\r
- this.indentationOffset = indentationOffset;\r
- this.resourceBasePath = parent.resourceBasePath;\r
- this.contextNodeStack = parent.contextNodeStack;\r
- this.contextNodeAttributeCursor = parent.contextNodeAttributeCursor;\r
- this.isAttributeOpen = parent.isAttributeOpen;\r
- //this.eventQueue = parent.eventQueue;\r
-\r
- }\r
-\r
- private SilkEnv(SilkEnv parent, String resourceBasePath)\r
- {\r
- this.indentationOffset = parent.indentationOffset;\r
- // use input resource base path\r
- this.resourceBasePath = resourceBasePath;\r
- this.contextNodeStack = parent.contextNodeStack;\r
- this.contextNodeAttributeCursor = parent.contextNodeAttributeCursor;\r
- this.isAttributeOpen = parent.isAttributeOpen;\r
- //this.eventQueue = parent.eventQueue;\r
-\r
- }\r
-\r
- private SilkEnv(String resourceBasePath)\r
- {\r
- this.resourceBasePath = resourceBasePath;\r
- }\r
-\r
- public static SilkEnv newEnv()\r
- {\r
- return newEnv(null);\r
- }\r
-\r
- public static SilkEnv newEnv(SilkEnv parent, String resourceBasePath)\r
- {\r
- return new SilkEnv(parent, resourceBasePath);\r
- }\r
-\r
- public static SilkEnv newEnv(String resourceBasePath)\r
- {\r
- if (resourceBasePath != null)\r
- return new SilkEnv(resourceBasePath);\r
- else\r
- {\r
- File base = new File(System.getProperty("user.dir", ""));\r
- try\r
- {\r
- return new SilkEnv(base.toURL().toExternalForm());\r
- }\r
- catch (MalformedURLException e)\r
- {\r
- throw new XerialError(XerialErrorCode.INVALID_STATE, e);\r
- }\r
- }\r
- }\r
-\r
- public SilkEnv newEnvFor(SilkFunction f)\r
- {\r
- int offset = indentationOffset;\r
-\r
- if (f.getIndentLevel() == SilkFunction.NO_INDENT)\r
- {\r
- SilkNode contextNode = getContextNode();\r
- if (contextNode != null)\r
- offset = contextNode.getIndentLevel();\r
- }\r
- else\r
- offset = f.getIndentLevel();\r
-\r
- return new SilkEnv(this, offset);\r
- }\r
-\r
- public Logger getLogger()\r
- {\r
- return _logger;\r
- }\r
-\r
- /**\r
- * Get the baseline indentation level\r
- * \r
- * @return the baseline indentation\r
- */\r
- public int getIndentationOffset()\r
- {\r
- return indentationOffset;\r
- }\r
-\r
- /**\r
- * Get the base path for finding resources, e.g. import files.\r
- * \r
- * @return the resource base path\r
- */\r
- public String getResourceBasePath()\r
- {\r
- return resourceBasePath;\r
- }\r
-\r
- Deque<SilkContext> getContextNodeStack()\r
- {\r
- return contextNodeStack;\r
- }\r
-\r
- public void pushContext(SilkContext context)\r
- {\r
- contextNodeStack.addLast(context);\r
- }\r
-\r
- public boolean isContextNodeStackEmpty()\r
- {\r
- return contextNodeStack.isEmpty();\r
- }\r
-\r
- public SilkContext peekLastContext()\r
- {\r
- return contextNodeStack.peekLast();\r
- }\r
-\r
- public SilkContext popLastContext()\r
- {\r
- return contextNodeStack.removeLast();\r
- }\r
-\r
- /**\r
- * Get the context node\r
- * \r
- * @return\r
- */\r
- public SilkNode getContextNode()\r
- {\r
- if (contextNodeStack.isEmpty())\r
- return null;\r
- else\r
- return contextNodeStack.getLast().contextNode;\r
- }\r
-\r
-}\r