Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
**** BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse Public License Version 1.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.eclipse.org/legal/epl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2009-2011 Yoko Harada <yokolet@gmail.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the EPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the EPL, the GPL or the LGPL. **** END LICENSE BLOCK *****
 
 package org.jruby.embed.jsr223;
 
 import java.io.Reader;
 import java.io.Writer;
 import java.util.List;
Implementation of javax.script.ScriptContext.

Author(s):
Yoko Harada <yokolet@gmail.com>
 
 class JRubyContext implements ScriptContext {
     private ScriptingContainer container;
     private final List<IntegerscopeList;
     private Bindings globalMap = null;
     private Bindings engineMap = new SimpleBindings();
     private Reader reader = null;
     private Writer writer = null;
     private Writer errorWriter = null;
     
     public enum Scope {
 
         ENGINE(.),
         GLOBAL(.);
         private final int priority;
 
         Scope(int priority) {
             this. = priority;
         }
 
         int getPriority() {
             return ;
         }
     }
 
     JRubyContext(ScriptingContainer container) {
         this. = container;
         List<Integerlist = new ArrayList<Integer>();
         for (Scope scope : Scope.values()) {
             list.add(scope.getPriority());
         }
          = Collections.unmodifiableList(list);
     }
 
     private void checkName(String name) {
         if (name == null) {
             throw new NullPointerException("name is null");
         }
         if (name.length() == 0) {
             throw new IllegalArgumentException("name is empty");
         }
     }
 
     public Object getAttribute(String name) {
         Object ret = null;
         for (Scope scope : Scope.values()) {
             ret = getAttributeFromScope(scope.getPriority(), name);
             if (ret != null) {
                 return ret;
             }
         }
         return ret;
    }
    private Object getAttributeFromScope(int priorityString name) {
        checkName(name);
        Object value;
        if (priority == ..getPriority()) {
            value = .get(name);
            if (value == null && Utils.isRubyVariable(name)) {
                value = .get(Utils.getReceiver(this), name);
                .put(namevalue);
            }
            return value;
        } else if (priority == ..getPriority()) {
            if ( == null) {
                return null;
            }
            return .get(name);
        } else {
            throw new IllegalArgumentException("invalid scope");
        }
    }
    public Object getAttribute(String nameint scope) {
        return getAttributeFromScope(scopename);
    }
    public int getAttributesScope(String name) {
        for (Scope scope : Scope.values()) {
            Object ret = getAttributeFromScope(scope.getPriority(), name);
            if (ret != null) {
                return scope.getPriority();
            }
        }
        return -1;
    }
    public Bindings getBindings(int priority) {
        if (priority == ..getPriority()) {
            return ;
        } else if (priority == ..getPriority()) {
            return ;
        } else {
            throw new IllegalArgumentException("invalid scope");
        }
    }
        return ;
    }
        return ;
    }
    public Writer getErrorWriter() {
        return ;
    }
    public Reader getReader() {
        return ;
    }
    public List<IntegergetScopes() {
        return ;
    }
    public Writer getWriter() {
        return ;
    }
    public Object removeAttribute(String nameint priority) {
        checkName(name);
        Bindings bindings = getBindings(priority);
        if (bindings == null) {
            return null;
        }
        return bindings.remove(name);
    }
    public void setAttribute(String keyObject valueint priority) {
        Bindings bindings = getBindings(priority);
        if (bindings == null) {
            return;
        }
        bindings.put(keyvalue);
    }
    public void setBindings(Bindings bindingsint scope) {
        if (scope == ..getPriority() && bindings == null) {
            throw new NullPointerException("null bindings in ENGINE scope");
        }
        if (scope == ..getPriority()) {
             = bindings;
        } else if (scope == ..getPriority()) {
             = bindings;
        } else {
            throw new IllegalArgumentException("invalid scope");
        }
    }
    void setEngineScopeBindings(Bindings bindings) {
         = bindings;
    }
    void setGlobalScopeBindings(Bindings bindings) {
         = bindings;
    }
    public void setErrorWriter(Writer errorWriter) {
        if (errorWriter == null) {
            return;
        }
        if (getErrorWriter() == errorWriter) {
            return;
        }
        this. = errorWriter;
    }
    public void setReader(Reader reader) {
        setReader(readertrue);
    }
    void setReader(Reader readerboolean updateContainer) {
        if (reader == null) {
            return;
        }
        if (getReader() == reader) {
            return;
        }
        this. = reader;
    }
    public void setWriter(Writer writer) {
        if (writer == null) {
            return;
        }
        if (getWriter() == writer) {
            return;
        }
        this. = writer;
    }
New to GrepCode? Check out our FAQ X