1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2009 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
20 // Since: Feb 9, 2009 5:43:18 PM
\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/silk/SilkEnv.java $
\r
24 //--------------------------------------
\r
25 package org.xerial.silk;
\r
27 import java.io.File;
\r
28 import java.net.MalformedURLException;
\r
30 import org.xerial.core.XerialError;
\r
31 import org.xerial.core.XerialErrorCode;
\r
32 import org.xerial.silk.impl.SilkFunction;
\r
33 import org.xerial.silk.impl.SilkNode;
\r
34 import org.xerial.util.ArrayDeque;
\r
35 import org.xerial.util.Deque;
\r
36 import org.xerial.util.log.Logger;
\r
39 * Environment variable holder for evaluating Silk functions.
\r
44 public class SilkEnv
\r
46 private static Logger _logger = Logger.getLogger(SilkEnv.class);
\r
48 private int indentationOffset = 0;
\r
49 private String resourceBasePath = null;
\r
50 private Deque<SilkContext> contextNodeStack = new ArrayDeque<SilkContext>();
\r
51 public int contextNodeAttributeCursor = 0;
\r
52 public boolean isAttributeOpen = false;
\r
54 private SilkEnv(SilkEnv parent, int indentationOffset)
\r
56 this.indentationOffset = indentationOffset;
\r
57 this.resourceBasePath = parent.resourceBasePath;
\r
58 this.contextNodeStack = parent.contextNodeStack;
\r
59 this.contextNodeAttributeCursor = parent.contextNodeAttributeCursor;
\r
60 this.isAttributeOpen = parent.isAttributeOpen;
\r
61 //this.eventQueue = parent.eventQueue;
\r
65 private SilkEnv(SilkEnv parent, String resourceBasePath)
\r
67 this.indentationOffset = parent.indentationOffset;
\r
68 // use input resource base path
\r
69 this.resourceBasePath = resourceBasePath;
\r
70 this.contextNodeStack = parent.contextNodeStack;
\r
71 this.contextNodeAttributeCursor = parent.contextNodeAttributeCursor;
\r
72 this.isAttributeOpen = parent.isAttributeOpen;
\r
73 //this.eventQueue = parent.eventQueue;
\r
77 private SilkEnv(String resourceBasePath)
\r
79 this.resourceBasePath = resourceBasePath;
\r
82 public static SilkEnv newEnv()
\r
84 return newEnv(null);
\r
87 public static SilkEnv newEnv(SilkEnv parent, String resourceBasePath)
\r
89 return new SilkEnv(parent, resourceBasePath);
\r
92 public static SilkEnv newEnv(String resourceBasePath)
\r
94 if (resourceBasePath != null)
\r
95 return new SilkEnv(resourceBasePath);
\r
98 File base = new File(System.getProperty("user.dir", ""));
\r
101 return new SilkEnv(base.toURL().toExternalForm());
\r
103 catch (MalformedURLException e)
\r
105 throw new XerialError(XerialErrorCode.INVALID_STATE, e);
\r
110 public SilkEnv newEnvFor(SilkFunction f)
\r
112 int offset = indentationOffset;
\r
114 if (f.getIndentLevel() == SilkFunction.NO_INDENT)
\r
116 SilkNode contextNode = getContextNode();
\r
117 if (contextNode != null)
\r
118 offset = contextNode.getIndentLevel();
\r
121 offset = f.getIndentLevel();
\r
123 return new SilkEnv(this, offset);
\r
126 public Logger getLogger()
\r
132 * Get the baseline indentation level
\r
134 * @return the baseline indentation
\r
136 public int getIndentationOffset()
\r
138 return indentationOffset;
\r
142 * Get the base path for finding resources, e.g. import files.
\r
144 * @return the resource base path
\r
146 public String getResourceBasePath()
\r
148 return resourceBasePath;
\r
151 Deque<SilkContext> getContextNodeStack()
\r
153 return contextNodeStack;
\r
156 public void pushContext(SilkContext context)
\r
158 contextNodeStack.addLast(context);
\r
161 public boolean isContextNodeStackEmpty()
\r
163 return contextNodeStack.isEmpty();
\r
166 public SilkContext peekLastContext()
\r
168 return contextNodeStack.peekLast();
\r
171 public SilkContext popLastContext()
\r
173 return contextNodeStack.removeLast();
\r
177 * Get the context node
\r
181 public SilkNode getContextNode()
\r
183 if (contextNodeStack.isEmpty())
\r
186 return contextNodeStack.getLast().contextNode;
\r