LibreOffice Module xmerge (master) 1
ActiveSyncDriver.java
Go to the documentation of this file.
1/*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19package org.openoffice.xmerge.util;
20
21import java.io.File;
22import java.io.FileOutputStream;
23import java.io.FileInputStream;
24import java.io.IOException;
25
26import java.util.StringTokenizer;
27import java.util.NoSuchElementException;
28
36
37public class ActiveSyncDriver {
38 public static void main(String[] args) {
39 if (args.length != 4) {
40 return;
41 }
42
44
45 try {
46 // At the moment can't really signal back to the calling DLL
47 asd.Convert(args[0], args[1], args[2], args[3]);
48 }
49 catch (Exception e) {
50 return;
51 }
52 }
53
54 private boolean Convert(String srcMime, String dstMime, String srcFile,
55 String dstFile) throws Exception {
56 /*
57 * The classpath passed in by XMergeSync.dll should contain all of the
58 * jar files, but at the least it will contain xmerge.jar, so strip off
59 * the xmerge.jar portion and use the remainder to provide a root for
60 * the Pocket Word and Pocket Excel plugins.
61 */
62 String ooClassDir = null;
63 String strClassPath = System.getProperty("java.class.path");
64
65 StringTokenizer st = new StringTokenizer(strClassPath, ";");
66
67 // There should be at least one element, but just in case
68 while (st.hasMoreTokens()) {
69 String s = st.nextToken();
70
71 if (s.endsWith("xmerge.jar")) {
72 ooClassDir = s.substring(0, s.indexOf("xmerge.jar"));
73 }
74 }
75
76 if (ooClassDir == null) {
77 return true;
78 }
79
80 /*
81 * The XMergeSync.dll should will have checked for the presence of the
82 * jars at the same location already.
83 *
84 * Because they can be installed separately, though, the MIME types need
85 * to be check to see which one to load.
86 */
87 File pluginJar;
88 if (srcMime.equals("staroffice/sxw") || srcMime.equals("application/x-pocket-word")) {
89 pluginJar = new File(ooClassDir + "pocketWord.jar");
90 } else {
91 if (srcMime.equals("staroffice/sxc") || srcMime.equals("application/x-pocket-excel")) {
92 pluginJar = new File(ooClassDir + "pexcel.jar");
93 } else {
94 return false;
95 }
96 }
97
98 ConverterInfoReader cirPlugin = new ConverterInfoReader(pluginJar.toURI().toURL().toString(), false);
99
101
103 Convert conv = cf.getConverter(srcMime, dstMime);
104
105 if (conv == null) {
106 return false;
107 }
108
109 // Everything is registered so do the conversion
110 boolean bOK = true;
111 FileInputStream fis = null;
112 FileOutputStream fos = null;
113 try {
114 fis = new FileInputStream(srcFile);
115 conv.addInputStream(srcFile, fis);
116 try {
117 fos = new FileOutputStream(dstFile);
118 ConvertData dataOut = conv.convert();
119
120 // Get the document and write it out.
121 Document doc = (Document)dataOut.getDocumentEnumeration().next();
122 doc.write(fos);
123 fos.flush();
124 } finally {
125 if (fos != null)
126 fos.close();
127 }
128 } catch (RuntimeException e) {
129 bOK = false;
130 } finally {
131 if (fis != null)
132 fis.close();
133 }
134
135 return bOK;
136 }
137}
ConvertData is used as a container for passing Document objects in and out of the Convert class.
Iterator< Object > getDocumentEnumeration()
Gets an Enumeration to access the Vector of Document objects.
This Exception is thrown by convert algorithms.
The Convert class manages a conversion from one mime-type to another.
Definition: Convert.java:38
void addInputStream(String name, InputStream is)
Adds an InputStream to be used as input by the Convert class.
Definition: Convert.java:80
ConvertData convert()
Convert the input specified in calls to the addInputStream method to the output format specified by t...
Definition: Convert.java:181
Factory that provides access to Convert objects, which are used to do a conversion.
Convert getConverter(String mimeTypeIn, String mimeTypeOut)
Returns the Convert object that converts the specified device/office mime type conversion.
boolean Convert(String srcMime, String dstMime, String srcFile, String dstFile)
Manages the converter plug-ins that are currently active.
static void addPlugIn(ConverterInfo ci)
Adds a converter plug-in to the registry.
The ConverterInfoReader pulls a META-INF/converter.xml file out of a jar file and parses it,...
Iterator< ConverterInfo > getConverterInfoEnumeration()
Returns an Enumeration of ConverterInfo objects.
A Document represents any Document to be converted and the resulting Document from any conversion.
Definition: Document.java:36
void write(OutputStream os)
Writes out the Document content to the specified OutputStream.
@ Exception
args
Provides an interface for plug-in registration.
Provides general purpose utilities.
Provides interfaces for converting between two Document formats, and supports a "merge" interface for...
Definition: Convert.java:19