1 package charactermanaj.model.io;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
8 import java.util.Collection;
9 import java.util.Collections;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
13 import charactermanaj.model.CharacterData;
14 import charactermanaj.util.FileNameNormalizer;
21 public class CharacterDataDirectoryFile extends AbstractCharacterDataArchiveFile {
26 private static final Logger logger = Logger.getLogger(CharacterDataDirectoryFile.class.getName());
32 protected File baseDir;
39 protected static class DirFileContent implements FileContent {
42 * ディレクトリ名 + ファイル名からなるエントリ名.<br>
43 * エントリ名の区切り文字は常に「/」とする.<br>
45 private String entryName;
52 protected DirFileContent(File entry, String entryName) {
54 this.entryName = entryName;
57 public String getEntryName() {
61 public long lastModified() {
62 return entry.lastModified();
65 public InputStream openStream() throws IOException {
66 return new FileInputStream(entry);
71 * アーカイブファイルをベースとしたURIを返す.<br>
79 protected URI getContentURI(String name) throws IOException {
80 return new File(baseDir, name).toURI();
84 public Collection<PartsImageContent> getPartsImageContents(
85 CharacterData characterData, boolean newly) {
86 if (!newly && isOverlapped(characterData)) {
87 // 既存のプロファイルへのインポートで指定されたインポートもととなるディレクトリが
88 // 既存のプロファイルのディレクトリと重なっていれば自分自身のインポートであるとして
90 return Collections.emptyList();
92 return super.getPartsImageContents(characterData, newly);
96 * このディレクトリに対してターゲットプロファイルのディレクトリがかぶっているか? つまり、ターゲット自身のディレクトリをソースとしていないか?
98 * @param characterData
100 * @return 被っている場合はtrue、被っていない場合はfalse
102 protected boolean isOverlapped(CharacterData characterData) {
103 if (characterData == null) {
106 URI docBase = characterData.getDocBase();
107 if (docBase == null || !"file".equals(docBase.getScheme())) {
111 String folderPlace = File.separator;
112 String basePath = new File(docBase).getParent() + folderPlace;
113 String sourcePath = baseDir.getPath() + folderPlace;
115 // インポートもとディレクトリがインポート先のキャラクター定義のディレクトリを含むか?
116 boolean result = basePath.contains(sourcePath);
117 logger.log(Level.FINE, "checkOverlapped: " + basePath + " * " + sourcePath + " :" + result);
122 public void close() throws IOException {
123 // ディレクトリなのでクローズ処理は必要ない.
126 public CharacterDataDirectoryFile(File file) throws IOException {
133 private void load(File dir, String prefix) {
134 if (!dir.exists() || !dir.isDirectory()) {
140 FileNameNormalizer normalizer = FileNameNormalizer.getDefault();
142 File[] files = dir.listFiles();
144 for (File file : files) {
145 String name = normalizer.normalize(file.getName());
146 String entryName = prefix + name;
147 if (file.isDirectory()) {
148 // エントリ名の区切り文字は常に「/」とする. (ZIP/JARのentry互換のため)
149 load(file, entryName + "/");
151 addEntry(new DirFileContent(file, entryName));