• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.clearsilver.jsilver.values;
18 
19 import com.google.clearsilver.jsilver.autoescape.EscapeMode;
20 import com.google.clearsilver.jsilver.data.Data;
21 import com.google.clearsilver.jsilver.data.DataContext;
22 import com.google.clearsilver.jsilver.data.TypeConverter;
23 
24 /**
25  * A value linked to a variable reference. When this value is evaluated in an expression a Data
26  * object will be fetched from the provided DataContext and and its value returned or {@code null}
27  * if there is no Data object associated with the given name.
28  *
29  * @see Value
30  * @see Data
31  */
32 public class VariableValue extends VariantValue {
33   // TODO: Make this non-public and come up with a smarter way
34   // for it to be used elsewhere without having to be used in a cast.
35 
36   private final String name;
37   private final DataContext dataContext;
38 
39   private boolean gotRef = false;
40   private Data reference;
41 
VariableValue(String name, DataContext dataContext)42   public VariableValue(String name, DataContext dataContext) {
43     // VariableValues always have partiallyEscaped=false since they represent
44     // a Data object, not a compound expression containing escaping functions.
45     // We override getEscapeMode() to return the Data object's escape mode,
46     // so the mode we pass here is just ignored.
47     super(EscapeMode.ESCAPE_NONE, false);
48     this.dataContext = dataContext;
49     this.name = name;
50   }
51 
getName()52   public String getName() {
53     return name;
54   }
55 
getReference()56   public Data getReference() {
57     if (!gotRef) {
58       reference = dataContext.findVariable(name, false);
59       gotRef = true;
60     }
61     return reference;
62   }
63 
64   @Override
value()65   protected String value() {
66     Data data = getReference();
67     return data == null ? null : data.getValue();
68   }
69 
70   @Override
exists()71   public boolean exists() {
72     return TypeConverter.exists(getReference());
73   }
74 
75   // Note we are not overriding asString as we want that to return the value
76   // of the node located at this address.
77   @Override
toString()78   public String toString() {
79     return name;
80   }
81 
82   @Override
getEscapeMode()83   public EscapeMode getEscapeMode() {
84     Data data = getReference();
85     if (data == null) {
86       return super.getEscapeMode();
87     }
88     return data.getEscapeMode();
89   }
90 
91 }
92