Handle BibTeX files with non-ascii encodings

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@263 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2015-07-27 07:52:38 +00:00
parent 5e9dfc8678
commit 658952d328
22 changed files with 207 additions and 113 deletions

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2015-05-29)
* Version 1.6 (2015-07-24)
*
*/
@ -61,6 +61,7 @@ import com.sun.star.text.XTextViewCursor;
import com.sun.star.text.XTextViewCursorSupplier;
import com.sun.star.ui.dialogs.ExecutableDialogResults;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
@ -68,7 +69,10 @@ import org.jbibtex.ParseException;
import org.openoffice.da.comp.w2lcommon.helper.DialogAccess;
import org.openoffice.da.comp.w2lcommon.helper.DialogBase;
import org.openoffice.da.comp.w2lcommon.helper.MessageBox;
import org.openoffice.da.comp.w2lcommon.helper.RegistryHelper;
import org.openoffice.da.comp.w2lcommon.helper.XPropertySetHelper;
import writer2latex.latex.i18n.ClassicI18n;
import writer2latex.office.BibMark;
import writer2latex.office.BibMark.EntryType;
import writer2latex.util.Misc;
@ -95,6 +99,9 @@ public class BibTeXDialog extends DialogBase implements com.sun.star.lang.XIniti
// The BibTeX directory (passed at initialization)
File bibTeXDirectory = null;
// The encoding for BibTeX files (set in constructor from the registry)
String sBibTeXJavaEncoding = null;
// Cache of BibTeX files in the BibTeX directory
File[] files = null;
@ -121,6 +128,22 @@ public class BibTeXDialog extends DialogBase implements com.sun.star.lang.XIniti
/** Create a new BibTeXDialog */
public BibTeXDialog(XComponentContext xContext) {
super(xContext);
sBibTeXJavaEncoding = getBibTeXJavaEncoding();
}
private String getBibTeXJavaEncoding() {
RegistryHelper registry = new RegistryHelper(xContext);
try {
Object view = registry.getRegistryView(BibliographyDialog.REGISTRY_PATH, false);
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,view);
int nBibTeXEncoding = XPropertySetHelper.getPropertyValueAsShort(xProps, "BibTeXEncoding"); //$NON-NLS-1$
registry.disposeRegistryView(view);
return ClassicI18n.writeJavaEncoding(nBibTeXEncoding);
}
catch (Exception e) {
// Failed to get registry view
}
return null;
}
/** Return the name of the library containing the dialog
@ -257,11 +280,12 @@ public class BibTeXDialog extends DialogBase implements com.sun.star.lang.XIniti
int nFile = getListBoxSelectedItem("File"); //$NON-NLS-1$
if (nFile>=0) {
try {
currentFile = new BibTeXReader(files[nFile]);
currentFile = new BibTeXReader(files[nFile],sBibTeXJavaEncoding);
} catch (IOException e) {
System.err.println(e.getMessage());
currentFile = null;
} catch (ParseException e) {
System.out.println(e.getMessage());
System.err.println(e.getMessage());
currentFile = null;
}
@ -502,10 +526,12 @@ public class BibTeXDialog extends DialogBase implements com.sun.star.lang.XIniti
BibTeXReader[] readers = new BibTeXReader[nFiles];
for (int i=0; i<nFiles; i++) {
try {
readers[i] = new BibTeXReader(files[i]);
readers[i] = new BibTeXReader(files[i],sBibTeXJavaEncoding);
} catch (IOException e) {
System.err.println(e.getMessage());
readers[i] = null;
} catch (ParseException e) {
System.err.println(e.getMessage());
readers[i] = null;
}
}

View file

@ -16,19 +16,20 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2014 by Henrik Just
* Copyright: 2002-2015 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.6 (2014-12-27)
* Version 1.6 (2015-07-24)
*
*/
package org.openoffice.da.comp.writer2latex;
import java.io.File;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.Collection;
@ -43,6 +44,7 @@ import org.jbibtex.Key;
import org.jbibtex.LaTeXParser;
import org.jbibtex.LaTeXPrinter;
import org.jbibtex.ParseException;
import org.jbibtex.TokenMgrException;
import org.jbibtex.Value;
import writer2latex.bibtex.BibTeXEntryMap;
@ -56,16 +58,19 @@ import writer2latex.office.BibMark.EntryType;
public class BibTeXReader {
private File file;
private String sEncoding;
private Map<String, BibMark> entries;
/** Construct a new <code>BibTeXReader</code> based on a file
*
* @param file the file to read
* @param sEncoding the character encoding of the file
* @throws IOException if any error occurs reading the file
* @throws ParseException if any error occurs interpreting the contents of the file
*/
public BibTeXReader(File file) throws IOException, ParseException {
public BibTeXReader(File file, String sEncoding) throws IOException, ParseException {
this.file = file;
this.sEncoding = sEncoding;
reload();
}
@ -73,8 +78,8 @@ public class BibTeXReader {
*/
public void reload() throws IOException, ParseException {
entries = new HashMap<String, BibMark>();
BibTeXDatabase database = parseBibTeX(file);
readEntries(database);
BibTeXDatabase database = parseBibTeX(file,sEncoding);
readEntries(database);
}
/** Get the file associated with this <code>BibTeXReader</code>
@ -93,8 +98,9 @@ public class BibTeXReader {
return entries;
}
private static BibTeXDatabase parseBibTeX(File file) throws ParseException, IOException {
Reader reader = new FileReader(file);
private static BibTeXDatabase parseBibTeX(File file, String sEncoding) throws ParseException, IOException {
FileInputStream is = new FileInputStream(file);
Reader reader = new InputStreamReader(is,sEncoding);
try {
BibTeXParser parser = new BibTeXParser() {
@Override
@ -114,7 +120,8 @@ public class BibTeXReader {
};
return parser.parse(reader);
} finally {
reader.close();
if (reader!=null) { reader.close(); }
if (is!=null) { is.close(); }
}
}
@ -148,6 +155,9 @@ public class BibTeXReader {
} catch (ParseException e) {
// If parsing fails, return the original string
return string;
} catch (TokenMgrException e) {
// If the string contains invalid characters, return the original string
return string;
} finally {
try {
reader.close();

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2015-05-29)
* Version 1.6 (2015-07-23)
*
*/
@ -179,6 +179,8 @@ public final class BibliographyDialog
XPropertySetHelper.getPropertyValueAsShort(xProps, "BibTeXLocation")); //$NON-NLS-1$
dlg.setTextFieldText("BibTeXDir", //$NON-NLS-1$
XPropertySetHelper.getPropertyValueAsString(xProps, "BibTeXDir")); //$NON-NLS-1$
dlg.setListBoxSelectedItem("BibTeXEncoding", //$NON-NLS-1$
XPropertySetHelper.getPropertyValueAsShort(xProps, "BibTeXEncoding")); //$NON-NLS-1$
dlg.setCheckBoxStateAsBoolean("UseNatbib", //$NON-NLS-1$
XPropertySetHelper.getPropertyValueAsBoolean(xProps, "UseNatbib")); //$NON-NLS-1$
dlg.setTextFieldText("NatbibOptions", //$NON-NLS-1$
@ -206,6 +208,7 @@ public final class BibliographyDialog
XPropertySetHelper.setPropertyValue(xProps, "IncludeOriginalCitations", dlg.getCheckBoxStateAsBoolean("IncludeOriginalCitations")); //$NON-NLS-1$ //$NON-NLS-2$
XPropertySetHelper.setPropertyValue(xProps, "BibTeXLocation", dlg.getListBoxSelectedItem("BibTeXLocation")); //$NON-NLS-1$ //$NON-NLS-2$
XPropertySetHelper.setPropertyValue(xProps, "BibTeXDir", dlg.getTextFieldText("BibTeXDir")); //$NON-NLS-1$ //$NON-NLS-2$
XPropertySetHelper.setPropertyValue(xProps, "BibTeXEncoding", dlg.getListBoxSelectedItem("BibTeXEncoding")); //$NON-NLS-1$ //$NON-NLS-2$
XPropertySetHelper.setPropertyValue(xProps, "UseNatbib", dlg.getCheckBoxStateAsBoolean("UseNatbib")); //$NON-NLS-1$ //$NON-NLS-2$
XPropertySetHelper.setPropertyValue(xProps, "NatbibOptions", dlg.getTextFieldText("NatbibOptions")); //$NON-NLS-1$ //$NON-NLS-2$
@ -263,6 +266,8 @@ public final class BibliographyDialog
dlg.setControlEnabled("BibTeXDirLabel", bEnableSettings && bEnableDir); //$NON-NLS-1$
dlg.setControlEnabled("BibTeXDir", bEnableSettings && bEnableDir); //$NON-NLS-1$
dlg.setControlEnabled("BibTeXDirButton", bEnableSettings && bEnableDir); //$NON-NLS-1$
dlg.setControlEnabled("BibTeXEncodingLabel", bEnableSettings); //$NON-NLS-1$
dlg.setControlEnabled("BibTeXEncoding", bEnableSettings); //$NON-NLS-1$
dlg.setControlEnabled("ConvertZoteroCitations", bEnableSettings); //$NON-NLS-1$
dlg.setControlEnabled("ConvertJabRefCitations", bEnableSettings); //$NON-NLS-1$
dlg.setControlEnabled("IncludeOriginalCitations", bEnableSettings && bEnableOriginalCitations); //$NON-NLS-1$
@ -348,6 +353,3 @@ public final class BibliographyDialog
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2015-05-29)
* Version 1.6 (2015-07-23)
*
*/
package org.openoffice.da.comp.writer2latex;
@ -34,6 +34,7 @@ import org.openoffice.da.comp.w2lcommon.helper.PropertyHelper;
import org.openoffice.da.comp.w2lcommon.helper.RegistryHelper;
import org.openoffice.da.comp.w2lcommon.helper.XPropertySetHelper;
import writer2latex.latex.i18n.ClassicI18n;
import writer2latex.util.CSVList;
import writer2latex.util.Misc;
@ -107,6 +108,8 @@ public class LaTeXUNOPublisher extends UNOPublisher {
XPropertySetHelper.getPropertyValueAsString(xProps, "BibTeXDir")); //$NON-NLS-1$
if (XPropertySetHelper.getPropertyValueAsBoolean(xProps, "UseExternalBibTeXFiles")) { //$NON-NLS-1$
filterHelper.put("external_bibtex_files", sBibTeXFiles); //$NON-NLS-1$
filterHelper.put("bibtex_encoding", ClassicI18n.writeInputenc( //$NON-NLS-1$
XPropertySetHelper.getPropertyValueAsShort(xProps, "BibTeXEncoding"))); //$NON-NLS-1$
if (XPropertySetHelper.getPropertyValueAsBoolean(xProps, "ConvertZoteroCitations")) { //$NON-NLS-1$
filterHelper.put("zotero_bibtex_files", sBibTeXFiles); //$NON-NLS-1$
}