[P2play-commit] SF.net SVN: p2play: [12] trunk/P2Play/src/org/p2play
Status: Pre-Alpha
Brought to you by:
tisoft
|
From: <ti...@us...> - 2006-12-27 17:39:32
|
Revision: 12
http://p2play.svn.sourceforge.net/p2play/?rev=12&view=rev
Author: tisoft
Date: 2006-12-27 09:39:32 -0800 (Wed, 27 Dec 2006)
Log Message:
-----------
transfered from university cvs
added license header
changed package name to org.p2play.*
Added Paths:
-----------
trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsHandler.java
trunk/P2Play/src/org/p2play/util/logging/
trunk/P2Play/src/org/p2play/util/logging/LogManager.java
trunk/P2Play/src/org/p2play/util/logging/SysoutLogger.java
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsHandler.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsHandler.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsHandler.java 2006-12-27 17:39:32 UTC (rev 12)
@@ -0,0 +1,247 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts;
+
+/**
+ * NutCracker is an Class that provides a simple possibility to run a Pnuts-Script.
+ * The variable Package rootPackage represents the highest level of available functionality for all scripts.
+ * The Vector packageVector stores a set of custom Packages, to expand the functional range. For all scripts the
+ * should only be ONE set of Packages.
+ *
+ * For each script there should be at least ONE context existing. Each Context will be filled with the
+ * PackageSet available bye the NutCracker-class.
+ *
+ */
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.p2play.resource.Resource;
+import org.p2play.resource.ResourceClassLoader;
+import org.p2play.scripting.pnuts.manager.PackageHandler;
+
+import pnuts.compiler.Compiler;
+import pnuts.lang.Context;
+import pnuts.lang.Package;
+import pnuts.lang.PackageFactory;
+import pnuts.lang.ParseException;
+import pnuts.lang.Pnuts;
+import pnuts.lang.PnutsFunction;
+
+/**
+ * @author Johannes Lintner, Markus Heberling
+ *
+ * NutCracker provides functionality to work with Pnuts. It stores a Package,
+ * the Script-Source and creates a Context. It also switches between Normal- and
+ * Debug-Mode.
+ *
+ */
+
+public class PnutsHandler implements PackageFactory {
+ private Context context;
+
+ private PackageHandler packageHandler;
+
+ /**
+ * Constructor. It needs the PackagesHandler to add the packages to the
+ * created Context. Creates the Context.
+ *
+ * @param packageHandler,
+ * the PackageHandler, which stores the available PnutsFunctions.
+ */
+ public PnutsHandler(PackageHandler packageHandler) {
+ this.packageHandler = packageHandler;
+ context = this.createContext();
+ }
+
+ /**
+ * Creates a new package, named by pPkgName. The parentPackage is the
+ * Package, which is in the hierarchy the next higher Package.
+ *
+ * @param pPkgName
+ * the name of the new Package
+ * @param parentPackage
+ * the Package, of the next higher layer.
+ *
+ * @return returns the new Package
+ */
+ public Package createPackage(String pPkgName, Package parentPackage) {
+ Package pkg = new Package(pPkgName, parentPackage);
+ return pkg;
+ }
+
+ /**
+ * Creates a Context, filled with the added Packages stored in
+ * PackageHandler. First it creates a new Context, filled with the
+ * rootPackage. If the debug is true, the ScriptDebugger is initiated and a
+ * DebugContext is created. To use the published function, each package are
+ * added to the context.
+ *
+ * @return the created Context
+ */
+ private Context createContext() {
+ Context newContext = new Context(packageHandler.getRootPackage());
+ // Context newContext = new
+ // Terminator(packageHandler.getPackage("ROOT"));
+ newContext.setClassLoader(ResourceClassLoader.getResourceClassLoader());
+ return newContext;
+ }
+
+ /**
+ * Executes a given Pnuts-object
+ *
+ * @param pnuts,
+ * a parsed or compiled Punts-bject
+ * @return Object, returns the result of the executed script.
+ */
+ public Object execute(Pnuts pnuts) {
+ return pnuts.run(context);
+ }
+
+ /**
+ * Calls the given function in the actual context with the specified
+ * parameters
+ *
+ * @param function,
+ * A PnutsFunctions
+ * @param parameters,
+ * A set of parameters expected by the PnutsFunction
+ * @return returns the return value of the called function
+ */
+ protected Object callFunction(PnutsFunction function, Object... parameters) {
+ return function.call(parameters, context);
+ }
+
+ /**
+ * Calls a named function in the actual context with the specified
+ * parameters in a static way
+ *
+ * @param name,
+ * The name of a PnutsFunctions
+ * @param parameters,
+ * A set of parameters expected by the PnutsFunction
+ * @return returns the return value of the called function
+ */
+ public Object callFunction(String name, Object... parameters) {
+ return PnutsFunction.call(name, parameters, context);
+ }
+
+ /**
+ * Parses and, if not debugMode (debug = true), compiles the given Reader
+ * and initializes the ScriptLogger. If a ParseException occurs, the
+ * Exception will also be loged with the ScriptLogger
+ *
+ * @param reader,
+ * A Reader will be parsed to a Pnuts-Object.
+ * @return Pnuts, returns the parsed or compiled Pnuts-Object.
+ */
+ public Pnuts getPnuts(Reader reader) {
+ Pnuts pnuts = null;
+ // LogManager logM = new LogManager();
+ try {
+ pnuts = Pnuts.parse(reader);
+ Compiler comp = new Compiler();
+ pnuts = comp.compile(pnuts, this.context);
+ comp.setTraceMode(true);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ return pnuts;
+ }
+
+ /**
+ * Parses and, if not debugMode (debug = true), compiles the given String
+ * and initializes the ScriptLogger. If a ParseException occurs, the
+ * Exception will also be loged with the ScriptLogger
+ *
+ * @param reader,
+ * A Reader will be parsed to a Pnuts-Object.
+ * @return Pnuts, returns the parsed or compiled Pnuts-Object.
+ *
+ * @deprecated use getPnuts(String)
+ */
+ public Pnuts compileString(String expr) throws ParseException {
+ Pnuts pnuts = null;
+ try {
+ pnuts = Pnuts.parse(expr);
+ Compiler comp = new Compiler();
+ pnuts = comp.compile(pnuts, this.context);
+ comp.setTraceMode(true);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ return pnuts;
+ }
+
+ /**
+ * TODO: Change description of param String script!!!
+ *
+ */
+
+ /**
+ * Redirects the call to getPnuts(Reader)
+ *
+ * @param script,
+ * XXXXXXXXXXXXXXXXXXXXXX
+ * @return Pnuts, the parsed or compiled Pnuts-object.
+ */
+ public Pnuts getPnuts(String script) // throws ParseException
+ {
+ return getPnuts(new StringReader(script));
+ }
+
+ /**
+ * Redirects the call to getPnuts(Reader)
+ *
+ * @param inputStream,
+ * the ScriptFile as InputStream.
+ * @return Pnuts, the parsed or compiled Pnuts-object.
+ */
+ public Pnuts getPnuts(InputStream inputStream)// throws ParseException
+ {
+ return getPnuts(new InputStreamReader(inputStream));
+ }
+
+ /**
+ * Redirects the call to getPnuts(Reader)
+ *
+ * @param script,
+ * the ScriptFile as DataStream from the RessourceManager.
+ * @return Pnuts, the parsed or compiled Pnuts-object.
+ */
+ public Pnuts getPnuts(Resource resource)// throws ParseException
+ {
+ return getPnuts(resource.getDataAsStream());
+ }
+
+ /**
+ * Returns the actual context
+ *
+ * @return Context, the actual Context.
+ */
+ public Context getContext() {
+ return this.context;
+ }
+
+ public PackageHandler getPackageHandler() {
+ return packageHandler;
+ }
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/util/logging/LogManager.java
===================================================================
--- trunk/P2Play/src/org/p2play/util/logging/LogManager.java (rev 0)
+++ trunk/P2Play/src/org/p2play/util/logging/LogManager.java 2006-12-27 17:39:32 UTC (rev 12)
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.util.logging;
+
+import java.util.Iterator;
+import java.util.Vector;
+
+import rice.environment.logging.Logger;
+
+
+public class LogManager implements rice.environment.logging.LogManager {
+ private Vector<Logger> logger;
+
+ public LogManager() {
+ logger=new Vector<Logger>();
+ }
+
+ public void registerLogger(Logger logger) {
+ this.logger.addElement(logger);
+ }
+
+ public Logger getLogger(Class clazz, String instance) {
+ return new LoggerLogger(this);
+ }
+
+ void log(String message) {
+ Iterator<Logger> iterator = logger
+ .iterator();
+ while (iterator.hasNext()) {
+ Logger logger = iterator.next();
+ logger.log(message);
+ }
+ }
+}
+
+class LoggerLogger extends Logger {
+ private LogManager logmanager;
+
+ public LoggerLogger(LogManager logmanager) {
+ super();
+ this.logmanager = logmanager;
+ level=Logger.WARNING;
+ }
+
+ @Override
+ public void log(String message) {
+ logmanager.log(message);
+ }
+
+ @Override
+ public void logException(String message, Throwable exception) {
+ logmanager.log(message);
+ exception.printStackTrace();
+ }
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/util/logging/SysoutLogger.java
===================================================================
--- trunk/P2Play/src/org/p2play/util/logging/SysoutLogger.java (rev 0)
+++ trunk/P2Play/src/org/p2play/util/logging/SysoutLogger.java 2006-12-27 17:39:32 UTC (rev 12)
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.util.logging;
+
+import rice.environment.logging.Logger;
+
+public class SysoutLogger extends Logger {
+
+ @Override
+ public void log(String message) {
+ System.out.println(message);
+ }
+
+ @Override
+ public void logException(String message, Throwable exception) {
+ System.out.println(message);
+ exception.printStackTrace();
+ }
+}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|