001: /*
002: * Copyright (C) 2003-2007 Kepler Project.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining
005: * a copy of this software and associated documentation files (the
006: * "Software"), to deal in the Software without restriction, including
007: * without limitation the rights to use, copy, modify, merge, publish,
008: * distribute, sublicense, and/or sell copies of the Software, and to
009: * permit persons to whom the Software is furnished to do so, subject to
010: * the following conditions:
011: *
012: * The above copyright notice and this permission notice shall be
013: * included in all copies or substantial portions of the Software.
014: *
015: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
018: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
019: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
020: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
021: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022: */
023:
024: package org.keplerproject.luajava.test.node;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: import org.keplerproject.luajava.LuaException;
030: import org.keplerproject.luajava.LuaObject;
031: import org.keplerproject.luajava.LuaState;
032: import org.keplerproject.luajava.LuaStateFactory;
033:
034: /**
035: * Example that ilustrate the usage of Lua as a configuration file.
036: *
037: * @author thiago
038: */
039: public class LuaNode {
040: private static LuaState L;
041: private static IConfig configLib;
042: private LuaObject configFile;
043:
044: private static final String configFileName = "testConfig.lua";
045:
046: /**
047: * Parses the Lua file to be used.
048: *
049: * @param fileName The Lua file
050: */
051: public static LuaNode proccessFile(String fileName)
052: throws LuaException {
053: try {
054: return new LuaNode(fileName);
055: } catch (ClassNotFoundException e) {
056: throw new LuaException(e);
057: }
058: }
059:
060: protected LuaNode(String fileName) throws ClassNotFoundException,
061: LuaException {
062: if (L == null) {
063: L = LuaStateFactory.newLuaState();
064: L.openLibs();
065: int err = L.LdoFile(configFileName);
066: if (err != 0) {
067: switch (err) {
068: case 1:
069: throw new LuaException("Runtime error. "
070: + L.toString(-1));
071: case 2:
072: throw new LuaException("File not found. "
073: + L.toString(-1));
074: case 3:
075: throw new LuaException("Syntax error. "
076: + L.toString(-1));
077: case 4:
078: throw new LuaException("Memory error. "
079: + L.toString(-1));
080: default:
081: throw new LuaException("Error. " + L.toString(-1));
082: }
083: }
084: configLib = (IConfig) L.getLuaObject("configLib")
085: .createProxy("test.node.IConfig");
086: }
087:
088: configFile = configLib.processConfigFile(fileName);
089: }
090:
091: protected LuaNode(LuaObject obj) {
092: configFile = obj;
093: }
094:
095: /**
096: * Name of the node represented by <code>this</code>.
097: */
098: public String getName() {
099: return configLib.getName(configFile);
100: }
101:
102: /**
103: * Returns a LuaNode representing the child of the current node
104: * with the name <code>childName</code>
105: */
106: public LuaNode getChild(String childName) {
107: return new LuaNode(configLib.getChild(configFile, childName));
108: }
109:
110: /**
111: * Returns a list with all the node children of the current node.
112: */
113: public List getChildren() {
114: LuaObject obj = configLib.getChildren(configFile);
115: List list = new ArrayList();
116:
117: int i = 1;
118: obj.push();
119: L.pushNumber(i);
120: L.getTable(-2);
121: while (!L.isNil(-1)) {
122: if (L.isTable(-1)) {
123: list.add(new LuaNode(L.getLuaObject(-1)));
124: }
125: L.pop(1);
126:
127: i++;
128: L.pushNumber(i);
129: L.getTable(-2);
130: }
131:
132: L.pop(1);
133:
134: return list;
135: }
136:
137: /**
138: * Returns a list with all the children of the current node with the name
139: * <code>childName</code>.
140: */
141: public List getChildren(String childName) {
142: LuaObject obj = configLib.getChildren(configFile, childName);
143: List list = new ArrayList();
144:
145: int i = 1;
146: obj.push();
147: L.pushNumber(i);
148: L.getTable(-2);
149: while (!L.isNil(-1)) {
150: if (L.isTable(-1)) {
151: list.add(new LuaNode(L.getLuaObject(-1)));
152: }
153: L.pop(1);
154:
155: i++;
156: L.pushNumber(i);
157: L.getTable(-2);
158: }
159:
160: L.pop(1);
161:
162: return list;
163: }
164:
165: /**
166: * Returns the attribute with name <code>attributeName</code>.
167: */
168: public String getAttribute(String attributeName) {
169: return configLib.getAttribute(configFile, attributeName);
170: }
171:
172: /**
173: * Returns the value of the current node.
174: */
175: public String getValue() {
176: return configLib.getValue(configFile);
177: }
178: }
|