| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Fitnesse
Revision: 326
Author: deanwampler
Date: 21 Aug 2008 20:46:05
Changes:Moved to a separate zip directory.
Files:| ... | ...@@ -0,0 +1,254 @@ | |
| 1 | package fitnesse.revisioncontrol.zip; | |
| 2 | ||
| 3 | import static fitnesse.revisioncontrol.NullState.VERSIONED; | |
| 4 | ||
| 5 | import java.io.File; | |
| 6 | import java.io.FileInputStream; | |
| 7 | import java.io.FileOutputStream; | |
| 8 | import java.io.IOException; | |
| 9 | import java.io.InputStream; | |
| 10 | import java.text.SimpleDateFormat; | |
| 11 | import java.util.Collection; | |
| 12 | import java.util.Date; | |
| 13 | import java.util.HashSet; | |
| 14 | import java.util.Iterator; | |
| 15 | import java.util.Properties; | |
| 16 | import java.util.Set; | |
| 17 | import java.util.regex.Pattern; | |
| 18 | import java.util.zip.ZipEntry; | |
| 19 | import java.util.zip.ZipFile; | |
| 20 | import java.util.zip.ZipOutputStream; | |
| 21 | ||
| 22 | import fitnesse.revisioncontrol.RevisionControlException; | |
| 23 | import fitnesse.revisioncontrol.RevisionController; | |
| 24 | import fitnesse.revisioncontrol.State; | |
| 25 | import fitnesse.util.StreamReader; | |
| 26 | import fitnesse.wiki.FileSystemPage; | |
| 27 | import fitnesse.wiki.NoSuchVersionException; | |
| 28 | import fitnesse.wiki.PageData; | |
| 29 | import fitnesse.wiki.PageVersionPruner; | |
| 30 | import fitnesse.wiki.VersionInfo; | |
| 31 | import fitnesse.wiki.WikiPage; | |
| 32 | import fitnesse.wiki.WikiPageProperties; | |
| 33 | ||
| 34 | public class ZipFileRevisionController implements RevisionController { | |
| 35 | public static SimpleDateFormat dateFormat() { | |
| 36 | return new SimpleDateFormat("yyyyMMddHHmmss"); | |
| 37 | } | |
| 38 | ||
| 39 | public ZipFileRevisionController() { | |
| 40 | this(new Properties()); | |
| 41 | } | |
| 42 | ||
| 43 | public ZipFileRevisionController(Properties properties) { | |
| 44 | } | |
| 45 | ||
| 46 | public void add(String... filePaths) throws RevisionControlException { | |
| 47 | } | |
| 48 | ||
| 49 | public void checkin(String... filePaths) throws RevisionControlException { | |
| 50 | } | |
| 51 | ||
| 52 | public void checkout(String... filePaths) throws RevisionControlException { | |
| 53 | } | |
| 54 | ||
| 55 | public State checkState(String... filePaths) throws RevisionControlException { | |
| 56 | return VERSIONED; | |
| 57 | } | |
| 58 | ||
| 59 | public void delete(String... filePaths) throws RevisionControlException { | |
| 60 | } | |
| 61 | ||
| 62 | public PageData getRevisionData(FileSystemPage page, String label) { | |
| 63 | final String filename = page.getFileSystemPath() + "/" + label + ".zip"; | |
| 64 | final File file = new File(filename); | |
| 65 | if (!file.exists()) | |
| 66 | throw new NoSuchVersionException("There is no version '" + label + "'"); | |
| 67 | ||
| 68 | ZipFile zipFile = null; | |
| 69 | try { | |
| 70 | final PageData data = new PageData(page); | |
| 71 | zipFile = new ZipFile(file); | |
| 72 | loadVersionContent(zipFile, data); | |
| 73 | loadVersionAttributes(zipFile, data); | |
| 74 | data.addVersions(loadVersions(page)); | |
| 75 | return data; | |
| 76 | } catch (Throwable th) { | |
| 77 | throw new RuntimeException(th); | |
| 78 | } finally { | |
| 79 | try { | |
| 80 | zipFile.close(); | |
| 81 | } catch (IOException e) { | |
| 82 | e.printStackTrace(); | |
| 83 | } | |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 87 | public Collection<VersionInfo> history(FileSystemPage page) { | |
| 88 | final File dir = new File(page.getFileSystemPath()); | |
| 89 | final File[] files = dir.listFiles(); | |
| 90 | final Set<VersionInfo> versions = new HashSet<VersionInfo>(); | |
| 91 | if (files != null) | |
| 92 | for (final File file : files) | |
| 93 | if (isVersionFile(file)) | |
| 94 | versions.add(new VersionInfo(makeVersionName(file))); | |
| 95 | return versions; | |
| 96 | } | |
| 97 | ||
| 98 | public boolean isReversionControlEnabled() { | |
| 99 | return true; | |
| 100 | } | |
| 101 | ||
| 102 | public boolean isExternalReversionControlEnabled() { | |
| 103 | return false; | |
| 104 | } | |
| 105 | ||
| 106 | public VersionInfo makeVersion(FileSystemPage page, PageData data) { | |
| 107 | final String dirPath = page.getFileSystemPath(); | |
| 108 | final Set filesToZip = getFilesToZip(dirPath); | |
| 109 | ||
| 110 | final VersionInfo version = makeVersionInfo(data); | |
| 111 | ||
| 112 | if (filesToZip.size() == 0) | |
| 113 | return new VersionInfo("first_commit", "", new Date()); | |
| 114 | ZipOutputStream zos = null; | |
| 115 | try { | |
| 116 | final String filename = makeVersionFileName(page, version.getName()); | |
| 117 | zos = new ZipOutputStream(new FileOutputStream(filename)); | |
| 118 | for (final Iterator iterator = filesToZip.iterator(); iterator.hasNext();) | |
| 119 | addToZip((File) iterator.next(), zos); | |
| 120 | return new VersionInfo(version.getName()); | |
| 121 | } catch (Throwable th) { | |
| 122 | throw new RuntimeException(th); | |
| 123 | } finally { | |
| 124 | try { | |
| 125 | zos.finish(); | |
| 126 | zos.close(); | |
| 127 | } catch (IOException e) { | |
| 128 | e.printStackTrace(); | |
| 129 | } | |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | public void prune(FileSystemPage page) { | |
| 134 | PageVersionPruner.pruneVersions(page, history(page)); | |
| 135 | } | |
| 136 | ||
| 137 | public void removeVersion(FileSystemPage page, String versionName) { | |
| 138 | final String versionFileName = makeVersionFileName(page, versionName); | |
| 139 | final File versionFile = new File(versionFileName); | |
| 140 | versionFile.delete(); | |
| 141 | } | |
| 142 | ||
| 143 | public void revert(String... filePaths) throws RevisionControlException { | |
| 144 | } | |
| 145 | ||
| 146 | public void update(String... filePaths) throws RevisionControlException { | |
| 147 | } | |
| 148 | ||
| 149 | private void addToZip(File file, ZipOutputStream zos) throws IOException { | |
| 150 | final ZipEntry entry = new ZipEntry(file.getName()); | |
| 151 | zos.putNextEntry(entry); | |
| 152 | final FileInputStream is = new FileInputStream(file); | |
| 153 | final int size = (int) file.length(); | |
| 154 | final byte[] bytes = new byte[size]; | |
| 155 | is.read(bytes); | |
| 156 | is.close(); | |
| 157 | zos.write(bytes, 0, size); | |
| 158 | } | |
| 159 | ||
| 160 | private Set getFilesToZip(String dirPath) { | |
| 161 | final Set<File> filesToZip = new HashSet<File>(); | |
| 162 | final File dir = new File(dirPath); | |
| 163 | final File[] files = dir.listFiles(); | |
| 164 | if (files == null) | |
| 165 | return filesToZip; | |
| 166 | for (final File file : files) | |
| 167 | if (!(isVersionFile(file) || file.isDirectory())) | |
| 168 | filesToZip.add(file); | |
| 169 | return filesToZip; | |
| 170 | } | |
| 171 | ||
| 172 | private boolean isVersionFile(File file) { | |
| 173 | return Pattern.matches("(\\S+)?\\d+\\.zip", file.getName()); | |
| 174 | } | |
| 175 | ||
| 176 | private void loadVersionAttributes(ZipFile zipFile, PageData data) { | |
| 177 | final ZipEntry attributes = zipFile.getEntry("properties.xml"); | |
| 178 | if (attributes != null) { | |
| 179 | InputStream attributeIS = null; | |
| 180 | try { | |
| 181 | attributeIS = zipFile.getInputStream(attributes); | |
| 182 | final WikiPageProperties props = new WikiPageProperties(attributeIS); | |
| 183 | data.setProperties(props); | |
| 184 | } catch (Throwable th) { | |
| 185 | throw new RuntimeException(th); | |
| 186 | } finally { | |
| 187 | try { | |
| 188 | attributeIS.close(); | |
| 189 | } catch (IOException e) { | |
| 190 | e.printStackTrace(); | |
| 191 | } | |
| 192 | } | |
| 193 | } | |
| 194 | } | |
| 195 | ||
| 196 | private void loadVersionContent(ZipFile zipFile, PageData data) { | |
| 197 | String content = ""; | |
| 198 | final ZipEntry contentEntry = zipFile.getEntry("content.txt"); | |
| 199 | if (contentEntry != null) { | |
| 200 | StreamReader reader = null; | |
| 201 | try { | |
| 202 | final InputStream contentIS = zipFile.getInputStream(contentEntry); | |
| 203 | reader = new StreamReader(contentIS); | |
| 204 | content = reader.read((int) contentEntry.getSize()); | |
| 205 | } catch (Throwable th) { | |
| 206 | throw new RuntimeException(th); | |
| 207 | } finally { | |
| 208 | reader.close(); | |
| 209 | } | |
| 210 | ||
| 211 | reader.close(); | |
| 212 | } | |
| 213 | data.setContent(content); | |
| 214 | } | |
| 215 | ||
| 216 | private Collection loadVersions(FileSystemPage page) { | |
| 217 | final File dir = new File(page.getFileSystemPath()); | |
| 218 | final File[] files = dir.listFiles(); | |
| 219 | final Set<VersionInfo> versions = new HashSet<VersionInfo>(); | |
| 220 | if (files != null) | |
| 221 | for (final File file : files) | |
| 222 | if (isVersionFile(file)) | |
| 223 | versions.add(new VersionInfo(makeVersionName(file))); | |
| 224 | return versions; | |
| 225 | } | |
| 226 | ||
| 227 | private String makeVersionFileName(FileSystemPage page, String name) { | |
| 228 | return page.getFileSystemPath() + "/" + name + ".zip"; | |
| 229 | } | |
| 230 | ||
| 231 | private VersionInfo makeVersionInfo(PageData data) { | |
| 232 | final Date time = data.getProperties().getLastModificationTime(); | |
| 233 | String versionName = VersionInfo.nextId() + "-" + dateFormat().format(time); | |
| 234 | final String user = data.getAttribute(WikiPage.LAST_MODIFYING_USER); | |
| 235 | if (user != null && !"".equals(user)) | |
| 236 | versionName = user + "-" + versionName; | |
| 237 | ||
| 238 | return new VersionInfo(versionName, user, time); | |
| 239 | } | |
| 240 | ||
| 241 | private String makeVersionName(File file) { | |
| 242 | final String name = file.getName(); | |
| 243 | return name.substring(0, name.length() - 4); | |
| 244 | } | |
| 245 | ||
| 246 | public String toString() { | |
| 247 | return this.getClass().getSimpleName(); | |
| 248 | } | |
| 249 | ||
| 250 | public String getControllerName() { | |
| 251 | return "Zipped Version History"; | |
| 252 | } | |
| 253 | ||
| 254 | } |
| ... | ...@@ -1,254 +0,0 @@ | |
| 1 | package fitnesse.revisioncontrol.zip; | |
| 2 | ||
| 3 | import static fitnesse.revisioncontrol.NullState.VERSIONED; | |
| 4 | ||
| 5 | import java.io.File; | |
| 6 | import java.io.FileInputStream; | |
| 7 | import java.io.FileOutputStream; | |
| 8 | import java.io.IOException; | |
| 9 | import java.io.InputStream; | |
| 10 | import java.text.SimpleDateFormat; | |
| 11 | import java.util.Collection; | |
| 12 | import java.util.Date; | |
| 13 | import java.util.HashSet; | |
| 14 | import java.util.Iterator; | |
| 15 | import java.util.Properties; | |
| 16 | import java.util.Set; | |
| 17 | import java.util.regex.Pattern; | |
| 18 | import java.util.zip.ZipEntry; | |
| 19 | import java.util.zip.ZipFile; | |
| 20 | import java.util.zip.ZipOutputStream; | |
| 21 | ||
| 22 | import fitnesse.revisioncontrol.RevisionControlException; | |
| 23 | import fitnesse.revisioncontrol.RevisionController; | |
| 24 | import fitnesse.revisioncontrol.State; | |
| 25 | import fitnesse.util.StreamReader; | |
| 26 | import fitnesse.wiki.FileSystemPage; | |
| 27 | import fitnesse.wiki.NoSuchVersionException; | |
| 28 | import fitnesse.wiki.PageData; | |
| 29 | import fitnesse.wiki.PageVersionPruner; | |
| 30 | import fitnesse.wiki.VersionInfo; | |
| 31 | import fitnesse.wiki.WikiPage; | |
| 32 | import fitnesse.wiki.WikiPageProperties; | |
| 33 | ||
| 34 | public class ZipFileRevisionController implements RevisionController { | |
| 35 | public static SimpleDateFormat dateFormat() { | |
| 36 | return new SimpleDateFormat("yyyyMMddHHmmss"); | |
| 37 | } | |
| 38 | ||
| 39 | public ZipFileRevisionController() { | |
| 40 | this(new Properties()); | |
| 41 | } | |
| 42 | ||
| 43 | public ZipFileRevisionController(Properties properties) { | |
| 44 | } | |
| 45 | ||
| 46 | public void add(String... filePaths) throws RevisionControlException { | |
| 47 | } | |
| 48 | ||
| 49 | public void checkin(String... filePaths) throws RevisionControlException { | |
| 50 | } | |
| 51 | ||
| 52 | public void checkout(String... filePaths) throws RevisionControlException { | |
| 53 | } | |
| 54 | ||
| 55 | public State checkState(String... filePaths) throws RevisionControlException { | |
| 56 | return VERSIONED; | |
| 57 | } | |
| 58 | ||
| 59 | public void delete(String... filePaths) throws RevisionControlException { | |
| 60 | } | |
| 61 | ||
| 62 | public PageData getRevisionData(FileSystemPage page, String label) { | |
| 63 | final String filename = page.getFileSystemPath() + "/" + label + ".zip"; | |
| 64 | final File file = new File(filename); | |
| 65 | if (!file.exists()) | |
| 66 | throw new NoSuchVersionException("There is no version '" + label + "'"); | |
| 67 | ||
| 68 | ZipFile zipFile = null; | |
| 69 | try { | |
| 70 | final PageData data = new PageData(page); | |
| 71 | zipFile = new ZipFile(file); | |
| 72 | loadVersionContent(zipFile, data); | |
| 73 | loadVersionAttributes(zipFile, data); | |
| 74 | data.addVersions(loadVersions(page)); | |
| 75 | return data; | |
| 76 | } catch (Throwable th) { | |
| 77 | throw new RuntimeException(th); | |
| 78 | } finally { | |
| 79 | try { | |
| 80 | zipFile.close(); | |
| 81 | } catch (IOException e) { | |
| 82 | e.printStackTrace(); | |
| 83 | } | |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 87 | public Collection<VersionInfo> history(FileSystemPage page) { | |
| 88 | final File dir = new File(page.getFileSystemPath()); | |
| 89 | final File[] files = dir.listFiles(); | |
| 90 | final Set<VersionInfo> versions = new HashSet<VersionInfo>(); | |
| 91 | if (files != null) | |
| 92 | for (final File file : files) | |
| 93 | if (isVersionFile(file)) | |
| 94 | versions.add(new VersionInfo(makeVersionName(file))); | |
| 95 | return versions; | |
| 96 | } | |
| 97 | ||
| 98 | public boolean isReversionControlEnabled() { | |
| 99 | return true; | |
| 100 | } | |
| 101 | ||
| 102 | public boolean isExternalReversionControlEnabled() { | |
| 103 | return false; | |
| 104 | } | |
| 105 | ||
| 106 | public VersionInfo makeVersion(FileSystemPage page, PageData data) { | |
| 107 | final String dirPath = page.getFileSystemPath(); | |
| 108 | final Set filesToZip = getFilesToZip(dirPath); | |
| 109 | ||
| 110 | final VersionInfo version = makeVersionInfo(data); | |
| 111 | ||
| 112 | if (filesToZip.size() == 0) | |
| 113 | return new VersionInfo("first_commit", "", new Date()); | |
| 114 | ZipOutputStream zos = null; | |
| 115 | try { | |
| 116 | final String filename = makeVersionFileName(page, version.getName()); | |
| 117 | zos = new ZipOutputStream(new FileOutputStream(filename)); | |
| 118 | for (final Iterator iterator = filesToZip.iterator(); iterator.hasNext();) | |
| 119 | addToZip((File) iterator.next(), zos); | |
| 120 | return new VersionInfo(version.getName()); | |
| 121 | } catch (Throwable th) { | |
| 122 | throw new RuntimeException(th); | |
| 123 | } finally { | |
| 124 | try { | |
| 125 | zos.finish(); | |
| 126 | zos.close(); | |
| 127 | } catch (IOException e) { | |
| 128 | e.printStackTrace(); | |
| 129 | } | |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | public void prune(FileSystemPage page) { | |
| 134 | PageVersionPruner.pruneVersions(page, history(page)); | |
| 135 | } | |
| 136 | ||
| 137 | public void removeVersion(FileSystemPage page, String versionName) { | |
| 138 | final String versionFileName = makeVersionFileName(page, versionName); | |
| 139 | final File versionFile = new File(versionFileName); | |
| 140 | versionFile.delete(); | |
| 141 | } | |
| 142 | ||
| 143 | public void revert(String... filePaths) throws RevisionControlException { | |
| 144 | } | |
| 145 | ||
| 146 | public void update(String... filePaths) throws RevisionControlException { | |
| 147 | } | |
| 148 | ||
| 149 | private void addToZip(File file, ZipOutputStream zos) throws IOException { | |
| 150 | final ZipEntry entry = new ZipEntry(file.getName()); | |
| 151 | zos.putNextEntry(entry); | |
| 152 | final FileInputStream is = new FileInputStream(file); | |
| 153 | final int size = (int) file.length(); | |
| 154 | final byte[] bytes = new byte[size]; | |
| 155 | is.read(bytes); | |
| 156 | is.close(); | |
| 157 | zos.write(bytes, 0, size); | |
| 158 | } | |
| 159 | ||
| 160 | private Set getFilesToZip(String dirPath) { | |
| 161 | final Set<File> filesToZip = new HashSet<File>(); | |
| 162 | final File dir = new File(dirPath); | |
| 163 | final File[] files = dir.listFiles(); | |
| 164 | if (files == null) | |
| 165 | return filesToZip; | |
| 166 | for (final File file : files) | |
| 167 | if (!(isVersionFile(file) || file.isDirectory())) | |
| 168 | filesToZip.add(file); | |
| 169 | return filesToZip; | |
| 170 | } | |
| 171 | ||
| 172 | private boolean isVersionFile(File file) { | |
| 173 | return Pattern.matches("(\\S+)?\\d+\\.zip", file.getName()); | |
| 174 | } | |
| 175 | ||
| 176 | private void loadVersionAttributes(ZipFile zipFile, PageData data) { | |
| 177 | final ZipEntry attributes = zipFile.getEntry("properties.xml"); | |
| 178 | if (attributes != null) { | |
| 179 | InputStream attributeIS = null; | |
| 180 | try { | |
| 181 | attributeIS = zipFile.getInputStream(attributes); | |
| 182 | final WikiPageProperties props = new WikiPageProperties(attributeIS); | |
| 183 | data.setProperties(props); | |
| 184 | } catch (Throwable th) { | |
| 185 | throw new RuntimeException(th); | |
| 186 | } finally { | |
| 187 | try { | |
| 188 | attributeIS.close(); | |
| 189 | } catch (IOException e) { | |
| 190 | e.printStackTrace(); | |
| 191 | } | |
| 192 | } | |
| 193 | } | |
| 194 | } | |
| 195 | ||
| 196 | private void loadVersionContent(ZipFile zipFile, PageData data) { | |
| 197 | String content = ""; | |
| 198 | final ZipEntry contentEntry = zipFile.getEntry("content.txt"); | |
| 199 | if (contentEntry != null) { | |
| 200 | StreamReader reader = null; | |
| 201 | try { | |
| 202 | final InputStream contentIS = zipFile.getInputStream(contentEntry); | |
| 203 | reader = new StreamReader(contentIS); | |
| 204 | content = reader.read((int) contentEntry.getSize()); | |
| 205 | } catch (Throwable th) { | |
| 206 | throw new RuntimeException(th); | |
| 207 | } finally { | |
| 208 | reader.close(); | |
| 209 | } | |
| 210 | ||
| 211 | reader.close(); | |
| 212 | } | |
| 213 | data.setContent(content); | |
| 214 | } | |
| 215 | ||
| 216 | private Collection loadVersions(FileSystemPage page) { | |
| 217 | final File dir = new File(page.getFileSystemPath()); | |
| 218 | final File[] files = dir.listFiles(); | |
| 219 | final Set<VersionInfo> versions = new HashSet<VersionInfo>(); | |
| 220 | if (files != null) | |
| 221 | for (final File file : files) | |
| 222 | if (isVersionFile(file)) | |
| 223 | versions.add(new VersionInfo(makeVersionName(file))); | |
| 224 | return versions; | |
| 225 | } | |
| 226 | ||
| 227 | private String makeVersionFileName(FileSystemPage page, String name) { | |
| 228 | return page.getFileSystemPath() + "/" + name + ".zip"; | |
| 229 | } | |
| 230 | ||
| 231 | private VersionInfo makeVersionInfo(PageData data) { | |
| 232 | final Date time = data.getProperties().getLastModificationTime(); | |
| 233 | String versionName = VersionInfo.nextId() + "-" + dateFormat().format(time); | |
| 234 | final String user = data.getAttribute(WikiPage.LAST_MODIFYING_USER); | |
| 235 | if (user != null && !"".equals(user)) | |
| 236 | versionName = user + "-" + versionName; | |
| 237 | ||
| 238 | return new VersionInfo(versionName, user, time); | |
| 239 | } | |
| 240 | ||
| 241 | private String makeVersionName(File file) { | |
| 242 | final String name = file.getName(); | |
| 243 | return name.substring(0, name.length() - 4); | |
| 244 | } | |
| 245 | ||
| 246 | public String toString() { | |
| 247 | return this.getClass().getSimpleName(); | |
| 248 | } | |
| 249 | ||
| 250 | public String getControllerName() { | |
| 251 | return "Zipped Version History"; | |
| 252 | } | |
| 253 | ||
| 254 | } |