• 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 org.clearsilver.jni;
18 
19 import org.clearsilver.CS;
20 import org.clearsilver.CSFileLoader;
21 import org.clearsilver.HDF;
22 
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 
26 /**
27  * JNI implementation of the CS interface.
28  */
29 public class JniCs implements CS {
30   long csptr;
31 
32   protected JniHdf globalHDF;
33   protected JniHdf localHDF;
34 
35   static {
JNI.loadLibrary()36     JNI.loadLibrary();
37   }
38 
JniCs(JniHdf ho)39   JniCs(JniHdf ho) {
40     this.globalHDF = null;
41     this.localHDF = ho;
42     csptr = _init(ho.hdfptr);
43   }
44 
JniCs(JniHdf ho, JniHdf global)45   JniCs(JniHdf ho, JniHdf global) {
46     this(ho);
47 
48     this.globalHDF = global;
49     if (global != null) {
50       _setGlobalHdf(csptr,global.hdfptr);
51     }
52   }
53 
54   // Specify a new/different global HDF
setGlobalHDF(HDF global)55   public void setGlobalHDF(HDF global) {
56     JniHdf globalHdf = JniHdf.cast(global);
57     _setGlobalHdf(csptr, globalHdf.hdfptr);
58     this.globalHDF = globalHdf;
59   }
60 
61   // Return global hdf in use
getGlobalHDF()62   public HDF getGlobalHDF() {
63     return this.globalHDF;
64   }
65 
close()66   public void close() {
67     if (csptr != 0) {
68       _dealloc(csptr);
69       csptr = 0;
70     }
71   }
72 
73   // Don't rely on this being called.
finalize()74   protected void finalize() {
75     close();
76   }
77 
78   /**
79    * Parses the specified file as if it has template content. The file will
80    * be located using the HDF's loadpaths.
81    * @param filename the name of file to read in and parse.
82    * @throws java.io.FileNotFoundException if the specified file does not
83    * exist.
84    * @throws IOException other problems reading the file.
85    */
parseFile(String filename)86   public void parseFile(String filename) throws IOException {
87     if (csptr == 0) {
88      throw new NullPointerException("CS is closed.");
89     }
90     _parseFile(csptr, filename, fileLoader != null);
91   }
92 
parseStr(String content)93   public void parseStr(String content) {
94     if (csptr == 0) {
95       throw new NullPointerException("CS is closed.");
96     }
97     _parseStr(csptr,content);
98   }
99 
render()100   public String render() {
101     if (csptr == 0) {
102       throw new NullPointerException("CS is closed.");
103     }
104     return _render(csptr, fileLoader != null);
105   }
106 
107 
fileLoad(String filename)108   protected String fileLoad(String filename) throws IOException,
109       FileNotFoundException {
110     if (csptr == 0) {
111       throw new NullPointerException("CS is closed.");
112     }
113     CSFileLoader aFileLoader = fileLoader;
114     if (aFileLoader == null) {
115       throw new NullPointerException("No fileLoader specified.");
116     } else {
117       String result = aFileLoader.load(localHDF, filename);
118       if (result == null) {
119         throw new NullPointerException("CSFileLoader.load() returned null");
120       }
121       return result;
122     }
123   }
124 
125   // The optional CS file loader to use to read in files
126   private CSFileLoader fileLoader = null;
127 
128   /**
129    * Get the file loader in use, if any.
130    * @return the file loader in use.
131    */
getFileLoader()132   public CSFileLoader getFileLoader() {
133     return fileLoader;
134   }
135 
136   /**
137    * Set the CS file loader to use
138    * @param fileLoader the file loader that should be used.
139    */
setFileLoader(CSFileLoader fileLoader)140   public void setFileLoader(CSFileLoader fileLoader) {
141     this.fileLoader = fileLoader;
142   }
143 
144 
145   // Native methods
_init(long ptr)146   private native long    _init(long ptr);
_dealloc(long ptr)147   private native void   _dealloc(long ptr);
_parseFile(long ptr, String filename, boolean use_cb)148   private native void   _parseFile(long ptr, String filename,
149       boolean use_cb) throws IOException;
_parseStr(long ptr, String content)150   private native void   _parseStr(long ptr, String content);
_render(long ptr, boolean use_cb)151   private native String _render(long ptr, boolean use_cb);
_setGlobalHdf(long csptr, long hdfptr)152   private native void   _setGlobalHdf(long csptr, long hdfptr);
153 }
154