Skip to content
Browse files

[#1682] Don't try to generate project docs if no documentation folder

[#1681] Docviewer: allow (and replace) invalid file name characters in project name in doc:export
Removed unused imports from docviewer DocumentationGenerator
  • Loading branch information...
1 parent 71f8cc1 commit c0ddd5084fda50e82819b20adf0b824fc5e7ec0e @marekpiechut marekpiechut committed with Notalifeform Jul 1, 2013
View
56 framework/src/play/libs/Files.java
@@ -17,6 +17,14 @@
public class Files {
/**
+ * Characters that are invalid in Windows OS file names (Unix only forbids '/' character)
+ */
+ public static final char[] ILLEGAL_FILENAME_CHARS = {34, 60, 62, 124, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
+ 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 58, 42, 63, 92, 47};
+
+ public static final char ILLEGAL_FILENAME_CHARS_REPLACE = '_';
+
+ /**
* Just copy a file
* @param from
* @param to
@@ -121,6 +129,54 @@ public static void zip(File directory, File zipFile) {
}
}
+ /**
+ * Replace all characters that are invalid in file names on Windows or Unix operating systems
+ * with {@link Files#ILLEGAL_FILENAME_CHARS_REPLACE} character.
+ * <p/>
+ * This method makes sure your file name can successfully be used to write new file to disk.
+ * Invalid characters are listed in {@link Files#ILLEGAL_FILENAME_CHARS} array.
+ *
+ * @param fileName File name to sanitize
+ * @return Sanitized file name (new String object) if found invalid characters or same string if not
+ */
+ public static String sanitizeFileName(String fileName) {
+ return sanitizeFileName(fileName, ILLEGAL_FILENAME_CHARS_REPLACE);
+ }
+
+ /**
+ * Replace all characters that are invalid in file names on Windows or Unix operating systems
+ * with passed in character.
+ * <p/>
+ * This method makes sure your file name can successfully be used to write new file to disk.
+ * Invalid characters are listed in {@link Files#ILLEGAL_FILENAME_CHARS} array.
+ *
+ * @param fileName File name to sanitize
+ * @param replacement character to use as replacement for invalid chars
+ * @return Sanitized file name (new String object) if found invalid characters or same string if not
+ */
+ public static String sanitizeFileName(String fileName, char replacement) {
+ if (fileName == null || fileName.isEmpty()) {
+ return fileName;
+ }
+
+ char[] chars = fileName.toCharArray();
+ boolean changed = false;
+ for (int i = 0; i < chars.length; i++) {
+ char c = chars[i];
+ if (c < 128) {
+ for (char illegal : ILLEGAL_FILENAME_CHARS) {
+ if (c == illegal) {
+ chars[i] = replacement;
+ changed = true;
+ break;
+ }
+ }
+ }
+ }
+
+ return changed ? new String(chars) : fileName;
+ }
+
static void zipDirectory(File root, File directory, ZipOutputStream zos) throws Exception {
for (File item : directory.listFiles()) {
if (item.isDirectory()) {
View
34 framework/test-src/play/libs/FilesTest.java
@@ -0,0 +1,34 @@
+package play.libs;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author Marek Piechut
+ */
+public class FilesTest {
+
+ @Test
+ public void testSanitizeFileName() throws Exception {
+ //File names to test are on odd indexes and expected results are on even indexes, ex:
+ //test_file_name, expected_file_name
+ final String[] FILE_NAMES = {
+ null, null,
+ "", "",
+ "a", "a",
+ "test.file", "test.file",
+ "validfilename-,^&'@{}[],$=!-#()%.+~_.&&&", "validfilename-,^&'@{}[],$=!-#()%.+~_.&&&",
+ "invalid/file", "invalid_file",
+ "invalid\\file", "invalid_file",
+ "invalid:*?\\<>|/file", "invalid________file",
+ };
+
+ for (int i = 0; i < FILE_NAMES.length; i += 2) {
+ String actual = Files.sanitizeFileName(FILE_NAMES[i]);
+ String expected = FILE_NAMES[i + 1];
+
+ assertEquals("String was not sanitized properly", expected, actual);
+ }
+ }
+}
View
7 modules/docviewer/src/play/modules/docviewer/DocumentationGenerator.java
@@ -1,26 +1,19 @@
package play.modules.docviewer;
import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.lang.StringUtils;
-import org.apache.tools.ant.util.FileUtils;
import play.Play;
-import play.libs.Files;
import play.libs.IO;
import play.templates.Template;
import play.templates.TemplateLoader;
-import play.test.PlayJUnitRunner;
-import play.vfs.VirtualFile;
import java.io.File;
import java.io.FilenameFilter;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Map.Entry;
/**
* @author Marek Piechut
View
18 modules/docviewer/src/play/modules/docviewer/ExportDocumentationGenerator.java
@@ -2,9 +2,11 @@
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.filefilter.SuffixFileFilter;
+import play.Logger;
import play.Play;
import play.libs.Files;
import play.libs.IO;
+import play.vfs.VirtualFile;
import java.io.File;
import java.io.FilenameFilter;
@@ -53,6 +55,11 @@ public static void main(String[] args) throws IOException {
}
DocumentationGenerator generator = new ExportDocumentationGenerator();
+ if(!generator.projectDocsPath.isDirectory()) {
+ Logger.error("Project documentation folder does not exist. Nothing to generate.");
+ return;
+ }
+
File targetFolder = new File(Play.tmpDir, "documentation");
Files.deleteDirectory(targetFolder);
@@ -81,10 +88,13 @@ public static void main(String[] args) throws IOException {
if (files.exists()) {
IO.copyDirectory(files, new File(targetFolder, "files"));
}
-
- File zipFile = new File(generator.projectDocsPath,
- Play.configuration.getProperty("application.name") + "-docs.zip");
+
+ String fileName = Play.configuration.getProperty("application.name") + "-docs.zip";
+ //Make sure zip can be written
+ fileName = Files.sanitizeFileName(fileName);
+
+ File zipFile = new File(generator.projectDocsPath, fileName);
Files.zip(targetFolder, zipFile);
- System.out.println("Project documentation exported to: " + zipFile.getAbsolutePath());
+ Logger.info("Project documentation exported to: %s", VirtualFile.open(zipFile).relativePath());
}
}

0 comments on commit c0ddd50

Please sign in to comment.
Something went wrong with that request. Please try again.