Second draft of w2x toolbar

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@185 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2014-10-22 11:57:55 +00:00
parent a50dad69f4
commit 934dd30696
20 changed files with 365 additions and 222 deletions

View file

@ -0,0 +1,253 @@
/************************************************************************
*
* UNOPublisher.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-2014 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.6 (2014-10-22)
*
*/
package org.openoffice.da.comp.w2lcommon.filter;
import java.io.IOException;
import org.openoffice.da.comp.w2lcommon.helper.MessageBox;
import writer2latex.util.Misc;
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.io.XInputStream;
import com.sun.star.task.XStatusIndicator;
import com.sun.star.task.XStatusIndicatorFactory;
import com.sun.star.ucb.XSimpleFileAccess2;
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 com.sun.star.util.XModifiable;
/** This class converts an open office document to another format
*/
public class UNOPublisher {
public enum TargetFormat { xhtml, xhtml11, xhtml_mathml, html5, epub, latex };
private XComponentContext xContext;
private 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
*/
public UNOPublisher(XComponentContext xContext, XFrame xFrame) {
this.xContext = xContext;
this.xFrame = xFrame;
// Get the model for the document from the frame
XController xController = xFrame.getController();
if (xController!=null) {
xModel = xController.getModel();
}
}
/** Publish the document associated with this <code>UNOPublisher</code>. This involves four 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.
*
* @param format the target format
* @return the URL of the converted document, or null if the document was not converted
*/
public String 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.setValue(1); // At least we have started, that's 10% :-)
try {
// Save document if required
saveDocument();
xStatus.setValue(4);
// Convert to desired format
UNOConverter converter = new UNOConverter(mediaProps, xContext);
// Initialize the file access (to read the office document)
XSimpleFileAccess2 sfa2 = null;
try {
Object sfaObject = xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.ucb.SimpleFileAccess", xContext);
sfa2 = (XSimpleFileAccess2) UnoRuntime.queryInterface(XSimpleFileAccess2.class, sfaObject);
}
catch (com.sun.star.uno.Exception e) {
// failed to get SimpleFileAccess service (should not happen)
}
XInputStream xis = sfa2.openFileRead(xModel.getURL());
converter.convert(xis);
xis.closeInput();
}
catch (IOException e) {
xStatus.end();
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage("Writer2xhtml","Error: Failed to export document");
return null;
}
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;
}
xStatus.setValue(10); // Export is finished (The user will usually not see this...)
xStatus.end();
return getTargetURL(format);
}
return null;
}
private 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");
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)");
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");
return false;
}
return true;
}
private boolean saveDocument() {
XModifiable xModifiable = (XModifiable) UnoRuntime.queryInterface(XModifiable.class, xModel);
if (xModifiable.isModified()) { // The document is modified and need to be saved
XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, xModel);
try {
xStorable.store();
} catch (com.sun.star.io.IOException e) {
return false;
}
}
return true;
}
// Some utility methods
private String getTargetURL(TargetFormat format) {
return Misc.removeExtension(xModel.getURL())+getTargetExtension(format);
}
private void prepareMediaProperties(TargetFormat format) {
// Create inital media properties
mediaProps = new PropertyValue[2];
mediaProps[0] = new PropertyValue();
mediaProps[0].Name = "FilterName";
mediaProps[0].Value = getFilterName(format);
mediaProps[1] = new PropertyValue();
mediaProps[1].Name = "URL";
mediaProps[1].Value = getTargetURL(format);
}
private boolean updateMediaProperties(TargetFormat format) {
prepareMediaProperties(format);
try {
// Display options dialog
Object dialog = xContext.getServiceManager()
.createInstanceWithContext(getDialogName(format), 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 static String getTargetExtension(TargetFormat format) {
switch (format) {
case xhtml: return ".html";
case xhtml11: return ".xhtml";
case xhtml_mathml: return ".xhtml";
case html5: return ".html";
case epub: return ".epub";
case latex: return ".tex";
default: return "";
}
}
private static String getDialogName(TargetFormat format) {
switch (format) {
case xhtml: return "org.openoffice.da.comp.writer2xhtml.XhtmlOptionsDialog";
case xhtml11: return "org.openoffice.da.comp.writer2xhtml.XhtmlOptionsDialog";
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";
default: return "";
}
}
private static String getFilterName(TargetFormat format) {
switch (format) {
case xhtml: return "org.openoffice.da.writer2xhtml";
case xhtml11: return "org.openoffice.da.writer2xhtml11";
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";
default: return "";
}
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2014-10-09)
* Version 1.6 (2014-10-21)
*
*/
@ -33,27 +33,20 @@ 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.XDispatchHelper;
import com.sun.star.frame.XDispatchProvider;
import com.sun.star.frame.XFrame;
import com.sun.star.frame.XModel;
import com.sun.star.frame.XStorable;
import com.sun.star.io.XInputStream;
import com.sun.star.lang.XComponent;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.task.XStatusIndicator;
import com.sun.star.task.XStatusIndicatorFactory;
import com.sun.star.ucb.XSimpleFileAccess2;
import com.sun.star.ui.dialogs.ExecutableDialogResults;
import com.sun.star.ui.dialogs.XExecutableDialog;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XModifiable;
import org.openoffice.da.comp.w2lcommon.filter.UNOConverter;
import org.openoffice.da.comp.w2lcommon.filter.UNOPublisher;
import org.openoffice.da.comp.w2lcommon.filter.UNOPublisher.TargetFormat;
import org.openoffice.da.comp.w2lcommon.helper.MessageBox;
import writer2latex.util.Misc;
/** This class implements the ui (dispatch) commands provided by Writer2xhtml.
*/
@ -65,56 +58,19 @@ public final class Writer2xhtml extends WeakBase
private static final String PROTOCOL = "org.openoffice.da.writer2xhtml:";
private enum TargetFormat { xhtml, xhtml11, xhtml_mathml, html5, epub };
// From constructor+initialization
private final XComponentContext m_xContext;
private XFrame m_xFrame;
private XModel xModel = null;
private UNOPublisher unoPublisher = null;
// Global data
private PropertyValue[] mediaProps = null;
public static final String __implementationName = Writer2xhtml.class.getName();
public static final String __serviceName = "com.sun.star.frame.ProtocolHandler";
private static final String[] m_serviceNames = { __serviceName };
// TODO: These should be configurable
private TargetFormat xhtmlFormat = TargetFormat.xhtml_mathml;
private TargetFormat epubFormat = TargetFormat.epub;
private String getTargetExtension(TargetFormat format) {
switch (format) {
case xhtml: return ".html";
case xhtml11: return ".xhtml";
case xhtml_mathml: return ".xhtml";
case html5: return ".html";
case epub: return ".epub";
default: return "";
}
}
private String getDialogName(TargetFormat format) {
switch (format) {
case xhtml: return "org.openoffice.da.comp.writer2xhtml.XhtmlOptionsDialog";
case xhtml11: return "org.openoffice.da.comp.writer2xhtml.XhtmlOptionsDialog";
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";
default: return "";
}
}
private String getFilterName(TargetFormat format) {
switch (format) {
case xhtml: return "org.openoffice.da.writer2xhtml";
case xhtml11: return "org.openoffice.da.writer2xhtml11";
case xhtml_mathml: return "org.openoffice.da.writer2xhtml.mathml";
case html5: return "org.openoffice.da.writer2xhtml5";
case epub: return "org.openoffice.da.writer2xhtml.epub";
default: return "";
}
}
private TargetFormat epubFormat = TargetFormat.epub;
public Writer2xhtml(XComponentContext xContext) {
m_xContext = xContext;
@ -127,11 +83,6 @@ public final class Writer2xhtml extends WeakBase
// 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();
}
}
}
@ -163,6 +114,8 @@ public final class Writer2xhtml extends WeakBase
return this;
else if ( aURL.Path.compareTo("PublishAsEPUB") == 0 )
return this;
else if ( aURL.Path.compareTo("EditEPUBDocumentProperties") == 0 )
return this;
}
return null;
}
@ -186,7 +139,6 @@ public final class Writer2xhtml extends WeakBase
public void dispatch( com.sun.star.util.URL aURL,
com.sun.star.beans.PropertyValue[] aArguments ) {
if ( aURL.Protocol.compareTo(PROTOCOL) == 0 ) {
System.out.println(aURL.Protocol+" "+aURL.Path);
if ( aURL.Path.compareTo("PublishAsXHTML") == 0 ) {
publish(xhtmlFormat);
return;
@ -195,6 +147,10 @@ public final class Writer2xhtml extends WeakBase
publish(epubFormat);
return;
}
else if ( aURL.Path.compareTo("EditEPUBDocumentProperties") == 0 ) {
editDocumentProperties();
return;
}
}
}
@ -207,141 +163,43 @@ public final class Writer2xhtml extends WeakBase
}
// The actual commands...
private void editDocumentProperties() {
Object dialog;
try {
dialog = m_xContext.getServiceManager().createInstanceWithContext("org.openoffice.da.writer2xhtml.EpubMetadataDialog", m_xContext);
XExecutableDialog xDialog = (XExecutableDialog) UnoRuntime.queryInterface(XExecutableDialog.class, dialog);
xDialog.execute();
// Dispose the dialog after execution (to free up the memory)
XComponent xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, dialog);
if (xComponent!=null) {
xComponent.dispose();
}
} catch (Exception e) {
// Failed to get dialog
}
}
private void publish(TargetFormat format) {
if (saveDocument() && updateMediaProperties(xhtmlFormat)) {
// 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("Writer2xhtml",10);
xStatus.setValue(1); // At least we have started, that's 10% :-)
System.out.println("Document location "+xModel.getURL());
try {
// Convert to desired format
UNOConverter converter = new UNOConverter(mediaProps, m_xContext);
// Initialize the file access (to read the office document)
XSimpleFileAccess2 sfa2 = null;
try {
Object sfaObject = m_xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.ucb.SimpleFileAccess", m_xContext);
sfa2 = (XSimpleFileAccess2) UnoRuntime.queryInterface(XSimpleFileAccess2.class, sfaObject);
if (unoPublisher==null) {
unoPublisher = new UNOPublisher(m_xContext,m_xFrame);
}
String sTargetURL = unoPublisher.publish(format);
// Display the result
File file = urlToFile(sTargetURL);
if (file.exists()) {
// Open the file in the default application on this system (if any)
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(file);
} catch (IOException e) {
System.err.println(e.getMessage());
}
catch (com.sun.star.uno.Exception e) {
// failed to get SimpleFileAccess service (should not happen)
}
XInputStream xis = sfa2.openFileRead(xModel.getURL());
converter.convert(xis);
xis.closeInput();
}
catch (IOException | com.sun.star.uno.Exception e) {
xStatus.end();
MessageBox msgBox = new MessageBox(m_xContext, m_xFrame);
msgBox.showMessage("Writer2xhtml Error","Failed to export document");
return;
}
xStatus.setValue(6); // Export is finished, that's more than half :-)
if (xModel.getURL().startsWith("file:")) {
File file = urlToFile(getTargetURL(format));
if (file.exists()) {
// Open the file in the default application on this system (if any)
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(file);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
else {
MessageBox msgBox = new MessageBox(m_xContext, m_xFrame);
msgBox.showMessage("Writer2xhtml Error","Failed to open exported document");
}
}
else {
MessageBox msgBox = new MessageBox(m_xContext, m_xFrame);
msgBox.showMessage("Writer2xhtml Error","Cannot open document on the location "+getTargetURL(format));
}
xStatus.setValue(10); // The user will usually not see this...
xStatus.end();
}
}
}
private boolean saveDocument() {
String sDocumentUrl = xModel.getURL();
if (sDocumentUrl.length()!=0) { // The document has a location
XModifiable xModifiable = (XModifiable) UnoRuntime.queryInterface(XModifiable.class, xModel);
if (xModifiable.isModified()) { // It is modified and need to be saved
XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, xModel);
try {
xStorable.store();
} catch (com.sun.star.io.IOException e) {
return false;
}
}
}
else { // No location, ask the user to save the document
else {
MessageBox msgBox = new MessageBox(m_xContext, m_xFrame);
msgBox.showMessage("Document not saved!","Please save the document before publishing the file");
return false;
}
return true;
}
// Some utility methods
private String getTargetURL(TargetFormat format) {
return Misc.removeExtension(xModel.getURL())+getTargetExtension(format);
}
private void prepareMediaProperties(TargetFormat format) {
// Create inital media properties
mediaProps = new PropertyValue[2];
mediaProps[0] = new PropertyValue();
mediaProps[0].Name = "FilterName";
mediaProps[0].Value = getFilterName(format);
mediaProps[1] = new PropertyValue();
mediaProps[1].Name = "URL";
mediaProps[1].Value = getTargetURL(format);
}
private boolean updateMediaProperties(TargetFormat format) {
prepareMediaProperties(format);
try {
// Display options dialog
Object dialog = m_xContext.getServiceManager()
.createInstanceWithContext(getDialogName(format), 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;
msgBox.showMessage("Writer2xhtml","Error: Failed to open exported document");
}
}
@ -353,6 +211,8 @@ public final class Writer2xhtml extends WeakBase
return new File(".");
}
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2014-10-06)
* Version 1.6 (2014-10-22)
*
*/
@ -33,7 +33,7 @@ public class ConverterFactory {
// Version information
private static final String VERSION = "1.5.1";
private static final String DATE = "2014-10-06";
private static final String DATE = "2014-10-22";
/** Return the Writer2LaTeX version in the form
* (major version).(minor version).(patch level)<br/>

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2014-10-10)
* Version 1.4 (2014-10-21)
*
*/
@ -33,7 +33,6 @@ import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.Math;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.text.Collator;
@ -44,7 +43,6 @@ import java.util.Arrays;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
//import java.util.Hashtable;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@ -303,14 +301,7 @@ public class Misc{
* @return the file name
*/
public static final String getFileName(String sURL) {
try {
URI uri = new URI(sURL);
String sPath = uri.getPath();
return sPath.substring(sPath.lastIndexOf('/')+1);
} catch (URISyntaxException e) {
e.printStackTrace();
return "";
}
return sURL.substring(sURL.lastIndexOf('/')+1);
}
/** Get the file extension from an URL