View Javadoc

1   /*
2    * Copyright 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.pluto.descriptors.services.impl;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.InputStreamReader;
21  import java.io.OutputStream;
22  import java.io.OutputStreamWriter;
23  
24  import org.apache.xml.serialize.OutputFormat;
25  import org.apache.xml.serialize.XMLSerializer;
26  import org.exolab.castor.mapping.Mapping;
27  import org.exolab.castor.mapping.MappingException;
28  import org.exolab.castor.xml.Marshaller;
29  import org.exolab.castor.xml.Unmarshaller;
30  
31  /***
32   * Abstract deployment descriptor support class.
33   * This Base class provides support for reading
34   * and writing deployment descriptors using Castor.
35   *
36   * @author <a href="ddewolf@apache.org">David H. DeWolf</a>
37   * @version $Id: AbstractCastorDescriptorService.java 156743 2005-03-10 05:50:30Z ddewolf $
38   * @since Mar 5, 2005
39   */
40  abstract class AbstractCastorDescriptorService {
41  
42      private String contextPath;
43  
44      /***
45       * Default Constructor.
46       */
47      protected AbstractCastorDescriptorService(String contextPath) {
48          this.contextPath = contextPath;
49      }
50  
51      public String getContextPath() {
52          return contextPath;
53      }
54  
55  
56      /***
57       * Read the and convert the descriptor into it's Object graph.
58       * @return
59       * @throws IOException
60       */
61      protected Object readInternal() throws IOException {
62          Object object = null;
63          InputStream is = getInputStream();
64          try {
65              Mapping mapping = getCastorMapping();
66              Unmarshaller unmarshaller = new Unmarshaller(mapping);
67              unmarshaller.setEntityResolver(new EntityResolverImpl());
68              unmarshaller.setIgnoreExtraElements(getIgnoreExtraElements());
69              if(is!=null) {
70                  InputStreamReader in = new InputStreamReader(is);
71                  object = unmarshaller.unmarshal(in);
72              }
73          }
74          catch(Exception me) {
75              me.printStackTrace();
76              throw new IOException(me.getMessage());
77          }
78          finally {
79              if(is != null) {
80                  is.close();
81              }
82          }
83          return object;
84      }
85  
86      /***
87       * Write the object graph to it's descriptor.
88       * @param object
89       * @throws IOException
90       */
91      protected void writeInternal(Object object) throws IOException {
92          OutputFormat of = new OutputFormat();
93          of.setIndenting(true);
94          of.setIndent(2);
95          of.setLineWidth(600);
96          of.setDoctype(getPublicId(), getDTDUri());
97  
98          OutputStreamWriter writer =
99              new OutputStreamWriter(getOutputStream());
100         XMLSerializer serializer = new XMLSerializer(writer, of);
101 
102         try {
103             Marshaller marshaller =
104                 new Marshaller(serializer.asDocumentHandler());
105             marshaller.setMapping(getCastorMapping());
106             marshaller.marshal(object);
107         } catch(IOException io) {
108             throw io;
109         } catch (Exception e) {
110             throw new IOException(e.getMessage());
111         }
112         finally {
113             writer.flush();
114             writer.close();
115         }
116     }
117 
118     protected boolean getIgnoreExtraElements() {
119         return false;
120     }
121 
122     protected abstract Mapping getCastorMapping() throws IOException, MappingException;
123     protected abstract InputStream getInputStream() throws IOException;
124     protected abstract OutputStream getOutputStream() throws IOException;
125 
126     protected abstract String getPublicId();
127     protected abstract String getDTDUri();
128 
129 
130 }
131