1 16 package org.apache.cocoon.portal.tools.copletManagement.generation; 17 18 import java.beans.PropertyDescriptor ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.StringReader ; 22 import java.lang.reflect.Method ; 23 import java.util.HashMap ; 24 import java.util.Map ; 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 ; 37 import org.xml.sax.SAXException ; 38 39 40 44 public class XMLProfileGenerator 45 extends ServiceableGenerator { 46 47 50 public void generate() 51 throws IOException , SAXException , ProcessingException { 52 53 SAXParser parser = null; 54 CastorSourceConverter converter = null; 55 final Object context = FlowHelper.getContextObject(this.objectModel); 56 Map myMap = new HashMap (); 57 if (context instanceof Map ) { 58 myMap = (Map ) context; 59 } else { 60 fillContext(context, myMap); 61 } 62 try { 63 Object layout = myMap.get("layout"); 64 converter = (CastorSourceConverter) this.manager.lookup(org.apache.cocoon.components.persistence.CastorSourceConverter.ROLE); 65 ByteArrayOutputStream os = new java.io.ByteArrayOutputStream (); 66 HashMap para = new HashMap (); 67 para.put("profiletype", "layout"); 68 converter.storeObject(os, para, layout); 69 String xml = new String (); 70 xml = os.toString(); 71 final InputSource inputSource = new InputSource (new StringReader (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 private void fillContext(Object contextObject, Map map) { 86 if (contextObject != null) { 87 final JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(contextObject.getClass()); 90 if (bi.isDynamic()) { 91 Class cl = bi.getDynamicPropertyHandlerClass(); 92 try { 93 DynamicPropertyHandler h = (DynamicPropertyHandler)cl.newInstance(); 94 String [] 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 exc) { 100 exc.printStackTrace(); 101 } 102 } 103 } catch (Exception ignored) { 104 ignored.printStackTrace(); 105 } 106 } else { 107 PropertyDescriptor [] props = bi.getPropertyDescriptors(); 108 int len = props.length; 109 for (int i = 0; i < len; i++) { 110 try { 111 Method read = props[i].getReadMethod(); 112 if (read != null) { 113 map.put(props[i].getName(), read.invoke(contextObject, null)); 114 } 115 } catch (Exception ignored) { 116 ignored.printStackTrace(); 117 } 118 } 119 } 120 } 121 } 122 } 123 124 | Popular Tags |