1 48 49 package org.jpublish.repository; 50 51 import java.io.IOException ; 52 import java.io.InputStream ; 53 import java.io.StringWriter ; 54 import java.util.HashMap ; 55 import java.util.List ; 56 import java.util.Locale ; 57 import java.util.Map ; 58 59 import com.anthonyeden.lib.util.IOUtilities; 60 import org.apache.commons.logging.Log; 61 import org.apache.commons.logging.LogFactory; 62 import org.jpublish.RequestContext; 63 import org.jpublish.SiteContext; 64 import org.jpublish.page.Page; 65 import org.jpublish.util.PathUtilities; 66 import org.jpublish.util.Property; 67 import org.jpublish.view.ViewRenderException; 68 import org.jpublish.view.ViewRenderer; 69 70 81 82 public class Content { 83 84 private Log log = LogFactory.getLog(Content.class); 85 86 private ContentInstance contentInstance = null; 87 88 private Map properties = new HashMap (); 89 private String viewRendererName = null; 90 private Locale locale = Locale.getDefault(); 91 92 97 98 public Content(ContentInstance contentInstance) { 99 this.contentInstance = contentInstance; 100 } 101 102 107 108 public ContentInstance getContentInstance() { 109 return contentInstance; 110 } 111 112 117 118 public String getPath() { 119 return contentInstance.getPath(); 120 } 121 122 127 128 public String getViewRendererName() { 129 if (viewRendererName == null) { 130 viewRendererName = contentInstance.getViewRendererName(); 131 } 132 return viewRendererName; 133 } 134 135 140 141 public void setViewRendererName(String viewRendererName) { 142 this.viewRendererName = viewRendererName; 143 } 144 145 151 152 public List getContentActions() { 153 return contentInstance.getContentActions(); 154 } 155 156 162 163 public String getProperty(String name) { 164 return getProperty(name, getLocale()); 165 } 166 167 177 178 public String getProperty(String name, Locale locale) { 179 Property property = (Property) properties.get(name); 180 if (property == null) { 181 String value = contentInstance.getProperty(name, locale); 182 if (value != null) { 183 setProperty(name, value, locale); 184 return value; 185 } 186 } 187 188 if (property == null) { 189 return null; 190 } else { 191 return property.getValue(locale); 192 } 193 } 194 195 202 203 public String get(String name) { 204 return getProperty(name); 205 } 206 207 213 214 public void setProperty(String name, String value) { 215 setProperty(name, value, getLocale()); 216 } 217 218 225 226 public void setProperty(String name, String value, Locale locale) { 227 Property property = (Property) properties.get(name); 228 if (property == null) { 229 property = new Property(name); 231 properties.put(name, property); 232 } 233 property.setValue(value, locale); 234 } 235 236 241 242 public Locale getLocale() { 243 return locale; 244 } 245 246 252 253 public void setLocale(Locale locale) { 254 if (locale == null) { 255 locale = Locale.getDefault(); 256 } 257 this.locale = locale; 258 } 259 260 263 264 public void executeActions() { 265 log.debug("Executing content actions"); 266 contentInstance.executeActions(); 267 } 268 269 275 276 public String getContentString() throws IOException { 277 if (log.isDebugEnabled()) { 278 log.debug("getContentString() [path=" + 279 contentInstance.getPath()); 280 } 281 executeActions(); 282 return contentInstance.getContentString(); 283 } 284 285 291 292 public InputStream getContentInputStream() throws IOException { 293 if (log.isDebugEnabled()) { 294 log.debug("getContentInputStream() [path=" + 295 contentInstance.getPath()); 296 } 297 executeActions(); 298 return contentInstance.getContentInputStream(); 299 } 300 301 310 311 public String render(RequestContext context) throws IOException , 312 ViewRenderException { 313 if (log.isDebugEnabled()) { 314 log.debug("render() [path=" + contentInstance.getPath()); 315 } 316 317 String path = getPath(); 318 319 if (log.isDebugEnabled()) { 320 log.debug("Rendering content: " + path); 321 } 322 323 StringWriter writer = null; 324 try { 325 writer = new StringWriter (); 326 327 Repository repository = contentInstance.getRepository(); 328 String name = PathUtilities.makeRepositoryURI(repository.getName(), path); 329 getViewRenderer(context).render(context, name, writer); 330 331 return writer.toString(); 332 } finally { 333 IOUtilities.close(writer); 334 } 335 } 336 337 342 343 public String toString() { 344 try { 345 return getContentString(); 346 } catch (IOException e) { 347 log.error("IO exception: " + e.getMessage()); 348 if (log.isDebugEnabled()) { 349 e.printStackTrace(); 350 } 351 return "IOException: " + e.getMessage(); 352 } 353 } 354 355 363 364 protected ViewRenderer getViewRenderer(RequestContext context) { 365 Page page = context.getPage(); 366 SiteContext siteContext = contentInstance.getSiteContext(); 367 ViewRenderer renderer = siteContext.getViewRenderer(); 368 String viewRendererName = getViewRendererName(); 369 if (viewRendererName != null) { 370 if (log.isDebugEnabled()) { 371 log.debug("View renderer name: " + viewRendererName); 372 } 373 renderer = siteContext.getViewRenderer(viewRendererName); 374 } else if (page != null) { 375 log.debug("Page is not null...checking view renderer name"); 376 viewRendererName = page.getViewRendererName(); 377 if (log.isDebugEnabled()) { 378 log.debug("View renderer name: " + viewRendererName); 379 } 380 if (viewRendererName != null) { 381 renderer = siteContext.getViewRenderer(viewRendererName); 382 } 383 } 384 return renderer; 385 } 386 387 } 388 | Popular Tags |