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 org.clearsilver.jni;
18
19import org.clearsilver.CS;
20import org.clearsilver.ClearsilverFactory;
21import org.clearsilver.DelegatedHdf;
22import org.clearsilver.HDF;
23
24/**
25 * Factory implementation for the original JNI version of Java Clearsilver
26 */
27public class JniClearsilverFactory implements ClearsilverFactory {
28
29  private final boolean unwrapDelegatedHdfs;
30
31  /**
32   * Default constructor. Any {@link org.clearsilver.DelegatedHdf}s passed to
33   * {@link #newCs} will be fully unwrapped before being passed to CS
34   * implementation constructor.
35   */
36  public JniClearsilverFactory() {
37    this(true);
38  }
39
40  /**
41   * Constructor that takes the option whether to unwrap all
42   * {@link org.clearsilver.DelegatedHdf} objects before passing the
43   * {@link org.clearsilver.HDF} object to the {@link org.clearsilver.CS}
44   * implementation constructor.
45   * <br>
46   * Developers that want strict checking that the HDF passed to newCs matches
47   * HDF objects constructed by newHDF may want to pass in {@code false}.
48   *
49   * @param unwrapDelegatedHdfs true if {@link org.clearsilver.HDF}s passed to
50   * {@link #newCs} should be unwrapped if they are
51   * {@link org.clearsilver.DelegatedHdf} objects, false otherwise.
52   */
53  public JniClearsilverFactory(boolean unwrapDelegatedHdfs) {
54    this.unwrapDelegatedHdfs = unwrapDelegatedHdfs;
55  }
56
57  /**
58  * Create a new CS object.
59  * @param hdf the HDF object to use in constructing the CS object.
60  * @return a new CS object
61  */
62  public CS newCs(HDF hdf) {
63    if (unwrapDelegatedHdfs) {
64      hdf = DelegatedHdf.getFullyUnwrappedHdf(hdf);
65    }
66    return new JniCs(JniHdf.cast(hdf));
67  }
68
69  /**
70   * Create a new CS object.  Also checks and unwraps any DelegatedHdfs
71   * passed into the method.
72   * @param hdf the HDF object to use in constructing the CS object.
73   * @param globalHdf the global HDF object to use in constructing the
74   * CS object.
75   * @return a new CS object
76   */
77  public CS newCs(HDF hdf, HDF globalHdf) {
78    if (unwrapDelegatedHdfs) {
79      hdf = DelegatedHdf.getFullyUnwrappedHdf(hdf);
80      globalHdf = DelegatedHdf.getFullyUnwrappedHdf(globalHdf);
81    }
82    return new JniCs(JniHdf.cast(hdf), JniHdf.cast(globalHdf));
83  }
84
85  /**
86   * Create a new HDF object.
87   * @return a new HDF object
88   */
89  public HDF newHdf() {
90    return new JniHdf();
91  }
92}
93