BibTeX support

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@223 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2014-12-11 20:10:51 +00:00
parent ceddd94461
commit 86c96621a7
13 changed files with 461 additions and 146 deletions

View file

@ -0,0 +1,86 @@
/************************************************************************
*
* BibTeXDialog.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-12-11)
*
*/
package org.openoffice.da.comp.writer2latex;
import com.sun.star.awt.XDialog;
import com.sun.star.uno.XComponentContext;
import org.openoffice.da.comp.w2lcommon.helper.DialogBase;
/** This class provides a UNO dialog to insert a BibTeX bibliographic reference
*/
public class BibTeXDialog extends DialogBase {
//implements com.sun.star.lang.XInitialization {
/** The component will be registered under this name.
*/
public static String __serviceName = "org.openoffice.da.writer2latex.BibTeXDialog";
/** The component should also have an implementation name.
*/
public static String __implementationName = "org.openoffice.da.comp.writer2latex.BibTeXDialog";
/** Return the name of the library containing the dialog
*/
public String getDialogLibraryName() {
return "W4LDialogs";
}
/** Return the name of the dialog within the library
*/
public String getDialogName() {
return "BibTeXEntry";
}
public void initialize() {
}
public void endDialog() {
}
/** Create a new BibTeXDialog */
public BibTeXDialog(XComponentContext xContext) {
super(xContext);
}
// Implement com.sun.star.lang.XInitialization
/*public void initialize( Object[] object )
throws com.sun.star.uno.Exception {
}*/
// Implement XDialogEventHandler
public boolean callHandlerMethod(XDialog xDialog, Object event, String sMethod) {
return true;
}
public String[] getSupportedMethodNames() {
String[] sNames = { };
return sNames;
}
}

View file

@ -0,0 +1,141 @@
/************************************************************************
*
* BibTeXReader.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-11-24)
*
*/
package org.openoffice.da.comp.writer2latex;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.jbibtex.BibTeXDatabase;
import org.jbibtex.BibTeXEntry;
import org.jbibtex.BibTeXParser;
import org.jbibtex.BibTeXString;
import org.jbibtex.Key;
import org.jbibtex.LaTeXParser;
import org.jbibtex.LaTeXPrinter;
import org.jbibtex.ParseException;
import org.jbibtex.Value;
import writer2latex.bibtex.BibTeXEntryMap;
import writer2latex.office.BibMark;
import writer2latex.office.BibMark.EntryType;
/**
* The class reads the contents of a BibTeX file and makes it available as a map
* of ODF <code>BibMark</code> objects
*/
public class BibTeXReader {
private Map<String, BibMark> entries;
/**
* 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 {
BibTeXDatabase database = parseBibTeX(file);
readEntries(database);
}
/** Get the entries of this BibTeX file
*
* @return the entries
*/
public Map<String, BibMark> getEntries() {
return entries;
}
private static BibTeXDatabase parseBibTeX(File file) throws IOException, ParseException {
Reader reader = new FileReader(file);
try {
BibTeXParser parser = new BibTeXParser() {
@Override
public void checkStringResolution(Key key, BibTeXString string) {
if (string == null) {
System.err.println("Unresolved string: \"" + key.getValue() + "\"");
}
}
@Override
public void checkCrossReferenceResolution(Key key,
BibTeXEntry entry) {
if (entry == null) {
System.err.println("Unresolved cross-reference: \"" + key.getValue() + "\"");
}
}
};
return parser.parse(reader);
} finally {
reader.close();
}
}
private void readEntries(BibTeXDatabase database) throws IOException {
entries = new HashMap<String, BibMark>();
Map<Key, BibTeXEntry> entryMap = database.getEntries();
Collection<BibTeXEntry> bibentries = entryMap.values();
for (BibTeXEntry bibentry : bibentries) {
String sKey = bibentry.getKey().toString();
String sType = bibentry.getType().toString();
BibMark entry = new BibMark(sKey,sType);
entries.put(sKey, entry);
Map<Key,Value> fields = bibentry.getFields();
for (Key key : fields.keySet()) {
Value value = fields.get(key);
EntryType entryType = BibTeXEntryMap.getEntryType(key.getValue());
if (entryType!=null) {
entry.setField(entryType, parseLaTeX(value.toUserString()));
}
}
}
}
private static String parseLaTeX(String string) throws IOException {
Reader reader = new StringReader(string);
try {
LaTeXParser parser = new LaTeXParser();
LaTeXPrinter printer = new LaTeXPrinter();
return printer.print(parser.parse(reader));
} catch (ParseException e) {
// If parsing fails, return the original string
return string;
} finally {
reader.close();
}
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2014-10-29)
* Version 1.6 (2014-12-11)
*
*/
@ -120,6 +120,12 @@ public class W2LRegistration {
multiFactory,
regKey);
}
else if (implName.equals(BibTeXDialog.__implementationName) ) {
xSingleServiceFactory = FactoryHelper.getServiceFactory(BibTeXDialog.class,
BibTeXDialog.__serviceName,
multiFactory,
regKey);
}
return xSingleServiceFactory;
}
@ -153,7 +159,8 @@ public class W2LRegistration {
FactoryHelper.writeRegistryServiceInfo(BibliographyDialog.__implementationName,
BibliographyDialog.__serviceName, regKey) &
FactoryHelper.writeRegistryServiceInfo(LogViewerDialog.__implementationName,
LogViewerDialog.__serviceName, regKey);
LogViewerDialog.__serviceName, regKey) &
FactoryHelper.writeRegistryServiceInfo(BibTeXDialog.__implementationName,
BibTeXDialog.__serviceName, regKey);
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2014-11-03)
* Version 1.6 (2014-12-11)
*
*/
@ -34,7 +34,6 @@ import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import org.openoffice.da.comp.w2lcommon.filter.UNOPublisher.TargetFormat;
import org.openoffice.da.comp.w2lcommon.helper.MessageBox;
/** This class implements the ui (dispatch) commands provided by the Writer2LaTeX toolbar.
* The actual processing is done by the core classes <code>UNOPublisher</code>,
@ -176,8 +175,19 @@ public final class Writer2LaTeX extends WeakBase
}
private void insertBibTeX() {
MessageBox msgBox = new MessageBox(m_xContext, m_xFrame);
msgBox.showMessage("Writer2LaTeX","This feature is not implemented yet");
// 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) {
}
}
private void createUNOPublisher() {