KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > libs > freemarker > RsrcLoader


1 /*
2 * The contents of this file are subject to the terms of the
3 * Common Development and Distribution License, Version 1.0 only
4 * (the "License"). You may not use this file except in compliance
5 * with the License. A copy of the license is available
6 * at http://www.opensource.org/licenses/cddl1.php
7 *
8 * See the License for the specific language governing permissions
9 * and limitations under the License.
10 *
11 * The Original Code is the nbdoclet.sf.net project.
12 * The Initial Developer of the Original Code is Petr Zajac.
13 * Portions created by Petr Zajac are Copyright (C) 2006.
14 * Portions created by Jaroslav Tulach are Copyright (C) 2006.
15 * Portions Copyrighted 2007 Sun Microsystems, Inc.
16 * All Rights Reserved.
17 */

18 package org.netbeans.libs.freemarker;
19
20 import freemarker.cache.TemplateLoader;
21 import freemarker.template.Configuration;
22 import freemarker.template.TemplateModel;
23 import freemarker.template.TemplateModelException;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.LinkedHashSet JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.script.ScriptContext;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.Repository;
33 import org.openide.util.Exceptions;
34
35 /**
36  * Velocity templates resource loader rewritten for Freemarker to
37  * access resources via FileSystem.
38  *
39  * @author Petr Zajac, adopted by Jaroslav Tulach
40  */

41
42 final class RsrcLoader extends Configuration implements TemplateLoader {
43     private FileObject fo;
44     private ScriptContext map;
45
46     RsrcLoader(FileObject fo, ScriptContext map) {
47         this.fo = fo;
48         this.map = map;
49         setTemplateLoader(this);
50     }
51
52     private FileObject getFile(String JavaDoc name) {
53        FileObject fo = (getFolder() == null) ? null : getFolder().getFileObject(name);
54        return fo;
55     }
56
57     private FileObject getFolder() {
58         return fo != null ? fo.getParent() : Repository.getDefault().getDefaultFileSystem().getRoot();
59     }
60
61     public Object JavaDoc findTemplateSource(String JavaDoc string) throws IOException JavaDoc {
62         FileObject fo = getFile(string);
63         return fo == null ? null : new Wrap(fo);
64     }
65
66     public long getLastModified(Object JavaDoc object) {
67         return ((Wrap)object).fo.lastModified().getTime();
68     }
69
70     public Reader JavaDoc getReader(Object JavaDoc object, String JavaDoc encoding) throws IOException JavaDoc {
71         Wrap w = (Wrap)object;
72         if (w.reader == null) {
73             w.reader = new InputStreamReader JavaDoc(w.fo.getInputStream(), encoding);
74         }
75         return w.reader;
76     }
77
78     public void closeTemplateSource(Object JavaDoc object) throws IOException JavaDoc {
79         Wrap w = (Wrap)object;
80         if (w.reader != null) {
81             w.reader.close();
82         }
83     }
84         
85     public Object JavaDoc put(String JavaDoc string, Object JavaDoc object) {
86         assert false;
87         return null;
88     }
89
90     public TemplateModel getSharedVariable(String JavaDoc string) {
91         Object JavaDoc value = map == null ? null : map.getAttribute(string);
92         if (value == null || fo != null) {
93             value = fo.getAttribute(string);
94         }
95         try {
96             return getObjectWrapper().wrap(value);
97         } catch (TemplateModelException ex) {
98             Exceptions.printStackTrace(ex);
99             return null;
100         }
101     }
102
103     public Set JavaDoc getSharedVariableNames() {
104         LinkedHashSet JavaDoc<String JavaDoc> keys = new LinkedHashSet JavaDoc<String JavaDoc>();
105
106         if (map != null) {
107             keys.addAll(map.getBindings(map.ENGINE_SCOPE).keySet());
108         }
109
110         if (fo != null) {
111             Enumeration JavaDoc<String JavaDoc> en = fo.getAttributes();
112             while (en.hasMoreElements()) {
113                 keys.add(en.nextElement());
114             }
115         }
116
117         return keys;
118     }
119
120     private static final class Wrap {
121         public FileObject fo;
122         public Reader JavaDoc reader;
123         
124         public Wrap(FileObject fo) {
125             this.fo = fo;
126         }
127     } // end Wrap
128
}
129
Popular Tags