Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export to Office 2007 #1765

Merged
merged 6 commits into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Fixed [#1531](https://github.com/JabRef/jabref/issues/1531): `\relax` can be used for abbreviation of author names
- Fixed [#1771](https://github.com/JabRef/jabref/issues/1771): Show all supported import types as default
- Fixed [#1804](https://github.com/JabRef/jabref/issues/1804): Integrity check no longer removes URL field by mistake
- Fixed [#1750](https://github.com/JabRef/jabref/issues/1750): BibLaTeX `date` field is now correctly exported as `year` in MS-Office 2007 xml format
- Fixed: LaTeX characters in author names are now converted to Unicode before export in MS-Office 2007 xml format
- Fixed: `volume`, `journaltitle`, `issue` and `number`(for patents) fields are now exported correctly in MS-Office 2007 xml format
- Fixed [#1687](https://github.com/JabRef/jabref/issues/1687): "month" field ascending/descending sorting swapped


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* Can be used stand-alone.
*/
public class BibtexParser {

private static final Log LOGGER = LogFactory.getLog(BibtexParser.class);

private final PushbackReader pushbackReader;
Expand All @@ -64,6 +65,7 @@ public class BibtexParser {
private final Deque<Character> pureTextFromFile = new LinkedList<>();
private final ImportFormatPreferences importFormatPreferences;


public BibtexParser(Reader in, ImportFormatPreferences importFormatPreferences) {
Objects.requireNonNull(in);
this.importFormatPreferences = Objects.requireNonNull(importFormatPreferences);
Expand All @@ -77,21 +79,18 @@ public BibtexParser(Reader in, ImportFormatPreferences importFormatPreferences)
* @param in the Reader to read from
* @throws IOException
*/
public static ParserResult parse(Reader in, ImportFormatPreferences importFormatPreferences)
throws IOException {
public static ParserResult parse(Reader in, ImportFormatPreferences importFormatPreferences) throws IOException {
BibtexParser parser = new BibtexParser(in, importFormatPreferences);
return parser.parse();
}


/**
* Parses BibtexEntries from the given string and returns the collection of all entries found.
*
* @param bibtexString
* @return Returns returns an empty collection if no entries where found or if an error occurred.
*/
public static List<BibEntry> fromString(String bibtexString,
ImportFormatPreferences importFormatPreferences) {
public static List<BibEntry> fromString(String bibtexString, ImportFormatPreferences importFormatPreferences) {
StringReader reader = new StringReader(bibtexString);
BibtexParser parser = new BibtexParser(reader, importFormatPreferences);

Expand All @@ -103,7 +102,6 @@ public static List<BibEntry> fromString(String bibtexString,
}
}


/**
* Parses BibtexEntries from the given string and returns one entry found (or null if none found)
* <p>
Expand Down Expand Up @@ -149,7 +147,6 @@ public ParserResult parse() throws IOException {
}
}


private void initializeParserResult() {
database = new BibDatabase();
entryTypes = new HashMap<>(); // To store custom entry types parsed.
Expand Down Expand Up @@ -218,9 +215,10 @@ private void parseAndAddEntry(String type) {

BibEntry entry = parseEntry(type);
// store comments collected without type definition
entry.setCommentsBeforeEntry(commentsAndEntryTypeDefinition.substring(0,commentsAndEntryTypeDefinition.lastIndexOf('@')));
entry.setCommentsBeforeEntry(
commentsAndEntryTypeDefinition.substring(0, commentsAndEntryTypeDefinition.lastIndexOf('@')));
// store complete parsed serialization (comments, type definition + type contents)
entry.setParsedSerialization(commentsAndEntryTypeDefinition+dumpTextReadSoFarToString());
entry.setParsedSerialization(commentsAndEntryTypeDefinition + dumpTextReadSoFarToString());

boolean duplicateKey = database.insertEntry(entry);
if (duplicateKey) {
Expand All @@ -231,8 +229,8 @@ private void parseAndAddEntry(String type) {
}
} catch (IOException ex) {
LOGGER.warn("Could not parse entry", ex);
parserResult.addWarning(Localization.lang("Error occurred when parsing entry") + ": '"
+ ex.getMessage() + "'. " + Localization.lang("Skipped entry."));
parserResult.addWarning(Localization.lang("Error occurred when parsing entry") + ": '" + ex.getMessage()
+ "'. " + Localization.lang("Skipped entry."));

}
}
Expand All @@ -251,16 +249,11 @@ private void parseJabRefComment(Map<String, String> meta) {
}

String comment = buffer.toString().replaceAll("[\\x0d\\x0a]", "");
if (comment.substring(0,
Math.min(comment.length(), MetaData.META_FLAG.length())).equals(
MetaData.META_FLAG)) {

if (comment.substring(0, Math.min(comment.length(), MetaData.META_FLAG.length())).equals(MetaData.META_FLAG)) {

if (comment.substring(0, MetaData.META_FLAG.length()).equals(
MetaData.META_FLAG)) {
if (comment.substring(0, MetaData.META_FLAG.length()).equals(MetaData.META_FLAG)) {
String rest = comment.substring(MetaData.META_FLAG.length());


int pos = rest.indexOf(':');

if (pos > 0) {
Expand All @@ -282,8 +275,7 @@ private void parseJabRefComment(Map<String, String> meta) {
if (typ.isPresent()) {
entryTypes.put(typ.get().getName(), typ.get());
} else {
parserResult.addWarning(Localization.lang("Ill-formed entrytype comment in BIB file") + ": " +
comment);
parserResult.addWarning(Localization.lang("Ill-formed entrytype comment in BIB file") + ": " + comment);
}

// custom entry types are always re-written by JabRef and not stored in the file
Expand All @@ -292,7 +284,6 @@ private void parseJabRefComment(Map<String, String> meta) {

}


private void parseBibtexString() throws IOException {
BibtexString bibtexString = parseString();
bibtexString.setParsedSerialization(dumpTextReadSoFarToString());
Expand All @@ -303,7 +294,6 @@ private void parseBibtexString() throws IOException {
}
}


/**
* Puts all text that has been read from the reader, including newlines, etc., since the last call of this method into a string.
* Removes the JabRef file header, if it is found
Expand Down Expand Up @@ -592,8 +582,7 @@ private String parseFieldContent(String key) throws IOException {
String textToken = parseTextToken();
if (textToken.isEmpty()) {
throw new IOException("Error in line " + line + " or above: "
+ "Empty text token.\nThis could be caused "
+ "by a missing comma between two fields.");
+ "Empty text token.\nThis could be caused " + "by a missing comma between two fields.");
}
value.append('#').append(textToken).append('#');
}
Expand Down Expand Up @@ -652,59 +641,59 @@ private String fixKey() throws IOException {

// Restore if possible:
switch (currentChar) {
case '=':
// Get entryfieldname, push it back and take rest as key
key = key.reverse();
case '=':
// Get entryfieldname, push it back and take rest as key
key = key.reverse();

boolean matchedAlpha = false;
for (int i = 0; i < key.length(); i++) {
currentChar = key.charAt(i);
boolean matchedAlpha = false;
for (int i = 0; i < key.length(); i++) {
currentChar = key.charAt(i);

/// Skip spaces:
if (!matchedAlpha && (currentChar == ' ')) {
continue;
}
matchedAlpha = true;
/// Skip spaces:
if (!matchedAlpha && (currentChar == ' ')) {
continue;
}
matchedAlpha = true;

// Begin of entryfieldname (e.g. author) -> push back:
unread(currentChar);
if ((currentChar == ' ') || (currentChar == '\n')) {
// Begin of entryfieldname (e.g. author) -> push back:
unread(currentChar);
if ((currentChar == ' ') || (currentChar == '\n')) {

/*
* found whitespaces, entryfieldname completed -> key in
* keybuffer, skip whitespaces
*/
StringBuilder newKey = new StringBuilder();
for (int j = i; j < key.length(); j++) {
currentChar = key.charAt(j);
if (!Character.isWhitespace(currentChar)) {
newKey.append(currentChar);
}
StringBuilder newKey = new StringBuilder();
for (int j = i; j < key.length(); j++) {
currentChar = key.charAt(j);
if (!Character.isWhitespace(currentChar)) {
newKey.append(currentChar);
}

// Finished, now reverse newKey and remove whitespaces:
parserResult.addWarning(Localization.lang("Line %0: Found corrupted BibTeX key.",
String.valueOf(line)));
key = newKey.reverse();
}

// Finished, now reverse newKey and remove whitespaces:
parserResult.addWarning(
Localization.lang("Line %0: Found corrupted BibTeX key.", String.valueOf(line)));
key = newKey.reverse();
}
break;
}
break;

case ',':
parserResult.addWarning(Localization.lang("Line %0: Found corrupted BibTeX key (contains whitespaces).",
String.valueOf(line)));
break;
case ',':
parserResult.addWarning(Localization.lang("Line %0: Found corrupted BibTeX key (contains whitespaces).",
String.valueOf(line)));
break;

case '\n':
parserResult.addWarning(Localization.lang("Line %0: Found corrupted BibTeX key (comma missing).",
String.valueOf(line)));
break;
case '\n':
parserResult.addWarning(
Localization.lang("Line %0: Found corrupted BibTeX key (comma missing).", String.valueOf(line)));
break;

default:
default:

// No more lookahead, give up:
unreadBuffer(key);
return "";
// No more lookahead, give up:
unreadBuffer(key);
return "";
}

return removeWhitespaces(key).toString();
Expand Down Expand Up @@ -775,8 +764,8 @@ private String parseKey() throws IOException {
// the entry lacked a comma signifying the end of the key.
return token.toString();
} else {
throw new IOException("Error in line " + line + ":" + "Character '" + (char) character
+ "' is not " + "allowed in bibtex keys.");
throw new IOException("Error in line " + line + ":" + "Character '" + (char) character + "' is not "
+ "allowed in bibtex keys.");
}

}
Expand Down Expand Up @@ -900,8 +889,8 @@ private void consume(char expected) throws IOException {
int character = read();

if (character != expected) {
throw new IOException("Error in line " + line + ": Expected " + expected
+ " but received " + (char) character);
throw new IOException(
"Error in line " + line + ": Expected " + expected + " but received " + (char) character);
}
}

Expand All @@ -925,8 +914,8 @@ private void consume(char firstOption, char secondOption) throws IOException {
int character = read();

if ((character != firstOption) && (character != secondOption)) {
throw new IOException("Error in line " + line + ": Expected " + firstOption + " or "
+ secondOption + " but received " + (char) character);
throw new IOException("Error in line " + line + ": Expected " + firstOption + " or " + secondOption
+ " but received " + (char) character);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public boolean isRecognizedFormat(BufferedReader reader) throws IOException {
*/
Document docin;
try {
DocumentBuilder dbuild = DocumentBuilderFactory.
newInstance().
newDocumentBuilder();
DocumentBuilder dbuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
docin = dbuild.parse(new InputSource(reader));
} catch (Exception e) {
return false;
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/net/sf/jabref/logic/msbib/BibTeXConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,26 @@
import net.sf.jabref.logic.mods.PersonName;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.entry.MonthUtil;
import net.sf.jabref.model.entry.MonthUtil.Month;

public class BibTeXConverter {

private static final String MSBIB_PREFIX = "msbib-";


public static BibEntry convert(MSBibEntry entry) {
BibEntry result;
Map<String, String> fieldValues = new HashMap<>();

String bibTexEntryType = MSBibMapping.getBibLaTeXEntryType(entry.getType());
if (entry.getCiteKey() == null) {
result = new BibEntry(ImportFormat.DEFAULT_BIBTEXENTRY_ID, MSBibMapping.getBibTeXEntryType(entry.getType()));
result = new BibEntry(ImportFormat.DEFAULT_BIBTEXENTRY_ID, bibTexEntryType);

} else {
// TODO: the cite key should not be the ID?!
// id assumes an existing database so don't
result = new BibEntry(entry.getCiteKey(), MSBibMapping.getBibTeXEntryType(entry.getType()));
result = new BibEntry(entry.getCiteKey(), bibTexEntryType);
}

// add String fields
Expand Down Expand Up @@ -77,6 +83,17 @@ public static BibEntry convert(MSBibEntry entry) {
fieldValues.put(MSBIB_PREFIX + "accessed", entry.dateAccessed);
}

if (entry.journalName != null) {
fieldValues.put(FieldName.JOURNALTITLE, entry.journalName);
}
if (entry.month != null) {
Month month = MonthUtil.getMonth(entry.month);
fieldValues.put(FieldName.MONTH, month.shortName);
}
if (entry.number != null) {
fieldValues.put(FieldName.NUMBER, entry.number);
}

// set all fields
result.setField(fieldValues);

Expand All @@ -92,7 +109,8 @@ private static void addAuthor(Map<String, String> map, String type, List<PersonN
map.put(type, allAuthors);
}

private static void parseSingleStandardNumber(String type, String bibtype, String standardNum, Map<String, String> map) {
private static void parseSingleStandardNumber(String type, String bibtype, String standardNum,
Map<String, String> map) {
Pattern pattern = Pattern.compile(':' + type + ":(.[^:]+)");
Matcher matcher = pattern.matcher(standardNum);
if (matcher.matches()) {
Expand Down
Loading