1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml.xslt;
25 
26 import org.openoffice.xmerge.Document;
27 import org.openoffice.xmerge.ConvertData;
28 import org.openoffice.xmerge.DocumentSerializer;
29 import org.openoffice.xmerge.DocumentSerializerFactory;
30 import org.openoffice.xmerge.DocumentDeserializer;
31 import org.openoffice.xmerge.DocumentDeserializerFactory;
32 import org.openoffice.xmerge.PluginFactory;
33 import org.openoffice.xmerge.converter.dom.DOMDocument;
34 //import org.openoffice.xmerge.converter.xml.sxw.SxwDocument;
35 //import org.openoffice.xmerge.converter.xml.OfficeDocument;
36 import org.openoffice.xmerge.converter.xml.xslt.GenericOfficeDocument;
37 import org.openoffice.xmerge.util.registry.ConverterInfo;
38 import org.openoffice.xmerge.DocumentMerger;
39 import org.openoffice.xmerge.DocumentMergerFactory;
40 import org.openoffice.xmerge.ConverterCapabilities;
41 
42 import java.io.InputStream;
43 import java.util.Enumeration;
44 import java.io.IOException;
45 import java.util.Properties;
46 
47 /**
48  *  <p>Xslt implementation of the <code>PluginFactory</code>.
49  *  This encapsulates conversion of StarWriter XML format to and from
50  *  a supported format.</p>
51  *
52  *  <p>The superclass produces a particular
53  *  {@link org.openoffice.xmerge.Document Document}
54  *  object, i.e. {@link
55  *  org.openoffice.xmerge.converter.xml.sxw.SxwDocument
56  *  SxwDocument} that the converters in this class work with.	Thus,
57  *  this class only implements the methods that produces the converters,
58  *  i.e. {@link
59  *  org.openoffice.xmerge.DocumentSerializer
60  *  DocumentSerializer} and {@link
61  *  org.openoffice.xmerge.DocumentDeserializer
62  *  DocumentDeserializer}</p>
63  *
64  *  @author   Aidan Butler
65  */
66 public final class PluginFactoryImpl extends PluginFactory
67     implements DocumentDeserializerFactory, DocumentSerializerFactory, DocumentMergerFactory
68 {
69 
PluginFactoryImpl(ConverterInfo ci)70 	public PluginFactoryImpl (ConverterInfo ci) {
71 	       super(ci);
72 	}
73 
74     /** ConverterCapabilities object for this type of conversion. */
75     private final static ConverterCapabilities converterCap =
76         new ConverterCapabilitiesImpl();
77 
78 
79     /**
80      *  Returns an instance of <code>DocumentSerializerImpl</code>,
81      *  which is an implementation of the <code>DocumentSerializer</code>
82      *  interface.
83      *
84      *  @param  doc  <code>Document</code> object to be
85      *               converted/serialized.
86      *
87      *  @return  A <code>DocumentSerializerImpl</code> object.
88      */
createDocumentSerializer(Document doc)89     public DocumentSerializer createDocumentSerializer(Document doc) {
90         return new DocumentSerializerImpl(this,doc);
91     }
92 
93 
94     /**
95      *  Returns an instance of <code>DocumentDeserializerImpl</code>,
96      *  which is an implementation of the <code>DocumentDeserializer</code>
97      *  interface.
98      *
99      *  @param  cd   <code>ConvertData</code> object.
100      *
101      *  @return  A DocumentDeserializerImpl object.
102      */
createDocumentDeserializer(ConvertData cd)103     public DocumentDeserializer createDocumentDeserializer(ConvertData cd) {
104 
105         return new DocumentDeserializerImpl(this,cd);
106     }
107 
createDeviceDocument(java.lang.String str, java.io.InputStream inputStream)108      public org.openoffice.xmerge.Document createDeviceDocument(java.lang.String str, java.io.InputStream inputStream) throws java.io.IOException {
109 	    String ext = this.getDeviceFileExtension();
110 	    DOMDocument domDoc = new DOMDocument(str,ext);
111 	    domDoc.read(inputStream);
112 	    return domDoc;
113     }
114 
115 
createOfficeDocument(String name, InputStream is)116      public Document createOfficeDocument(String name, InputStream is)
117         throws IOException {
118 
119         // read zipped XML stream
120         GenericOfficeDocument doc = new GenericOfficeDocument(name);
121         doc.read(is);
122         return doc;
123     }
124 
createOfficeDocument(String name, InputStream is,boolean isZip)125      public Document createOfficeDocument(String name, InputStream is,boolean isZip)
126         throws IOException {
127 
128         // read zipped XML stream
129         GenericOfficeDocument doc = new GenericOfficeDocument(name);
130         doc.read(is,isZip);
131         return doc;
132     }
133 
134     /**
135      *  Returns a <code>String</code> containing the file extension of a
136      *  <code>Document</code>. This method uses a properties file to determine
137      *  a mapping from the device mime in the <code>ConverterInfo</code> to a
138      *  particular file extension. If a mapping is not specified, the default
139      *  is ".txt".
140      *
141      *  @return  <code>String</code>.
142      */
143 
144 
getDeviceFileExtension()145     public String getDeviceFileExtension(){
146 	Class c = this.getClass();
147 	InputStream is = c.getResourceAsStream("XsltPlugin.properties");
148 	Properties props = new Properties();
149 	String ext= ".txt";
150 	String mimeType = null;
151 	ConverterInfo ci = this.getConverterInfo();
152 	Enumeration enumerate = ci.getDeviceMime();
153 	while (enumerate.hasMoreElements()) {
154 	    mimeType= (String) enumerate.nextElement();
155 	}
156 	try {
157 	    props.load(is);
158 
159 		 String info = props.getProperty(mimeType);
160 		 if (info != null) {
161 		     ext = info;
162 		 }
163 	} catch (Exception e) {
164 
165 	    // It is okay for the property file to not exist.
166 	    //
167 	}
168 	return ext;
169     }
170 
171     /**
172      *  Returns an instance of <code>DocumentMergerImpl</code>,
173      *  which is an implementation of the <code>DocumentMerger</code>
174      *  interface.
175      *
176      *  @param  doc  <code>Document</code> to merge.
177      *
178      *  @return  A DocumentMergerImpl object.
179      */
createDocumentMerger(Document doc)180     public DocumentMerger createDocumentMerger(Document doc) {
181 	ConverterCapabilities cc = converterCap;
182         DocumentMergerImpl merger = new DocumentMergerImpl(doc, cc);
183         return merger;
184 
185     }
186 
187 }
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199