/wonderland/web/wfs/src/java/org/jdesktop/wonderland/tools/wfs/file/FileCellDelegate.java

https://github.com/jehc/MondocosmOS · Java · 168 lines · 73 code · 14 blank · 81 comment · 4 complexity · 8fa741b62ee4920f0e0d80149ab055a9 MD5 · raw file

  1. /**
  2. * Project Wonderland
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., All Rights Reserved
  5. *
  6. * Redistributions in source code form must reproduce the above
  7. * copyright and this condition.
  8. *
  9. * The contents of this file are subject to the GNU General Public
  10. * License, Version 2 (the "License"); you may not use this file
  11. * except in compliance with the License. A copy of the License is
  12. * available at http://www.opensource.org/licenses/gpl-license.php.
  13. *
  14. * Sun designates this particular file as subject to the "Classpath"
  15. * exception as provided by Sun in the License file that accompanied
  16. * this code.
  17. */
  18. package org.jdesktop.wonderland.tools.wfs.file;
  19. import java.io.BufferedReader;
  20. import java.io.BufferedWriter;
  21. import java.io.File;
  22. import java.io.FileReader;
  23. import java.io.FileWriter;
  24. import java.io.IOException;
  25. import java.util.logging.Logger;
  26. import org.jdesktop.wonderland.tools.wfs.WFS;
  27. import org.jdesktop.wonderland.tools.wfs.WFSCell;
  28. import org.jdesktop.wonderland.tools.wfs.delegate.CellDelegate;
  29. import org.jdesktop.wonderland.tools.wfs.delegate.DirectoryDelegate;
  30. /**
  31. * The FileCellDelegate class implements methods to support accessing WFS files
  32. * on disk.
  33. *
  34. * @author Jordan Slott <jslott@dev.java.net>
  35. */
  36. public class FileCellDelegate implements CellDelegate {
  37. /* The File of the referring WFS Directory */
  38. private File file = null;
  39. /** Constructor, takes a new instance of the cell file */
  40. public FileCellDelegate(File file) {
  41. this.file = file;
  42. }
  43. /**
  44. * Creates a new directory delegate for the cell directory containing the
  45. * child cells of this cell.
  46. *
  47. * @return A new directory delegate
  48. */
  49. public DirectoryDelegate createDirectoryDelegate() {
  50. /*
  51. * Parse off the -wlc.xml suffix from the cell file and put a -wld
  52. * suffix instead.
  53. */
  54. Logger logger = WFS.getLogger();
  55. try {
  56. String filePath = this.getFile().getAbsolutePath();
  57. int index = filePath.indexOf(WFS.CELL_FILE_SUFFIX);
  58. String path = filePath.substring(0, index) + WFS.CELL_DIRECTORY_SUFFIX;
  59. File newFile = new File(path);
  60. return new FileDirectoryDelegate(newFile);
  61. } catch (java.lang.IndexOutOfBoundsException excp) {
  62. /* Quietly return null */
  63. }
  64. return null;
  65. }
  66. /**
  67. * Calculate the last modified date of the file this cell represents
  68. * @return the time this file was last modified
  69. */
  70. public long getLastModified() {
  71. return this.getFile().lastModified();
  72. }
  73. /**
  74. * Returns true if the cell directory associated with this cell exists,
  75. * false if not.
  76. *
  77. * @return True if the cell's directory exists, false if not
  78. */
  79. public boolean cellDirectoryExists() {
  80. /*
  81. * Parse off the cell file suffix. Create a new file object, and
  82. * check if it exists
  83. */
  84. String fileName = this.getFile().getAbsolutePath();
  85. String path = WFSCell.stripCellFileSuffix(fileName) + WFS.CELL_DIRECTORY_SUFFIX;
  86. File newFile = new File(path);
  87. return newFile.exists();
  88. }
  89. /**
  90. * Creates the cell's directory on the medium, if it does not exist
  91. */
  92. public void createCellDirectory() {
  93. /*
  94. * Parse off the cell file suffix. Create a new file object, and
  95. * if it does not exist, create it
  96. */
  97. String fileName = this.getFile().getAbsolutePath();
  98. String path = WFSCell.stripCellFileSuffix(fileName) + WFS.CELL_DIRECTORY_SUFFIX;
  99. File newFile = new File(path);
  100. if (newFile.exists() == false) {
  101. newFile.mkdir();
  102. }
  103. }
  104. /**
  105. * Removes the cell's directory on the medium, if it exists.
  106. */
  107. public void removeCellDirectory() {
  108. /*
  109. * Parse off the cell file suffix. Create a new file object, and
  110. * if it exists, delete it
  111. */
  112. String fileName = this.getFile().getAbsolutePath();
  113. String path = WFSCell.stripCellFileSuffix(fileName) + WFS.CELL_DIRECTORY_SUFFIX;
  114. File newFile = new File(path);
  115. WFSFileUtils.deleteDirectory(newFile);
  116. }
  117. /**
  118. * Returns the File associated with this WFS file
  119. */
  120. private File getFile() {
  121. return this.file;
  122. }
  123. /**
  124. * Returns the cell's setup information, encoded as a String.
  125. *
  126. * @throw IOException Upon general I/O error
  127. */
  128. public String decode() throws IOException {
  129. /*
  130. * Read the data from disk as a string and return
  131. */
  132. BufferedReader reader = new BufferedReader(new FileReader(this.getFile()));
  133. StringBuffer data = new StringBuffer();
  134. char buf[] = new char[4 * 1024];
  135. int ret = -1;
  136. while ((ret = reader.read(buf)) != -1) {
  137. data.append(buf, 0, ret);
  138. }
  139. reader.close();
  140. return data.toString();
  141. }
  142. /**
  143. * Updates the cell's setup information, encoded as a String.
  144. *
  145. * @param cellSetup The cell setup properties
  146. * @throw IOException Upon general I/O error
  147. */
  148. public void encode(String cellSetup) throws IOException {
  149. BufferedWriter writer = new BufferedWriter(new FileWriter(this.getFile()));
  150. writer.write(cellSetup, 0, cellSetup.length());
  151. writer.flush();
  152. writer.close();
  153. }
  154. }