1 44 package org.jpublish.repository; 45 46 import java.io.IOException ; 47 import java.io.InputStream ; 48 import java.io.InputStreamReader ; 49 import java.util.*; 50 51 import com.anthonyeden.lib.config.Configuration; 52 import com.anthonyeden.lib.config.ConfigurationException; 53 import com.anthonyeden.lib.config.sax.SAXConfigurationFactory; 54 import com.anthonyeden.lib.util.IOUtilities; 55 import com.anthonyeden.lib.util.MessageUtilities; 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 import org.apache.commons.vfs.FileContent; 59 import org.apache.commons.vfs.FileName; 60 import org.apache.commons.vfs.FileObject; 61 import org.apache.commons.vfs.FileSystemException; 62 import org.jpublish.JPublishEngine; 63 import org.jpublish.RequestContext; 64 import org.jpublish.SiteContext; 65 import org.jpublish.action.Action; 66 import org.jpublish.action.ActionWrapper; 67 import org.jpublish.util.Property; 68 69 80 81 public class ContentInstance { 82 83 private Log log = LogFactory.getLog(ContentInstance.class); 84 85 private SiteContext siteContext = null; 86 private List contentActions = new ArrayList(); 87 private FileObject file = null; 88 private FileName fileName = null; 89 private FileContent fileContent = null; 90 private String path = null; 91 private String viewRendererName = null; 92 private Map properties = new HashMap(); 93 private Repository repository = null; 94 95 private long timeLastLoaded = -1; 96 private String contentString = null; 97 98 107 108 public ContentInstance(SiteContext siteContext, FileObject file, 109 String path, Repository repository) throws FileSystemException { 110 this.siteContext = siteContext; 111 this.file = file; 112 this.fileName = file.getName(); 113 this.fileContent = file.getContent(); 114 this.path = path; 115 this.repository = repository; 116 } 117 118 123 124 public SiteContext getSiteContext() { 125 return siteContext; 126 } 127 128 133 134 public Repository getRepository() { 135 return repository; 136 } 137 138 143 144 public FileObject getFile() { 145 return file; 146 } 147 148 153 154 public String getPath() { 155 return path; 156 } 157 158 163 164 public String getViewRendererName() { 165 return viewRendererName; 166 } 167 168 173 174 public void setViewRendererName(String viewRendererName) { 175 this.viewRendererName = viewRendererName; 176 } 177 178 184 185 public List getContentActions() { 186 return contentActions; 187 } 188 189 195 196 public String getProperty(String name) { 197 return getProperty(name, Locale.getDefault()); 198 } 199 200 210 211 public String getProperty(String name, Locale locale) { 212 if (log.isDebugEnabled()) { 213 log.debug("Get property [name=" + name + ",locale=" + locale + "]"); 214 } 215 Property property = (Property) properties.get(name); 216 if (property != null) { 217 return property.getValue(locale); 218 } else { 219 return null; 220 } 221 } 222 223 230 231 public String get(String name) { 232 if (log.isDebugEnabled()) { 233 log.debug("get(" + name + ") called to retrieve property"); 234 } 235 return getProperty(name); 236 } 237 238 241 242 public void executeActions() { 243 RequestContext context = RequestContext.getRequestContext(); 244 Iterator contentActions = getContentActions().iterator(); 245 while (contentActions.hasNext()) { 246 ((ActionWrapper) contentActions.next()).execute(context); 247 } 248 } 249 250 256 257 public synchronized String getContentString() throws IOException { 258 log.debug("getContentString()"); 259 boolean reloadContent = false; 261 long contentLastModified = fileContent.getLastModifiedTime(); 262 if (contentLastModified > timeLastLoaded) { 263 if (log.isDebugEnabled()) { 264 log.debug("Loading updated or new content: " + fileName); 265 } 266 reloadContent = true; 267 } 268 269 if (reloadContent || contentString == null) { 271 timeLastLoaded = System.currentTimeMillis(); 272 contentString = IOUtilities.getStringFromReader(new InputStreamReader (fileContent.getInputStream())); 273 } 274 275 return contentString; 276 } 277 278 284 285 public InputStream getContentInputStream() throws IOException { 286 log.debug("getContentInputStream()"); 287 return fileContent.getInputStream(); 288 } 289 290 296 297 public synchronized void loadConfiguration(InputStream in) throws ConfigurationException { 298 log.debug("Loading content configuration."); 299 Configuration configuration = 300 SAXConfigurationFactory.getInstance().getConfiguration(fileName.getPath(), in); 301 loadConfiguration(configuration); 302 } 303 304 310 311 public synchronized void loadConfiguration(Configuration configuration) 312 throws ConfigurationException { 313 contentActions.clear(); 314 setViewRendererName(configuration.getChildValue("view-renderer")); 315 316 log.debug("Looping through content-action elements."); 318 Iterator contentActionElements = 319 configuration.getChildren("content-action").iterator(); 320 while (contentActionElements.hasNext()) { 321 Configuration contentActionElement = 322 (Configuration) contentActionElements.next(); 323 String name = contentActionElement.getAttribute("name"); 324 325 if (name == null) { 326 Object [] args = {}; 327 String msg = MessageUtilities.getMessage(getClass(), 328 JPublishEngine.MESSAGE_PACKAGE, "actionNameRequired", 329 args); 330 throw new ConfigurationException(msg, contentActionElement); 331 } 332 333 Action action = siteContext.getActionManager().findAction(name); 334 335 contentActions.add(new ActionWrapper(action, contentActionElement)); 336 } 337 338 log.debug("Loading content properties"); 340 Iterator propertyElements = 341 configuration.getChildren("property").iterator(); 342 while (propertyElements.hasNext()) { 343 Configuration propertyElement = 344 (Configuration) propertyElements.next(); 345 setProperty(propertyElement.getAttribute("name"), 346 propertyElement.getValue(), 347 propertyElement.getAttribute("locale")); 348 } 349 } 350 351 358 359 private void setProperty(String name, String value, String locale) { 360 if (log.isDebugEnabled()) { 361 log.debug("setProperty() [name=" + name + ",value=" + value + 362 ",locale=" + locale); 363 } 364 Property property = (Property) properties.get(name); 365 if (property == null) { 366 property = new Property(name); 368 properties.put(name, property); 369 } 370 property.setValue(value, locale); 371 } 372 373 } 374 375 | Popular Tags |