Java 5 + Writer4LaTeX + bugfixes
git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@11 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
parent
be54e842f4
commit
9241a44f6c
83 changed files with 2373 additions and 631 deletions
|
@ -378,7 +378,7 @@ public abstract class ExportFilterBase implements
|
|||
}
|
||||
|
||||
// Write out files
|
||||
Iterator docEnum = dataOut.iterator();
|
||||
Iterator<OutputFile> docEnum = dataOut.iterator();
|
||||
|
||||
// Remove the file name part of the url
|
||||
String sNewURL = null;
|
||||
|
@ -393,7 +393,7 @@ public abstract class ExportFilterBase implements
|
|||
}
|
||||
|
||||
while (docEnum.hasNext() && sURL.startsWith("file:")) {
|
||||
OutputFile docOut = (OutputFile)docEnum.next();
|
||||
OutputFile docOut = docEnum.next();
|
||||
|
||||
if (dataOut.getMasterDocument()==docOut) {
|
||||
// The master document is written to the XOutStream supplied
|
||||
|
|
|
@ -232,9 +232,9 @@ public class FilterDataParser {
|
|||
}
|
||||
|
||||
// Read further configuration properties
|
||||
Enumeration keys = props.keys();
|
||||
Enumeration<String> keys = props.keys();
|
||||
while (keys.hasMoreElements()) {
|
||||
String sKey = (String) keys.nextElement();
|
||||
String sKey = keys.nextElement();
|
||||
if (!"ConfigURL".equals(sKey) && !"TemplateURL".equals(sKey) && !"AutoCreate".equals(sKey)) {
|
||||
Object value = props.get(sKey);
|
||||
if (AnyConverter.isString(value)) {
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.0 (2009-03-08)
|
||||
* Version 1.2 (2009-03-26)
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -58,14 +58,14 @@ import writer2latex.api.MIMETypes;
|
|||
public class GraphicConverterImpl2 implements GraphicConverter {
|
||||
|
||||
private XComponentContext xComponentContext;
|
||||
private Hashtable importFilter;
|
||||
private Hashtable exportFilter;
|
||||
private Hashtable<String,String> importFilter;
|
||||
private Hashtable<String,String> exportFilter;
|
||||
private EPSCleaner epsCleaner;
|
||||
|
||||
public GraphicConverterImpl2(XComponentContext xComponentContext) {
|
||||
this.xComponentContext = xComponentContext;
|
||||
|
||||
importFilter = new Hashtable();
|
||||
importFilter = new Hashtable<String,String>();
|
||||
importFilter.put(MIMETypes.BMP, "BMP - MS Windows");
|
||||
//importFilter.put(MIMETypes.EMF, "EMF - MS Windows Metafile");
|
||||
importFilter.put(MIMETypes.EPS, "EPS - Encapsulated PostScript");
|
||||
|
@ -76,7 +76,7 @@ public class GraphicConverterImpl2 implements GraphicConverter {
|
|||
importFilter.put(MIMETypes.TIFF, "TIF - Tag Image File");
|
||||
importFilter.put(MIMETypes.WMF, "WMF - MS Windows Metafile");
|
||||
|
||||
exportFilter = new Hashtable();
|
||||
exportFilter = new Hashtable<String,String>();
|
||||
exportFilter.put(MIMETypes.BMP,"draw_bmp_Export");
|
||||
//exportFilter.put(MIMETypes.EMF,"draw_emf_Export");
|
||||
exportFilter.put(MIMETypes.EPS,"draw_eps_Export");
|
||||
|
|
|
@ -87,7 +87,7 @@ public abstract class OptionsDialogBase extends DialogBase implements
|
|||
this.xMSF = null; // must be set properly by subclass
|
||||
mediaProps = null;
|
||||
sConfigNames = null;
|
||||
lockedOptions = new HashSet();
|
||||
lockedOptions = new HashSet<String>();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
@ -163,7 +163,7 @@ public abstract class OptionsDialogBase extends DialogBase implements
|
|||
private String[] sConfigNames;
|
||||
|
||||
// Set of locked controls
|
||||
private HashSet lockedOptions;
|
||||
private HashSet<String> lockedOptions;
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -35,14 +35,14 @@ import com.sun.star.beans.PropertyValue;
|
|||
*/
|
||||
public class PropertyHelper {
|
||||
|
||||
private Hashtable data;
|
||||
private Hashtable<String, Object> data;
|
||||
|
||||
public PropertyHelper() {
|
||||
data = new Hashtable();
|
||||
data = new Hashtable<String, Object>();
|
||||
}
|
||||
|
||||
public PropertyHelper(PropertyValue[] props) {
|
||||
data = new Hashtable();
|
||||
data = new Hashtable<String, Object>();
|
||||
int nLen = props.length;
|
||||
for (int i=0; i<nLen; i++) {
|
||||
data.put(props[i].Name,props[i].Value);
|
||||
|
@ -57,7 +57,7 @@ public class PropertyHelper {
|
|||
return data.get(sName);
|
||||
}
|
||||
|
||||
public Enumeration keys() {
|
||||
public Enumeration<String> keys() {
|
||||
return data.keys();
|
||||
}
|
||||
|
||||
|
@ -65,9 +65,9 @@ public class PropertyHelper {
|
|||
int nSize = data.size();
|
||||
PropertyValue[] props = new PropertyValue[nSize];
|
||||
int i=0;
|
||||
Enumeration keys = keys();
|
||||
Enumeration<String> keys = keys();
|
||||
while (keys.hasMoreElements()) {
|
||||
String sKey = (String) keys.nextElement();
|
||||
String sKey = keys.nextElement();
|
||||
props[i] = new PropertyValue();
|
||||
props[i].Name = sKey;
|
||||
props[i++].Value = get(sKey);
|
||||
|
|
|
@ -0,0 +1,304 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* ConfigurationDialog.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2009 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.2 (2009-03-30)
|
||||
*
|
||||
*/
|
||||
|
||||
package org.openoffice.da.comp.writer4latex;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
|
||||
import com.sun.star.awt.XControl;
|
||||
import com.sun.star.awt.XControlContainer;
|
||||
import com.sun.star.awt.XControlModel;
|
||||
import com.sun.star.awt.XContainerWindowEventHandler;
|
||||
import com.sun.star.awt.XWindow;
|
||||
import com.sun.star.beans.XPropertySet;
|
||||
import com.sun.star.lang.XComponent;
|
||||
import com.sun.star.lang.XServiceInfo;
|
||||
import com.sun.star.ui.dialogs.ExecutableDialogResults;
|
||||
import com.sun.star.ui.dialogs.XExecutableDialog;
|
||||
import com.sun.star.ui.dialogs.XFilePicker;
|
||||
import com.sun.star.uno.AnyConverter;
|
||||
import com.sun.star.uno.UnoRuntime;
|
||||
import com.sun.star.uno.XComponentContext;
|
||||
|
||||
import com.sun.star.lib.uno.helper.WeakBase;
|
||||
|
||||
/** This class provides a uno component which implements the configuration
|
||||
* of Writer4LaTeX
|
||||
*/
|
||||
public final class ConfigurationDialog
|
||||
extends WeakBase
|
||||
implements XServiceInfo, XContainerWindowEventHandler {
|
||||
|
||||
private XComponentContext xContext;
|
||||
|
||||
private ExternalApps externalApps;
|
||||
|
||||
/** The component will be registered under this name.
|
||||
*/
|
||||
public static String __serviceName = "org.openoffice.da.writer4latex.ConfigurationDialog";
|
||||
|
||||
/** The component should also have an implementation name.
|
||||
*/
|
||||
public static String __implementationName = "org.openoffice.da.comp.writer4latex.ConfigurationDialog";
|
||||
|
||||
/** Create a new ConfigurationDialog */
|
||||
public ConfigurationDialog(XComponentContext xContext) {
|
||||
this.xContext = xContext;
|
||||
externalApps = new ExternalApps(xContext);
|
||||
}
|
||||
|
||||
// Implement XContainerWindowEventHandler
|
||||
public boolean callHandlerMethod(XWindow xWindow, Object event, String sMethod)
|
||||
throws com.sun.star.lang.WrappedTargetException {
|
||||
try {
|
||||
if (sMethod.equals("external_event") ){
|
||||
return handleExternalEvent(xWindow, event);
|
||||
}
|
||||
else if (sMethod.equals("ApplicationChange")) {
|
||||
return changeApplication(xWindow);
|
||||
}
|
||||
else if (sMethod.equals("BrowseClick")) {
|
||||
return browseForExecutable(xWindow);
|
||||
}
|
||||
else if (sMethod.equals("ExecutableUnfocus")) {
|
||||
return updateApplication(xWindow);
|
||||
}
|
||||
else if (sMethod.equals("OptionsUnfocus")) {
|
||||
return updateApplication(xWindow);
|
||||
}
|
||||
else if (sMethod.equals("AutomaticClick")) {
|
||||
return autoConfigure(xWindow);
|
||||
}
|
||||
}
|
||||
catch (com.sun.star.uno.RuntimeException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (com.sun.star.uno.Exception e) {
|
||||
throw new com.sun.star.lang.WrappedTargetException(sMethod, this, e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String[] getSupportedMethodNames() {
|
||||
String[] sNames = { "external_event", "ApplicationChange", "BrowseClick", "ExecutableUnfocus", "OptionsUnfocus", "AutomaticClick" };
|
||||
return sNames;
|
||||
}
|
||||
|
||||
// Implement the interface XServiceInfo
|
||||
public boolean supportsService(String sServiceName) {
|
||||
return sServiceName.equals(__serviceName);
|
||||
}
|
||||
|
||||
public String getImplementationName() {
|
||||
return __implementationName;
|
||||
}
|
||||
|
||||
public String[] getSupportedServiceNames() {
|
||||
String[] sSupportedServiceNames = { __serviceName };
|
||||
return sSupportedServiceNames;
|
||||
}
|
||||
|
||||
// Private stuff
|
||||
|
||||
private boolean handleExternalEvent(com.sun.star.awt.XWindow xWindow, Object aEventObject)
|
||||
throws com.sun.star.uno.Exception {
|
||||
try {
|
||||
String sMethod = AnyConverter.toString(aEventObject);
|
||||
if (sMethod.equals("ok")) {
|
||||
externalApps.save();
|
||||
return true;
|
||||
} else if (sMethod.equals("back") || sMethod.equals("initialize")) {
|
||||
externalApps.load();
|
||||
return changeApplication(xWindow);
|
||||
}
|
||||
}
|
||||
catch (com.sun.star.lang.IllegalArgumentException e) {
|
||||
throw new com.sun.star.lang.IllegalArgumentException(
|
||||
"Method external_event requires a string in the event object argument.", this,(short) -1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean changeApplication(XWindow xWindow) {
|
||||
String sAppName = getSelectedAppName(xWindow);
|
||||
if (sAppName!=null) {
|
||||
String[] s = externalApps.getApplication(sAppName);
|
||||
setComboBoxText(xWindow, "Executable", s[0]);
|
||||
setComboBoxText(xWindow, "Options", s[1]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean browseForExecutable(XWindow xWindow) {
|
||||
XComponent xComponent = null;
|
||||
try {
|
||||
// Create FilePicker
|
||||
Object filePicker = xContext.getServiceManager()
|
||||
.createInstanceWithContext("com.sun.star.ui.dialogs.FilePicker", xContext);
|
||||
XFilePicker xFilePicker = (XFilePicker)
|
||||
UnoRuntime.queryInterface(XFilePicker.class, filePicker);
|
||||
xComponent = (XComponent)
|
||||
UnoRuntime.queryInterface(XComponent.class, xFilePicker);
|
||||
|
||||
// Display the FilePicker
|
||||
XExecutableDialog xExecutable = (XExecutableDialog)
|
||||
UnoRuntime.queryInterface(XExecutableDialog.class, xFilePicker);
|
||||
|
||||
// Get the path
|
||||
if (xExecutable.execute() == ExecutableDialogResults.OK) {
|
||||
String[] sPathList = xFilePicker.getFiles();
|
||||
if (sPathList.length > 0) {
|
||||
setComboBoxText(xWindow, "Executable", new File(new URI(sPathList[0])).getCanonicalPath());
|
||||
updateApplication(xWindow);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (com.sun.star.uno.Exception e) {
|
||||
}
|
||||
catch (java.net.URISyntaxException e) {
|
||||
}
|
||||
catch (java.io.IOException e) {
|
||||
}
|
||||
finally{
|
||||
// Always dispose the FilePicker component
|
||||
if (xComponent!=null) {
|
||||
xComponent.dispose();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean updateApplication(XWindow xWindow) {
|
||||
String sAppName = getSelectedAppName(xWindow);
|
||||
if (sAppName!=null) {
|
||||
externalApps.setApplication(sAppName, getComboBoxText(xWindow, "Executable"), getComboBoxText(xWindow, "Options"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean autoConfigure(XWindow xWindow) {
|
||||
externalApps.setApplication(ExternalApps.LATEX, "latex", "--interaction=batchmode %s");
|
||||
externalApps.setApplication(ExternalApps.PDFLATEX, "pdflatex", "--interaction=batchmode %s");
|
||||
externalApps.setApplication(ExternalApps.XELATEX, "xelatex", "--interaction=batchmode %s");
|
||||
externalApps.setApplication(ExternalApps.DVIPS, "dvips", "%s");
|
||||
externalApps.setApplication(ExternalApps.BIBTEX, "bibtex", "%s");
|
||||
externalApps.setApplication(ExternalApps.MAKEINDEX, "makeindex", "%s");
|
||||
externalApps.setApplication(ExternalApps.OOLATEX, "oolatex", "%s");
|
||||
|
||||
String sOsName = System.getProperty("os.name");
|
||||
if ("Linux".equals(sOsName)) {
|
||||
// TODO: Search for applications (which...)
|
||||
// Viewers may be evince, okular, xdvi, xpdf, ghostview or...
|
||||
externalApps.setApplication(ExternalApps.DVIVIEWER, "evince", "%s");
|
||||
externalApps.setApplication(ExternalApps.PDFVIEWER, "evince", "%s");
|
||||
externalApps.setApplication(ExternalApps.POSTSCRIPTVIEWER, "evince", "%s");
|
||||
}
|
||||
else if ("Windows".equals(sOsName)) {
|
||||
// Assume MikTeX
|
||||
externalApps.setApplication(ExternalApps.DVIVIEWER, "yap", "--single-instance %s");
|
||||
// And assume gsview for pdf and ps
|
||||
// gsview32 may not be in the path, but at least this helps a bit
|
||||
externalApps.setApplication(ExternalApps.PDFVIEWER, "gsview32.exe", "-e \"%s\"");
|
||||
externalApps.setApplication(ExternalApps.POSTSCRIPTVIEWER, "gsview32.exe", "-e \"%s\"");
|
||||
}
|
||||
changeApplication(xWindow);
|
||||
return true;
|
||||
}
|
||||
|
||||
private String getSelectedAppName(XWindow xWindow) {
|
||||
short nItem = getListBoxSelectedItem(xWindow, "Application");
|
||||
//String sAppName = null;
|
||||
switch (nItem) {
|
||||
case 0: return ExternalApps.LATEX;
|
||||
case 1: return ExternalApps.PDFLATEX;
|
||||
case 2: return ExternalApps.XELATEX;
|
||||
case 3: return ExternalApps.DVIPS;
|
||||
case 4: return ExternalApps.BIBTEX;
|
||||
case 5: return ExternalApps.MAKEINDEX;
|
||||
case 6: return ExternalApps.OOLATEX;
|
||||
case 7: return ExternalApps.DVIVIEWER;
|
||||
case 8: return ExternalApps.PDFVIEWER;
|
||||
case 9: return ExternalApps.POSTSCRIPTVIEWER;
|
||||
}
|
||||
return "???";
|
||||
}
|
||||
|
||||
// Some helpers copied from DialogBase
|
||||
private XPropertySet getControlProperties(XWindow xWindow, String sControlName) {
|
||||
XControlContainer xContainer = (XControlContainer)
|
||||
UnoRuntime.queryInterface(XControlContainer.class, xWindow);
|
||||
XControl xControl = xContainer.getControl(sControlName);
|
||||
XControlModel xModel = xControl.getModel();
|
||||
XPropertySet xPropertySet = (XPropertySet)
|
||||
UnoRuntime.queryInterface(XPropertySet.class, xModel);
|
||||
return xPropertySet;
|
||||
}
|
||||
|
||||
private String getComboBoxText(XWindow xWindow, String sControlName) {
|
||||
// Returns the text of a combobox
|
||||
XPropertySet xPropertySet = getControlProperties(xWindow, sControlName);
|
||||
try {
|
||||
return (String) xPropertySet.getPropertyValue("Text");
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Will fail if the control does not exist or is not a combo
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private void setComboBoxText(XWindow xWindow, String sControlName, String sText) {
|
||||
XPropertySet xPropertySet = getControlProperties(xWindow, sControlName);
|
||||
try {
|
||||
xPropertySet.setPropertyValue("Text", sText);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Will fail if the control does not exist or is not a combo box or
|
||||
// nText is an illegal value
|
||||
}
|
||||
}
|
||||
|
||||
private short getListBoxSelectedItem(XWindow xWindow, String sControlName) {
|
||||
// Returns the first selected element in case of a multiselection
|
||||
XPropertySet xPropertySet = getControlProperties(xWindow, sControlName);
|
||||
try {
|
||||
short[] selection = (short[]) xPropertySet.getPropertyValue("SelectedItems");
|
||||
return selection[0];
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Will fail if the control does not exist or is not a list box
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,250 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* ExternalApps.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2009 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.2 (2009-03-30)
|
||||
*
|
||||
*/
|
||||
|
||||
package org.openoffice.da.comp.writer4latex;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.Process;
|
||||
import java.lang.ProcessBuilder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Vector;
|
||||
//import java.util.Map;
|
||||
|
||||
import com.sun.star.beans.PropertyValue;
|
||||
import com.sun.star.beans.XMultiHierarchicalPropertySet;
|
||||
import com.sun.star.lang.XComponent;
|
||||
import com.sun.star.lang.XMultiServiceFactory;
|
||||
import com.sun.star.uno.UnoRuntime;
|
||||
import com.sun.star.uno.XComponentContext;
|
||||
import com.sun.star.util.XChangesBatch;
|
||||
|
||||
|
||||
/** This class manages and executes external applications used by Writer4LaTeX.
|
||||
* These include TeX and friends as well as viewers for the various backend
|
||||
* formats. The registry is used for persistent storage of the settings.
|
||||
*/
|
||||
public class ExternalApps {
|
||||
|
||||
public final static String LATEX = "LaTeX";
|
||||
public final static String PDFLATEX = "PdfLaTeX";
|
||||
public final static String XELATEX = "XeLaTeX";
|
||||
public final static String BIBTEX = "BibTeX";
|
||||
public final static String MAKEINDEX = "Makeindex";
|
||||
public final static String OOLATEX = "Oolatex";
|
||||
public final static String DVIPS = "Dvips";
|
||||
public final static String DVIVIEWER = "DVIViewer";
|
||||
public final static String POSTSCRIPTVIEWER = "PostscriptViewer";
|
||||
public final static String PDFVIEWER = "PdfViewer";
|
||||
|
||||
private final static String[] sApps = { LATEX, PDFLATEX, XELATEX, BIBTEX, MAKEINDEX, OOLATEX, DVIPS, DVIVIEWER, POSTSCRIPTVIEWER, PDFVIEWER };
|
||||
|
||||
private XComponentContext xContext;
|
||||
|
||||
private HashMap<String,String[]> apps;
|
||||
|
||||
/** Construct a new ExternalApps object, with empty definitions */
|
||||
public ExternalApps(XComponentContext xContext) {
|
||||
this.xContext = xContext;
|
||||
apps = new HashMap<String,String[]>();
|
||||
for (int i=0; i<sApps.length; i++) {
|
||||
setApplication(sApps[i], "?", "?");
|
||||
}
|
||||
}
|
||||
|
||||
/** Define an external application
|
||||
* @param sAppName the name of the application to define
|
||||
* @param sExecutable the system dependent path to the executable file
|
||||
* @param sOptions the options to the external application; %s will be
|
||||
* replaced by the filename on execution
|
||||
*/
|
||||
public void setApplication(String sAppName, String sExecutable, String sOptions) {
|
||||
String[] sValue = { sExecutable, sOptions };
|
||||
apps.put(sAppName, sValue);
|
||||
}
|
||||
|
||||
/** Get the definition for an external application
|
||||
* @param sAppName the name of the application to get
|
||||
* @return a String array containg the system dependent path to the
|
||||
* executable file as entry 0, and the parameters as entry 1
|
||||
* returns null if the application is unknown
|
||||
*/
|
||||
public String[] getApplication(String sAppName) {
|
||||
return apps.get(sAppName);
|
||||
}
|
||||
|
||||
/** Execute an external application
|
||||
* @param sAppName the name of the application to execute
|
||||
* @param sFileName the file name to use
|
||||
* @param workDir the working directory to use
|
||||
* @param bWaitFor true if the method should wait for the execution to finish
|
||||
* @return error code
|
||||
*/
|
||||
public int execute(String sAppName, String sFileName, File workDir, boolean bWaitFor) {
|
||||
// Assemble the command
|
||||
String[] sApp = getApplication(sAppName);
|
||||
if (sApp==null) { return 1; }
|
||||
|
||||
try {
|
||||
Vector<String> command = new Vector<String>();
|
||||
command.add(sApp[0]);
|
||||
String[] sArguments = sApp[1].split(" ");
|
||||
for (String s : sArguments) {
|
||||
command.add(s.replace("%s",sFileName));
|
||||
}
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(command);
|
||||
//Map<String, String> env = pb.environment();
|
||||
//env.put("VAR1", "myValue");
|
||||
pb.directory(workDir);
|
||||
Process proc = pb.start();
|
||||
|
||||
|
||||
//Runtime rt = Runtime.getRuntime();
|
||||
//Process proc = rt.exec(sCommand, new String[0], workDir);
|
||||
|
||||
// Gobble the error stream of the application
|
||||
StreamGobbler errorGobbler = new
|
||||
StreamGobbler(proc.getErrorStream(), "ERROR");
|
||||
|
||||
// Gooble the output stream of the application
|
||||
StreamGobbler outputGobbler = new
|
||||
StreamGobbler(proc.getInputStream(), "OUTPUT");
|
||||
|
||||
// Kick them off
|
||||
errorGobbler.start();
|
||||
outputGobbler.start();
|
||||
|
||||
// Any error?
|
||||
return bWaitFor ? proc.waitFor() : 0;
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
return 1;
|
||||
}
|
||||
catch (IOException e) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/** Load the external applications from the registry
|
||||
*/
|
||||
public void load() {
|
||||
Object view;
|
||||
try {
|
||||
view = getRegistryView(false);
|
||||
}
|
||||
catch (com.sun.star.uno.Exception e) {
|
||||
// Give up...
|
||||
//setApplication(LATEX,"Error!",e.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
XMultiHierarchicalPropertySet xProps = (XMultiHierarchicalPropertySet)
|
||||
UnoRuntime.queryInterface(XMultiHierarchicalPropertySet.class, view);
|
||||
for (int i=0; i<sApps.length; i++) {
|
||||
String[] sNames = new String[2];
|
||||
sNames[0] = sApps[i]+"/Executable";
|
||||
sNames[1] = sApps[i]+"/Options";
|
||||
try {
|
||||
Object[] values = xProps.getHierarchicalPropertyValues(sNames);
|
||||
setApplication(sApps[i], (String) values[0], (String) values[1]);
|
||||
}
|
||||
catch (com.sun.star.uno.Exception e) {
|
||||
// Ignore...
|
||||
}
|
||||
}
|
||||
|
||||
disposeRegistryView(view);
|
||||
}
|
||||
|
||||
/** Save the external applications to the registry
|
||||
*/
|
||||
public void save() {
|
||||
Object view;
|
||||
try {
|
||||
view = getRegistryView(true);
|
||||
}
|
||||
catch (com.sun.star.uno.Exception e) {
|
||||
// Give up...
|
||||
return;
|
||||
}
|
||||
|
||||
XMultiHierarchicalPropertySet xProps = (XMultiHierarchicalPropertySet)
|
||||
UnoRuntime.queryInterface(XMultiHierarchicalPropertySet.class, view);
|
||||
for (int i=0; i<sApps.length; i++) {
|
||||
String[] sNames = new String[2];
|
||||
sNames[0] = sApps[i]+"/Executable";
|
||||
sNames[1] = sApps[i]+"/Options";
|
||||
String[] sValues = getApplication(sApps[i]);
|
||||
try {
|
||||
xProps.setHierarchicalPropertyValues(sNames, sValues);
|
||||
}
|
||||
catch (com.sun.star.uno.Exception e) {
|
||||
// Ignore...
|
||||
}
|
||||
}
|
||||
|
||||
// Commit registry changes
|
||||
XChangesBatch xUpdateContext = (XChangesBatch)
|
||||
UnoRuntime.queryInterface(XChangesBatch.class, view);
|
||||
try {
|
||||
xUpdateContext.commitChanges();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
disposeRegistryView(view);
|
||||
}
|
||||
|
||||
// Get a view of the options root in the registry
|
||||
private Object getRegistryView(boolean bUpdate)
|
||||
throws com.sun.star.uno.Exception {
|
||||
//Object provider = xMSF.createInstance(
|
||||
Object provider = xContext.getServiceManager().createInstanceWithContext(
|
||||
"com.sun.star.configuration.ConfigurationProvider", xContext);
|
||||
XMultiServiceFactory xProvider = (XMultiServiceFactory)
|
||||
UnoRuntime.queryInterface(XMultiServiceFactory.class,provider);
|
||||
PropertyValue[] args = new PropertyValue[1];
|
||||
args[0] = new PropertyValue();
|
||||
args[0].Name = "nodepath";
|
||||
args[0].Value = "/org.openoffice.da.Writer4LaTeX.Options/Applications";
|
||||
String sServiceName = bUpdate ?
|
||||
"com.sun.star.configuration.ConfigurationUpdateAccess" :
|
||||
"com.sun.star.configuration.ConfigurationAccess";
|
||||
Object view = xProvider.createInstanceWithArguments(sServiceName,args);
|
||||
return view;
|
||||
}
|
||||
|
||||
// Dispose a previously obtained registry view
|
||||
private void disposeRegistryView(Object view) {
|
||||
XComponent xComponent = (XComponent)
|
||||
UnoRuntime.queryInterface(XComponent.class,view);
|
||||
xComponent.dispose();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,146 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* LogViewerDialog.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2009 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.2 (2009-03-26)
|
||||
*
|
||||
*/
|
||||
|
||||
package org.openoffice.da.comp.writer4latex;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import com.sun.star.awt.XDialog;
|
||||
import com.sun.star.uno.XComponentContext;
|
||||
|
||||
import org.openoffice.da.comp.w2lcommon.helper.DialogBase;
|
||||
|
||||
/** This class provides a uno component which displays logfiles
|
||||
*/
|
||||
public class LogViewerDialog extends DialogBase
|
||||
implements com.sun.star.lang.XInitialization {
|
||||
|
||||
/** The component will be registered under this name.
|
||||
*/
|
||||
public static String __serviceName = "org.openoffice.da.writer4latex.LogViewerDialog";
|
||||
|
||||
/** The component should also have an implementation name.
|
||||
*/
|
||||
public static String __implementationName = "org.openoffice.da.comp.writer4latex.LogViewerDialog";
|
||||
|
||||
/** Return the name of the library containing the dialog
|
||||
*/
|
||||
public String getDialogLibraryName() {
|
||||
return "W4LDialogs";
|
||||
}
|
||||
|
||||
private String sBaseUrl = null;
|
||||
private String sLaTeXLog = null;
|
||||
private String sBibTeXLog = null;
|
||||
private String sMakeindexLog = null;
|
||||
|
||||
/** Return the name of the dialog within the library
|
||||
*/
|
||||
public String getDialogName() {
|
||||
return "LogViewer";
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
if (sBaseUrl!=null) {
|
||||
sLaTeXLog = readTextFile(sBaseUrl+".log");
|
||||
sBibTeXLog = readTextFile(sBaseUrl+".blg");
|
||||
sMakeindexLog = readTextFile(sBaseUrl+".ilg");
|
||||
setComboBoxText("LogContents",sLaTeXLog);
|
||||
}
|
||||
}
|
||||
|
||||
public void finalize() {
|
||||
}
|
||||
|
||||
/** Create a new LogViewerDialog */
|
||||
public LogViewerDialog(XComponentContext xContext) {
|
||||
super(xContext);
|
||||
}
|
||||
|
||||
// Implement com.sun.star.lang.XInitialization
|
||||
public void initialize( Object[] object )
|
||||
throws com.sun.star.uno.Exception {
|
||||
if ( object.length > 0 ) {
|
||||
if (object[0] instanceof String) {
|
||||
sBaseUrl = (String) object[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implement XDialogEventHandler
|
||||
public boolean callHandlerMethod(XDialog xDialog, Object event, String sMethod) {
|
||||
if (sMethod.equals("ViewLaTeXLog")) {
|
||||
setComboBoxText("LogContents", sLaTeXLog);
|
||||
}
|
||||
else if (sMethod.equals("ViewBibTeXLog")) {
|
||||
setComboBoxText("LogContents", sBibTeXLog);
|
||||
}
|
||||
else if (sMethod.equals("ViewMakeindexLog")) {
|
||||
setComboBoxText("LogContents", sMakeindexLog);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String[] getSupportedMethodNames() {
|
||||
String[] sNames = { "ViewLaTeXLog", "ViewBibTeXLog", "ViewMakeindexLog" };
|
||||
return sNames;
|
||||
}
|
||||
|
||||
// Utility methods
|
||||
|
||||
private String readTextFile(String sUrl) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
try {
|
||||
File file = new File(new URI(sUrl));
|
||||
if (file.exists() && file.isFile()) {
|
||||
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
|
||||
int n;
|
||||
do {
|
||||
n = isr.read();
|
||||
if (n>-1) { buf.append((char)n); }
|
||||
}
|
||||
while (n>-1);
|
||||
isr.close();
|
||||
}
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
return "";
|
||||
}
|
||||
catch (IOException e) {
|
||||
return "";
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* StreamGobbler.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2009 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.2 (2009-03-30)
|
||||
*
|
||||
*/
|
||||
|
||||
package org.openoffice.da.comp.writer4latex;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
class StreamGobbler extends Thread {
|
||||
InputStream is;
|
||||
String type;
|
||||
|
||||
StreamGobbler(InputStream is, String type) {
|
||||
this.is = is;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
InputStreamReader isr = new InputStreamReader(is);
|
||||
BufferedReader br = new BufferedReader(isr);
|
||||
String line=null;
|
||||
while ( (line = br.readLine()) != null) {
|
||||
//while ( br.readLine() != null) {
|
||||
// Do nothing...
|
||||
System.out.println(type + ">" + line);
|
||||
}
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
124
source/java/org/openoffice/da/comp/writer4latex/TeXify.java
Normal file
124
source/java/org/openoffice/da/comp/writer4latex/TeXify.java
Normal file
|
@ -0,0 +1,124 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* TeXify.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2009 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.2 (2009-03-30)
|
||||
*
|
||||
*/
|
||||
|
||||
package org.openoffice.da.comp.writer4latex;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.sun.star.uno.XComponentContext;
|
||||
|
||||
/** This class builds LaTeX documents into dvi, postscript or pdf and displays
|
||||
* the result.
|
||||
*/
|
||||
public final class TeXify {
|
||||
|
||||
/** Backend format generic (dvi) */
|
||||
public static final short GENERIC = 1;
|
||||
|
||||
/** Backend format dvips (postscript) */
|
||||
public static final short DVIPS = 2;
|
||||
|
||||
/** Backend format pdfTeX (pdf) */
|
||||
public static final short PDFTEX = 3;
|
||||
|
||||
// Define the applications to run for each backend
|
||||
private static final String[] genericTexify = {
|
||||
ExternalApps.LATEX, ExternalApps.BIBTEX, ExternalApps.MAKEINDEX,
|
||||
ExternalApps.LATEX, ExternalApps.MAKEINDEX, ExternalApps.LATEX };
|
||||
private static final String[] pdfTexify = {
|
||||
ExternalApps.PDFLATEX, ExternalApps.BIBTEX, ExternalApps.MAKEINDEX,
|
||||
ExternalApps.PDFLATEX, ExternalApps.MAKEINDEX, ExternalApps.PDFLATEX };
|
||||
private static final String[] dvipsTexify = {
|
||||
ExternalApps.LATEX, ExternalApps.BIBTEX, ExternalApps.MAKEINDEX,
|
||||
ExternalApps.LATEX, ExternalApps.MAKEINDEX, ExternalApps.LATEX,
|
||||
ExternalApps.DVIPS };
|
||||
|
||||
// Global objects
|
||||
//private XComponentContext xContext;
|
||||
private ExternalApps externalApps;
|
||||
|
||||
public TeXify(XComponentContext xContext) {
|
||||
//this.xContext = xContext;
|
||||
externalApps = new ExternalApps(xContext);
|
||||
}
|
||||
|
||||
/** Process a document
|
||||
* @param file the LaTeX file to process
|
||||
* @param nBackend the desired backend format (generic, dvips, pdftex)
|
||||
* @param bView set the true if the result should be displayed in the viewer
|
||||
* @throws IOException if the document cannot be read
|
||||
*/
|
||||
public void process(File file, short nBackend, boolean bView) throws IOException {
|
||||
// Remove extension from file
|
||||
if (file.getName().endsWith(".tex")) {
|
||||
file = new File(file.getParentFile(),
|
||||
file.getName().substring(0,file.getName().length()-4));
|
||||
}
|
||||
|
||||
// Update external apps from registry
|
||||
externalApps.load();
|
||||
|
||||
// Process LaTeX document
|
||||
if (nBackend==GENERIC) {
|
||||
doTeXify(genericTexify, file);
|
||||
if (externalApps.execute(ExternalApps.DVIVIEWER,
|
||||
new File(file.getParentFile(),file.getName()+".dvi").getPath(),
|
||||
file.getParentFile(), false)>0) {
|
||||
throw new IOException("Error executing dvi viewer");
|
||||
}
|
||||
}
|
||||
else if (nBackend==PDFTEX) {
|
||||
doTeXify(pdfTexify, file);
|
||||
if (externalApps.execute(ExternalApps.PDFVIEWER,
|
||||
new File(file.getParentFile(),file.getName()+".pdf").getPath(),
|
||||
file.getParentFile(), false)>0) {
|
||||
throw new IOException("Error executing pdf viewer");
|
||||
}
|
||||
}
|
||||
else if (nBackend==DVIPS) {
|
||||
doTeXify(dvipsTexify, file);
|
||||
if (externalApps.execute(ExternalApps.POSTSCRIPTVIEWER,
|
||||
new File(file.getParentFile(),file.getName()+".ps").getPath(),
|
||||
file.getParentFile(), false)>0) {
|
||||
throw new IOException("Error executing postscript viewer");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void doTeXify(String[] sAppList, File file) throws IOException {
|
||||
for (int i=0; i<sAppList.length; i++) {
|
||||
// Execute external application
|
||||
int nReturnCode = externalApps.execute(
|
||||
sAppList[i], file.getName(), file.getParentFile(), true);
|
||||
if (nReturnCode>0) {
|
||||
//throw new IOException("Error executing "+sAppList[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* W4LRegistration.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2009 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.2 (2009-03-26)
|
||||
*
|
||||
*/
|
||||
|
||||
package org.openoffice.da.comp.writer4latex;
|
||||
|
||||
import com.sun.star.lang.XMultiServiceFactory;
|
||||
import com.sun.star.lang.XSingleServiceFactory;
|
||||
import com.sun.star.registry.XRegistryKey;
|
||||
|
||||
import com.sun.star.comp.loader.FactoryHelper;
|
||||
|
||||
/** This class provides a static method to instantiate our uno components
|
||||
* on demand (__getServiceFactory()), and a static method to give
|
||||
* information about the components (__writeRegistryServiceInfo()).
|
||||
* Furthermore, it saves the XMultiServiceFactory provided to the
|
||||
* __getServiceFactory method for future reference by the componentes.
|
||||
*/
|
||||
public class W4LRegistration {
|
||||
|
||||
public static XMultiServiceFactory xMultiServiceFactory;
|
||||
|
||||
/**
|
||||
* Returns a factory for creating the service.
|
||||
* This method is called by the <code>JavaLoader</code>
|
||||
*
|
||||
* @return returns a <code>XSingleServiceFactory</code> for creating the
|
||||
* component
|
||||
*
|
||||
* @param implName the name of the implementation for which a
|
||||
* service is desired
|
||||
* @param multiFactory the service manager to be used if needed
|
||||
* @param regKey the registryKey
|
||||
*
|
||||
* @see com.sun.star.comp.loader.JavaLoader
|
||||
*/
|
||||
public static XSingleServiceFactory __getServiceFactory(String implName,
|
||||
XMultiServiceFactory multiFactory, XRegistryKey regKey) {
|
||||
xMultiServiceFactory = multiFactory;
|
||||
XSingleServiceFactory xSingleServiceFactory = null;
|
||||
if (implName.equals(Writer4LaTeX.__implementationName) ) {
|
||||
xSingleServiceFactory = FactoryHelper.getServiceFactory(Writer4LaTeX.class,
|
||||
Writer4LaTeX.__serviceName,
|
||||
multiFactory,
|
||||
regKey);
|
||||
}
|
||||
else if (implName.equals(ConfigurationDialog.__implementationName) ) {
|
||||
xSingleServiceFactory = FactoryHelper.getServiceFactory(ConfigurationDialog.class,
|
||||
ConfigurationDialog.__serviceName,
|
||||
multiFactory,
|
||||
regKey);
|
||||
}
|
||||
else if (implName.equals(LogViewerDialog.__implementationName) ) {
|
||||
xSingleServiceFactory = FactoryHelper.getServiceFactory(LogViewerDialog.class,
|
||||
LogViewerDialog.__serviceName,
|
||||
multiFactory,
|
||||
regKey);
|
||||
}
|
||||
|
||||
return xSingleServiceFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the service information into the given registry key.
|
||||
* This method is called by the <code>JavaLoader</code>
|
||||
* <p>
|
||||
* @return returns true if the operation succeeded
|
||||
* @param regKey the registryKey
|
||||
* @see com.sun.star.comp.loader.JavaLoader
|
||||
*/
|
||||
public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
|
||||
return
|
||||
FactoryHelper.writeRegistryServiceInfo(Writer4LaTeX.__implementationName,
|
||||
Writer4LaTeX.__serviceName, regKey) &
|
||||
FactoryHelper.writeRegistryServiceInfo(ConfigurationDialog.__implementationName,
|
||||
ConfigurationDialog.__serviceName, regKey) &
|
||||
FactoryHelper.writeRegistryServiceInfo(LogViewerDialog.__implementationName,
|
||||
LogViewerDialog.__serviceName, regKey);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,408 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* Writer4LaTeX.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2009 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.2 (2009-03-30)
|
||||
*
|
||||
*/
|
||||
|
||||
package org.openoffice.da.comp.writer4latex;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import com.sun.star.beans.PropertyValue;
|
||||
import com.sun.star.beans.XPropertyAccess;
|
||||
import com.sun.star.frame.XController;
|
||||
import com.sun.star.frame.XFrame;
|
||||
import com.sun.star.frame.XModel;
|
||||
import com.sun.star.frame.XStorable;
|
||||
import com.sun.star.lib.uno.helper.WeakBase;
|
||||
import com.sun.star.task.XStatusIndicator;
|
||||
import com.sun.star.task.XStatusIndicatorFactory;
|
||||
import com.sun.star.ui.dialogs.ExecutableDialogResults;
|
||||
import com.sun.star.ui.dialogs.XExecutableDialog;
|
||||
import com.sun.star.uno.UnoRuntime;
|
||||
import com.sun.star.uno.XComponentContext;
|
||||
|
||||
import org.openoffice.da.comp.w2lcommon.helper.MessageBox;
|
||||
import org.openoffice.da.comp.w2lcommon.helper.PropertyHelper;
|
||||
|
||||
/** This class implements the ui (dispatch) commands provided by Writer4LaTeX.
|
||||
* The actual processing is done by the three core classes <code>TeXify</code>,
|
||||
* <code>LaTeXImporter</code> and <code>BibTeXImporter</code>
|
||||
*/
|
||||
public final class Writer4LaTeX extends WeakBase
|
||||
implements com.sun.star.lang.XServiceInfo,
|
||||
com.sun.star.frame.XDispatchProvider,
|
||||
com.sun.star.lang.XInitialization,
|
||||
com.sun.star.frame.XDispatch {
|
||||
|
||||
private static final String PROTOCOL = "org.openoffice.da.writer4latex:";
|
||||
|
||||
// From constructor+initialization
|
||||
private final XComponentContext m_xContext;
|
||||
private XFrame m_xFrame;
|
||||
private XModel xModel = null;
|
||||
|
||||
// Global data
|
||||
private TeXify texify = null;
|
||||
private PropertyValue[] mediaProps = null;
|
||||
private String sBasePath = null;
|
||||
private String sBaseFileName = null;
|
||||
|
||||
public static final String __implementationName = Writer4LaTeX.class.getName();
|
||||
public static final String __serviceName = "com.sun.star.frame.ProtocolHandler";
|
||||
private static final String[] m_serviceNames = { __serviceName };
|
||||
|
||||
public Writer4LaTeX( XComponentContext xContext ) {
|
||||
m_xContext = xContext;
|
||||
}
|
||||
|
||||
// com.sun.star.lang.XInitialization:
|
||||
public void initialize( Object[] object )
|
||||
throws com.sun.star.uno.Exception {
|
||||
if ( object.length > 0 ) {
|
||||
// The first item is the current frame
|
||||
m_xFrame = (com.sun.star.frame.XFrame) UnoRuntime.queryInterface(
|
||||
com.sun.star.frame.XFrame.class, object[0]);
|
||||
// Get the model for the document from the frame
|
||||
XController xController = m_xFrame.getController();
|
||||
if (xController!=null) {
|
||||
xModel = xController.getModel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// com.sun.star.lang.XServiceInfo:
|
||||
public String getImplementationName() {
|
||||
return __implementationName;
|
||||
}
|
||||
|
||||
public boolean supportsService( String sService ) {
|
||||
int len = m_serviceNames.length;
|
||||
|
||||
for( int i=0; i < len; i++) {
|
||||
if (sService.equals(m_serviceNames[i]))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String[] getSupportedServiceNames() {
|
||||
return m_serviceNames;
|
||||
}
|
||||
|
||||
|
||||
// com.sun.star.frame.XDispatchProvider:
|
||||
public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,
|
||||
String sTargetFrameName, int iSearchFlags ) {
|
||||
if ( aURL.Protocol.compareTo(PROTOCOL) == 0 ) {
|
||||
if ( aURL.Path.compareTo("ProcessDocument") == 0 )
|
||||
return this;
|
||||
else if ( aURL.Path.compareTo("ProcessDirectly") == 0 )
|
||||
return this;
|
||||
else if ( aURL.Path.compareTo("ViewLog") == 0 )
|
||||
return this;
|
||||
else if ( aURL.Path.compareTo("UseBibTeX") == 0 )
|
||||
return this;
|
||||
else if ( aURL.Path.compareTo("ImportBibTeX") == 0 )
|
||||
return this;
|
||||
else if ( aURL.Path.compareTo("ImportLaTeX") == 0 )
|
||||
return this;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public com.sun.star.frame.XDispatch[] queryDispatches(
|
||||
com.sun.star.frame.DispatchDescriptor[] seqDescriptors ) {
|
||||
int nCount = seqDescriptors.length;
|
||||
com.sun.star.frame.XDispatch[] seqDispatcher =
|
||||
new com.sun.star.frame.XDispatch[seqDescriptors.length];
|
||||
|
||||
for( int i=0; i < nCount; ++i ) {
|
||||
seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,
|
||||
seqDescriptors[i].FrameName,
|
||||
seqDescriptors[i].SearchFlags );
|
||||
}
|
||||
return seqDispatcher;
|
||||
}
|
||||
|
||||
|
||||
// com.sun.star.frame.XDispatch:
|
||||
public void dispatch( com.sun.star.util.URL aURL,
|
||||
com.sun.star.beans.PropertyValue[] aArguments ) {
|
||||
if ( aURL.Protocol.compareTo(PROTOCOL) == 0 ) {
|
||||
if ( aURL.Path.compareTo("ProcessDocument") == 0 ) {
|
||||
if (updateLocation()) {
|
||||
if (updateMediaProperties()) {
|
||||
process();
|
||||
}
|
||||
}
|
||||
else {
|
||||
warnNotSaved();
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if ( aURL.Path.compareTo("ProcessDirectly") == 0 ) {
|
||||
if (updateLocation()) {
|
||||
if (mediaProps!=null || updateMediaProperties()) {
|
||||
process();
|
||||
}
|
||||
}
|
||||
else {
|
||||
warnNotSaved();
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if ( aURL.Path.compareTo("ViewLog") == 0 ) {
|
||||
viewLog();
|
||||
return;
|
||||
}
|
||||
else if ( aURL.Path.compareTo("UseBibTeX") == 0 ) {
|
||||
org.openoffice.da.comp.w2lcommon.helper.MessageBox msgBox = new org.openoffice.da.comp.w2lcommon.helper.MessageBox(m_xContext);
|
||||
msgBox.showMessage("Writer4LaTeX", "This feature has not been implemented yet");
|
||||
return;
|
||||
}
|
||||
else if ( aURL.Path.compareTo("ImportBibTeX") == 0 ) {
|
||||
org.openoffice.da.comp.w2lcommon.helper.MessageBox msgBox = new org.openoffice.da.comp.w2lcommon.helper.MessageBox(m_xContext);
|
||||
msgBox.showMessage("Writer4LaTeX", "This feature has not been implemented yet");
|
||||
return;
|
||||
}
|
||||
else if ( aURL.Path.compareTo("ImportLaTeX") == 0 ) {
|
||||
org.openoffice.da.comp.w2lcommon.helper.MessageBox msgBox = new org.openoffice.da.comp.w2lcommon.helper.MessageBox(m_xContext);
|
||||
msgBox.showMessage("Writer4LaTeX", "This feature has not been implemented yet");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addStatusListener( com.sun.star.frame.XStatusListener xControl,
|
||||
com.sun.star.util.URL aURL ) {
|
||||
}
|
||||
|
||||
public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,
|
||||
com.sun.star.util.URL aURL ) {
|
||||
}
|
||||
|
||||
// The actual commands...
|
||||
|
||||
private void process() {
|
||||
// Create a (somewhat coarse grained) status indicator/progress bar
|
||||
XStatusIndicatorFactory xFactory = (com.sun.star.task.XStatusIndicatorFactory)
|
||||
UnoRuntime.queryInterface(com.sun.star.task.XStatusIndicatorFactory.class, m_xFrame);
|
||||
XStatusIndicator xStatus = xFactory.createStatusIndicator();
|
||||
xStatus.start("Writer4LaTeX",10);
|
||||
xStatus.setValue(1); // At least we have started, that's 10% :-)
|
||||
|
||||
try {
|
||||
// Convert to LaTeX
|
||||
String sTargetUrl = sBasePath+sBaseFileName+".tex";
|
||||
XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, xModel);
|
||||
xStorable.storeToURL(sTargetUrl, mediaProps);
|
||||
}
|
||||
catch (com.sun.star.io.IOException e) {
|
||||
xStatus.end();
|
||||
MessageBox msgBox = new MessageBox(m_xContext, m_xFrame);
|
||||
msgBox.showMessage("Writer4LaTeX Error","Failed to export document to LaTeX");
|
||||
return;
|
||||
}
|
||||
|
||||
xStatus.setValue(6); // Export is finished, that's more than half :-)
|
||||
|
||||
// Get the backend from the media properties
|
||||
String sBackend = "generic";
|
||||
Object filterData = (new PropertyHelper(mediaProps)).get("FilterData");
|
||||
if (filterData instanceof PropertyValue[]) {
|
||||
Object backend = (new PropertyHelper((PropertyValue[])filterData)).get("backend");
|
||||
if (backend instanceof String) {
|
||||
sBackend = (String) backend;
|
||||
}
|
||||
}
|
||||
|
||||
if (texify==null) { texify = new TeXify(m_xContext); }
|
||||
File file = new File(urlToFile(sBasePath),sBaseFileName);
|
||||
|
||||
try {
|
||||
if (sBackend=="pdftex") {
|
||||
texify.process(file, TeXify.PDFTEX, true);
|
||||
}
|
||||
else if (sBackend=="dvips") {
|
||||
texify.process(file, TeXify.DVIPS, true);
|
||||
}
|
||||
else if (sBackend=="generic") {
|
||||
texify.process(file, TeXify.GENERIC, true);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
MessageBox msgBox = new MessageBox(m_xContext, m_xFrame);
|
||||
msgBox.showMessage("Writer4LaTeX Error",e.getMessage());
|
||||
}
|
||||
|
||||
xStatus.setValue(10); // The user will not really see this...
|
||||
xStatus.end();
|
||||
}
|
||||
|
||||
private void viewLog() {
|
||||
if (updateLocation()) {
|
||||
// Execute the log viewer dialog
|
||||
try {
|
||||
Object[] args = new Object[1];
|
||||
args[0] = sBasePath+sBaseFileName;
|
||||
Object dialog = m_xContext.getServiceManager()
|
||||
.createInstanceWithArgumentsAndContext(
|
||||
"org.openoffice.da.writer4latex.LogViewerDialog", args, m_xContext);
|
||||
XExecutableDialog xDialog = (XExecutableDialog)
|
||||
UnoRuntime.queryInterface(XExecutableDialog.class, dialog);
|
||||
if (xDialog.execute()==ExecutableDialogResults.OK) {
|
||||
// Closed with the close button
|
||||
}
|
||||
}
|
||||
catch (com.sun.star.uno.Exception e) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
warnNotSaved();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Some utility methods
|
||||
|
||||
private boolean updateMediaProperties() {
|
||||
// Create inital media properties
|
||||
mediaProps = new PropertyValue[2];
|
||||
mediaProps[0] = new PropertyValue();
|
||||
mediaProps[0].Name = "FilterName";
|
||||
mediaProps[0].Value = "org.openoffice.da.writer2latex";
|
||||
mediaProps[1] = new PropertyValue();
|
||||
mediaProps[1].Name = "Overwrite";
|
||||
mediaProps[1].Value = "true";
|
||||
|
||||
try {
|
||||
// Display options dialog
|
||||
Object dialog = m_xContext.getServiceManager()
|
||||
.createInstanceWithContext("org.openoffice.da.writer2latex.LaTeXOptionsDialog", m_xContext);
|
||||
|
||||
XPropertyAccess xPropertyAccess = (XPropertyAccess)
|
||||
UnoRuntime.queryInterface(XPropertyAccess.class, dialog);
|
||||
xPropertyAccess.setPropertyValues(mediaProps);
|
||||
|
||||
XExecutableDialog xDialog = (XExecutableDialog)
|
||||
UnoRuntime.queryInterface(XExecutableDialog.class, dialog);
|
||||
if (xDialog.execute()==ExecutableDialogResults.OK) {
|
||||
mediaProps = xPropertyAccess.getPropertyValues();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
mediaProps = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (com.sun.star.beans.UnknownPropertyException e) {
|
||||
// setPropertyValues will not fail..
|
||||
mediaProps = null;
|
||||
return false;
|
||||
}
|
||||
catch (com.sun.star.uno.Exception e) {
|
||||
// getServiceManager will not fail..
|
||||
mediaProps = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean updateLocation() {
|
||||
String sDocumentUrl = xModel.getURL();
|
||||
if (sDocumentUrl.length()!=0) {
|
||||
// Get the file name (without extension)
|
||||
File f = urlToFile(sDocumentUrl);
|
||||
sBaseFileName = f.getName();
|
||||
int iDot = sBaseFileName.lastIndexOf(".");
|
||||
if (iDot>-1) { // remove extension
|
||||
sBaseFileName = sBaseFileName.substring(0,iDot);
|
||||
}
|
||||
sBaseFileName=makeTeXSafe(sBaseFileName);
|
||||
|
||||
// Get the path
|
||||
int iSlash = sDocumentUrl.lastIndexOf("/");
|
||||
if (iSlash>-1) {
|
||||
sBasePath = sDocumentUrl.substring(0,iSlash+1);
|
||||
}
|
||||
else {
|
||||
sBasePath = "";
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void warnNotSaved() {
|
||||
MessageBox msgBox = new MessageBox(m_xContext, m_xFrame);
|
||||
msgBox.showMessage("Document not saved!","Please save the document before processing the file");
|
||||
}
|
||||
|
||||
private String makeTeXSafe(String sArgument) {
|
||||
String sResult = "";
|
||||
for (int i=0; i<sArgument.length(); i++) {
|
||||
char c = sArgument.charAt(i);
|
||||
if ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='-' || c=='.') {
|
||||
sResult += Character.toString(c);
|
||||
}
|
||||
// TODO: Create replacement table for other latin characters..
|
||||
else if (c==' ') { sResult += "-"; }
|
||||
else if (c=='\u00c6') { sResult += "AE"; }
|
||||
else if (c=='\u00d8') { sResult += "OE"; }
|
||||
else if (c=='\u00c5') { sResult += "AA"; }
|
||||
else if (c=='\u00e6') { sResult += "ae"; }
|
||||
else if (c=='\u00f8') { sResult += "oe"; }
|
||||
else if (c=='\u00e5') { sResult += "aa"; }
|
||||
}
|
||||
if (sResult.length()==0) { return "writer4latex"; }
|
||||
else { return sResult; }
|
||||
}
|
||||
|
||||
private File urlToFile(String sUrl) {
|
||||
try {
|
||||
return new File(new URI(sUrl));
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
return new File(".");
|
||||
}
|
||||
}
|
||||
|
||||
/*private String urlToPath(String sUrl) {
|
||||
try {
|
||||
return (new File(new URI(sUrl))).getCanonicalPath();
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
return ".";
|
||||
}
|
||||
catch (IOException e) {
|
||||
return ".";
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue