001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.type.ref; 020 021import lucee.runtime.PageContext; 022import lucee.runtime.exp.PageException; 023import lucee.runtime.op.Caster; 024import lucee.runtime.type.Collection; 025import lucee.runtime.type.KeyImpl; 026import lucee.runtime.type.StructImpl; 027 028/** 029 * represent a reference to a Object 030 */ 031public final class NativeReference implements Reference { 032 033 private Object o; 034 private String key; 035 036 037 /** 038 * Constructor of the class 039 * @param o 040 * @param key 041 */ 042 private NativeReference(Object o, String key) { 043 this.o=o; 044 this.key=key; 045 } 046 047 048 /** 049 * returns a Reference Instance 050 * @param o 051 * @param key 052 * @return Reference Instance 053 */ 054 public static Reference getInstance(Object o, String key) { 055 if(o instanceof Reference) { 056 return new ReferenceReference((Reference)o,key); 057 } 058 Collection coll = Caster.toCollection(o,null); 059 if(coll!=null) return new VariableReference(coll,key); 060 return new NativeReference(o,key); 061 } 062 063 @Override 064 public Object getParent() { 065 return o; 066 } 067 068 public Collection.Key getKey() { 069 return KeyImpl.init(key); 070 } 071 072 @Override 073 public String getKeyAsString() { 074 return key; 075 } 076 077 @Override 078 public Object get(PageContext pc) throws PageException { 079 return pc.getCollection(o,key); 080 } 081 082 @Override 083 public Object get(PageContext pc, Object defaultValue) { 084 return pc.getCollection(o,key,null); 085 } 086 087 @Override 088 public Object touch(PageContext pc) throws PageException { 089 Object rtn=pc.getCollection(o,key,null); 090 if(rtn!=null) return rtn; 091 return pc.set(o,key,new StructImpl()); 092 } 093 public Object touchEL(PageContext pc) { 094 Object rtn=pc.getCollection(o,key,null); 095 if(rtn!=null) return rtn; 096 try { 097 return pc.set(o,key,new StructImpl()); 098 } catch (PageException e) { 099 return null; 100 } 101 } 102 103 @Override 104 public Object set(PageContext pc,Object value) throws PageException { 105 return pc.set(o,key,value); 106 } 107 108 public Object setEL(PageContext pc,Object value) { 109 try { 110 return pc.set(o,key,value); 111 } catch (PageException e) { 112 return null; 113 } 114 } 115 116 @Override 117 public Object remove(PageContext pc) throws PageException { 118 return pc.getVariableUtil().remove(o,key); 119 } 120 121 @Override 122 public Object removeEL(PageContext pc) { 123 return pc.getVariableUtil().removeEL(o,key); 124 } 125 126 127}