1 4 package com.openedit.page.manage; 5 6 import java.util.ArrayList ; 7 import java.util.Collections ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Locale ; 12 import java.util.Map ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.dom4j.Element; 17 import org.openedit.repository.ContentItem; 18 19 import com.openedit.Generator; 20 import com.openedit.OpenEditException; 21 import com.openedit.config.Configuration; 22 import com.openedit.generators.CompositeGenerator; 23 import com.openedit.generators.GeneratorWithAcceptFilter; 24 import com.openedit.generators.GeneratorWithMimeTypeFilter; 25 import com.openedit.page.Page; 26 import com.openedit.page.PageAction; 27 import com.openedit.page.PageProperty; 28 import com.openedit.page.PageSettings; 29 import com.openedit.page.XconfConfiguration; 30 import com.openedit.util.OutputFiller; 31 import com.openedit.util.PathUtilities; 32 import com.openedit.util.XmlUtil; 33 import com.openedit.util.strainer.Filter; 34 import com.openedit.util.strainer.FilterReader; 35 36 40 public class XConfToPageSettingsConverter 41 { 42 private static final Log log = LogFactory.getLog(XConfToPageSettingsConverter.class); 43 protected PageSettingsManager fieldPageSettingsManager; 44 protected FilterReader reader = new FilterReader(); 45 46 protected XmlUtil fieldXmlUtil = new XmlUtil(); 47 protected OutputFiller fieldOutputFiller = new OutputFiller(); 48 protected List loadActions( List inPageActionList ) throws OpenEditException 49 { 50 if ( inPageActionList.size() == 0) 51 { 52 return Collections.EMPTY_LIST; 53 } 54 List pageActions = new ArrayList (inPageActionList.size()); 55 Iterator pageActionElements = inPageActionList.iterator(); 56 while (pageActionElements.hasNext()) 57 { 58 Configuration pageActionElement = (Configuration) pageActionElements.next(); 59 PageAction currentPageAction = createAction( pageActionElement ); 60 61 pageActions.add( currentPageAction ); 62 } 63 return pageActions; 64 } 65 66 private PageAction createAction(Configuration inPageActionElement) 67 { 68 String actionName = inPageActionElement.getAttribute("name"); 69 PageAction currentPageAction = new PageAction( actionName ); 70 71 currentPageAction.setConfig( inPageActionElement ); 72 currentPageAction.setIncludesAll(Boolean.parseBoolean( inPageActionElement.getAttribute("alltypes") ) ); 73 return currentPageAction; 74 } 75 76 protected void loadAlternateContentFile( PageSettings inPageConfig, String inAlternatePath ) 77 { 78 if ( inAlternatePath != null ) 79 { 80 String path = PathUtilities.resolveRelativePath(inAlternatePath,inPageConfig.getPath()); 81 inPageConfig.setAlternateContentPath( path ); 82 } 83 } 84 protected void loadGenerators( PageSettings inPageConfig, Configuration inParentConfig) throws OpenEditException 85 { 86 if ( inParentConfig == null ) 87 { 88 return; 89 } 90 List allGens = new ArrayList (2); 91 List root = inParentConfig.getChildren("generator"); for (Iterator iter = root.iterator(); iter.hasNext();) 93 { 94 Configuration rootconfig = (Configuration) iter.next(); 95 Generator generator = createGenerator(rootconfig); 96 allGens.add(generator); 97 } 98 inPageConfig.setGenerators(allGens); 99 } 100 101 protected Generator createGenerator(Configuration inRootconfig) throws OpenEditException 102 { 103 String name = inRootconfig.getAttribute("name"); 104 Generator generator = null; 105 if ( name.equals("composite")) 106 { 107 List children = inRootconfig.getChildren("generator"); 109 List all = new ArrayList (children.size()); 110 for (Iterator iter = children.iterator(); iter.hasNext();) 111 { 112 Configuration config = (Configuration) iter.next(); 113 Generator child = createGenerator(config); 114 all.add(child); 115 } 116 CompositeGenerator composite = new CompositeGenerator(); 117 composite.setGenerators(all); 118 generator = composite; 119 } 120 else 121 { 122 generator = getPageSettingsManager().getGenerator( name ); 123 } 124 generator = addFilter(inRootconfig,generator); 125 return generator; 126 } 127 128 protected Generator addFilter(Configuration config, Generator generator) 129 { 130 String types = config.getAttribute("mimetypes"); 131 if( types != null) 132 { 133 generator = new GeneratorWithMimeTypeFilter(generator,types); 134 } 135 String accepts = config.getAttribute("accepts"); 136 if ( accepts != null) 137 { 138 generator = new GeneratorWithAcceptFilter(generator,accepts); 139 } 140 return generator; 141 } 142 181 182 183 protected void loadLayout( PageSettings inPageConfig, Configuration inLayoutConfig ) throws OpenEditException 184 { 185 if ( inLayoutConfig == null ) 186 { 187 return; 188 } 189 190 String layoutPath = inLayoutConfig.getValue(); 191 if ( layoutPath == null ) 192 { 193 inPageConfig.setLayout(Page.BLANK_LAYOUT); 194 return; 195 } 196 layoutPath = PathUtilities.resolveRelativePath( layoutPath, inPageConfig.getPath() ); 197 if ( layoutPath.equals(inPageConfig.getPath())) 198 { 199 inPageConfig.setLayout(null); 201 return; 202 } 203 inPageConfig.setLayout( layoutPath ); 204 } 205 206 protected void loadInnerLayout( PageSettings inPageConfig, Configuration inInnerLayoutConfig ) throws OpenEditException 207 { 208 if ( inInnerLayoutConfig == null ) 209 { 210 return; 211 } 212 213 String innerLayoutPath = inInnerLayoutConfig.getValue(); 214 if ( innerLayoutPath == null ) 215 { 216 inPageConfig.setInnerLayout(Page.BLANK_LAYOUT); 217 return; 218 } 219 innerLayoutPath = PathUtilities.resolveRelativePath( innerLayoutPath, inPageConfig.getPath() ); 220 if ( innerLayoutPath.equals(inPageConfig.getPath())) 221 { 222 inPageConfig.setInnerLayout(Page.BLANK_LAYOUT); 224 return; 225 } 226 inPageConfig.setInnerLayout( innerLayoutPath ); 227 } 228 protected void loadPermissionFilters( PageSettings inPageConfig, XconfConfiguration inConfig ) throws OpenEditException 229 { 230 Filter viewf = reader.readFilterCollection( inConfig.getViewRequirements() ); 231 inPageConfig.setViewFilter(viewf ); 232 Configuration config = inConfig.getEditRequirements(); 233 if (config == null) 234 { 235 inPageConfig.setEditFilter(getPageSettingsManager().getDefaultEditPageFilter()); 236 } 237 else 238 { 239 inPageConfig.setEditFilter(reader.readFilterCollection(config)); 240 } 241 } 242 243 protected Map loadProperties( List inPropertyList ) 244 { 245 Map properties = new HashMap (inPropertyList.size()); 246 Iterator propertyElements = inPropertyList.iterator(); 247 while (propertyElements.hasNext()) 248 { 249 Configuration propertyElement = (Configuration) propertyElements.next(); 250 String name = propertyElement.getAttribute("name"); 251 PageProperty property = new PageProperty(name); 252 boolean hasvalue = false; 253 for (Iterator iter = propertyElement.getChildIterator("value"); iter.hasNext();) 254 { 255 hasvalue = true; 256 Configuration val = (Configuration) iter.next(); 257 String locale = val.getAttribute("locale"); 258 property.setValue(val.getValue(), locale); } 260 if( !hasvalue ) 261 { 262 String value = propertyElement.getValue(); 263 property.setValue(value, (Locale )null); 264 } 265 266 properties.put(name, property); 267 } 268 return properties; 269 } 270 271 public PageSettingsManager getPageSettingsManager() 272 { 273 return fieldPageSettingsManager; 274 } 275 public void setPageSettingsManager(PageSettingsManager inPageSettingsManager) 276 { 277 fieldPageSettingsManager = inPageSettingsManager; 278 } 279 280 284 public void configure(PageSettings inPageSettings, String inUrlPath) throws OpenEditException 285 { 286 if ( !inPageSettings.exists() ) 287 { 288 loadFallBackDirectory( inPageSettings, inUrlPath ); 289 return; 291 } 292 log.debug( "Configure: " + inPageSettings.getPath() ); 293 XconfConfiguration config = new XconfConfiguration( ); 294 295 297 Element root = fieldXmlUtil.getXml(inPageSettings.getReader(),inPageSettings.getPageCharacterEncoding()); 299 300 config.populate(root); 302 inPageSettings.getProperties().putAll( loadProperties( config.getProperties())); 303 304 loadAlternateContentFile( inPageSettings, config.getContentFile() ); 305 loadFallBackDirectory( inPageSettings, inUrlPath ); 306 307 loadPermissionFilters( inPageSettings, config ); 308 309 loadGenerators(inPageSettings, config); 310 loadLayout( inPageSettings, config.getLayout() ); 311 loadInnerLayout( inPageSettings, config.getInnerLayout() ); 312 314 String value = inPageSettings.getPropertyValue("encoding", null); 315 inPageSettings.setPageCharacterEncoding(value); 316 317 List pagea = loadActions( config.getPageActions() ); 322 324 inPageSettings.setPageActions( pagea ); 325 326 inPageSettings.setPathActions(loadActions(config.getPathActions())); 327 inPageSettings.setUserDefinedData(config); 332 334 } 335 336 340 protected void loadFallBackDirectory(PageSettings inPageConfig, String inUrlPath) throws OpenEditException 341 { 342 if ( inUrlPath.endsWith(".xconf")) 343 { 344 return; } 346 347 PageProperty fallBackDir = inPageConfig.getProperty("fallbackdirectory"); 348 if ( fallBackDir != null && fallBackDir.getValue() != null && !fallBackDir.getValue().equals("NO_FALLBACK") ) 349 { 350 if ( fallBackDir.getValue().endsWith("/")){ 351 throw new OpenEditException("Fall back setting must not end in /"); 352 } 353 try 354 { 355 String root = PathUtilities.extractDirectoryPath(fallBackDir.getPath()); 357 String endPart = inUrlPath.substring(root.length(),inUrlPath.length() ); 358 359 String basexconf = fallBackDir.getValue() + endPart; PageSettings otherxconf = getPageSettingsManager().getPageSettings(basexconf); 362 inPageConfig.setFallBack(otherxconf); 363 364 ContentItem contentitem = getPageSettingsManager().getRepository().get( inUrlPath ); 366 boolean contentexists = contentitem != null && contentitem.exists(); 367 if ( contentexists ) 368 { 369 inPageConfig.setAlternateContentPath(inUrlPath); 371 372 } 373 else 374 { 375 if ( lookforFallbackContent(fallBackDir, endPart,inPageConfig) ) { 377 return; 378 } 379 else if ( lookforFallbackContent(otherxconf.getProperty("fallbackdirectory"), endPart,otherxconf) ) { 381 return; 382 } 383 } 384 } 385 catch ( Exception ex ) 386 { 387 log.error(ex); 388 throw new OpenEditException(ex); 389 } 390 } 391 } 392 393 396 private boolean lookforFallbackContent(PageProperty fallBackDir, String inUrlEndPart, PageSettings inSettings) throws Exception { 397 398 if ( fallBackDir != null && fallBackDir.getValue() != null && !fallBackDir.getValue().equals("NO_FALLBACK")) 399 { 400 String rootDir = fallBackDir.getValue(); 401 String alternativecontentfile2 = rootDir + inUrlEndPart; 402 404 ContentItem contentitem = getPageSettingsManager().getRepository().get( alternativecontentfile2); 405 boolean exists = contentitem != null && contentitem.exists(); 407 if ( exists ) 408 { 409 log.debug("alternative:" + alternativecontentfile2); 410 inSettings.setAlternateContentPath(alternativecontentfile2); 411 return true; 412 } 413 } 414 return false; 415 } 416 } 417 | Popular Tags |