BibTeX support

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@225 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2014-12-29 12:40:53 +00:00
parent 86c96621a7
commit 951bcc0f85
13 changed files with 445 additions and 74 deletions

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2014-11-08)
* Version 1.6 (2014-12-27)
*
*/
package org.openoffice.da.comp.w2lcommon.filter;
@ -173,7 +173,7 @@ public class UNOPublisher {
String sDocumentUrl = xModel.getURL();
if (sDocumentUrl.length()==0) { // The document has no location
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage(sAppName,"Please save the document before publishing the file");
msgBox.showMessage(sAppName,"Please save the document first");
return false;
}
else if (!".odt".equals(Misc.getFileExtension(sDocumentUrl)) && !".fodt".equals(Misc.getFileExtension(sDocumentUrl)) && !".ods".equals(Misc.getFileExtension(sDocumentUrl)) && !".fods".equals(Misc.getFileExtension(sDocumentUrl))) {

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2011 by Henrik Just
* Copyright: 2002-2014 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2011-02-25)
* Version 1.6 (2014-12-16)
*
*/
@ -392,6 +392,27 @@ public abstract class DialogBase implements
}
}
public String getLabelText(String sControlName) {
XPropertySet xPropertySet = getControlProperties(sControlName);
try {
return (String) xPropertySet.getPropertyValue("Label");
}
catch (Exception e) {
// Will fail if the control does not exist or is not a label
return "";
}
}
public void setLabelText(String sControlName, String sLabel) {
XPropertySet xPropertySet = getControlProperties(sControlName);
try {
xPropertySet.setPropertyValue("Label",sLabel);
}
catch (Exception e) {
// Will fail if the control does not exist or is not a label
}
}
protected String getFormattedFieldText(String sControlName) {
XPropertySet xPropertySet = getControlProperties(sControlName);
try {

View file

@ -20,21 +20,31 @@
*
* All Rights Reserved.
*
* Version 1.6 (2014-12-11)
* Version 1.6 (2014-12-27)
*
*/
package org.openoffice.da.comp.writer2latex;
import java.awt.Desktop;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import com.sun.star.awt.XDialog;
import com.sun.star.frame.XFrame;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import org.jbibtex.ParseException;
import org.openoffice.da.comp.w2lcommon.helper.DialogBase;
import org.openoffice.da.comp.w2lcommon.helper.MessageBox;
import writer2latex.office.BibMark;
import writer2latex.office.BibMark.EntryType;
/** This class provides a UNO dialog to insert a BibTeX bibliographic reference
*/
public class BibTeXDialog extends DialogBase {
//implements com.sun.star.lang.XInitialization {
public class BibTeXDialog extends DialogBase implements com.sun.star.lang.XInitialization {
/** The component will be registered under this name.
*/
@ -49,7 +59,19 @@ public class BibTeXDialog extends DialogBase {
public String getDialogLibraryName() {
return "W4LDialogs";
}
// The current frame (passed at initialization)
XFrame xFrame = null;
// The BibTeX directory (passed at initialization)
File bibTeXDirectory = null;
// Cache of BibTeX files in the BibTeX directory
File[] files = null;
// Cache of the current BibTeX file
BibTeXReader currentFile = null;
/** Return the name of the dialog within the library
*/
public String getDialogName() {
@ -57,6 +79,7 @@ public class BibTeXDialog extends DialogBase {
}
public void initialize() {
refresh();
}
public void endDialog() {
@ -68,19 +91,216 @@ public class BibTeXDialog extends DialogBase {
}
// Implement com.sun.star.lang.XInitialization
/*public void initialize( Object[] object )
// We expect to get the current frame and a comma separated list of BibTeX files to use
public void initialize( Object[] objects )
throws com.sun.star.uno.Exception {
}*/
for (Object object : objects) {
if (object instanceof XFrame) {
xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, object);
}
if (object instanceof String) {
bibTeXDirectory = new File((String) object);
}
}
}
// Implement XDialogEventHandler
public boolean callHandlerMethod(XDialog xDialog, Object event, String sMethod) {
if (sMethod.equals("FileChange")) {
fileChange();
}
else if (sMethod.equals("EntryChange")) {
entryChange();
}
else if (sMethod.equals("InsertReference")) {
insertReference();
}
else if (sMethod.equals("Edit")) {
edit();
}
else if (sMethod.equals("Refresh")) {
refresh();
}
return true;
}
public String[] getSupportedMethodNames() {
String[] sNames = { };
String[] sNames = { "FileChange", "EntryChange", "InsertReference", "Edit", "Refresh" };
return sNames;
}
// (Re)load the list of BibTeX files
private void refresh() {
// Remember current file selection, if any
String sFile = null;
short nFile = getListBoxSelectedItem("File");
if (nFile>=0 && files[nFile]!=null) {
sFile = getListBoxStringItemList("File")[nFile];
}
if (bibTeXDirectory!=null && bibTeXDirectory.isDirectory()) {
// Populate the file list based on the BibTeX directory
files = bibTeXDirectory.listFiles(
new FilenameFilter() {
public boolean accept(File file, String sName) { return sName!=null && sName.endsWith(".bib"); }
}
);
int nFileCount = files.length;
String[] sFileNames = new String[nFileCount];
// Select either the first or the previous item
nFile = 0;
for (short i=0; i<nFileCount; i++) {
sFileNames[i] = files[i].getName();
if (sFileNames[i].equals(sFile)) { nFile = i; }
}
setListBoxStringItemList("File", sFileNames);
setListBoxSelectedItem("File",(short)nFile);
if (nFileCount>0) {
setControlEnabled("FileLabel",true);
setControlEnabled("File",true);
setControlEnabled("EntryLabel",true);
setControlEnabled("Entry",true);
setControlEnabled("Edit",true);
setControlEnabled("Insert",true);
fileChange();
return;
}
}
// The directory did not contain any BibTeX files
setControlEnabled("FileLabel",false);
setControlEnabled("File",false);
setControlEnabled("EntryLabel",false);
setControlEnabled("Entry",false);
setControlEnabled("Edit",false);
setControlEnabled("Insert",false);
setLabelText("EntryInformation","No BibTeX files were found");
}
// Update the list of entries based on the current selection in the file list
private void fileChange() {
// Remember current entry selection, if any
String sEntry = null;
short nEntry = getListBoxSelectedItem("Entry");
if (nEntry>=0) {
sEntry = getListBoxStringItemList("Entry")[nEntry];
}
// Parse the selected file
int nFile = getListBoxSelectedItem("File");
if (nFile>=0) {
try {
currentFile = new BibTeXReader(files[nFile]);
} catch (IOException e) {
currentFile = null;
} catch (ParseException e) {
currentFile = null;
}
if (currentFile!=null) {
// Populate the entry list with the keys from the current file, if any
String[] sCurrentKeys = currentFile.getEntries().keySet().toArray(new String[0]);
setListBoxStringItemList("Entry", sCurrentKeys);
if (sCurrentKeys.length>0) {
// Select either the first or the previous entry
nEntry = 0;
if (sEntry!=null) {
int nEntryCount = sCurrentKeys.length;
for (short i=0; i<nEntryCount; i++) {
if (sEntry.equals(sCurrentKeys[i])) {
nEntry = i;
}
}
}
setListBoxSelectedItem("Entry",nEntry);
setControlEnabled("EntryLabel",true);
setControlEnabled("Entry",true);
setControlEnabled("Insert",true);
entryChange();
}
else { // No entries, disable controls
setControlEnabled("EntryLabel",false);
setControlEnabled("Entry",false);
setControlEnabled("Insert",false);
setLabelText("EntryInformation","This file does not contain any entries");
}
setControlEnabled("Edit",true);
}
else { // Failed to parse, disable controls
setListBoxStringItemList("Entry", new String[0]);
setControlEnabled("EntryLabel",false);
setControlEnabled("Entry",false);
setControlEnabled("Edit",false);
setControlEnabled("Insert",false);
setLabelText("EntryInformation","There was an error reading this file");
}
}
}
// Update the entry information based on the current selection in the entry list
private void entryChange() {
BibMark bibMark = getCurrentEntry();
if (bibMark!=null) {
String sAuthor = bibMark.getField(EntryType.author);
if (sAuthor==null) { sAuthor = ""; }
String sTitle = bibMark.getField(EntryType.title);
if (sTitle==null) { sTitle = ""; }
String sPublisher = bibMark.getField(EntryType.publisher);
if (sPublisher==null) { sPublisher = ""; }
String sYear = bibMark.getField(EntryType.year);
if (sYear==null) { sYear = ""; }
setLabelText("EntryInformation", sAuthor+"\n"+sTitle+"\n"+sPublisher+"\n"+sYear);
}
else {
setLabelText("EntryInformation", "No information");
}
}
// Insert the currently selected entry as a reference in the text document
private void insertReference() {
if (xFrame!=null) {
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage("Writer2LaTeX","This feature has not been implemented yet");
}
}
// Get the currently selected entry, or null if none is selected
private BibMark getCurrentEntry() {
BibMark bibMark = null;
int nEntry = getListBoxSelectedItem("Entry");
if (nEntry>=0) {
String[] sCurrentKeys = getListBoxStringItemList("Entry");
String sKey = sCurrentKeys[nEntry];
bibMark = currentFile.getEntries().get(sKey);
}
return bibMark;
}
// Edit the current BibTeX file
private void edit() {
int nFile = getListBoxSelectedItem("File");
if (nFile>=0) {
if (files[nFile].exists()) {
// Open the file in the default application on this system (if any)
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(files[nFile]);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
else if (xFrame!=null) {
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage("Writer2LaTeX","Error: No BibTeX editor was found");
}
}
}
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2014-11-24)
* Version 1.6 (2014-12-27)
*
*/
@ -55,18 +55,34 @@ import writer2latex.office.BibMark.EntryType;
*/
public class BibTeXReader {
private File file;
private Map<String, BibMark> entries;
/**
* Construct a new <code>BibTeXReader</code> based on a file
/** Construct a new <code>BibTeXReader</code> based on a file
*
* @param file the file to read
* @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 {
this.file = file;
reload();
}
/** Parse the contents of the file, replacing any previous entries in this <code>BibTeXReader</code>
*/
public void reload() throws IOException, ParseException {
entries = new HashMap<String, BibMark>();
BibTeXDatabase database = parseBibTeX(file);
readEntries(database);
readEntries(database);
}
/** Get the file associated with this <code>BibTeXReader</code>
*
* @return the file
*/
public File getFile() {
return file;
}
/** Get the entries of this BibTeX file
@ -77,7 +93,7 @@ public class BibTeXReader {
return entries;
}
private static BibTeXDatabase parseBibTeX(File file) throws IOException, ParseException {
private static BibTeXDatabase parseBibTeX(File file) throws ParseException, IOException {
Reader reader = new FileReader(file);
try {
BibTeXParser parser = new BibTeXParser() {
@ -102,8 +118,7 @@ public class BibTeXReader {
}
}
private void readEntries(BibTeXDatabase database) throws IOException {
entries = new HashMap<String, BibMark>();
private void readEntries(BibTeXDatabase database) {
Map<Key, BibTeXEntry> entryMap = database.getEntries();
Collection<BibTeXEntry> bibentries = entryMap.values();
@ -124,7 +139,7 @@ public class BibTeXReader {
}
}
private static String parseLaTeX(String string) throws IOException {
private static String parseLaTeX(String string) {
Reader reader = new StringReader(string);
try {
LaTeXParser parser = new LaTeXParser();
@ -134,7 +149,11 @@ public class BibTeXReader {
// If parsing fails, return the original string
return string;
} finally {
reader.close();
try {
reader.close();
} catch (IOException e) {
// Reading from a String will not fail :-)
}
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2014-11-03)
* Version 1.6 (2014-12-27)
*
*/
package org.openoffice.da.comp.writer2latex;
@ -55,7 +55,26 @@ public class LaTeXUNOPublisher extends UNOPublisher {
super(xContext, xFrame, sAppName);
}
/** Make a file name LaTeX friendly
/** Get the directory containing the BibTeX files (as defined in the registry)
*
* @return the directory
*/
public File getBibTeXDirectory() {
// Get the BibTeX settings from the registry
RegistryHelper registry = new RegistryHelper(xContext);
Object view;
try {
view = registry.getRegistryView(BibliographyDialog.REGISTRY_PATH, false);
} catch (Exception e) {
// Failed to get registry settings
return null;
}
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,view);
return getDirectory(XPropertySetHelper.getPropertyValueAsShort(xProps, "BibTeXLocation"),
XPropertySetHelper.getPropertyValueAsString(xProps, "BibTeXDir"));
}
/** Make a file name LaTeX friendly
*/
@Override protected String filterFileName(String sFileName) {
String sResult = "";
@ -229,31 +248,35 @@ public class LaTeXUNOPublisher extends UNOPublisher {
}
}
private String getFileList(short nType, String sDirectory) {
File dir;
private File getDirectory(short nType, String sDirectory) {
switch (nType) {
case 0: // absolute path
dir = new File(sDirectory);
break;
return new File(sDirectory);
case 1: // relative path
dir = new File(Misc.urlToFile(getTargetPath()),sDirectory);
break;
return new File(Misc.urlToFile(getTargetPath()),sDirectory);
default: // document directory
dir = Misc.urlToFile(getTargetPath());
return Misc.urlToFile(getTargetPath());
}
CSVList filelist = new CSVList(",");
}
private String getFileList(short nType, String sDirectory) {
File dir = getDirectory(nType,sDirectory);
File[] files;
if (dir.isDirectory()) {
File[] files = dir.listFiles();
files = dir.listFiles();
}
else {
return null;
}
CSVList filelist = new CSVList(",");
if (files!=null) {
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".bib")) {
//filelist.addValue(file.getAbsolutePath());
filelist.addValue(Misc.removeExtension(file.getName()));
}
}
}
String sFileList = filelist.toString();
return sFileList.length()>0 ? sFileList : "dummy";
return filelist.toString();
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2014-12-11)
* Version 1.6 (2014-12-27)
*
*/
@ -175,19 +175,26 @@ public final class Writer2LaTeX extends WeakBase
}
private void insertBibTeX() {
// Execute the BibTeX dialog
try {
Object dialog = m_xContext.getServiceManager()
.createInstanceWithContext(
"org.openoffice.da.writer2latex.BibTeXDialog", m_xContext);
XExecutableDialog xDialog = (XExecutableDialog)
UnoRuntime.queryInterface(XExecutableDialog.class, dialog);
if (xDialog.execute()==ExecutableDialogResults.OK) {
// Closed with the close button
}
}
catch (com.sun.star.uno.Exception e) {
}
createUNOPublisher();
if (unoPublisher.documentSaved()) {
// Execute the BibTeX dialog
try {
// The dialog needs the current frame and the path to the BibTeX directory
Object[] args = new Object[2];
args[0] = m_xFrame;
args[1] = unoPublisher.getBibTeXDirectory().getPath();
Object dialog = m_xContext.getServiceManager()
.createInstanceWithArgumentsAndContext(
"org.openoffice.da.writer2latex.BibTeXDialog", args, m_xContext);
XExecutableDialog xDialog = (XExecutableDialog)
UnoRuntime.queryInterface(XExecutableDialog.class, dialog);
if (xDialog.execute()==ExecutableDialogResults.OK) {
// Closed with the close button
}
}
catch (com.sun.star.uno.Exception e) {
}
}
}
private void createUNOPublisher() {

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2014-11-28)
* Version 1.6 (2014-12-16)
*
*/
package writer2latex.bibtex;
@ -104,6 +104,7 @@ public class BibTeXEntryMap {
if (bibTeXFields == null) {
createMaps();
}
sFieldName = sFieldName.toLowerCase();
return entryTypes.containsKey(sFieldName) ? entryTypes.get(sFieldName) : null;
}
}