incubator-connectors-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwri...@apache.org
Subject svn commit: r1161572 - in /incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine: ErrorCommand.java NewArray.java ScriptParser.java VariableArray.java
Date Thu, 25 Aug 2011 14:14:52 GMT
Author: kwright
Date: Thu Aug 25 14:14:52 2011
New Revision: 1161572

URL: http://svn.apache.org/viewvc?rev=1161572&view=rev
Log:
Add array support

Added:
    incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ErrorCommand.java
  (with props)
    incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/NewArray.java
  (with props)
    incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/VariableArray.java
  (with props)
Modified:
    incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ScriptParser.java

Added: incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ErrorCommand.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ErrorCommand.java?rev=1161572&view=auto
==============================================================================
--- incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ErrorCommand.java
(added)
+++ incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ErrorCommand.java
Thu Aug 25 14:14:52 2011
@@ -0,0 +1,50 @@
+/* $Id$ */
+
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.manifoldcf.scriptengine;
+
+/** Error command. */
+public class ErrorCommand implements Command
+{
+  /** Parse and execute.  Parsing begins right after the command name, and should stop before
the trailing semicolon.
+  *@param sp is the script parser to use to help in the parsing.
+  *@param currentStream is the current token stream.
+  *@return true to send a break signal, false otherwise.
+  */
+  public boolean parseAndExecute(ScriptParser sp, TokenStream currentStream)
+    throws ScriptException
+  {
+    VariableReference vr = sp.evaluateExpression(currentStream);
+    if (vr == null)
+      sp.syntaxError(currentStream,"Missing error message expression");
+    Variable v = sp.resolveMustExist(currentStream,vr);
+    throw new ScriptException(v.getStringValue());
+  }
+  
+  /** Parse and skip.  Parsing begins right after the command name, and should stop before
the trailing semicolon.
+  *@param sp is the script parser to use to help in the parsing.
+  *@param currentStream is the current token stream.
+  */
+  public void parseAndSkip(ScriptParser sp, TokenStream currentStream)
+    throws ScriptException
+  {
+    if (sp.skipExpression(currentStream) == false)
+      sp.syntaxError(currentStream,"Missing error message expression");
+  }
+
+}

Propchange: incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ErrorCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ErrorCommand.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/NewArray.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/NewArray.java?rev=1161572&view=auto
==============================================================================
--- incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/NewArray.java
(added)
+++ incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/NewArray.java
Thu Aug 25 14:14:52 2011
@@ -0,0 +1,46 @@
+/* $Id$ */
+
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+package org.apache.manifoldcf.scriptengine;
+
+/** Class defining the creation of a new array object.
+*/
+public class NewArray implements NewOperation
+{
+  /** Parse and execute.  Parsing begins right after the "new" keyword and the operation
name token.
+  *@param sp is the script parser to use to help in the parsing.
+  *@param currentStream is the current token stream.
+  *@return the variable reference that got created.  Should never be null.
+  */
+  public VariableReference parseAndCreate(ScriptParser sp, TokenStream currentStream)
+    throws ScriptException
+  {
+    return new VariableArray();
+  }
+  
+  /** Parse and skip.  Parsing begins right after the "new" keyword and the operation name
token.
+  *@param sp is the script parser to use to help in the parsing.
+  *@param currentStream is the current token stream.
+  */
+  public void parseAndSkip(ScriptParser sp, TokenStream currentStream)
+    throws ScriptException
+  {
+  }
+  
+}

Propchange: incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/NewArray.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/NewArray.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ScriptParser.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ScriptParser.java?rev=1161572&r1=1161571&r2=1161572&view=diff
==============================================================================
--- incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ScriptParser.java
(original)
+++ incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ScriptParser.java
Thu Aug 25 14:14:52 2011
@@ -57,6 +57,16 @@ public class ScriptParser
     newOperations.put(operationName,operation);
   }
   
+  /** Add a variable.
+  */
+  public void addVariable(String variableName, Variable v)
+    throws ScriptException
+  {
+    ContextVariableReference cvr = new ContextVariableReference();
+    cvr.setReference(v);
+    context.put(variableName,cvr);
+  }
+  
   // Statement return codes
   protected static final int STATEMENT_NOTME = 0;
   protected static final int STATEMENT_ISME = 1;
