001: /*
002: * Helma License Notice
003: *
004: * The contents of this file are subject to the Helma License
005: * Version 2.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://adele.helma.org/download/helma/license.txt
008: *
009: * Copyright 1998-2003 Helma Software. All Rights Reserved.
010: *
011: * $RCSfile$
012: * $Author: root $
013: * $Revision: 8604 $
014: * $Date: 2007-09-28 15:16:38 +0200 (Fre, 28 Sep 2007) $
015: */
016:
017: package helma.scripting.rhino;
018:
019: import helma.framework.core.RequestPath;
020: import org.mozilla.javascript.*;
021:
022: import java.io.UnsupportedEncodingException;
023:
024: /**
025: * This class wraps around instances of helma.framework.core.RequestPath and
026: * exposes them in an array-like fashion to the JavaScript runtime.
027: *
028: * @see helma.framework.core.RequestPath
029: */
030: public class PathWrapper extends ScriptableObject {
031:
032: RequestPath path;
033: RhinoCore core;
034:
035: /**
036: * Zero arg constructor for creating the PathWrapper prototype.
037: */
038: public PathWrapper(RhinoCore core) throws PropertyException,
039: NoSuchMethodException {
040: this .core = core;
041: // create a dummy path object
042: this .path = new RequestPath(core.app);
043:
044: // initialize properties and functions
045: int attributes = DONTENUM | READONLY | PERMANENT;
046: setParentScope(core.getScope());
047: setPrototype(null);
048: defineProperty("length", PathWrapper.class, attributes);
049: defineFunctionProperties(new String[] { "href", "contains" },
050: PathWrapper.class, attributes);
051: }
052:
053: /**
054: * Creates a new PathWrapper around a RequestPath.
055: */
056: PathWrapper(RequestPath path, RhinoCore core) {
057: this .path = path;
058: this .core = core;
059: }
060:
061: /**
062: * Returns a path object in the wrapped path by property name.
063: */
064: public Object get(String name, Scriptable start) {
065: Object obj = path.getByPrototypeName(name);
066:
067: if (obj != null) {
068: return Context.toObject(obj, core.getScope());
069: }
070:
071: return super .get(name, start);
072: }
073:
074: /**
075: * Returns a path object in the wrapped path by property name.
076: */
077: public Object get(int idx, Scriptable start) {
078: Object obj = path.get(idx);
079:
080: if (obj != null) {
081: return Context.toObject(obj, core.getScope());
082: }
083:
084: return null;
085: }
086:
087: /**
088: * Checks if an object with the given name is contained in the path.
089: */
090: public boolean has(String name, Scriptable start) {
091: return path.getByPrototypeName(name) != null;
092: }
093:
094: /**
095: * Checks if an object with the given index is contained in the path.
096: */
097: public boolean has(int index, Scriptable start) {
098: return index >= 0 && index < path.size();
099: }
100:
101: /**
102: * Returns a list of array indices 0..length-1.
103: */
104: public Object[] getIds() {
105: Object[] ids = new Object[path.size()];
106:
107: for (int i = 0; i < ids.length; i++) {
108: ids[i] = new Integer(i);
109: }
110:
111: return ids;
112: }
113:
114: /**
115: * Getter for length property.
116: */
117: public long getLength() {
118: return path.size();
119: }
120:
121: /**
122: * Returns the wrapped path rendered as URL path.
123: */
124: public String href(Object action)
125: throws UnsupportedEncodingException {
126: if (action != null && action != Undefined.instance) {
127: return path.href(action.toString());
128: }
129:
130: return path.href(null);
131: }
132:
133: /**
134: * Checks if the given object is contained in the request path
135: *
136: * @param obj the element to check
137: * @return the index of the element, or -1 if it isn't contained
138: */
139: public int contains(Object obj) {
140: if (obj instanceof Wrapper)
141: obj = ((Wrapper) obj).unwrap();
142: return path.contains(obj);
143: }
144:
145: public String getClassName() {
146: return "[PathWrapper]";
147: }
148:
149: public String toString() {
150: return "PathWrapper[" + path.toString() + "]";
151: }
152:
153: /**
154: * Return a primitive representation for this object.
155: * FIXME: We always return a string representation.
156: *
157: * @param hint the type hint
158: * @return the default value for the object
159: */
160: public Object getDefaultValue(Class hint) {
161: return toString();
162: }
163: }
|