Support EPUB style resources (embed images and fonts used in custom CSS file)

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@108 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2011-06-07 09:01:15 +00:00
parent 2ad8f3b7a2
commit ef05312e21
14 changed files with 342 additions and 97 deletions

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2010 by Henrik Just
* Copyright: 2002-2011 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2010-12-09)
* Version 1.2 (2011-06-06)
*
*/
@ -79,13 +79,13 @@ public abstract class ConfigurationDialogBase extends WeakBase implements XConta
protected XComponentContext xContext;
// File picker wrapper
private FilePicker filePicker = null;
protected FilePicker filePicker = null;
// UNO simple file access service
protected XSimpleFileAccess2 sfa2 = null;
// UNO path substitution
private XStringSubstitution xPathSub = null;
protected XStringSubstitution xPathSub = null;
// The configuration implementation
protected Config config;
@ -287,6 +287,38 @@ public abstract class ConfigurationDialogBase extends WeakBase implements XConta
config.setOption(sConfigName, sConfigValues[dlg.getListBoxSelectedItem(sListBoxName)]);
}
// Method to get a named dialog
protected XDialog getDialog(String sDialogName) {
XMultiComponentFactory xMCF = xContext.getServiceManager();
try {
Object provider = xMCF.createInstanceWithContext(
"com.sun.star.awt.DialogProvider2", xContext);
XDialogProvider2 xDialogProvider = (XDialogProvider2)
UnoRuntime.queryInterface(XDialogProvider2.class, provider);
String sDialogUrl = "vnd.sun.star.script:"+sDialogName+"?location=application";
return xDialogProvider.createDialog(sDialogUrl);
}
catch (Exception e) {
return null;
}
}
// Method to display delete dialog
protected boolean deleteItem(String sName) {
XDialog xDialog=getDialog(getDialogLibraryName()+".DeleteDialog");
if (xDialog!=null) {
DialogAccess ddlg = new DialogAccess(xDialog);
String sLabel = ddlg.getLabelText("DeleteLabel");
sLabel = sLabel.replaceAll("%s", sName);
ddlg.setLabelText("DeleteLabel", sLabel);
boolean bDelete = xDialog.execute()==ExecutableDialogResults.OK;
xDialog.endExecute();
return bDelete;
}
return false;
}
}
protected abstract class CustomFileHandler extends PageHandler {
@ -373,7 +405,7 @@ public abstract class ConfigurationDialogBase extends WeakBase implements XConta
// Helpers for sfa2
// Checks that the file exists
private boolean fileExists(String sFileName) {
protected boolean fileExists(String sFileName) {
try {
return sfa2!=null && sfa2.exists(sFileName);
}
@ -385,7 +417,7 @@ public abstract class ConfigurationDialogBase extends WeakBase implements XConta
}
// Delete a file if it exists, return true on success
private boolean killFile(String sFileName) {
protected boolean killFile(String sFileName) {
try {
if (sfa2!=null && sfa2.exists(sFileName)) {
sfa2.kill(sFileName);
@ -453,35 +485,6 @@ public abstract class ConfigurationDialogBase extends WeakBase implements XConta
protected abstract class UserListPageHandler extends PageHandler {
// Methods to handle user controlled lists
protected XDialog getDialog(String sDialogName) {
XMultiComponentFactory xMCF = xContext.getServiceManager();
try {
Object provider = xMCF.createInstanceWithContext(
"com.sun.star.awt.DialogProvider2", xContext);
XDialogProvider2 xDialogProvider = (XDialogProvider2)
UnoRuntime.queryInterface(XDialogProvider2.class, provider);
String sDialogUrl = "vnd.sun.star.script:"+sDialogName+"?location=application";
return xDialogProvider.createDialog(sDialogUrl);
}
catch (Exception e) {
return null;
}
}
private boolean deleteItem(String sName) {
XDialog xDialog=getDialog(getDialogLibraryName()+".DeleteDialog");
if (xDialog!=null) {
DialogAccess ddlg = new DialogAccess(xDialog);
String sLabel = ddlg.getLabelText("DeleteLabel");
sLabel = sLabel.replaceAll("%s", sName);
ddlg.setLabelText("DeleteLabel", sLabel);
boolean bDelete = xDialog.execute()==ExecutableDialogResults.OK;
xDialog.endExecute();
return bDelete;
}
return false;
}
protected boolean deleteCurrentItem(DialogAccess dlg, String sListName) {
String[] sItems = dlg.getListBoxStringItemList(sListName);
short nSelected = dlg.getListBoxSelectedItem(sListName);

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.2 (2011-01-15)
* Version 1.2 (2011-06-07)
*
*/
@ -38,6 +38,7 @@ import com.sun.star.io.XOutputStream;
import com.sun.star.ucb.CommandAbortedException;
import com.sun.star.ucb.XSimpleFileAccess2;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XStringSubstitution;
@ -109,7 +110,7 @@ public class FilterDataParser {
PropertyHelper props = new PropertyHelper(filterData);
// Get the special properties TemplateURL, StyleSheetURL, Resources, ConfigURL and AutoCreate
// Get the special properties TemplateURL, StyleSheetURL, ResourceURL, Resources, ConfigURL and AutoCreate
Object tpl = props.get("TemplateURL");
String sTemplate = null;
if (tpl!=null && AnyConverter.isString(tpl)) {
@ -132,7 +133,19 @@ public class FilterDataParser {
}
}
// This property accepts a semicolon separated list of <mime type>!<file name>!<URL> (not very elegant)
// This property accepts an URL pointing to a folder containing the resources to include
Object resourcedir = props.get("ResourceURL");
String sResourceURL = null;
if (resourcedir!=null && AnyConverter.isString(resourcedir)) {
try {
sResourceURL = substituteVariables(AnyConverter.toString(resourcedir));
}
catch (com.sun.star.lang.IllegalArgumentException e) {
// Failed to convert to String; should not happen - ignore
}
}
// This property accepts a semicolon separated list of <URL>[[::<file name>]::<mime type>]
Object resources = props.get("Resources");
String[] sResources = null;
if (resources!=null && AnyConverter.isString(resources)) {
@ -218,33 +231,70 @@ public class FilterDataParser {
}
}
// Load the resource from the specified folder URL, if any
if (sfa2!=null && sResourceURL!=null && sResourceURL.length()>0) {
String[] sURLs;
try {
sURLs = sfa2.getFolderContents(sResourceURL, false); // do not include folders
for (String sURL : sURLs) {
XInputStream xIs = sfa2.openFileRead(sURL);
if (xIs!=null) {
String sFileName = sURL.substring(sURL.lastIndexOf('/')+1);
InputStream is = new XInputStreamToInputStreamAdapter(xIs);
converter.readResource(is,sFileName,null);
is.close();
xIs.closeInput();
}
}
} catch (IOException e) {
// ignore
} catch (CommandAbortedException e1) {
// ignore
} catch (Exception e1) {
// ignore
}
}
// Load the resources from the specified URLs, if any
if (sfa2!=null && sResources!=null) {
for (String sResource : sResources) {
try {
String[] sParts = sResource.split("!");
// Format is <URL>[[::<file name>]::<mime type>]
String[] sParts = sResource.split("::");
if (sParts.length>0) {
String sURL=sParts[0];
String sFileName;
String sMediaType=null;
if (sParts.length==3) {
// Format is <mime type>!<file name>!<URL>
XInputStream xIs = sfa2.openFileRead(sParts[2]);
sFileName = sParts[1];
sMediaType = sParts[2];
}
else {
sFileName = sURL.substring(sURL.lastIndexOf('/')+1);
if (sParts.length==2) {
sMediaType = sParts[1];
}
}
try {
XInputStream xIs = sfa2.openFileRead(sURL);
if (xIs!=null) {
InputStream is = new XInputStreamToInputStreamAdapter(xIs);
converter.readResource(is, sParts[1], sParts[0]);
converter.readResource(is, sFileName, sMediaType);
is.close();
xIs.closeInput();
}
} // otherwise wrong format, ignore
}
catch (IOException e) {
// ignore
}
catch (NotConnectedException e) {
// ignore
}
catch (CommandAbortedException e) {
// ignore
}
catch (com.sun.star.uno.Exception e) {
// ignore
} // otherwise wrong format, ignore
}
catch (IOException e) {
// ignore
}
catch (NotConnectedException e) {
// ignore
}
catch (CommandAbortedException e) {
// ignore
}
catch (com.sun.star.uno.Exception e) {
// ignore
}
}
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.2 (2011-02-23)
* Version 1.2 (2011-06-07)
*
*/
@ -393,21 +393,39 @@ public abstract class OptionsDialogBase extends DialogBase implements
short nConfig = getListBoxSelectedItem("Config");
int nStdConfigs = getListBoxStringItemList("Config").length - sConfigNames.length;
if (nConfig>=nStdConfigs) { // only handle registry configurations
int i = nConfig-nStdConfigs;
try {
Object config = xNameAccess.getByName(sConfigNames[i]);
XPropertySet xCfgProps = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class,config);
MacroExpander expander = new MacroExpander(xContext);
filterData.put("ConfigURL",expander.expandMacros(XPropertySetHelper.getPropertyValueAsString(xCfgProps,"ConfigURL")));
filterData.put("TemplateURL",expander.expandMacros(XPropertySetHelper.getPropertyValueAsString(xCfgProps,"TargetTemplateURL")));
filterData.put("StyleSheetURL",expander.expandMacros(XPropertySetHelper.getPropertyValueAsString(xCfgProps,"StyleSheetURL")));
// TODO: Resources...
XPropertySetHelper.setPropertyValue(xProps,"ConfigName",sConfigNames[i]);
bFound = true;
}
catch (Exception e) {
}
int i = nConfig-nStdConfigs;
try {
Object config = xNameAccess.getByName(sConfigNames[i]);
XPropertySet xCfgProps = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class,config);
MacroExpander expander = new MacroExpander(xContext);
filterData.put("ConfigURL",expander.expandMacros(XPropertySetHelper.getPropertyValueAsString(xCfgProps,"ConfigURL")));
filterData.put("TemplateURL",expander.expandMacros(XPropertySetHelper.getPropertyValueAsString(xCfgProps,"TargetTemplateURL")));
filterData.put("StyleSheetURL",expander.expandMacros(XPropertySetHelper.getPropertyValueAsString(xCfgProps,"StyleSheetURL")));
// The resources are provided as a set
Object resources = XPropertySetHelper.getPropertyValue(xCfgProps,"Resources");
XNameAccess xResourceNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,resources);
if (xResourceNameAccess!=null) {
StringBuffer buf = new StringBuffer();
String[] sResourceNames = xResourceNameAccess.getElementNames();
for (String sName : sResourceNames) {
Object resource = xResourceNameAccess.getByName(sName);
XPropertySet xResourceProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,resource);
String sURL = expander.expandMacros(XPropertySetHelper.getPropertyValueAsString(xResourceProps,"URL"));
String sFileName = expander.expandMacros(XPropertySetHelper.getPropertyValueAsString(xResourceProps,"FileName"));
String sMediaType = expander.expandMacros(XPropertySetHelper.getPropertyValueAsString(xResourceProps,"MediaType"));
if (buf.length()>0) { buf.append(';'); }
buf.append(sURL).append("::").append(sFileName).append("::").append(sMediaType);
}
filterData.put("Resources",buf.toString());
XPropertySetHelper.setPropertyValue(xProps,"ConfigName",sConfigNames[i]);
bFound = true;
}
}
catch (Exception e) {
}
}
XPropertySetHelper.setPropertyValue(xProps,"Config",nConfig);
if (!bFound) { XPropertySetHelper.setPropertyValue(xProps,"ConfigName",""); }