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

Relativizes paths to aux files #4792

Merged
merged 8 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/groups/GroupDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ private List<Path> getFileDirectoriesAsPaths() {
List<Path> fileDirs = new ArrayList<>();
MetaData metaData = basePanel.getBibDatabaseContext().getMetaData();
metaData.getLaTexFileDirectory(prefs.getFilePreferences().getUser())
.ifPresent(LaTexFileDirectory -> fileDirs.add(Paths.get(LaTexFileDirectory).toAbsolutePath().normalize()));
.ifPresent(laTexFileDirectory -> fileDirs.add(laTexFileDirectory));

return fileDirs;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.libraryproperties;

import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.Optional;

import javax.inject.Inject;
Expand Down Expand Up @@ -146,7 +147,7 @@ private void storeSettings() {
if (text.isEmpty()) {
metaData.clearLaTexFileDirectory(preferencesService.getUser());
} else {
metaData.setLaTexFileDirectory(preferencesService.getUser(), text);
metaData.setLaTexFileDirectory(preferencesService.getUser(), Paths.get(text));
}

if (viewModel.libraryProtectedProperty().getValue()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.libraryproperties;

import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Optional;

import javafx.beans.property.BooleanProperty;
Expand Down Expand Up @@ -61,8 +62,9 @@ public LibraryPropertiesDialogViewModel(BasePanel panel, DialogService dialogSer
Optional<String> fileDI = metaData.getUserFileDirectory(preferencesService.getUser());
fileDI.ifPresent(userSpecificFileDirectoryProperty::setValue);

Optional<String> fileDL = metaData.getLaTexFileDirectory(preferencesService.getUser());
fileDL.ifPresent(laTexFileDirectoryProperty::setValue);
Optional<Path> fileDL = metaData.getLaTexFileDirectory(preferencesService.getUser());
Optional<String> laTexFile = Optional.ofNullable(fileDL.toString());
laTexFile.ifPresent(laTexFileDirectoryProperty::setValue);

oldUserSpecificFileDir = generalFileDirectoryProperty.getValue();
oldGeneralFileDir = userSpecificFileDirectoryProperty.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static Map<String, String> getSerializedStringMap(MetaData metaData,
metaData.getUserFileDirectories().forEach((user, path) -> stringyMetaData
.put(MetaData.FILE_DIRECTORY + '-' + user, Collections.singletonList(path.trim())));
metaData.getLaTexFileDirectories().forEach((user, path) -> stringyMetaData
.put(MetaData.FILE_DIRECTORY + 'l' + user, Collections.singletonList(path.trim())));
.put(MetaData.FILE_DIRECTORY + "Latex-" + user, Collections.singletonList(path.toString().trim())));

for (ContentSelector selector: metaData.getContentSelectorList()) {
stringyMetaData.put(MetaData.SELECTOR_META_PREFIX + selector.getFieldName(), selector.getValues());
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/jabref/logic/importer/util/MetaDataParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -60,10 +62,11 @@ public MetaData parse(MetaData metaData, Map<String, String> data, Character key
} else if (entry.getKey().startsWith(MetaData.SELECTOR_META_PREFIX)) {
metaData.addContentSelector(ContentSelectors.parse(entry.getKey().substring(MetaData.SELECTOR_META_PREFIX.length()), StringUtil.unquote(entry.getValue(), MetaData.ESCAPE_CHARACTER)));
continue;
} else if (entry.getKey().startsWith(MetaData.FILE_DIRECTORY + 'l')) {
// The user name comes directly after "FILE_DIRECTORYl"
String user = entry.getKey().substring(MetaData.FILE_DIRECTORY.length() + 1);
metaData.setLaTexFileDirectory(user, getSingleItem(value));
} else if (entry.getKey().startsWith(MetaData.FILE_DIRECTORY + "Latex-")) {
// The user name comes directly after "FILE_DIRECTORYLatex-"
String user = entry.getKey().substring(MetaData.FILE_DIRECTORY.length() + 6);
Path path = Paths.get(getSingleItem(value)).normalize();
metaData.setLaTexFileDirectory(user, path);
continue;
}

Expand Down
21 changes: 12 additions & 9 deletions src/main/java/org/jabref/model/groups/TexGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import java.io.IOException;
import java.net.InetAddress;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.jabref.model.auxparser.AuxParser;
Expand All @@ -32,6 +30,16 @@ public class TexGroup extends AbstractGroup implements FileUpdateListener {
private final MetaData metaData;
private String user;

public TexGroup(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData, String user) throws IOException {
super(name, context);
this.metaData = metaData;
this.user = user;
this.filePath = expandPath(Objects.requireNonNull(filePath));
this.auxParser = auxParser;
this.fileMonitor = fileMonitor;
fileMonitor.addListenerForFile(this.filePath, this);
}

public TexGroup(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData) throws IOException {
super(name, context);
this.metaData = metaData;
Expand Down Expand Up @@ -115,19 +123,14 @@ private Path relativize(Path path) {

private Path expandPath(Path path) {
List<Path> fileDirectories = getFileDirectoriesAsPaths();
Optional<Path> fpath = FileHelper.expandFilenameAsPath(path.toString(), fileDirectories);
if (fpath.isPresent()) {
return fpath.get();
} else {
return path;
}
return FileHelper.expandFilenameAsPath(path.toString(), fileDirectories).orElse(path);
}

private List<Path> getFileDirectoriesAsPaths() {
List<Path> fileDirs = new ArrayList<>();

metaData.getLaTexFileDirectory(user)
.ifPresent(laTexFileDirectory -> fileDirs.add(Paths.get(laTexFileDirectory).toAbsolutePath().normalize()));
.ifPresent(laTexFileDirectory -> fileDirs.add(laTexFileDirectory));

return fileDirs;
}
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/jabref/model/metadata/MetaData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.model.metadata;

import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class MetaData {
private final EventBus eventBus = new EventBus();
private final Map<String, String> citeKeyPatterns = new HashMap<>(); // <BibType, Pattern>
private final Map<String, String> userFileDirectory = new HashMap<>(); // <User, FilePath>
private final Map<String, String> laTexFileDirectory = new HashMap<>(); // <User, FilePath>
private final Map<String, Path> laTexFileDirectory = new HashMap<>(); // <User, FilePath>
private GroupTreeNode groupsRoot;
private Charset encoding;
private SaveOrderConfig saveOrderConfig;
Expand All @@ -51,8 +52,8 @@ public class MetaData {
private BibDatabaseMode mode;
private boolean isProtected;
private String defaultFileDirectory;
private ContentSelectors contentSelectors = new ContentSelectors();
private Map<String, List<String>> unkownMetaData = new HashMap<>();
private final ContentSelectors contentSelectors = new ContentSelectors();
private final Map<String, List<String>> unkownMetaData = new HashMap<>();
private boolean isEventPropagationEnabled = true;

/**
Expand Down Expand Up @@ -210,11 +211,11 @@ public void clearUserFileDirectory(String user) {
postChange();
}

public Optional<String> getLaTexFileDirectory(String user) {
public Optional<Path> getLaTexFileDirectory(String user) {
return Optional.ofNullable(laTexFileDirectory.get(user));
}

public void setLaTexFileDirectory(String user, String path) {
public void setLaTexFileDirectory(String user, Path path) {
laTexFileDirectory.put(Objects.requireNonNull(user), Objects.requireNonNull(path));
postChange();
}
Expand Down Expand Up @@ -300,7 +301,7 @@ public Map<String, String> getUserFileDirectories() {
return Collections.unmodifiableMap(userFileDirectory);
}

public Map<String, String> getLaTexFileDirectories() {
public Map<String, Path> getLaTexFileDirectories() {
return Collections.unmodifiableMap(laTexFileDirectory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,13 @@ void writeProtectedFlag() throws Exception {
void writeFileDirectories() throws Exception {
metaData.setDefaultFileDirectory("\\Literature\\");
metaData.setUserFileDirectory("defaultOwner-user", "D:\\Documents");
metaData.setLaTexFileDirectory("defaultOwner-user", "D:\\Documents");
metaData.setLaTexFileDirectory("defaultOwner-user", Paths.get("D:\\Latex"));

databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList());

assertEquals(OS.NEWLINE + "@Comment{jabref-meta: fileDirectory:\\\\Literature\\\\;}" + OS.NEWLINE +
OS.NEWLINE + "@Comment{jabref-meta: fileDirectory-defaultOwner-user:D:\\\\Documents;}"
+ OS.NEWLINE + OS.NEWLINE + "@Comment{jabref-meta: fileDirectoryldefaultOwner-user:D:\\\\Documents;}" + OS.NEWLINE, stringWriter.toString());
+ OS.NEWLINE + OS.NEWLINE + "@Comment{jabref-meta: fileDirectoryLatex-defaultOwner-user:D:\\\\Latex;}" + OS.NEWLINE, stringWriter.toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1475,11 +1475,11 @@ void integrationTestFileDirectories() throws IOException {
ParserResult result = parser.parse(
new StringReader("@comment{jabref-meta: fileDirectory:\\\\Literature\\\\;}"
+ "@comment{jabref-meta: fileDirectory-defaultOwner-user:D:\\\\Documents;}"
+ "@Comment{jabref-meta: fileDirectoryldefaultOwner-user:D:\\\\Documents;}"));
+ "@comment{jabref-meta: fileDirectoryLatex-defaultOwner-user:D:\\\\Latex;}"));

assertEquals("\\Literature\\", result.getMetaData().getDefaultFileDirectory().get());
assertEquals("D:\\Documents", result.getMetaData().getUserFileDirectory("defaultOwner-user").get());
assertEquals("D:\\Documents", result.getMetaData().getLaTexFileDirectory("defaultOwner-user").get());
assertEquals("D:\\Latex", result.getMetaData().getLaTexFileDirectory("defaultOwner-user").get().toString());
}

@Test
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/org/jabref/model/groups/TexGroupTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.model.groups;

import java.net.InetAddress;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand All @@ -13,6 +12,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -46,12 +46,12 @@ public void containsReturnsTrueForEntryNotInAux() throws Exception {
}

@Test
public void ReturnsTrueIfPathRelative() throws Exception {
public void getFilePathReturnsRelativePath() throws Exception {
Path auxFile = Paths.get(TexGroupTest.class.getResource("paper.aux").toURI());
String user = System.getProperty("user.name") + '-' + InetAddress.getLocalHost().getHostName();
metaData.setLaTexFileDirectory(user, auxFile.getParent().toString());
TexGroup group = new TexGroup("paper", GroupHierarchyType.INDEPENDENT, auxFile, new DefaultAuxParser(new BibDatabase()), new DummyFileUpdateMonitor(), metaData);
String user = "Darwin";
metaData.setLaTexFileDirectory(user, auxFile.getParent());
TexGroup group = new TexGroup("paper", GroupHierarchyType.INDEPENDENT, auxFile, new DefaultAuxParser(new BibDatabase()), new DummyFileUpdateMonitor(), metaData, user);

assertTrue("paper.aux".matches(group.getFilePath().toString()));
assertEquals("paper.aux", group.getFilePath().toString());
}
}