Use UNOPublisher in the w2l toolbar

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@203 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2014-11-03 19:52:41 +00:00
parent 88c3a7295d
commit 435bbee042
8 changed files with 475 additions and 602 deletions

View file

@ -20,12 +20,13 @@
*
* All Rights Reserved.
*
* Version 1.6 (2014-10-22)
* Version 1.6 (2014-11-03)
*
*/
package org.openoffice.da.comp.w2lcommon.filter;
import java.io.IOException;
import java.util.regex.Pattern;
import org.openoffice.da.comp.w2lcommon.helper.MessageBox;
import writer2latex.util.Misc;
@ -52,19 +53,23 @@ public class UNOPublisher {
public enum TargetFormat { xhtml, xhtml11, xhtml_mathml, html5, epub, latex };
private XComponentContext xContext;
private XFrame xFrame;
private XModel xModel=null;
private String sAppName;
protected XComponentContext xContext;
protected XFrame xFrame;
private XModel xModel = null;
private PropertyValue[] mediaProps = null;
/** Create a new <code>UNOPublisher</code> based on a loaded office document
*
* @param xContext the component context from which new UNO services are instantiated
* @param xFrame the current frame
* @param sAppName the name of the application using the <code>UNOPublisher</code>
*/
public UNOPublisher(XComponentContext xContext, XFrame xFrame) {
public UNOPublisher(XComponentContext xContext, XFrame xFrame, String sAppName) {
this.xContext = xContext;
this.xFrame = xFrame;
this.sAppName = sAppName;
// Get the model for the document from the frame
XController xController = xFrame.getController();
if (xController!=null) {
@ -72,28 +77,29 @@ public class UNOPublisher {
}
}
/** Publish the document associated with this <code>UNOPublisher</code>. This involves four steps:
/** Publish the document associated with this <code>UNOPublisher</code>. This involves five steps:
* (1) Check that the document is saved in the local file system.
* (2) Display the options dialog.
* (3) Save the document (if the modified flag is true).
* (4) Convert the document.
* (5) Post process the document, e.g. displaying the result
*
* @param format the target format
* @return the URL of the converted document, or null if the document was not converted
* @return true if the publishing was succesful
*/
public String publish(TargetFormat format) {
public boolean publish(TargetFormat format) {
if (documentSaved() && updateMediaProperties(format)) {
// Create a (somewhat coarse grained) status indicator/progress bar
XStatusIndicatorFactory xFactory = (com.sun.star.task.XStatusIndicatorFactory)
UnoRuntime.queryInterface(com.sun.star.task.XStatusIndicatorFactory.class, xFrame);
XStatusIndicator xStatus = xFactory.createStatusIndicator();
xStatus.start("Writer2xhtml",10);
xStatus.start(sAppName,10);
xStatus.setValue(1); // At least we have started, that's 10% :-)
try {
// Save document if required
saveDocument();
xStatus.setValue(4);
xStatus.setValue(4); // Document saved, that's 40%
// Convert to desired format
UNOConverter converter = new UNOConverter(mediaProps, xContext);
@ -114,39 +120,82 @@ public class UNOPublisher {
catch (IOException e) {
xStatus.end();
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage("Writer2xhtml","Error: Failed to export document");
return null;
msgBox.showMessage(sAppName,"Error: Failed to export document");
return false;
}
catch (com.sun.star.uno.Exception e) {
xStatus.end();
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage("Writer2xhtml","Error: Failed to export document");
return null;
msgBox.showMessage(sAppName,"Error: Failed to export document");
return false;
}
xStatus.setValue(7); // Document is converted, that's 70%
postProcess(getTargetURL(format));
xStatus.setValue(10); // Export is finished (The user will usually not see this...)
xStatus.end();
return getTargetURL(format);
return true;
}
return null;
return false;
}
private boolean documentSaved() {
/** Filter the file name to avoid unwanted characters
*
* @param sFileName the original file name
* @return the filtered file name
*/
protected String filterFileName(String sFileName) {
return sFileName;
}
/** Post process the media properties after displaying the dialog
*
* @param mediaProps the media properties as set by the dialog
* @return the updated media properties
*/
protected PropertyValue[] postProcessMediaProps(PropertyValue[] mediaProps) {
return mediaProps;
}
/** Post process the document after conversion.
*
* @param format URL of the converted document
*/
protected void postProcess(String sTargetURL) {
}
/** Check that the document is saved in a location, we can use
*
* @return true if everthing is o.k.
*/
public boolean documentSaved() {
String sDocumentUrl = xModel.getURL();
if (sDocumentUrl.length()==0) { // The document has no location
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage("Writer2xhtml","Please save the document before publishing the file");
msgBox.showMessage(sAppName,"Please save the document before publishing the file");
return false;
}
else if (!".odt".equals(Misc.getFileExtension(sDocumentUrl)) && !".fodt".equals(Misc.getFileExtension(sDocumentUrl))) {
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage("Writer2xhtml","Please save the document in OpenDocument format (.odt)");
msgBox.showMessage(sAppName,"Please save the document in OpenDocument format (.odt)");
return false;
}
else if (!sDocumentUrl.startsWith("file:")) {
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage("Writer2xhtml","Please save the document in the local file system");
msgBox.showMessage(sAppName,"Please save the document in the local file system");
return false;
}
else if (System.getProperty("os.name").startsWith("Windows")) {
// Avoid UNC paths (LaTeX does not like them)
Pattern windowsPattern = Pattern.compile("^file:///[A-Za-z][|:].*");
if (!windowsPattern.matcher(sDocumentUrl).matches()) {
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage(sAppName,
"Please save the document on a location with a drive name");
return false;
}
}
return true;
}
@ -164,8 +213,33 @@ public class UNOPublisher {
}
// Some utility methods
private String getTargetURL(TargetFormat format) {
return Misc.removeExtension(xModel.getURL())+getTargetExtension(format);
/** Get the target path (or null if the document is not saved)
*
* @return the path
*/
public String getTargetPath() {
if (xModel.getURL().length()>0) {
String sBaseURL = Misc.removeExtension(xModel.getURL());
return Misc.getPath(sBaseURL);
}
return null;
}
/** Get the target file name (or null if the document is not saved)
*
* @return the file name
*/
public String getTargetFileName() {
if (xModel.getURL().length()>0) {
String sBaseURL = Misc.removeExtension(xModel.getURL());
return filterFileName(Misc.getFileName(sBaseURL));
}
return null;
}
protected String getTargetURL(TargetFormat format) {
return getTargetPath()+getTargetFileName()+getTargetExtension(format);
}
private void prepareMediaProperties(TargetFormat format) {
@ -194,7 +268,8 @@ public class UNOPublisher {
XExecutableDialog xDialog = (XExecutableDialog)
UnoRuntime.queryInterface(XExecutableDialog.class, dialog);
if (xDialog.execute()==ExecutableDialogResults.OK) {
mediaProps = xPropertyAccess.getPropertyValues();
mediaProps = postProcessMediaProps(xPropertyAccess.getPropertyValues());
return true;
}
else {
@ -233,7 +308,7 @@ public class UNOPublisher {
case xhtml_mathml: return "org.openoffice.da.comp.writer2xhtml.XhtmlOptionsDialogMath";
case html5: return "org.openoffice.da.comp.writer2xhtml.XhtmlOptionsDialogMath";
case epub: return "org.openoffice.da.comp.writer2xhtml.EpubOptionsDialog";
case latex: return "org.openoffice.da.comp.writer2xhtml.LaTeXOptionsDialog";
case latex: return "org.openoffice.da.comp.writer2latex.LaTeXOptionsDialog";
default: return "";
}
}
@ -245,7 +320,7 @@ public class UNOPublisher {
case xhtml_mathml: return "org.openoffice.da.writer2xhtml.mathml";
case html5: return "org.openoffice.da.writer2xhtml5";
case epub: return "org.openoffice.da.writer2xhtml.epub";
case latex: return "org.openoffice.da.writer2latex.latex";
case latex: return "org.openoffice.da.writer2latex";
default: return "";
}
}