Some more minor bugfixing, refactoring and rearrangement

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@167 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2014-08-27 09:00:49 +00:00
parent 6249ef406e
commit 9babea1b6c
7 changed files with 106 additions and 115 deletions

View file

@ -45,7 +45,6 @@ import com.sun.star.xml.XExportFilter;
import org.openoffice.da.comp.w2lcommon.helper.MessageBox; import org.openoffice.da.comp.w2lcommon.helper.MessageBox;
import writer2latex.api.Converter; import writer2latex.api.Converter;
import writer2latex.api.ConverterFactory; import writer2latex.api.ConverterFactory;
import writer2latex.api.ConverterResult;
import writer2latex.api.OutputFile; import writer2latex.api.OutputFile;
import writer2latex.util.Misc; import writer2latex.util.Misc;
import writer2latex.util.SimpleDOMBuilder; import writer2latex.util.SimpleDOMBuilder;
@ -226,105 +225,97 @@ XTypeProvider {
public void convert (org.w3c.dom.Document dom,com.sun.star.io.XOutputStream exportStream) public void convert (org.w3c.dom.Document dom,com.sun.star.io.XOutputStream exportStream)
throws com.sun.star.uno.RuntimeException, IOException { throws com.sun.star.uno.RuntimeException, IOException {
// Initialize the file access // Create converter and supply it with filter data and a suitable graphic converter
sfa2 = null;
try {
Object sfaObject = xComponentContext.getServiceManager().createInstanceWithContext(
"com.sun.star.ucb.SimpleFileAccess", xComponentContext);
sfa2 = (XSimpleFileAccess2) UnoRuntime.queryInterface(XSimpleFileAccess2.class, sfaObject);
}
catch (com.sun.star.uno.Exception e) {
// failed to get SimpleFileAccess service (should not happen)
}
// Get base name from the URL provided by OOo
String sName= getFileName(sURL);
// Create converter
Converter converter = ConverterFactory.createConverter(sdMime); Converter converter = ConverterFactory.createConverter(sdMime);
if (converter==null) { if (converter==null) {
throw new com.sun.star.uno.RuntimeException("Failed to create converter to "+sdMime); throw new com.sun.star.uno.RuntimeException("Failed to create converter to "+sdMime);
} }
// Adapter for output stream (Main output file)
XOutputStreamToOutputStreamAdapter newxos =new XOutputStreamToOutputStreamAdapter(exportStream);
// Apply the FilterData to the converter
if (filterData!=null) { if (filterData!=null) {
FilterDataParser fdp = new FilterDataParser(xComponentContext); FilterDataParser fdp = new FilterDataParser(xComponentContext);
fdp.applyFilterData(filterData,converter); fdp.applyFilterData(filterData,converter);
} }
// Do conversion
converter.setGraphicConverter(new GraphicConverterImpl(xComponentContext)); converter.setGraphicConverter(new GraphicConverterImpl(xComponentContext));
// Do conversion. The base name is take from the URL provided by the office
Iterator<OutputFile> docEnum = converter.convert(dom,Misc.makeFileName(getFileName(sURL))).iterator();
ConverterResult dataOut = converter.convert(dom,Misc.makeFileName(sName)); if (docEnum.hasNext()) {
// The master document is written to the XOutStream supplied by the XMLFilterAdaptor
// Write out files XOutputStreamToOutputStreamAdapter newxos =new XOutputStreamToOutputStreamAdapter(exportStream);
Iterator<OutputFile> docEnum = dataOut.iterator(); docEnum.next().write(newxos);
newxos.flush();
// Remove the file name part of the URL newxos.close();
String sNewURL = null;
if (sURL.lastIndexOf("/")>-1) { if (docEnum.hasNext() && sURL.startsWith("file:")) {
// Take the URL up to and including the last slash // Additional files are written directly using UCB
sNewURL = sURL.substring(0,sURL.lastIndexOf("/")+1); // Initialize the file access (used to write all additional output files)
sfa2 = null;
try {
Object sfaObject = xComponentContext.getServiceManager().createInstanceWithContext(
"com.sun.star.ucb.SimpleFileAccess", xComponentContext);
sfa2 = (XSimpleFileAccess2) UnoRuntime.queryInterface(XSimpleFileAccess2.class, sfaObject);
}
catch (com.sun.star.uno.Exception e) {
// failed to get SimpleFileAccess service (should not happen)
}
if (sfa2!=null) {
// Remove the file name part of the URL
String sNewURL = null;
if (sURL.lastIndexOf("/")>-1) {
// Take the URL up to and including the last slash
sNewURL = sURL.substring(0,sURL.lastIndexOf("/")+1);
}
else {
// The URL does not include a path; this should not really happen,
// but in this case we will write to the current default directory
sNewURL = "";
}
while (docEnum.hasNext()) {
OutputFile docOut = docEnum.next();
// Get the file name and the (optional) directory name
String sFullFileName = Misc.makeHref(docOut.getFileName());
String sDirName = "";
String sFileName = sFullFileName;
int nSlash = sFileName.indexOf("/");
if (nSlash>-1) {
sDirName = sFileName.substring(0,nSlash);
sFileName = sFileName.substring(nSlash+1);
}
try{
// Create subdirectory if required
if (sDirName.length()>0 && !sfa2.exists(sNewURL+sDirName)) {
sfa2.createFolder(sNewURL+sDirName);
}
// writeFile demands an InputStream, so we need a pipe
Object xPipeObj=xMSF.createInstance("com.sun.star.io.Pipe");
XInputStream xInStream = (XInputStream) UnoRuntime.queryInterface(XInputStream.class, xPipeObj );
XOutputStream xOutStream = (XOutputStream) UnoRuntime.queryInterface(XOutputStream.class, xPipeObj );
OutputStream outStream = new XOutputStreamToOutputStreamAdapter(xOutStream);
// Feed the pipe with content...
docOut.write(outStream);
outStream.flush();
outStream.close();
xOutStream.closeOutput();
// ...and then write the content to the URL
sfa2.writeFile(sNewURL+sFullFileName,xInStream);
}
catch (Throwable e){
MessageBox msgBox = new MessageBox(xComponentContext);
msgBox.showMessage(__displayName+": Error writing files",
e.toString()+" at "+e.getStackTrace()[0].toString());
}
}
}
}
} }
else { else {
// The URL does not include a path; this should not really happen, // The converter did not produce any files (should not happen)
// but in this case we will write to the current default directory MessageBox msgBox = new MessageBox(xComponentContext);
sNewURL = ""; msgBox.showMessage(__displayName+": Conversion failed","Internal error");
}
while (docEnum.hasNext() && sURL.startsWith("file:")) {
OutputFile docOut = docEnum.next();
if (dataOut.getMasterDocument()==docOut) {
// The master document is written to the XOutStream supplied
// by the XMLFilterAdaptor
docOut.write(newxos);
newxos.flush();
newxos.close();
}
else {
// Additional files are written directly using UCB
// Get the file name and the (optional) directory name
String sFullFileName = Misc.makeHref(docOut.getFileName());
String sDirName = "";
String sFileName = sFullFileName;
int nSlash = sFileName.indexOf("/");
if (nSlash>-1) {
sDirName = sFileName.substring(0,nSlash);
sFileName = sFileName.substring(nSlash+1);
}
try{
// Create subdirectory if required
if (sDirName.length()>0 && !sfa2.exists(sNewURL+sDirName)) {
sfa2.createFolder(sNewURL+sDirName);
}
// writeFile demands an InputStream, so we need a pipe
Object xPipeObj=xMSF.createInstance("com.sun.star.io.Pipe");
XInputStream xInStream
= (XInputStream) UnoRuntime.queryInterface(XInputStream.class, xPipeObj );
XOutputStream xOutStream
= (XOutputStream) UnoRuntime.queryInterface(XOutputStream.class, xPipeObj );
OutputStream outStream = new XOutputStreamToOutputStreamAdapter(xOutStream);
// Feed the pipe with content...
docOut.write(outStream);
outStream.flush();
outStream.close();
xOutStream.closeOutput();
// ...and then write the content to the URL
sfa2.writeFile(sNewURL+sFullFileName,xInStream);
}
catch (Throwable e){
MessageBox msgBox = new MessageBox(xComponentContext);
msgBox.showMessage(__displayName+": Error writing files",
e.toString()+" at "+e.getStackTrace()[0].toString());
}
}
} }
} }

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2012-08-25) * Version 1.4 (2012-08-27)
* *
*/ */
@ -33,7 +33,7 @@ public class ConverterFactory {
// Version information // Version information
private static final String VERSION = "1.3.2"; private static final String VERSION = "1.3.2";
private static final String DATE = "2014-08-24"; private static final String DATE = "2014-08-27";
/** Return the Writer2LaTeX version in the form /** Return the Writer2LaTeX version in the form
* (major version).(minor version).(patch level)<br/> * (major version).(minor version).(patch level)<br/>

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA * MA 02111-1307 USA
* *
* Copyright: 2002-2008 by Henrik Just * Copyright: 2002-2014 by Henrik Just
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.0 (2008-10-15) * Version 1.4 (2014-08-27)
* *
*/ */
@ -175,7 +175,7 @@ public abstract class BatchConverterBase implements BatchConverter {
handler.endFile(infile.getPath(),true); handler.endFile(infile.getPath(),true);
return dataOut.getMasterDocument().getFileName(); return dataOut.iterator().next().getFileName();
} }

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA * MA 02111-1307 USA
* *
* Copyright: 2002-2009 by Henrik Just * Copyright: 2002-2014 by Henrik Just
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.2 (2009-09-17) * Version 1.4 (2014-08-27)
* *
*/ */
@ -157,7 +157,7 @@ public class PageStyleConverter extends StyleConverter {
context.setInHeaderFooter(true); context.setInHeaderFooter(true);
Enumeration<Object> styles = ofr.getMasterPages().getStylesEnumeration(); Enumeration<OfficeStyle> styles = ofr.getMasterPages().getStylesEnumeration();
ldp.append("% Pages styles").nl(); ldp.append("% Pages styles").nl();
if (!config.useFancyhdr()) { if (!config.useFancyhdr()) {
ldp.append("\\makeatletter").nl(); ldp.append("\\makeatletter").nl();
@ -385,7 +385,7 @@ public class PageStyleConverter extends StyleConverter {
boolean bIncludeHead = false; boolean bIncludeHead = false;
boolean bIncludeFoot = false; boolean bIncludeFoot = false;
// Look through all applied page layouts and use largest heights // Look through all applied page layouts and use largest heights
Enumeration<Object> masters = ofr.getMasterPages().getStylesEnumeration(); Enumeration<OfficeStyle> masters = ofr.getMasterPages().getStylesEnumeration();
while (masters.hasMoreElements()) { while (masters.hasMoreElements()) {
MasterPage master = (MasterPage) masters.nextElement(); MasterPage master = (MasterPage) masters.nextElement();
if (styleNames.containsName(getDisplayName(master.getName()))) { if (styleNames.containsName(getDisplayName(master.getName()))) {

View file

@ -519,7 +519,7 @@ public class OfficeReader {
* @return the iso language * @return the iso language
*/ */
public String getMajorityLanguage() { public String getMajorityLanguage() {
Hashtable<Object, Integer> langs = new Hashtable<Object, Integer>(); Hashtable<String, Integer> langs = new Hashtable<String, Integer>();
// Read the default language from the default paragraph style // Read the default language from the default paragraph style
String sDefaultLang = null; String sDefaultLang = null;
@ -529,7 +529,7 @@ public class OfficeReader {
} }
// Collect languages from paragraph styles // Collect languages from paragraph styles
Enumeration<Object> enumeration = getParStyles().getStylesEnumeration(); Enumeration<OfficeStyle> enumeration = getParStyles().getStylesEnumeration();
while (enumeration.hasMoreElements()) { while (enumeration.hasMoreElements()) {
style = (StyleWithProperties) enumeration.nextElement(); style = (StyleWithProperties) enumeration.nextElement();
String sLang = style.getProperty(XMLString.FO_LANGUAGE); String sLang = style.getProperty(XMLString.FO_LANGUAGE);
@ -546,9 +546,9 @@ public class OfficeReader {
// Find the most common language // Find the most common language
int nMaxCount = 0; int nMaxCount = 0;
String sMajorityLanguage = null; String sMajorityLanguage = null;
enumeration = langs.keys(); Enumeration<String> langenum = langs.keys();
while (enumeration.hasMoreElements()) { while (langenum.hasMoreElements()) {
String sLang = (String) enumeration.nextElement(); String sLang = langenum.nextElement();
int nCount = langs.get(sLang).intValue(); int nCount = langs.get(sLang).intValue();
if (nCount>nMaxCount) { if (nCount>nMaxCount) {
nMaxCount = nCount; nMaxCount = nCount;

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA * MA 02111-1307 USA
* *
* Copyright: 2002-2008 by Henrik Just * Copyright: 2002-2014 by Henrik Just
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.0 (2008-11-23) * Version 1.4 (2014-08-27)
* *
*/ */
@ -33,8 +33,8 @@ import writer2latex.util.Misc;
/** Container class representing a style family in OOo */ /** Container class representing a style family in OOo */
public class OfficeStyleFamily { public class OfficeStyleFamily {
private Hashtable<String, Object> styles = new Hashtable<String, Object>(); private Hashtable<String, OfficeStyle> styles = new Hashtable<String, OfficeStyle>();
private Class styleClass; private Class<? extends OfficeStyle> styleClass;
private Hashtable<String, String> displayNames = new Hashtable<String, String>(); private Hashtable<String, String> displayNames = new Hashtable<String, String>();
@ -44,7 +44,7 @@ public class OfficeStyleFamily {
* @param styleClass the subclass of OfficeStyle used to represent styles * @param styleClass the subclass of OfficeStyle used to represent styles
* in this family * in this family
*/ */
public OfficeStyleFamily(Class styleClass) { public OfficeStyleFamily(Class<? extends OfficeStyle> styleClass) {
this.styleClass = styleClass; this.styleClass = styleClass;
} }
@ -70,7 +70,7 @@ public class OfficeStyleFamily {
*/ */
public OfficeStyle getStyle(String sName) { public OfficeStyle getStyle(String sName) {
if (sName==null) { return null; } if (sName==null) { return null; }
else { return (OfficeStyle) styles.get(sName); } else { return styles.get(sName); }
} }
/** Get a style by display name. Automatic styles does not have a display /** Get a style by display name. Automatic styles does not have a display
@ -101,7 +101,7 @@ public class OfficeStyleFamily {
/** Get all named styles in the family (ie. excluding the default style) /** Get all named styles in the family (ie. excluding the default style)
* @return an enumeration of all styles represented by OfficeStyle objects * @return an enumeration of all styles represented by OfficeStyle objects
*/ */
public Enumeration<Object> getStylesEnumeration(){ public Enumeration<OfficeStyle> getStylesEnumeration(){
return styles.elements(); return styles.elements();
} }
@ -113,7 +113,7 @@ public class OfficeStyleFamily {
String sName = Misc.getAttribute(node,XMLString.STYLE_NAME); String sName = Misc.getAttribute(node,XMLString.STYLE_NAME);
if (sName!=null) { if (sName!=null) {
try { try {
OfficeStyle style = (OfficeStyle) styleClass.newInstance(); OfficeStyle style = styleClass.newInstance();
style.sName=sName; style.sName=sName;
style.family=this; style.family=this;
style.bAutomatic=bAutomatic; style.bAutomatic=bAutomatic;

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-26) * Version 1.4 (2014-08-27)
* *
*/ */
@ -100,8 +100,8 @@ public class Converter extends ConverterBase {
private XhtmlDocument htmlDoc; // current outfile private XhtmlDocument htmlDoc; // current outfile
private Document htmlDOM; // current DOM, usually within htmlDoc private Document htmlDOM; // current DOM, usually within htmlDoc
private boolean bNeedHeaderFooter = false; private boolean bNeedHeaderFooter = false;
private int nTocFileIndex = -1; //private int nTocFileIndex = -1;
private int nAlphabeticalIndex = -1; //private int nAlphabeticalIndex = -1;
// Hyperlinks // Hyperlinks
Hashtable<String, Integer> targets = new Hashtable<String, Integer>(); Hashtable<String, Integer> targets = new Hashtable<String, Integer>();
@ -201,7 +201,7 @@ public class Converter extends ConverterBase {
protected void setTocFile(String sTarget) { protected void setTocFile(String sTarget) {
converterResult.setTocFile(new ContentEntryImpl(l10n.get(L10n.CONTENTS),1,htmlDoc,sTarget)); converterResult.setTocFile(new ContentEntryImpl(l10n.get(L10n.CONTENTS),1,htmlDoc,sTarget));
nTocFileIndex = nOutFileIndex; //nTocFileIndex = nOutFileIndex;
} }
protected void setLofFile(String sTarget) { protected void setLofFile(String sTarget) {
@ -214,7 +214,7 @@ public class Converter extends ConverterBase {
protected void setIndexFile(String sTarget) { protected void setIndexFile(String sTarget) {
converterResult.setIndexFile(new ContentEntryImpl(l10n.get(L10n.INDEX),1,htmlDoc,sTarget)); converterResult.setIndexFile(new ContentEntryImpl(l10n.get(L10n.INDEX),1,htmlDoc,sTarget));
nAlphabeticalIndex = nOutFileIndex; //nAlphabeticalIndex = nOutFileIndex;
} }
protected void setCoverFile(String sTarget) { protected void setCoverFile(String sTarget) {