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
17package com.google.clearsilver.jsilver.data;
18
19/**
20 * This is a special implementation of ChainedData to be used for holding the local and global Data
21 * objects (like local and global HDFs in Clearsilver). It prevents writes and modifications to the
22 * global Data object and applies them all to the local data object.
23 */
24public class LocalAndGlobalData extends ChainedData {
25
26  private final Data local;
27
28  /**
29   * Creates a Data object that encapsulates both request-scoped local HDF and an application
30   * global-scoped HDF that can be read from the template renderer. Part of the backwards
31   * compatibility with JNI Clearsilver and its globalHdf support.
32   *
33   * @param local the request-specific HDF data that takes priority.
34   * @param global application global HDF data that should be read but not written to from the
35   *        template renderer.
36   */
37  public LocalAndGlobalData(Data local, Data global) {
38    this(local, global, false);
39  }
40
41  /**
42   * Creates a Data object that encapsulates both request-scoped local HDF and an application
43   * global-scoped HDF that can be read from the template renderer. Part of the backwards
44   * compatibility with JNI Clearsilver and its globalHdf support. We wrap the global HDF in an
45   * UnmodifiableData delegate
46   *
47   * @param local the request-specific HDF data that takes priority.
48   * @param global application global HDF data that should be read but not written to from the
49   *        template renderer.
50   * @param allowGlobalDataModification if {@code true} then enable legacy mode where we do not wrap
51   *        the global Data with an Unmodifiable wrapper. Should not be set to {@code true} unless
52   *        incompatibilities or performance issues found. Note, that setting to {@code true} could
53   *        introduce bugs in templates that acquire local references to the global data structure
54   *        and then try to modify them, which is not the intended behavior.
55   */
56  public LocalAndGlobalData(Data local, Data global, boolean allowGlobalDataModification) {
57    super(local, prepareGlobal(global, allowGlobalDataModification));
58    this.local = local;
59  }
60
61  private static Data prepareGlobal(Data global, boolean allowGlobalDataModification) {
62    if (allowGlobalDataModification) {
63      return global;
64    } else {
65      return new UnmodifiableData(global);
66    }
67  }
68
69  @Override
70  public Data createChild(String path) {
71    // We never want to modify the global Data object.
72    return local.createChild(path);
73  }
74}
75