001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1998.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Igor Bukanov
026: * Bob Jervis
027: *
028: * Alternatively, the contents of this file may be used under the terms of
029: * the GNU General Public License Version 2 or later (the "GPL"), in which
030: * case the provisions of the GPL are applicable instead of those above. If
031: * you wish to allow use of your version of this file only under the terms of
032: * the GPL and not to allow others to use your version of this file under the
033: * MPL, indicate your decision by deleting the provisions above and replacing
034: * them with the notice and other provisions required by the GPL. If you do
035: * not delete the provisions above, a recipient may use your version of this
036: * file under either the MPL or the GPL.
037: *
038: * ***** END LICENSE BLOCK ***** */
039:
040: package org.mozilla.javascript.tools.shell;
041:
042: import org.mozilla.javascript.*;
043:
044: public class ShellContextFactory extends ContextFactory {
045: private boolean strictMode;
046: private boolean warningAsError;
047: private int languageVersion;
048: private int optimizationLevel;
049: private boolean generatingDebug;
050: private ErrorReporter errorReporter;
051:
052: protected boolean hasFeature(Context cx, int featureIndex) {
053: switch (featureIndex) {
054: case Context.FEATURE_STRICT_VARS:
055: case Context.FEATURE_STRICT_EVAL:
056: case Context.FEATURE_STRICT_MODE:
057: return strictMode;
058:
059: case Context.FEATURE_WARNING_AS_ERROR:
060: return warningAsError;
061: }
062: return super .hasFeature(cx, featureIndex);
063: }
064:
065: protected void onContextCreated(Context cx) {
066: cx.setLanguageVersion(languageVersion);
067: cx.setOptimizationLevel(optimizationLevel);
068: if (errorReporter != null) {
069: cx.setErrorReporter(errorReporter);
070: }
071: cx.setGeneratingDebug(generatingDebug);
072: super .onContextCreated(cx);
073: }
074:
075: public void setStrictMode(boolean flag) {
076: checkNotSealed();
077: this .strictMode = flag;
078: }
079:
080: public void setWarningAsError(boolean flag) {
081: checkNotSealed();
082: this .warningAsError = flag;
083: }
084:
085: public void setLanguageVersion(int version) {
086: Context.checkLanguageVersion(version);
087: checkNotSealed();
088: this .languageVersion = version;
089: }
090:
091: public void setOptimizationLevel(int optimizationLevel) {
092: Context.checkOptimizationLevel(optimizationLevel);
093: checkNotSealed();
094: this .optimizationLevel = optimizationLevel;
095: }
096:
097: public void setErrorReporter(ErrorReporter errorReporter) {
098: if (errorReporter == null)
099: throw new IllegalArgumentException();
100: this .errorReporter = errorReporter;
101: }
102:
103: public void setGeneratingDebug(boolean generatingDebug) {
104: this.generatingDebug = generatingDebug;
105: }
106: }
|