@@ -882,11 +892,7 @@ public class ScriptParser
   
   public static void main(String[] argv)
   {
-    if (argv.length > 1)
-    {
-      System.err.println("Usage: ScriptParser [<filename>]");
-      System.exit(1);
-    }
+    // Usage: ScriptParser [<filename> [arg1 [arg2 ...]]
     
     ScriptParser sp = new ScriptParser();
     
@@ -898,6 +904,7 @@ public class ScriptParser
     sp.addCommand("set",new SetCommand());
     sp.addCommand("insert",new InsertCommand());
     sp.addCommand("remove",new RemoveCommand());
+    sp.addCommand("error",new ErrorCommand());
     
     sp.addCommand("GET",new GETCommand());
     sp.addCommand("PUT",new PUTCommand());
@@ -909,14 +916,24 @@ public class ScriptParser
     sp.addNewOperation("configurationnode",new NewConfigurationNode());
     sp.addNewOperation("url",new NewURL());
     sp.addNewOperation("connectionname",new NewConnectionName());
+    sp.addNewOperation("array",new NewArray());
     
     try
     {
       Reader reader;
-      if (argv.length == 1)
+      if (argv.length >= 1)
       {
         File inputFile = new File(argv[0]);
         reader = new InputStreamReader(new FileInputStream(inputFile),"utf-8");
+        VariableArray va = new VariableArray();
+        int i = 0;
+        while (i < argv.length - 1)
+        {
+          String arg = argv[i-1];
+          va.insert(new VariableString(arg));
+          i++;
+        }
+        sp.addVariable("__args__",va);
       }
       else
         reader = new InputStreamReader(System.in);

Added: incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/VariableArray.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/VariableArray.java?rev=1161572&view=auto
==============================================================================
--- incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/VariableArray.java
(added)
+++ incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/VariableArray.java
Thu Aug 25 14:14:52 2011
@@ -0,0 +1,132 @@
+/* $Id$ */
+
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+package org.apache.manifoldcf.scriptengine;
+
+import org.apache.manifoldcf.core.interfaces.*;
+import java.util.*;
+
+/** Array variable object.
+*/
+public class VariableArray extends VariableBase
+{
+  protected List<Variable> array = new ArrayList<Variable>();
+  
+  public VariableArray()
+  {
+  }
+
+  /** Get a string from this */
+  public String getStringValue()
+    throws ScriptException
+  {
+    StringBuilder sb = new StringBuilder("[ ");
+    int i = 0;
+    while (i < array.size())
+    {
+      if (i > 0)
+        sb.append(", ");
+      Variable v = array.get(i++);
+      if (v == null)
+        sb.append("null");
+      else
+        sb.append(v.getStringValue());
+    }
+    sb.append(" ]");
+    return sb.toString();
+  }
+  
+  /** Get a named attribute of the variable; e.g. xxx.yyy */
+  public VariableReference getAttribute(String attributeName)
+    throws ScriptException
+  {
+    // We recognize only the __size__ attribute
+    if (attributeName.equals(ATTRIBUTE_SIZE))
+      return new VariableInt(array.size());
+    return super.getAttribute(attributeName);
+  }
+  
+  /** Get an indexed property of the variable */
+  public VariableReference getIndexed(int index)
+    throws ScriptException
+  {
+    if (index < array.size())
+      return new ElementReference(index);
+    return super.getIndexed(index);
+  }
+  
+  /** Insert an object into this variable at a position. */
+  public void insertAt(Variable v, int index)
+    throws ScriptException
+  {
+    if (index > array.size())
+      throw new ScriptException("Insert out of bounds");
+    array.add(index,v);
+  }
+
+  /** Insert an object into this variable at end. */
+  public void insert(Variable v)
+    throws ScriptException
+  {
+    array.add(v);
+  }
+
+  /** Delete an object from this variable at a position. */
+  public void removeAt(int index)
+    throws ScriptException
+  {
+    if (index >= array.size())
+      throw new ScriptException("Remove out of bounds");
+    array.remove(index);
+  }
+
+  /** Extend VariableReference class so we capture attempts to set the reference, and actually
overwrite the child when that is done */
+  protected class ElementReference implements VariableReference
+  {
+    protected int index;
+    
+    public ElementReference(int index)
+    {
+      this.index = index;
+    }
+    
+    public void setReference(Variable v)
+      throws ScriptException
+    {
+      if (index >= array.size())
+        throw new ScriptException("Index out of range for array children");
+      array.set(index,v);
+    }
+    
+    public Variable resolve()
+      throws ScriptException
+    {
+      if (index >= array.size())
+        throw new ScriptException("Index out of range for array children");
+      return array.get(index);
+    }
+    
+    /** Check if this reference is null */
+    public boolean isNull()
+    {
+      return index >= array.size() || array.get(index) == null;
+    }
+
+  }
+}

Propchange: incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/VariableArray.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/lcf/trunk/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/VariableArray.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message