1 16 package org.apache.cocoon.components.persistence; 17 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 import java.io.OutputStreamWriter ; 21 import java.io.Writer ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Map.Entry; 26 27 import org.apache.avalon.framework.activity.Initializable; 28 import org.apache.avalon.framework.component.Component; 29 import org.apache.avalon.framework.configuration.Configurable; 30 import org.apache.avalon.framework.configuration.Configuration; 31 import org.apache.avalon.framework.configuration.ConfigurationException; 32 import org.apache.avalon.framework.logger.AbstractLogEnabled; 33 import org.apache.avalon.framework.service.ServiceException; 34 import org.apache.avalon.framework.service.ServiceManager; 35 import org.apache.avalon.framework.service.Serviceable; 36 import org.apache.avalon.framework.thread.ThreadSafe; 37 import org.apache.cocoon.components.source.SourceUtil; 38 import org.apache.cocoon.portal.profile.ProfileLS; 39 import org.apache.cocoon.portal.util.ReferenceFieldHandler; 40 import org.apache.excalibur.source.Source; 41 import org.apache.excalibur.source.SourceResolver; 42 import org.exolab.castor.mapping.Mapping; 43 import org.exolab.castor.mapping.MappingException; 44 import org.exolab.castor.xml.Marshaller; 45 import org.exolab.castor.xml.Unmarshaller; 46 import org.xml.sax.InputSource ; 47 48 64 public class CastorSourceConverter 65 extends AbstractLogEnabled 66 implements Component, Serviceable, Configurable, Initializable, ThreadSafe { 67 68 public static final String ROLE = CastorSourceConverter.class.getName(); 69 70 private Map mappingSources = new HashMap (); 71 private ServiceManager manager; 72 private Map mappings = new HashMap (); 73 private boolean defaultSuppressXSIType; 74 private boolean defaultValidateUnmarshalling; 75 76 public Object getObject(InputStream stream, Map parameters) throws ConverterException { 77 try { 78 ReferenceFieldHandler.setObjectMap((Map )parameters.get(ProfileLS.PARAMETER_OBJECTMAP)); 79 Unmarshaller unmarshaller = (Unmarshaller)((Object [])this.mappings.get(parameters.get(ProfileLS.PARAMETER_PROFILETYPE)))[1]; 80 Object result = unmarshaller.unmarshal(new InputSource (stream)); 81 stream.close(); 82 return result; 83 } catch (Exception e) { 84 throw new ConverterException(e.getMessage(), e); 85 } finally { 86 ReferenceFieldHandler.clearObjectMap(); 87 } 88 } 89 90 public void storeObject(OutputStream stream, Map parameters, Object object) throws ConverterException { 91 Writer writer = new OutputStreamWriter (stream); 92 try { 93 Marshaller marshaller = new Marshaller( writer ); 94 marshaller.setMapping((Mapping)((Object [])this.mappings.get(parameters.get(ProfileLS.PARAMETER_PROFILETYPE)))[0]); 95 boolean suppressXSIType = this.defaultSuppressXSIType; 96 Boolean value = (Boolean )parameters.get("suppressXSIType"); 97 if (value != null) { 98 suppressXSIType = value.booleanValue(); 99 } 100 marshaller.setSuppressXSIType(suppressXSIType); 101 marshaller.marshal(object); 102 writer.close(); 103 } catch (MappingException e) { 104 throw new ConverterException("Can't create Unmarshaller", e); 105 } catch (Exception e) { 106 throw new ConverterException(e.getMessage(), e); 107 } 108 } 109 110 113 public void service(ServiceManager manager) throws ServiceException { 114 this.manager = manager; 115 } 116 117 120 public void configure(Configuration config) throws ConfigurationException { 121 Configuration[] children = config.getChildren("mapping-source"); 122 for (int i=0; i<children.length; i++) { 123 Configuration mappingSource = children[i]; 124 this.mappingSources.put(mappingSource.getAttribute("source"), mappingSource.getValue()); 125 } 126 this.defaultSuppressXSIType = config.getChild("suppressXSIType").getValueAsBoolean(false); 127 this.defaultValidateUnmarshalling = config.getChild("validate-on-unmarshalling").getValueAsBoolean(false); 128 } 129 130 133 public void initialize() throws Exception { 134 SourceResolver resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 135 Source source = null; 136 try { 137 Entry entry; 138 String name; 139 String mappingSource; 140 Mapping mapping; 141 Iterator iterator = this.mappingSources.entrySet().iterator(); 142 while (iterator.hasNext()) { 143 entry = (Map.Entry )iterator.next(); 144 name = (String )entry.getKey(); 145 mappingSource = (String )entry.getValue(); 146 147 source = resolver.resolveURI(mappingSource); 148 mapping = new Mapping(); 149 mapping.loadMapping(SourceUtil.getInputSource(source)); 150 151 final Unmarshaller unmarshaller = new Unmarshaller(mapping); 153 unmarshaller.setValidation(this.defaultValidateUnmarshalling); 154 this.mappings.put(name, new Object [] {mapping, unmarshaller}); 155 } 156 } finally { 157 if (source != null) { 158 resolver.release(source); 159 } 160 manager.release(resolver); 161 } 162 } 163 } 164 | Popular Tags |