@Override
protected boolean supports(URL url) {
- if (url.getPath().toLowerCase()
- .endsWith(File.separatorChar + "index.html")) {
- try {
- File file = new File(url.toURI()).getParentFile();
- return super.supports(file.toURI().toURL());
- } catch (URISyntaxException e) {
- } catch (MalformedURLException e) {
+ try {
+ File file = new File(url.toURI());
+ if (file.getName().equals("index.html")) {
+ file = file.getParentFile();
}
+
+ file = new File(file, file.getName());
+
+ return super.supports(file.toURI().toURL());
+ } catch (URISyntaxException e) {
+ } catch (MalformedURLException e) {
}
return false;
@Override
public URL getCanonicalUrl(URL source) {
- if (source.toString().endsWith(File.separator + "index.html")) {
- try {
- File fakeFile = new File(source.toURI()); // "story/index.html"
- fakeFile = new File(fakeFile.getParent()); // "story"
- fakeFile = new File(fakeFile, fakeFile.getName()); // "story/story"
- return fakeFile.toURI().toURL();
- } catch (Exception e) {
- Instance.getTraceHandler().error(
- new IOException("Cannot find the right URL for "
- + source, e));
+
+ try {
+ File fakeFile = new File(source.toURI());
+ if (fakeFile.getName().equals("index.html")) { // "story/index.html"
+ fakeFile = new File(fakeFile.getParent()); // -> "story/"
}
+
+ if (fakeFile.isDirectory()) { // "story/"
+ fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt"
+ }
+
+ return fakeFile.toURI().toURL();
+ } catch (Exception e) {
+ Instance.getTraceHandler().error(
+ new IOException("Cannot find the right URL for " + source,
+ e));
}
return source;
import java.net.URISyntaxException;
import java.net.URL;
-import be.nikiroo.fanfix.Instance;
import be.nikiroo.fanfix.data.MetaData;
/**
@Override
protected MetaData getMeta(URL source, InputStream in) throws IOException {
try {
- MetaData meta = InfoReader.readMeta(
- new File(new File(source.toURI()).getPath() + ".info"),
- true);
+ File sourceFile = new File(source.toURI());
+ sourceFile = assureNoTxt(sourceFile);
+
+ MetaData meta = InfoReader.readMeta(new File(sourceFile.getPath()
+ + ".info"), true);
// Some old .info files don't have those now required fields...
String test = meta.getTitle() == null ? "" : meta.getTitle();
@Override
protected boolean supports(URL url) {
- if ("file".equals(url.getProtocol())) {
- File file;
- try {
- file = new File(url.toURI());
- file = new File(file.getPath() + ".info");
- } catch (URISyntaxException e) {
- Instance.getTraceHandler().error(e);
- file = null;
- }
-
- return file != null && file.exists();
- }
-
- return false;
+ return supports(url, true);
}
}
@Override
protected boolean supports(URL url) {
+ return supports(url, false);
+ }
+
+ /**
+ * Check if we supports this {@link URL}, that is, if the info file can be
+ * found OR not found.
+ *
+ * @param url
+ * the {@link URL} to check
+ * @param info
+ * TRUE to require the info file, FALSE to forbid the info file
+ *
+ * @return TRUE if it is supported
+ */
+ protected boolean supports(URL url, boolean info) {
+ boolean infoPresent = false;
if ("file".equals(url.getProtocol())) {
File file;
try {
file = new File(url.toURI());
+ file = assureNoTxt(file);
file = new File(file.getPath() + ".info");
} catch (URISyntaxException e) {
Instance.getTraceHandler().error(e);
file = null;
}
- return file == null || !file.exists();
+ infoPresent = (file != null && file.exists());
}
- return false;
+ return infoPresent == info;
+ }
+
+ /**
+ * Remove the ".txt" extension if it is present.
+ *
+ * @param file
+ * the file to process
+ *
+ * @return the same file or a copy of it without the ".txt" extension if it
+ * was present
+ */
+ protected File assureNoTxt(File file) {
+ if (file.getName().endsWith(".txt")) {
+ file = new File(file.getPath().substring(0,
+ file.getPath().length() - 4));
+ }
+
+ return file;
}
/**
private File testFile;
private File expectedDir;
private File resultDir;
+ private List<BasicOutput.OutputType> realTypes;
public ConversionTest(String[] args) {
super("Conversion", args);
+ // Special mode SYSOUT is not a file type (System.out)
+ realTypes = new ArrayList<BasicOutput.OutputType>();
+ for (BasicOutput.OutputType type : BasicOutput.OutputType.values()) {
+ if (!BasicOutput.OutputType.SYSOUT.equals(type)) {
+ realTypes.add(type);
+ }
+ }
+
addTest(new TestCase("Read the test file") {
@Override
public void test() throws Exception {
}
});
- for (BasicOutput.OutputType type : BasicOutput.OutputType.values()) {
- // NOT for special mode SYSOUT
- if (!BasicOutput.OutputType.SYSOUT.equals(type)) {
- addTest(getTestFor(type));
- }
+ for (BasicOutput.OutputType type : realTypes) {
+ addTest(getTestFor(type));
}
}
// Check conversion:
compareFiles(this, expectedDir, resultDir, type, null);
- // Cross-checks:
-
// LATEX not supported as input
- if (!BasicOutput.OutputType.LATEX.equals(type)) {
- for (BasicOutput.OutputType crossType : BasicOutput.OutputType
- .values()) {
- // NOT for special mode SYSOUT
- if (!BasicOutput.OutputType.SYSOUT.equals(crossType)) {
- File crossDir = tempFiles
- .createTempDir("cross-result");
- generate(this, target, crossDir, crossType);
- compareFiles(this, crossDir, resultDir, crossType,
- crossType);
- }
- }
+ if (BasicOutput.OutputType.LATEX.equals(type)) {
+ return;
+ }
+
+ // Cross-checks:
+ for (BasicOutput.OutputType crossType : realTypes) {
+ File crossDir = tempFiles.createTempDir("cross-result");
+ generate(this, target, crossDir, crossType);
+ compareFiles(this, resultDir, crossDir, crossType,
+ crossType);
}
}
};