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.adaptor;
18
19import com.google.clearsilver.jsilver.JSilver;
20import com.google.clearsilver.jsilver.JSilverOptions;
21import com.google.clearsilver.jsilver.data.DataFactory;
22import com.google.clearsilver.jsilver.data.DefaultData;
23import com.google.clearsilver.jsilver.data.HDFDataFactory;
24
25import org.clearsilver.ClearsilverFactory;
26import org.clearsilver.DelegatedHdf;
27import org.clearsilver.HDF;
28
29/**
30 * ClearsilverFactory that adapts JSilver for use as any HDF/CS rendering engine.
31 */
32public class JSilverFactory implements ClearsilverFactory {
33
34  private static final JSilverOptions DEFAULT_OPTIONS = new JSilverOptions();
35
36  private final boolean unwrapDelegatedHdfs;
37  private final JSilver jSilver;
38  private final JSilverOptions options;
39  private final DataFactory dataFactory;
40
41  private final LoadPathToFileCache loadPathCache;
42
43  /**
44   * Default constructor. enables unwrapping of DelegatedHdfs.
45   */
46  public JSilverFactory() {
47    this(DEFAULT_OPTIONS);
48  }
49
50  public JSilverFactory(JSilverOptions options) {
51    this(options, true);
52  }
53
54  public JSilverFactory(JSilverOptions options, boolean unwrapDelegatedHdfs) {
55    this(new JSilver(null, options), unwrapDelegatedHdfs);
56  }
57
58  /**
59   * This constructor is available for those who already use JSilver and want to use the same
60   * attributes and caches for their Java Clearsilver Framework code. Users who use only JCF should
61   * use a different constructor.
62   *
63   * @param jSilver existing instance of JSilver to use for parsing and rendering.
64   * @param unwrapDelegatedHdfs whether to unwrap DelegetedHdfs or not before casting.
65   */
66  public JSilverFactory(JSilver jSilver, boolean unwrapDelegatedHdfs) {
67    this.unwrapDelegatedHdfs = unwrapDelegatedHdfs;
68    this.jSilver = jSilver;
69    this.options = jSilver.getOptions();
70    if (this.options.getLoadPathCacheSize() == 0) {
71      this.loadPathCache = null;
72    } else {
73      this.loadPathCache = new LoadPathToFileCache(this.options.getLoadPathCacheSize());
74    }
75    this.dataFactory =
76        new HDFDataFactory(options.getIgnoreAttributes(), options.getStringInternStrategy());
77  }
78
79  @Override
80  public JCs newCs(HDF hdf) {
81    if (unwrapDelegatedHdfs) {
82      hdf = DelegatedHdf.getFullyUnwrappedHdf(hdf);
83    }
84    return new JCs(JHdf.cast(hdf), jSilver, loadPathCache);
85  }
86
87  @Override
88  public JCs newCs(HDF hdf, HDF globalHdf) {
89    if (unwrapDelegatedHdfs) {
90      hdf = DelegatedHdf.getFullyUnwrappedHdf(hdf);
91      globalHdf = DelegatedHdf.getFullyUnwrappedHdf(globalHdf);
92    }
93    JCs cs = new JCs(JHdf.cast(hdf), jSilver, loadPathCache);
94    cs.setGlobalHDF(globalHdf);
95    return cs;
96  }
97
98  @Override
99  public JHdf newHdf() {
100    return new JHdf(new DefaultData(), dataFactory, loadPathCache, options);
101  }
102}
103