1 48 49 package org.jpublish.page; 50 51 import java.io.InputStream ; 52 import java.io.InputStreamReader ; 53 import java.io.UnsupportedEncodingException ; 54 import java.util.*; 55 56 import com.anthonyeden.lib.config.Configuration; 57 import com.anthonyeden.lib.config.ConfigurationException; 58 import com.anthonyeden.lib.config.sax.SAXConfigurationFactory; 59 import com.anthonyeden.lib.util.MessageUtilities; 60 import org.apache.commons.logging.Log; 61 import org.apache.commons.logging.LogFactory; 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 import org.jpublish.util.encoding.CharacterEncodingManager; 69 70 82 83 public class PageInstance { 84 85 private Log log = LogFactory.getLog(PageInstance.class); 86 87 private SiteContext siteContext = null; 88 private List pageActions = new ArrayList(); 89 private String path = null; 90 private String pageName = null; 91 private String pageType = null; 92 private String pageParent = null; 93 private String templateName = null; 94 private String viewRendererName = null; 95 private String encoding = null; 96 private Map properties = new HashMap(); 97 98 108 109 public PageInstance(SiteContext siteContext, String path, String pageName, 110 String pageType, String pageParent) { 111 this.siteContext = siteContext; 112 this.path = path; 113 log.debug("Page path: " + path); 114 this.pageName = pageName; 115 this.pageType = pageType; 116 this.pageParent = pageParent; 117 log.debug("Page parent: " + pageParent); 118 119 this.templateName = siteContext.getDefaultTemplate(); 120 121 CharacterEncodingManager encManager = 122 siteContext.getCharacterEncodingManager(); 123 this.encoding = encManager.getMap(path).getPageEncoding(); 124 } 125 126 131 132 public String getPath() { 133 return path; 134 } 135 136 141 142 public String getPageName() { 143 return pageName; 144 } 145 146 151 152 public String getPageType() { 153 return pageType; 154 } 155 156 161 162 public String getPageParent() { 163 return pageParent; 164 } 165 166 171 172 public String getFullTemplateName() { 173 return templateName + "." + pageType; 174 } 175 176 182 183 public String getTemplateName() { 184 return templateName; 185 } 186 187 193 194 public synchronized void setTemplateName(String templateName) { 195 if (log.isDebugEnabled()) { 196 log.debug("setTemplateName(" + templateName + ")"); 197 } 198 if (templateName == null) { 199 templateName = siteContext.getDefaultTemplate(); 200 if (log.isDebugEnabled()) { 201 log.debug("Using default template: " + templateName); 202 } 203 } 204 this.templateName = templateName; 205 } 206 207 212 213 public String getViewRendererName() { 214 return viewRendererName; 215 } 216 217 222 223 public void setViewRendererName(String viewRendererName) { 224 this.viewRendererName = viewRendererName; 225 } 226 227 233 234 public List getPageActions() { 235 return pageActions; 236 } 237 238 244 245 public String getProperty(String name) { 246 return getProperty(name, Locale.getDefault()); 247 } 248 249 259 260 public String getProperty(String name, Locale locale) { 261 if (log.isDebugEnabled()) { 262 log.debug("Get property [name=" + name + ",locale=" + locale + "]"); 263 } 264 Property property = (Property) properties.get(name); 265 if (property != null) { 266 return property.getValue(locale); 267 } else { 268 return null; 269 } 270 } 271 272 279 280 public String get(String name) { 281 if (log.isDebugEnabled()) { 282 log.debug("get(" + name + ") called to retrieve property"); 283 } 284 return getProperty(name); 285 } 286 287 293 294 public String getEncoding() { 295 return encoding; 296 } 297 298 303 304 public void setEncoding(String encoding) { 305 this.encoding = encoding; 306 } 307 308 313 314 public void executeActions(RequestContext context) { 315 Iterator pageActions = getPageActions().iterator(); 316 while (pageActions.hasNext()) { 317 ((ActionWrapper) pageActions.next()).execute(context); 318 if (context.getStopProcessing() != null) return; 319 } 320 } 321 322 328 329 public synchronized void load(InputStream in) 330 throws ConfigurationException { 331 try { 332 log.debug("Loading page with encoding " + getEncoding()); 333 Configuration configuration = 334 SAXConfigurationFactory.getInstance().getConfiguration(path, 335 new InputStreamReader (in, getEncoding())); 336 loadConfiguration(configuration); 337 } catch (UnsupportedEncodingException e) { 338 throw new ConfigurationException("Error loading page configuration", e); 339 } 340 } 341 342 348 349 public synchronized void loadConfiguration(Configuration configuration) 350 throws ConfigurationException { 351 pageActions.clear(); 352 353 setTemplateName(configuration.getChildValue("template")); 354 setViewRendererName(configuration.getChildValue("view-renderer")); 355 356 log.debug("Looping through page-action elements."); 358 Iterator pageActionElements = 359 configuration.getChildren("page-action").iterator(); 360 while (pageActionElements.hasNext()) { 361 Configuration pageActionElement = 362 (Configuration) pageActionElements.next(); 363 String name = pageActionElement.getAttribute("name"); 364 365 if (name == null) { 366 Object [] args = {}; 367 String msg = MessageUtilities.getMessage(getClass(), 368 JPublishEngine.MESSAGE_PACKAGE, "actionNameRequired", 369 args); 370 throw new ConfigurationException(msg, pageActionElement); 371 } 372 373 Action action = siteContext.getActionManager().findAction(name); 374 375 pageActions.add(new ActionWrapper(action, pageActionElement)); 376 } 377 378 log.debug("Loading page properties"); 380 Iterator propertyElements = 381 configuration.getChildren("property").iterator(); 382 while (propertyElements.hasNext()) { 383 Configuration propertyElement = 384 (Configuration) propertyElements.next(); 385 setProperty(propertyElement.getAttribute("name"), 386 propertyElement.getValue(), 387 propertyElement.getAttribute("locale")); 388 } 389 } 390 391 398 399 private void setProperty(String name, String value, String locale) { 400 if (log.isDebugEnabled()) { 401 log.debug("setProperty() [name=" + name + ",value=" + value + 402 ",locale=" + locale); 403 } 404 Property property = (Property) properties.get(name); 405 if (property == null) { 406 property = new Property(name); 408 properties.put(name, property); 409 } 410 property.setValue(value, locale); 411 } 412 413 } 414 | Popular Tags |