1 package org.apache.velocity.site.doxia.velocity;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Iterator;
27
28 import org.apache.commons.collections.iterators.ArrayIterator;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.velocity.exception.ResourceNotFoundException;
31 import org.codehaus.plexus.logging.LogEnabled;
32 import org.codehaus.plexus.logging.Logger;
33
34 /**
35 * This {@link org.apache.velocity.runtime.resource.loader.ResourceLoader} is
36 * responsible for loading the configured macro libraries. It can load only the
37 * defined macro libraries (no inclusion or parsing is possible for security
38 * reasons) from the <code>${siteDirectory}/velocity/resources</code>
39 * directory.
40 *
41 * This is a plexus-managed component which gets configured by the {@link
42 * org.apache.velocity.site.doxia.plugin.DoxiaVelocityRendererPlugin} during the
43 * post-site phase.
44 *
45 * @plexus.component role="org.apache.velocity.site.doxia.velocity.DoxiaLibraryLoader"
46 *
47 * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
48 * @version $Revision: 526751 $
49 */
50 public class DefaultDoxiaLibraryLoader extends AbstractDoxiaResourceLoader implements DoxiaLibraryLoader, LogEnabled
51 {
52
53 /** List of libraries known to this loader. */
54 private String[] knownLibraries = null;
55
56 /** File object representing the site source directory. */
57 private File siteDirectory = null;
58
59 /** Plexus Logger object */
60 private Logger logger = null;
61
62 /**
63 * Creates a new DefaultDoxiaLibraryLoader object.
64 */
65 public DefaultDoxiaLibraryLoader()
66 {
67 }
68
69 /**
70 * @see LogEnabled#enableLogging(Logger)
71 */
72 public void enableLogging(final Logger logger)
73 {
74 this.logger = logger;
75 }
76
77 /**
78 * Sets the list of known library. This is called from the
79 * {@link org.apache.velocity.site.doxia.plugin.DoxiaVelocityRendererPlugin}.
80 *
81 * @param knownLibraries
82 * An Array of Strings with the names of libraries to load. Can
83 * be null.
84 */
85 public void setKnownLibraries(final String[] knownLibraries)
86 {
87 this.knownLibraries = knownLibraries;
88
89 if (logger.isDebugEnabled())
90 {
91 logger.debug("Known Libraries: " + knownLibraries);
92 }
93 }
94
95 /**
96 * Returns the list of known macro libraries.
97 *
98 * @return An array of strings with the names of the known libraries or null
99 * if none are defined.
100 */
101 public String[] getKnownLibraries()
102 {
103 return this.knownLibraries;
104 }
105
106 /**
107 * Set the base directory for the maven site generation.
108 *
109 * @param siteDirectory
110 * A file object representing the site source directory.
111 */
112 public void setSiteDirectory(File siteDirectory)
113 {
114 this.siteDirectory = siteDirectory;
115
116 if (logger.isDebugEnabled())
117 {
118 logger.debug("Site Directory: " + siteDirectory);
119 }
120 }
121
122 /**
123 * Returns an input stream for the given template name. This resource loader
124 * can only load the macro libraries configured with the
125 * {@link #setKnownLibraries(String[])} method and they must be located in
126 * the <code>velocity/resources</code> sub-directory of the maven site
127 * source directory.
128 *
129 * @param templateName
130 * The name of the template to load.
131 *
132 * @return An {@link InputStream} object for the given template name or null
133 * when the template is now available through this resource loader.
134 *
135 * @throws ResourceNotFoundException
136 * When the resource loader should have been able to supply this
137 * resource but encountered an error.
138 */
139 public InputStream getResourceStream(final String templateName) throws ResourceNotFoundException
140 {
141
142 if ((knownLibraries == null) || (siteDirectory == null) || StringUtils.isEmpty(templateName))
143 {
144 return null;
145 }
146
147 for (Iterator it = new ArrayIterator(knownLibraries); it.hasNext();)
148 {
149 String library = (String) it.next();
150
151 if (StringUtils.equals(library, templateName))
152 {
153 File macroFile = new File(siteDirectory, "/velocity/resources/" + templateName);
154
155 if (logger.isDebugEnabled())
156 {
157 logger.debug("Macro File is " + macroFile + " (exists: " + macroFile.exists() + ")");
158 }
159
160 if (macroFile.exists() && macroFile.isFile())
161 {
162 try
163 {
164 return new FileInputStream(macroFile);
165 }
166 catch (IOException ioe)
167 {
168 throw new ResourceNotFoundException("While opening " + macroFile + ": ", ioe);
169 }
170 }
171 else
172 {
173 if (logger.isWarnEnabled())
174 {
175 throw new ResourceNotFoundException(macroFile + " could not be loaded!");
176 }
177 }
178
179 return null;
180 }
181 }
182
183 return null;
184 }
185 }