156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson/*
256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Copyright (C) 2010 Google Inc.
356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Licensed under the Apache License, Version 2.0 (the "License");
556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * you may not use this file except in compliance with the License.
656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * You may obtain a copy of the License at
756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * http://www.apache.org/licenses/LICENSE-2.0
956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
1056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Unless required by applicable law or agreed to in writing, software
1156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * distributed under the License is distributed on an "AS IS" BASIS,
1256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * See the License for the specific language governing permissions and
1456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * limitations under the License.
1556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson */
1656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
1756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonpackage com.google.clearsilver.jsilver.data;
1856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
1956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson/**
2056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * This is a special implementation of ChainedData to be used for holding the local and global Data
2156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * objects (like local and global HDFs in Clearsilver). It prevents writes and modifications to the
2256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * global Data object and applies them all to the local data object.
2356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson */
2456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonpublic class LocalAndGlobalData extends ChainedData {
2556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
2656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  private final Data local;
2756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
2856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  /**
2956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * Creates a Data object that encapsulates both request-scoped local HDF and an application
3056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * global-scoped HDF that can be read from the template renderer. Part of the backwards
3156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * compatibility with JNI Clearsilver and its globalHdf support.
3256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *
3356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param local the request-specific HDF data that takes priority.
3456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param global application global HDF data that should be read but not written to from the
3556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *        template renderer.
3656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   */
3756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  public LocalAndGlobalData(Data local, Data global) {
3856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    this(local, global, false);
3956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
4056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
4156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  /**
4256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * Creates a Data object that encapsulates both request-scoped local HDF and an application
4356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * global-scoped HDF that can be read from the template renderer. Part of the backwards
4456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * compatibility with JNI Clearsilver and its globalHdf support. We wrap the global HDF in an
4556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * UnmodifiableData delegate
4656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *
4756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param local the request-specific HDF data that takes priority.
4856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param global application global HDF data that should be read but not written to from the
4956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *        template renderer.
5056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param allowGlobalDataModification if {@code true} then enable legacy mode where we do not wrap
5156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *        the global Data with an Unmodifiable wrapper. Should not be set to {@code true} unless
5256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *        incompatibilities or performance issues found. Note, that setting to {@code true} could
5356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *        introduce bugs in templates that acquire local references to the global data structure
5456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *        and then try to modify them, which is not the intended behavior.
5556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   */
5656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  public LocalAndGlobalData(Data local, Data global, boolean allowGlobalDataModification) {
5756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    super(local, prepareGlobal(global, allowGlobalDataModification));
5856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    this.local = local;
5956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
6056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
6156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  private static Data prepareGlobal(Data global, boolean allowGlobalDataModification) {
6256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    if (allowGlobalDataModification) {
6356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      return global;
6456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    } else {
6556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      return new UnmodifiableData(global);
6656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    }
6756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
6856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
6956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  @Override
7056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  public Data createChild(String path) {
7156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    // We never want to modify the global Data object.
7256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    return local.createChild(path);
7356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
7456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson}
75