KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > repository > RepositoryWrapper


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.repository;
50
51 import java.io.IOException JavaDoc;
52
53 import org.apache.commons.logging.Log;
54 import org.apache.commons.logging.LogFactory;
55 import org.apache.commons.vfs.FileSystemManager;
56 import org.jpublish.RequestContext;
57
58 /**
59  * A wrapper around any repository. This wrapper is used to expose the Repository to the view renderer. The current
60  * context is stored when the wrapper is created which means that the context does not have to be passed to the
61  * <code>get()</code> methods at request-time.
62  *
63  * <p>The primary purpose of the wrapper is to allow content authors to include content from a repository using the
64  * form:</p>
65  *
66  * <blockquote> $repository.get("path/to/content") </blockquote>
67  *
68  * <p>Since the context is already in the wrapper the call above would then pass the path and the context to the actual
69  * repository to include the parsed content. Content authors can also include a second argument of 'false' to indicate
70  * that the content should not be parsed.</p>
71  *
72  * @author Anthony Eden
73  */

74
75 public class RepositoryWrapper {
76
77     private Log log = LogFactory.getLog(RepositoryWrapper.class);
78     private Repository repository;
79     private RequestContext context;
80
81     /**
82      * Construct a new RepositoryWrapper over the given repository using the given context for merging.
83      *
84      * @param repository The repository
85      * @param context The current context
86      */

87
88     public RepositoryWrapper(Repository repository, RequestContext context) {
89         this.repository = repository;
90         this.context = context;
91     }
92
93     /**
94      * Get a reference to the wrapped Repository.
95      *
96      * @return The wrapped repository
97      */

98
99     public Repository getRepository() {
100         return repository;
101     }
102
103     /**
104      * Return the virtual file system manager. This method may return null if the implementation does not support a
105      * virtual file system.
106      *
107      * @return The FileSystemManager
108      * @throws IOException
109      */

110
111     public FileSystemManager getFileSystemManager() throws IOException JavaDoc {
112         return repository.getFileSystemManager();
113     }
114
115     /**
116      * Get the name of the repository. This name is used to expose the Repository in the view renderer.
117      *
118      * @return The Repository name
119      */

120
121     public String JavaDoc getName() {
122         return repository.getName();
123     }
124
125     /**
126      * Get the content at the given path. The content will be merged with the associated context.
127      *
128      * @param path The content path
129      * @return The content as a String
130      */

131
132     public String JavaDoc get(String JavaDoc path) {
133         if (log.isDebugEnabled()) {
134             log.debug("get(" + path + ")");
135         }
136         return get(path, true);
137     }
138
139     /**
140      * Get the content at the given path, optinally merging it with the associated context. If merge is true then
141      * merging will occur. Errors will be caught and the error message will be returned in place of the content.
142      *
143      * @param path The content path
144      * @param merge True to merge
145      * @return The content as a String
146      */

147
148     public String JavaDoc get(String JavaDoc path, boolean merged) {
149         try {
150             Content content = repository.getContent(path);
151             if (merged) {
152                 return content.render(context);
153             } else {
154                 return content.getContentString();
155             }
156         } catch (Exception JavaDoc e) {
157             if (log.isDebugEnabled()) {
158                 e.printStackTrace();
159             }
160             return "Error loading content: " + e.getMessage();
161         }
162     }
163
164     /**
165      * Get the last modified time of the content with the given path. This method may return -1 if the last modified
166      * time is not known.
167      *
168      * @param path The content path
169      * @return The last modified time
170      * @throws IOException Any IOException
171      */

172
173     public long getLastModified(String JavaDoc path) throws IOException JavaDoc {
174         return repository.getLastModified(path);
175     }
176
177 }
178
Popular Tags