KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > tools > copletManagement > generation > XMLProfileGenerator


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.tools.copletManagement.generation;
17
18 import java.beans.PropertyDescriptor JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.cocoon.ProcessingException;
28 import org.apache.cocoon.components.flow.FlowHelper;
29 import org.apache.cocoon.components.persistence.CastorSourceConverter;
30 import org.apache.cocoon.components.persistence.ConverterException;
31 import org.apache.cocoon.generation.ServiceableGenerator;
32 import org.apache.commons.jxpath.DynamicPropertyHandler;
33 import org.apache.commons.jxpath.JXPathBeanInfo;
34 import org.apache.commons.jxpath.JXPathIntrospector;
35 import org.apache.excalibur.xml.sax.SAXParser;
36 import org.xml.sax.InputSource JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38
39
40 /**
41  *
42  * @version CVS $Id: XMLProfileGenerator.java 151725 2005-02-07 15:52:19Z cziegeler $
43  */

44 public class XMLProfileGenerator
45 extends ServiceableGenerator {
46
47     /* (non-Javadoc)
48      * @see org.apache.cocoon.generation.Generator#generate()
49      */

50     public void generate()
51     throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
52         
53         SAXParser parser = null;
54         CastorSourceConverter converter = null;
55         final Object JavaDoc context = FlowHelper.getContextObject(this.objectModel);
56         Map JavaDoc myMap = new HashMap JavaDoc();
57         if (context instanceof Map JavaDoc) {
58             myMap = (Map JavaDoc) context;
59         } else {
60             fillContext(context, myMap);
61         }
62         try {
63             Object JavaDoc layout = myMap.get("layout");
64             converter = (CastorSourceConverter) this.manager.lookup(org.apache.cocoon.components.persistence.CastorSourceConverter.ROLE);
65             ByteArrayOutputStream JavaDoc os = new java.io.ByteArrayOutputStream JavaDoc();
66             HashMap JavaDoc para = new HashMap JavaDoc();
67             para.put("profiletype", "layout");
68             converter.storeObject(os, para, layout);
69             String JavaDoc xml = new String JavaDoc();
70             xml = os.toString();
71             final InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(xml));
72             parser = (SAXParser)this.manager.lookup(SAXParser.ROLE);
73             parser.parse(inputSource, super.xmlConsumer);
74         } catch (ServiceException e) {
75             throw new ProcessingException(e);
76         } catch (ConverterException e) {
77             throw new ProcessingException(e);
78         } finally {
79             this.manager.release(parser);
80             this.manager.release(converter);
81         }
82     }
83     
84     // FIXME: Copy from JXTemplateGenerator
85
private void fillContext(Object JavaDoc contextObject, Map JavaDoc map) {
86         if (contextObject != null) {
87             // Hack: I use jxpath to populate the context object's properties
88
// in the jexl context
89
final JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(contextObject.getClass());
90             if (bi.isDynamic()) {
91                 Class JavaDoc cl = bi.getDynamicPropertyHandlerClass();
92                 try {
93                     DynamicPropertyHandler h = (DynamicPropertyHandler)cl.newInstance();
94                     String JavaDoc[] result = h.getPropertyNames(contextObject);
95                     int len = result.length;
96                     for (int i = 0; i < len; i++) {
97                         try {
98                             map.put(result[i], h.getProperty(contextObject, result[i]));
99                         } catch (Exception JavaDoc exc) {
100                             exc.printStackTrace();
101                         }
102                     }
103                 } catch (Exception JavaDoc ignored) {
104                     ignored.printStackTrace();
105                 }
106             } else {
107                 PropertyDescriptor JavaDoc[] props = bi.getPropertyDescriptors();
108                 int len = props.length;
109                 for (int i = 0; i < len; i++) {
110                     try {
111                         Method JavaDoc read = props[i].getReadMethod();
112                         if (read != null) {
113                             map.put(props[i].getName(), read.invoke(contextObject, null));
114                         }
115                     } catch (Exception JavaDoc ignored) {
116                         ignored.printStackTrace();
117                     }
118                 }
119             }
120         }
121     }
122 }
123
124
Popular Tags