KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > persistence > CastorSourceConverter


1 /*
2  * Copyright 1999-2004 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.components.persistence;
17
18 import java.io.InputStream JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.io.OutputStreamWriter JavaDoc;
21 import java.io.Writer JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
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 JavaDoc;
47
48 /**
49  * This is a component converting the profiles (= object tree) to XML and vice-versa
50  * using Castor. It could be used to persist objects as a XML representation.
51  *
52  * In order to work properly the methods provided by this interface require some
53  * parameters:
54  * objectmap : containing a map of objects for resolving references during load
55  * profiletype: specifying the mapping (e.g. in the portal this is one of layout, copletinstancedata, copletdata or copletbasedate)
56  * suppressXSIType: Sets whether or not the xsi:type attributes should appear on the marshalled document.
57  *
58  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
59  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
60  * @author <a HREF="mailto:bluetkemeier@s-und-n.de">Bj&ouml;rn L&uuml;tkemeier</a>
61  *
62  * @version CVS $Id: CastorSourceConverter.java 326102 2005-10-18 13:29:15Z cziegeler $
63  */

64 public class CastorSourceConverter
65     extends AbstractLogEnabled
66     implements Component, Serviceable, Configurable, Initializable, ThreadSafe {
67         
68     public static final String JavaDoc ROLE = CastorSourceConverter.class.getName();
69
70     private Map JavaDoc mappingSources = new HashMap JavaDoc();
71     private ServiceManager manager;
72     private Map JavaDoc mappings = new HashMap JavaDoc();
73     private boolean defaultSuppressXSIType;
74     private boolean defaultValidateUnmarshalling;
75
76     public Object JavaDoc getObject(InputStream JavaDoc stream, Map JavaDoc parameters) throws ConverterException {
77         try {
78             ReferenceFieldHandler.setObjectMap((Map JavaDoc)parameters.get(ProfileLS.PARAMETER_OBJECTMAP));
79             Unmarshaller unmarshaller = (Unmarshaller)((Object JavaDoc[])this.mappings.get(parameters.get(ProfileLS.PARAMETER_PROFILETYPE)))[1];
80             Object JavaDoc result = unmarshaller.unmarshal(new InputSource JavaDoc(stream));
81             stream.close();
82             return result;
83         } catch (Exception JavaDoc e) {
84             throw new ConverterException(e.getMessage(), e);
85         } finally {
86             ReferenceFieldHandler.clearObjectMap();
87         }
88     }
89
90     public void storeObject(OutputStream JavaDoc stream, Map JavaDoc parameters, Object JavaDoc object) throws ConverterException {
91         Writer JavaDoc writer = new OutputStreamWriter JavaDoc(stream);
92         try {
93             Marshaller marshaller = new Marshaller( writer );
94             marshaller.setMapping((Mapping)((Object JavaDoc[])this.mappings.get(parameters.get(ProfileLS.PARAMETER_PROFILETYPE)))[0]);
95             boolean suppressXSIType = this.defaultSuppressXSIType;
96             Boolean JavaDoc value = (Boolean JavaDoc)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 JavaDoc e) {
106             throw new ConverterException(e.getMessage(), e);
107         }
108     }
109
110     /**
111      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
112      */

113     public void service(ServiceManager manager) throws ServiceException {
114         this.manager = manager;
115     }
116
117     /**
118      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
119      */

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     /**
131      * @see org.apache.avalon.framework.activity.Initializable#initialize()
132      */

133     public void initialize() throws Exception JavaDoc {
134         SourceResolver resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
135         Source source = null;
136         try {
137             Entry entry;
138             String JavaDoc name;
139             String JavaDoc mappingSource;
140             Mapping mapping;
141             Iterator JavaDoc iterator = this.mappingSources.entrySet().iterator();
142             while (iterator.hasNext()) {
143                 entry = (Map.Entry JavaDoc)iterator.next();
144                 name = (String JavaDoc)entry.getKey();
145                 mappingSource = (String JavaDoc)entry.getValue();
146                 
147                 source = resolver.resolveURI(mappingSource);
148                 mapping = new Mapping();
149                 mapping.loadMapping(SourceUtil.getInputSource(source));
150
151                 // create unmarshaller
152
final Unmarshaller unmarshaller = new Unmarshaller(mapping);
153                 unmarshaller.setValidation(this.defaultValidateUnmarshalling);
154                 this.mappings.put(name, new Object JavaDoc[] {mapping, unmarshaller});
155             }
156         } finally {
157             if (source != null) {
158                 resolver.release(source);
159             }
160             manager.release(resolver);
161         }
162     }
163 }
164
Popular Tags