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.util;
25 
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.FileInputStream;
29 import java.util.StringTokenizer;
30 
31 import org.openoffice.xmerge.Convert;
32 import org.openoffice.xmerge.Document;
33 import org.openoffice.xmerge.ConvertData;
34 import org.openoffice.xmerge.ConverterFactory;
35 import org.openoffice.xmerge.util.registry.ConverterInfoMgr;
36 import org.openoffice.xmerge.util.registry.ConverterInfoReader;
37 
38 public class ActiveSyncDriver {
main(String[] args)39     public static void main(String[] args) {
40         if (args.length != 4) {
41             return;
42         }
43 
44         ActiveSyncDriver asd = new ActiveSyncDriver();
45 
46         try {
47             // At the moment can't really signal back to the calling DLL
48             asd.Convert(args[0], args[1], args[2], args[3]);
49         }
50         catch (Exception e) {
51             return;
52         }
53     }
54 
55 
Convert(String srcMime, String dstMime, String srcFile, String dstFile)56     private boolean Convert(String srcMime, String dstMime, String srcFile, String dstFile) throws Exception {
57         /*
58          * The classpath passed in by XMergeSync.dll should contain all of the
59          * jar files, but at the least it will contain xmerge.jar, so strip off
60          * the xmerge.jar portion and use the remainder to provide a root for
61          * the Pocket Word and Pocket Excel plugins.
62          */
63         String ooClassDir = null;
64         String strClassPath = System.getProperty("java.class.path");
65 
66         StringTokenizer st = new StringTokenizer(strClassPath, ";");
67 
68         // There should be at least one element, but just in case
69         while (st.hasMoreTokens()) {
70             String s = st.nextToken();
71 
72             if (s.endsWith("xmerge.jar")) {
73                 ooClassDir = s.substring(0, s.indexOf("xmerge.jar"));
74             }
75         }
76 
77         if (ooClassDir == null) {
78             return true;
79         }
80 
81 
82         /*
83          * The XMergeSync.dll should will have checked for the presence of the
84          * jars at the same location already.
85          *
86          * Because they can be installed separately, though, the MIME types need
87          * to be check to see which one to load.
88          */
89         File pluginJar = null;
90         if (srcMime.equals("staroffice/sxw") || srcMime.equals("application/x-pocket-word"))
91         {
92             pluginJar = new File(ooClassDir + "pocketWord.jar");
93         }
94         else if (srcMime.equals("staroffice/sxc") || srcMime.equals("application/x-pocket-excel"))
95         {
96             pluginJar = new File(ooClassDir + "pexcel.jar");
97         }
98 
99         ConverterInfoReader cirPlugin = new ConverterInfoReader(pluginJar.toURL().toString(), false);
100 
101         ConverterInfoMgr.addPlugIn(cirPlugin.getConverterInfoEnumeration());
102 
103         ConverterFactory cf = new ConverterFactory();
104         Convert conv = cf.getConverter(srcMime, dstMime);
105 
106         if (conv == null) {
107             return false;
108         }
109 
110         // Everything is registered so do the conversion
111         FileInputStream fis = new FileInputStream(srcFile);
112         FileOutputStream fos = new FileOutputStream(dstFile);
113 
114         conv.addInputStream(srcFile, fis);
115 
116         ConvertData dataOut;
117         try {
118             dataOut = conv.convert();
119         }
120         catch (Exception e) {
121             return false;
122         }
123 
124         if (dataOut == null) {
125             return false;
126         }
127 
128         // Get the document and write it out.
129         Document doc = (Document)dataOut.getDocumentEnumeration().nextElement();
130         if (doc == null) {
131             return false;
132         }
133 
134         doc.write(fos);
135         fos.flush();
136         fos.close();
137 
138         return true;
139     }
140 }
141