OSDN Git Service

imported from subversion repository
[xerial/xerial-core.git] / src / main / java / org / xerial / silk / SilkEnv.java
1 /*--------------------------------------------------------------------------\r
2  *  Copyright 2009 Taro L. Saito\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
17 // XerialJ\r
18 //\r
19 // SilkEnv.java\r
20 // Since: Feb 9, 2009 5:43:18 PM\r
21 //\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/silk/SilkEnv.java $\r
23 // $Author: leo $\r
24 //--------------------------------------\r
25 package org.xerial.silk;\r
26 \r
27 import java.io.File;\r
28 import java.net.MalformedURLException;\r
29 \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
37 \r
38 /**\r
39  * Environment variable holder for evaluating Silk functions.\r
40  * \r
41  * @author leo\r
42  * \r
43  */\r
44 public class SilkEnv\r
45 {\r
46     private static Logger _logger = Logger.getLogger(SilkEnv.class);\r
47 \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
53 \r
54     private SilkEnv(SilkEnv parent, int indentationOffset)\r
55     {\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
62 \r
63     }\r
64 \r
65     private SilkEnv(SilkEnv parent, String resourceBasePath)\r
66     {\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
74 \r
75     }\r
76 \r
77     private SilkEnv(String resourceBasePath)\r
78     {\r
79         this.resourceBasePath = resourceBasePath;\r
80     }\r
81 \r
82     public static SilkEnv newEnv()\r
83     {\r
84         return newEnv(null);\r
85     }\r
86 \r
87     public static SilkEnv newEnv(SilkEnv parent, String resourceBasePath)\r
88     {\r
89         return new SilkEnv(parent, resourceBasePath);\r
90     }\r
91 \r
92     public static SilkEnv newEnv(String resourceBasePath)\r
93     {\r
94         if (resourceBasePath != null)\r
95             return new SilkEnv(resourceBasePath);\r
96         else\r
97         {\r
98             File base = new File(System.getProperty("user.dir", ""));\r
99             try\r
100             {\r
101                 return new SilkEnv(base.toURL().toExternalForm());\r
102             }\r
103             catch (MalformedURLException e)\r
104             {\r
105                 throw new XerialError(XerialErrorCode.INVALID_STATE, e);\r
106             }\r
107         }\r
108     }\r
109 \r
110     public SilkEnv newEnvFor(SilkFunction f)\r
111     {\r
112         int offset = indentationOffset;\r
113 \r
114         if (f.getIndentLevel() == SilkFunction.NO_INDENT)\r
115         {\r
116             SilkNode contextNode = getContextNode();\r
117             if (contextNode != null)\r
118                 offset = contextNode.getIndentLevel();\r
119         }\r
120         else\r
121             offset = f.getIndentLevel();\r
122 \r
123         return new SilkEnv(this, offset);\r
124     }\r
125 \r
126     public Logger getLogger()\r
127     {\r
128         return _logger;\r
129     }\r
130 \r
131     /**\r
132      * Get the baseline indentation level\r
133      * \r
134      * @return the baseline indentation\r
135      */\r
136     public int getIndentationOffset()\r
137     {\r
138         return indentationOffset;\r
139     }\r
140 \r
141     /**\r
142      * Get the base path for finding resources, e.g. import files.\r
143      * \r
144      * @return the resource base path\r
145      */\r
146     public String getResourceBasePath()\r
147     {\r
148         return resourceBasePath;\r
149     }\r
150 \r
151     Deque<SilkContext> getContextNodeStack()\r
152     {\r
153         return contextNodeStack;\r
154     }\r
155 \r
156     public void pushContext(SilkContext context)\r
157     {\r
158         contextNodeStack.addLast(context);\r
159     }\r
160 \r
161     public boolean isContextNodeStackEmpty()\r
162     {\r
163         return contextNodeStack.isEmpty();\r
164     }\r
165 \r
166     public SilkContext peekLastContext()\r
167     {\r
168         return contextNodeStack.peekLast();\r
169     }\r
170 \r
171     public SilkContext popLastContext()\r
172     {\r
173         return contextNodeStack.removeLast();\r
174     }\r
175 \r
176     /**\r
177      * Get the context node\r
178      * \r
179      * @return\r
180      */\r
181     public SilkNode getContextNode()\r
182     {\r
183         if (contextNodeStack.isEmpty())\r
184             return null;\r
185         else\r
186             return contextNodeStack.getLast().contextNode;\r
187     }\r
188 \r
189 }\r