KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > page > manage > XConfToPageSettingsConverter


1 /*
2  * Created on Jan 28, 2005
3  */

4 package com.openedit.page.manage;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Locale JavaDoc;
12 import java.util.Map JavaDoc;
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 /**
37  * @author cburkey
38  *
39  */

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 JavaDoc loadActions( List JavaDoc inPageActionList ) throws OpenEditException
49     {
50         if ( inPageActionList.size() == 0)
51         {
52             return Collections.EMPTY_LIST;
53         }
54         List JavaDoc pageActions = new ArrayList JavaDoc(inPageActionList.size());
55         Iterator JavaDoc 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 JavaDoc 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 JavaDoc inAlternatePath )
77     {
78         if ( inAlternatePath != null )
79         {
80             String JavaDoc 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 JavaDoc allGens = new ArrayList JavaDoc(2);
91         List JavaDoc root = inParentConfig.getChildren("generator"); //these are top level generators
92
for (Iterator JavaDoc 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 JavaDoc name = inRootconfig.getAttribute("name");
104         Generator generator = null;
105         if ( name.equals("composite"))
106         {
107             //now add any children to a list
108
List JavaDoc children = inRootconfig.getChildren("generator");
109             List JavaDoc all = new ArrayList JavaDoc(children.size());
110             for (Iterator JavaDoc 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 JavaDoc types = config.getAttribute("mimetypes");
131         if( types != null)
132         {
133             generator = new GeneratorWithMimeTypeFilter(generator,types);
134         }
135         String JavaDoc accepts = config.getAttribute("accepts");
136         if ( accepts != null)
137         {
138             generator = new GeneratorWithAcceptFilter(generator,accepts);
139         }
140         return generator;
141     }
142 /* protected Generator getGeneratorX( String inName, String inTypes, String inAccepts ) throws OpenEditException
143     {
144         String id = inName;
145         if ( inTypes != null)
146         {
147             id += inTypes;
148         }
149         if ( inAccepts != null)
150         {
151             id += inAccepts;
152         }
153         Generator gen = (Generator)getGeneratorMap().get( id );
154         if ( gen == null)
155         {
156             gen = (Generator)getModuleManager().getBean(inName);
157             
158             //Create alternative generators that filter based on a name
159             if ( gen != null)
160             {
161                 if( inTypes != null)
162                 {
163                     gen = new GeneratorWithMimeTypeFilter(gen,inTypes);
164                 }
165                 gen.setName(inName);
166             }
167             if ( gen != null)
168             {
169                 if ( inAccepts != null)
170                 {
171                     gen = new GeneratorWithAcceptFilter(gen,inAccepts);
172                 }
173                 gen.setName(inName);
174             }
175             getGeneratorMap().put(id,gen);
176             
177         }
178         return gen;
179     }
180 */

181     
182     
183     protected void loadLayout( PageSettings inPageConfig, Configuration inLayoutConfig ) throws OpenEditException
184     {
185         if ( inLayoutConfig == null )
186         {
187             return;
188         }
189     
190         String JavaDoc 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             //dont set layout to self
200
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 JavaDoc 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             //dont set layout to self
223
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 JavaDoc loadProperties( List JavaDoc inPropertyList )
244     {
245         Map JavaDoc properties = new HashMap JavaDoc(inPropertyList.size());
246         Iterator JavaDoc propertyElements = inPropertyList.iterator();
247         while (propertyElements.hasNext())
248         {
249             Configuration propertyElement = (Configuration) propertyElements.next();
250             String JavaDoc name = propertyElement.getAttribute("name");
251             PageProperty property = new PageProperty(name);
252             boolean hasvalue = false;
253             for (Iterator JavaDoc iter = propertyElement.getChildIterator("value"); iter.hasNext();)
254             {
255                 hasvalue = true;
256                 Configuration val = (Configuration) iter.next();
257                 String JavaDoc locale = val.getAttribute("locale");
258                 property.setValue(val.getValue(), locale); //TODO: Should I pass "" if its the default locale already?
259
}
260             if( !hasvalue )
261             {
262                 String JavaDoc value = propertyElement.getValue();
263                 property.setValue(value, (Locale JavaDoc)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     /**
281      * @param inPageSettings
282      * @param inUrlPath
283      */

284     public void configure(PageSettings inPageSettings, String JavaDoc inUrlPath) throws OpenEditException
285     {
286         if ( !inPageSettings.exists() )
287         {
288             loadFallBackDirectory( inPageSettings, inUrlPath );
289         // loadOverrideDirectory( inPageSettings, inUrlPath );
290
return;
291         }
292         log.debug( "Configure: " + inPageSettings.getPath() );
293         XconfConfiguration config = new XconfConfiguration( );
294
295         //Reader filtered = filterContent( inPageSettings.getReader(), inPageSettings ); //TODO: What about varialbe in same file?
296

297         //TODO: Should we subpress XML errors and just leave it blank?
298
Element root = fieldXmlUtil.getXml(inPageSettings.getReader(),inPageSettings.getPageCharacterEncoding());
299         
300         //Filter for $ in values?
301
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 // inPageSettings.getProperties().putAll( loadProperties( config.getProperties()));
313

314         String JavaDoc value = inPageSettings.getPropertyValue("encoding", null);
315         inPageSettings.setPageCharacterEncoding(value);
316         
317         //TODO: Move this to a module
318
//List pageActions = new ArrayList();
319
//pageActions.addAll( loadValues( config.getPageValues(), "PageValue." ) );
320
//pageActions.addAll( loadValues( config.getSessionValues(), "SessionValue.") );
321
List JavaDoc pagea = loadActions( config.getPageActions() );
322         //pageActions.addAll( pagea );
323

324         inPageSettings.setPageActions( pagea );
325         
326         inPageSettings.setPathActions(loadActions(config.getPathActions()));
327         //turns out we need this for login-path and other places I am sure
328
//If there are unkown tags in the xconf then set the user defined data field
329
//if ( config.hasChild("product") || config.hasChild("userdefined"))
330
//{
331
inPageSettings.setUserDefinedData(config);
332         //}
333

334     }
335
336     /**
337      * @param inPageSettings
338      * @param inUrlPath
339      */

340     protected void loadFallBackDirectory(PageSettings inPageConfig, String JavaDoc inUrlPath) throws OpenEditException
341     {
342         if ( inUrlPath.endsWith(".xconf"))
343         {
344             return; //only deal with real pages
345
}
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                         //Find the fallback xconf file and save it
356
String JavaDoc root = PathUtilities.extractDirectoryPath(fallBackDir.getPath());
357                         String JavaDoc endPart = inUrlPath.substring(root.length(),inUrlPath.length() );
358                         
359                         String JavaDoc basexconf = fallBackDir.getValue() + endPart; //end part might be a file name or _site.xconf
360
//basexconf = basexconf.replaceAll("//","/");
361
PageSettings otherxconf = getPageSettingsManager().getPageSettings(basexconf);
362                         inPageConfig.setFallBack(otherxconf);
363                             
364                         //Now set the alternative content path if found someplace else
365
ContentItem contentitem = getPageSettingsManager().getRepository().get( inUrlPath );
366                         boolean contentexists = contentitem != null && contentitem.exists();
367                         if ( contentexists )
368                         {
369                             //we will set the alternative directory anyways so we can monitor it for changes
370
inPageConfig.setAlternateContentPath(inUrlPath);
371                             
372                         }
373                         else
374                         {
375                             if ( lookforFallbackContent(fallBackDir, endPart,inPageConfig) ) //look in fallback 1
376
{
377                                 return;
378                             }
379                             else if ( lookforFallbackContent(otherxconf.getProperty("fallbackdirectory"), endPart,otherxconf) ) //look in fallback fallback
380
{
381                                 return;
382                             }
383                         }
384                     }
385                     catch ( Exception JavaDoc ex )
386                     {
387                         log.error(ex);
388                         throw new OpenEditException(ex);
389                     }
390             }
391     }
392
393     /**
394      * @return
395      */

396     private boolean lookforFallbackContent(PageProperty fallBackDir, String JavaDoc inUrlEndPart, PageSettings inSettings) throws Exception JavaDoc{
397         
398         if ( fallBackDir != null && fallBackDir.getValue() != null && !fallBackDir.getValue().equals("NO_FALLBACK"))
399         {
400             String JavaDoc rootDir = fallBackDir.getValue();
401             String JavaDoc alternativecontentfile2 = rootDir + inUrlEndPart;
402             //alternativecontentfile2 = alternativecontentfile2.replaceAll("//","/");
403

404             ContentItem contentitem = getPageSettingsManager().getRepository().get( alternativecontentfile2);
405             //Do the final setting of the alternative content path since we found a valid fallback
406
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