mirror of
https://github.com/lacerta-doc/Lacerta.git
synced 2024-11-23 00:13:16 +00:00
Merge pull request #30 from lacerta-doc/improve/improve_file_manager
FileManagerの修正
This commit is contained in:
commit
3f2534b1eb
|
@ -169,7 +169,12 @@ public class ScannerDataManagerStubFragment extends Fragment {
|
||||||
}
|
}
|
||||||
this.documentProcessor = documentProcessorFactory.create(this.documentDetail);
|
this.documentProcessor = documentProcessorFactory.create(this.documentDetail);
|
||||||
Toast.makeText(getActivity(), "documentProcessor created", Toast.LENGTH_LONG).show();
|
Toast.makeText(getActivity(), "documentProcessor created", Toast.LENGTH_LONG).show();
|
||||||
|
try {
|
||||||
this.documentProcessor.init();
|
this.documentProcessor.init();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Toast.makeText(getActivity(), "Error occurred while initializing documentProcessor", Toast.LENGTH_LONG).show();
|
||||||
|
Log.e("ScannerDataManagerStubFragment", "Error occurred while initializing documentProcessor", e);
|
||||||
|
}
|
||||||
Toast.makeText(getActivity(), "documentProcessor initialized", Toast.LENGTH_LONG).show();
|
Toast.makeText(getActivity(), "documentProcessor initialized", Toast.LENGTH_LONG).show();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -185,9 +190,21 @@ public class ScannerDataManagerStubFragment extends Fragment {
|
||||||
bitmaps[i] = results.get(i).getBitmap();
|
bitmaps[i] = results.get(i).getBitmap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
this.documentProcessor.addNewPagesToLast(bitmaps);
|
this.documentProcessor.addNewPagesToLast(bitmaps);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Toast.makeText(getActivity(), "Error occurred while adding pages", Toast.LENGTH_LONG).show();
|
||||||
|
Log.e("ScannerDataManagerStubFragment", "Error occurred while adding pages", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
Toast.makeText(getActivity(), "pages added", Toast.LENGTH_LONG).show();
|
||||||
|
|
||||||
|
try {
|
||||||
this.documentProcessor.close();
|
this.documentProcessor.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Toast.makeText(getActivity(), "Error occurred while closing documentProcessor", Toast.LENGTH_LONG).show();
|
||||||
|
Log.e("ScannerDataManagerStubFragment", "Error occurred while closing documentProcessor", e);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,46 +63,46 @@ public class DebugMenuFileManagerTesterFragment extends Fragment {
|
||||||
EditText editText = view.findViewById(R.id.edit_text_dir_name);
|
EditText editText = view.findViewById(R.id.edit_text_dir_name);
|
||||||
String dirName = editText.getText().toString();
|
String dirName = editText.getText().toString();
|
||||||
|
|
||||||
FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory());
|
// FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory());
|
||||||
fileManager.createDir(dirName);
|
// fileManager.createDir(dirName);
|
||||||
});
|
});
|
||||||
|
|
||||||
view.findViewById(R.id.button_save_item).setOnClickListener(v -> {
|
view.findViewById(R.id.button_save_item).setOnClickListener(v -> {
|
||||||
FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory());
|
// FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory());
|
||||||
fileManager.createDir("test");
|
// fileManager.createDir("test");
|
||||||
fileManager.changeDir("test");
|
// fileManager.changeDir("test");
|
||||||
Bitmap bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
|
// Bitmap bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
|
||||||
// Bitmapに描画処理を行う
|
// // Bitmapに描画処理を行う
|
||||||
Canvas canvas = new Canvas(bitmap);
|
// Canvas canvas = new Canvas(bitmap);
|
||||||
// 大きな山の形状を作成
|
// // 大きな山の形状を作成
|
||||||
android.graphics.Path bigMountainPath = new android.graphics.Path();
|
// android.graphics.Path bigMountainPath = new android.graphics.Path();
|
||||||
bigMountainPath.moveTo(100, 800); // 左下の開始点
|
// bigMountainPath.moveTo(100, 800); // 左下の開始点
|
||||||
bigMountainPath.lineTo(500, 300); // 頂点
|
// bigMountainPath.lineTo(500, 300); // 頂点
|
||||||
bigMountainPath.lineTo(900, 800); // 右下
|
// bigMountainPath.lineTo(900, 800); // 右下
|
||||||
bigMountainPath.close(); // パスを閉じる
|
// bigMountainPath.close(); // パスを閉じる
|
||||||
|
//
|
||||||
// 山の描画設定
|
// // 山の描画設定
|
||||||
Paint mountainPaint = new Paint();
|
// Paint mountainPaint = new Paint();
|
||||||
mountainPaint.setColor(Color.GREEN);
|
// mountainPaint.setColor(Color.GREEN);
|
||||||
mountainPaint.setStyle(Paint.Style.FILL);
|
// mountainPaint.setStyle(Paint.Style.FILL);
|
||||||
|
//
|
||||||
// 大きな山を描画
|
// // 大きな山を描画
|
||||||
canvas.drawPath(bigMountainPath, mountainPaint);
|
// canvas.drawPath(bigMountainPath, mountainPaint);
|
||||||
|
//
|
||||||
// 小さな山の形状を作成
|
// // 小さな山の形状を作成
|
||||||
android.graphics.Path smallMountainPath = new android.graphics.Path();
|
// android.graphics.Path smallMountainPath = new android.graphics.Path();
|
||||||
smallMountainPath.moveTo(400, 800); // 左下の開始点
|
// smallMountainPath.moveTo(400, 800); // 左下の開始点
|
||||||
smallMountainPath.lineTo(650, 400); // 頂点
|
// smallMountainPath.lineTo(650, 400); // 頂点
|
||||||
smallMountainPath.lineTo(900, 800); // 右下
|
// smallMountainPath.lineTo(900, 800); // 右下
|
||||||
smallMountainPath.close(); // パスを閉じる
|
// smallMountainPath.close(); // パスを閉じる
|
||||||
|
//
|
||||||
Paint smallMountainPaint = new Paint();
|
// Paint smallMountainPaint = new Paint();
|
||||||
smallMountainPaint.setColor(Color.parseColor("#006e54"));
|
// smallMountainPaint.setColor(Color.parseColor("#006e54"));
|
||||||
smallMountainPaint.setStyle(Paint.Style.FILL);
|
// smallMountainPaint.setStyle(Paint.Style.FILL);
|
||||||
|
//
|
||||||
// 小さな山を描画
|
// // 小さな山を描画
|
||||||
canvas.drawPath(smallMountainPath, smallMountainPaint);
|
// canvas.drawPath(smallMountainPath, smallMountainPaint);
|
||||||
fileManager.saveBitmapAtCurrent(bitmap, "test.png");
|
// fileManager.saveBitmapAtCurrent(bitmap, "test.png");
|
||||||
});
|
});
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
|
|
|
@ -5,8 +5,8 @@ import android.graphics.Bitmap;
|
||||||
public interface DocumentProcessor {
|
public interface DocumentProcessor {
|
||||||
|
|
||||||
// ページ操作
|
// ページ操作
|
||||||
void addNewPageToLast(Bitmap bitmap);
|
void addNewPageToLast(Bitmap bitmap) throws Exception;
|
||||||
void addNewPagesToLast(Bitmap[] bitmaps);
|
void addNewPagesToLast(Bitmap[] bitmaps) throws Exception;
|
||||||
void addNewPageAfterIndex(Bitmap bitmap, int index);
|
void addNewPageAfterIndex(Bitmap bitmap, int index);
|
||||||
void addNewPageBeforeIndex(Bitmap bitmap, int index);
|
void addNewPageBeforeIndex(Bitmap bitmap, int index);
|
||||||
void removePageAtIndex(int index);
|
void removePageAtIndex(int index);
|
||||||
|
@ -18,7 +18,7 @@ public interface DocumentProcessor {
|
||||||
Bitmap getPageAtIndex(int index);
|
Bitmap getPageAtIndex(int index);
|
||||||
int getPageCount();
|
int getPageCount();
|
||||||
|
|
||||||
void close();
|
void close() throws Exception;
|
||||||
void init();
|
void init() throws Exception; // TODO-rca: 例外処理
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,25 +49,24 @@ public class DocumentProcessorImpl implements DocumentProcessor{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() throws Exception{
|
||||||
logger.debug("init", "called");
|
logger.debug("init", "called");
|
||||||
// Init Variables
|
// Init Variables
|
||||||
this.documentRootPath = this.documentDetail.getPath().getFullPath();
|
this.documentRootPath = this.documentDetail.getPath().getFullPath();
|
||||||
logger.debug("init", "documentRootPath: " + this.documentRootPath);
|
logger.debug("init", "documentRootPath: " + this.documentRootPath);
|
||||||
|
|
||||||
this.fileManager = fileManagerFactory.create(this.documentRootPath); //Initialize FileManager
|
this.fileManager = fileManagerFactory.create(this.documentRootPath).enableAutoCreateParent(); //Initialize FileManager
|
||||||
|
|
||||||
|
FileManager initFileManager = this.fileManager.getNewInstance();
|
||||||
|
|
||||||
logger.debug("init", "fileManager created");
|
logger.debug("init", "fileManager created");
|
||||||
|
|
||||||
this.fileManager.autoCreateDir(this.documentRootPath);
|
|
||||||
|
|
||||||
// rawディレクトリInit
|
|
||||||
this.fileManager.autoCreateDir(DEFAULT_SAVE_DIR);
|
|
||||||
|
|
||||||
// xmlファイルの読み込み
|
// xmlファイルの読み込み
|
||||||
if (fileManager.isExist("meta.xml")) {
|
if (initFileManager.isExist("meta.xml")) {
|
||||||
logger.debug("init", "meta.xml found");
|
logger.debug("init", "meta.xml found");
|
||||||
try {
|
try {
|
||||||
xmlMetaModel = xmlMetaParser.deserialize(this.fileManager.loadDocument("meta.xml"));
|
this.xmlMetaModel = xmlMetaParser.deserialize(this.fileManager.loadXml("meta.xml"));
|
||||||
|
logger.debug("init", "meta.xml parsed");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.debug("init", "meta.xml parse failed");
|
logger.debug("init", "meta.xml parse failed");
|
||||||
logger.trace("init", e.getMessage());
|
logger.trace("init", e.getMessage());
|
||||||
|
@ -83,7 +82,7 @@ public class DocumentProcessorImpl implements DocumentProcessor{
|
||||||
xmlMetaModel.setPages(new ArrayList<>());
|
xmlMetaModel.setPages(new ArrayList<>());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.fileManager.saveDocument(xmlMetaParser.serialize(xmlMetaModel), "meta.xml");
|
initFileManager.createFileIfNotExist("meta.xml").saveXml(xmlMetaParser.serialize(xmlMetaModel), "meta.xml");
|
||||||
logger.debug("init", "meta.xml saved");
|
logger.debug("init", "meta.xml saved");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("init", "meta.xml save failed");
|
logger.error("init", "meta.xml save failed");
|
||||||
|
@ -95,33 +94,23 @@ public class DocumentProcessorImpl implements DocumentProcessor{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addNewPageToLast(Bitmap bitmap) {
|
public void addNewPageToLast(Bitmap bitmap) throws Exception{
|
||||||
logger.debug("addNewPageToLast", "called");
|
logger.debug("addNewPageToLast", "called");
|
||||||
String filename = UUID.randomUUID().toString() + ".png"; // TODO-rca: 拡張子を動的にする
|
String filename = UUID.randomUUID().toString() + ".png"; // TODO-rca: 拡張子を動的にする
|
||||||
|
|
||||||
// FileManager
|
this.fileManager.getNewInstance().createDirectoryIfNotExist(DEFAULT_SAVE_DIR).resolve(DEFAULT_SAVE_DIR).saveBitmap(bitmap, filename);
|
||||||
if (this.fileManager.getCurrentDir().equals(this.documentRootPath.resolve(DEFAULT_SAVE_DIR))) { // TODO-rca: 効率化
|
|
||||||
logger.debug("addNewPageToLast", "currentDir is documentRootPath");
|
|
||||||
} else {
|
|
||||||
logger.debug("addNewPageToLast", "currentDir is not documentRootPath");
|
|
||||||
this.fileManager.backRootDir();
|
|
||||||
this.fileManager.autoCreateDir(DEFAULT_SAVE_DIR);
|
|
||||||
this.fileManager.changeDir(DEFAULT_SAVE_DIR);
|
|
||||||
}
|
|
||||||
logger.debug("addNewPageToLast", "DirInit finished");
|
|
||||||
|
|
||||||
// Save file
|
XmlMetaPageModel xmlMetaPageModel = new XmlMetaPageModel();
|
||||||
this.fileManager.saveBitmapAtCurrent(bitmap, filename);
|
xmlMetaPageModel.setFilename(filename);
|
||||||
|
xmlMetaPageModel.setIndex(xmlMetaModel.getPages().size() + 1);
|
||||||
|
xmlMetaModel.getPages().add(xmlMetaPageModel);
|
||||||
|
|
||||||
// Update meta
|
logger.info("addNewPageToLast", "finished");
|
||||||
XmlMetaPageModel page = new XmlMetaPageModel();
|
logger.info("addNewPageToLast", "filename: " + filename + ", index: " + xmlMetaPageModel.getIndex());
|
||||||
page.setIndex(xmlMetaModel.getPages().size() + 1);
|
|
||||||
page.setFilename(filename);
|
|
||||||
xmlMetaModel.addPage(page);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addNewPagesToLast(Bitmap[] bitmaps) {
|
public void addNewPagesToLast(Bitmap[] bitmaps) throws Exception{
|
||||||
logger.debug("addNewPagesToLast", "called");
|
logger.debug("addNewPagesToLast", "called");
|
||||||
|
|
||||||
for (Bitmap bitmap : bitmaps) {
|
for (Bitmap bitmap : bitmaps) {
|
||||||
|
@ -160,18 +149,14 @@ public class DocumentProcessorImpl implements DocumentProcessor{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() throws Exception{
|
||||||
logger.debug("close", "called");
|
logger.debug("close", "called");
|
||||||
// TODO-rca: ここでxmlファイルを保存する
|
|
||||||
this.fileManager.backRootDir();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.fileManager.saveDocument(xmlMetaParser.serialize(xmlMetaModel), "meta.xml");
|
this.fileManager.getNewInstance().createFileIfNotExist("meta.xml").saveXml(xmlMetaParser.serialize(xmlMetaModel), "meta.xml");
|
||||||
logger.debug("close", "meta.xml saved");
|
logger.debug("close", "meta.xml saved");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("close", "meta.xml save failed");
|
logger.error("close", "meta.xml save failed");
|
||||||
logger.trace("close", e.getMessage());
|
logger.trace("close", e.getMessage());
|
||||||
}
|
}
|
||||||
logger.info("close", "finished");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import android.graphics.Bitmap;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -12,48 +14,52 @@ import java.util.List;
|
||||||
/** @noinspection unused*/
|
/** @noinspection unused*/
|
||||||
public interface FileManager {
|
public interface FileManager {
|
||||||
|
|
||||||
Path getRootDir();
|
File getFileRef();
|
||||||
Path getCurrentDir();
|
boolean isExist(String name) throws IOException;
|
||||||
|
boolean isExist();
|
||||||
|
boolean isDirectory();
|
||||||
|
boolean isFile();
|
||||||
|
boolean isWritable();
|
||||||
|
boolean isReadable();
|
||||||
|
// Get current instance
|
||||||
|
FileManager getCurrentInstance();
|
||||||
|
FileManager getNewInstance();
|
||||||
|
|
||||||
void changeDir(String dirName); //cd
|
// Configure
|
||||||
void changeDir(Path path); //cd
|
FileManager enableAutoCreateParent();
|
||||||
void backDir(); //cd ..
|
FileManager disableRootDirCheck();
|
||||||
void backRootDir(); //cd /
|
|
||||||
List<Path> getList();
|
|
||||||
void createDir(String dirName);
|
|
||||||
void createDir(Path path);
|
|
||||||
void removeDir(String dirName);
|
|
||||||
void removeDir(Path path);
|
|
||||||
|
|
||||||
File createFile(String fileName);
|
FileManager setRootDir(Path rootDir);
|
||||||
void removeFile(String fileName);
|
FileManager setPath(Path path);
|
||||||
|
FileManager resolve(String path) throws IOException;
|
||||||
|
|
||||||
File getFile(String fileName);
|
// Create
|
||||||
File getFile(Path path);
|
FileManager createFile() throws IOException;
|
||||||
|
FileManager createFile(String fileName) throws IOException;
|
||||||
|
FileManager createFileIfNotExist() throws IOException;
|
||||||
|
FileManager createFileIfNotExist(String fileName) throws IOException;
|
||||||
|
FileManager createDirectory() throws IOException;
|
||||||
|
FileManager createDirectory(String directoryName) throws IOException;
|
||||||
|
FileManager createDirectoryIfNotExist() throws IOException;
|
||||||
|
FileManager createDirectoryIfNotExist(String directoryName) throws IOException;
|
||||||
|
|
||||||
String loadText(String fileName);
|
// Save
|
||||||
String loadText(Path path);
|
// XML
|
||||||
|
void saveXml(Document document, String fileName) throws IOException;
|
||||||
|
void saveXml(Document document) throws IOException;
|
||||||
|
|
||||||
void saveText(String text, String fileName);
|
// Bitmap
|
||||||
void saveText(String text, Path path);
|
void saveBitmap(Bitmap bitmap, String fileName) throws IOException; // TODO-rca: パラメータに対応させる
|
||||||
|
void saveBitmap(Bitmap bitmap) throws IOException; // TODO-rca: パラメータに対応させる
|
||||||
void saveDocument(Document document, String fileName);
|
|
||||||
void saveDocument(Document document, Path path);
|
|
||||||
|
|
||||||
Document loadDocument(String fileName);
|
|
||||||
Document loadDocument(Path path);
|
|
||||||
|
|
||||||
|
|
||||||
boolean isExist(Path path);
|
// Load
|
||||||
boolean isExist(String fileName);
|
// XML
|
||||||
|
Document loadXml(String fileName) throws IOException;
|
||||||
|
Document loadXml() throws IOException;
|
||||||
|
|
||||||
void autoCreateDir(Path path);
|
// Bitmap
|
||||||
void autoCreateDir(String dirName);
|
Bitmap loadBitmap(String fileName) throws IOException;
|
||||||
|
Bitmap loadBitmap() throws IOException;
|
||||||
void autoCreateToCurrentDir();
|
|
||||||
|
|
||||||
void saveBitmapAtCurrent(Bitmap bitmap, String fileName);
|
|
||||||
Bitmap loadBitmap(Path path);
|
|
||||||
void removeBitmap(Path path);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,14 @@
|
||||||
package one.nem.lacerta.source.file.impl;
|
package one.nem.lacerta.source.file.impl;
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.DirectoryStream;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
@ -28,320 +25,340 @@ import one.nem.lacerta.utils.LacertaLogger;
|
||||||
|
|
||||||
public class FileManagerImpl implements FileManager {
|
public class FileManagerImpl implements FileManager {
|
||||||
|
|
||||||
// RootDir
|
// variables
|
||||||
private Path rootDir;
|
private Path rootDir;
|
||||||
|
private Path path;
|
||||||
|
private boolean autoCreateParent = false;
|
||||||
|
private boolean disableRootDirCheck = false;
|
||||||
|
|
||||||
// CurrentDir
|
|
||||||
private Path currentDir;
|
|
||||||
|
|
||||||
// Internal Methods
|
// Injection
|
||||||
private Path convertPath(String path) {
|
private final LacertaLogger logger;
|
||||||
Path convertedPath = currentDir.resolve(path);
|
@AssistedInject
|
||||||
if (convertedPath.startsWith(rootDir)) { // 異常なパスの場合はnullを返す // TODO-rca: エラーハンドリング
|
public FileManagerImpl(LacertaLogger logger, @Assisted Path rootDir) {
|
||||||
return convertedPath;
|
this.logger = logger;
|
||||||
|
this.rootDir = rootDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
// for generate new instance
|
||||||
|
public FileManagerImpl(LacertaLogger logger, Path rootDir, Path path, boolean autoCreateParent, boolean disableRootDirCheck) {
|
||||||
|
this.logger = logger;
|
||||||
|
this.rootDir = rootDir;
|
||||||
|
this.path = path;
|
||||||
|
this.autoCreateParent = autoCreateParent;
|
||||||
|
this.disableRootDirCheck = disableRootDirCheck;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internal
|
||||||
|
private Path resolveStringPath(String path) throws IOException{
|
||||||
|
String[] pathArray = path.split("/");
|
||||||
|
Path resolvedPath = this.path;
|
||||||
|
for (String pathPart : pathArray) {
|
||||||
|
if (pathPart.equals("..")) {
|
||||||
|
resolvedPath = resolvedPath.getParent();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
resolvedPath = resolvedPath.resolve(pathPart);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException("Invalid path: " + path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.debug("resolveStringPath", "resolvedPath: " + resolvedPath);
|
||||||
|
return resolvedPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileManager newInstance(Path rootDir, Path path, boolean autoCreateParent, boolean disableRootDirCheck) {
|
||||||
|
logger.debug("newInstance", "Generating new instance");
|
||||||
|
logger.debug("newInstance", "Path: " + path);
|
||||||
|
return new FileManagerImpl(this.logger, rootDir, path, autoCreateParent, disableRootDirCheck);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getFileRef() {
|
||||||
|
if (this.isExist()) {
|
||||||
|
return this.path.toFile();
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Injection
|
@Override
|
||||||
private LacertaLogger logger;
|
public boolean isExist(String name) throws IOException {
|
||||||
|
Path resolvedPath = this.resolveStringPath(name);
|
||||||
@AssistedInject
|
return Files.exists(resolvedPath);
|
||||||
public FileManagerImpl(LacertaLogger logger, @Assisted Path rootDir) {
|
|
||||||
this.logger = logger;
|
|
||||||
this.rootDir = rootDir;
|
|
||||||
this.currentDir = rootDir;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Path getRootDir() {
|
public boolean isExist(){
|
||||||
return rootDir;
|
return Files.exists(this.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Path getCurrentDir() {
|
public boolean isDirectory() {
|
||||||
return currentDir;
|
if (this.isExist()) {
|
||||||
}
|
File file = this.path.toFile();
|
||||||
|
return file.isDirectory();
|
||||||
@Override
|
} else {
|
||||||
public void changeDir(String dirName) {
|
return false;
|
||||||
this.currentDir = rootDir.resolve(dirName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void changeDir(Path path) {
|
|
||||||
if (path.startsWith(rootDir)) {
|
|
||||||
this.currentDir = path;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
logger.debug("changeDir", "invalid path: " + path);
|
|
||||||
// TODO-rca: 例外を投げる
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void backDir() {
|
public boolean isFile() {
|
||||||
this.currentDir = currentDir.getParent();
|
if (this.isExist()) {
|
||||||
|
File file = this.path.toFile();
|
||||||
|
return file.isFile();
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void backRootDir() {
|
public boolean isReadable() {
|
||||||
this.currentDir = rootDir;
|
if (this.isExist()) {
|
||||||
|
File file = this.path.toFile();
|
||||||
|
return file.canRead();
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Path> getList() {
|
public boolean isWritable() {
|
||||||
List<Path> list = new ArrayList<>();
|
if (this.isExist()) {
|
||||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(currentDir)) {
|
File file = this.path.toFile();
|
||||||
for (Path entry : stream) { // TODO-rca: エラーハンドリング, 効率化
|
return file.canWrite();
|
||||||
list.add(entry);
|
} else {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createDir(String dirName) {
|
public FileManager getCurrentInstance() {
|
||||||
//ディレクトリ作成
|
return this;
|
||||||
logger.debug("createDir", "called");
|
}
|
||||||
|
|
||||||
Path path = currentDir.resolve(dirName);
|
@Override
|
||||||
logger.debug("createDir", "path: " + path);
|
public FileManager getNewInstance() {
|
||||||
|
return this.newInstance(this.rootDir, this.rootDir, this.autoCreateParent, this.disableRootDirCheck);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager enableAutoCreateParent() {
|
||||||
|
this.autoCreateParent = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager disableRootDirCheck() {
|
||||||
|
this.disableRootDirCheck = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager setRootDir(Path rootDir) {
|
||||||
|
return this.newInstance(rootDir, this.path, this.autoCreateParent, this.disableRootDirCheck);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager setPath(Path path) {
|
||||||
|
Path resolvedPath;
|
||||||
|
if (this.disableRootDirCheck) {
|
||||||
|
resolvedPath = path;
|
||||||
|
} else {
|
||||||
|
if (path.startsWith(this.rootDir)) {
|
||||||
|
resolvedPath = path;
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("path must be in rootDir");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.debug("setPath", "resolvedPath: " + resolvedPath);
|
||||||
|
return this.newInstance(this.rootDir, resolvedPath, this.autoCreateParent, this.disableRootDirCheck);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager resolve(String path) throws IOException{
|
||||||
|
Path resolvedPath;
|
||||||
try {
|
try {
|
||||||
Files.createDirectory(path);
|
resolvedPath = resolveStringPath(path);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
logger.error("resolve", e.getMessage());
|
||||||
|
throw new IOException("Invalid path: " + path);
|
||||||
}
|
}
|
||||||
|
return this.setPath(resolvedPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// Internal
|
||||||
public void createDir(Path path) {
|
private void createFileInternal(Path path) throws IOException {
|
||||||
logger.debug("createDir", "called");
|
|
||||||
try {
|
try {
|
||||||
Files.createDirectory(path);
|
if (this.autoCreateParent) {
|
||||||
} catch (IOException e) {
|
if (!path.getParent().toFile().exists()) {
|
||||||
e.printStackTrace();
|
Files.createDirectories(path.getParent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeDir(String dirName) {
|
|
||||||
logger.debug("removeDir", "called");
|
|
||||||
currentDir.resolve(dirName).toFile().delete(); // TODO-rca: エラーハンドリング
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeDir(Path path) {
|
|
||||||
logger.debug("removeDir", "called");
|
|
||||||
path.toFile().delete(); // TODO-rca: エラーハンドリング
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public File createFile(String fileName) {
|
|
||||||
logger.debug("createFile", "called");
|
|
||||||
return currentDir.resolve(fileName).toFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeFile(String fileName) {
|
|
||||||
logger.debug("removeFile", "called");
|
|
||||||
currentDir.resolve(fileName).toFile().delete(); // TODO-rca: エラーハンドリング
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public File getFile(String fileName) {
|
|
||||||
logger.debug("getFile", "called");
|
|
||||||
return currentDir.resolve(fileName).toFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public File getFile(Path path) {
|
|
||||||
logger.debug("getFile", "called");
|
|
||||||
return path.toFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String loadText(String fileName) { // TODO-rca: 統合
|
|
||||||
try(FileInputStream fileInputStream = new FileInputStream(currentDir.resolve(fileName).toFile())) {
|
|
||||||
byte[] bytes = new byte[fileInputStream.available()];
|
|
||||||
fileInputStream.read(bytes); // TODO-rca: エラーハンドリング
|
|
||||||
return new String(bytes);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String loadText(Path path) {
|
|
||||||
try(FileInputStream fileInputStream = new FileInputStream(path.toFile())) {
|
|
||||||
byte[] bytes = new byte[fileInputStream.available()];
|
|
||||||
fileInputStream.read(bytes); // TODO-rca: エラーハンドリング
|
|
||||||
return new String(bytes);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void saveText(String text, String fileName) { // TODO-rca: リファクタリング // TODO-rca: 統合
|
|
||||||
if (isExist(fileName)) {
|
|
||||||
logger.debug("saveText", "file already exists");
|
|
||||||
// Overwrite
|
|
||||||
try {
|
|
||||||
Files.write(currentDir.resolve(fileName), text.getBytes());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
try {
|
|
||||||
Files.createFile(currentDir.resolve(fileName));
|
|
||||||
Files.write(currentDir.resolve(fileName), text.getBytes());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void saveText(String text, Path path) {
|
|
||||||
if (isExist(path)) {
|
|
||||||
logger.debug("saveText", "file already exists");
|
|
||||||
// Overwrite
|
|
||||||
try {
|
|
||||||
Files.write(path, text.getBytes());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
try {
|
|
||||||
Files.createFile(path);
|
Files.createFile(path);
|
||||||
Files.write(path, text.getBytes());
|
} catch (Exception e) {
|
||||||
} catch (IOException e) {
|
logger.error("createFileInternal", e.getMessage());
|
||||||
e.printStackTrace();
|
throw new IOException("Failed to create file");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveDocument(Document document, String fileName) {
|
public FileManager createFile() throws IOException {
|
||||||
|
this.createFileInternal(this.path);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager createFile(String fileName) throws IOException { // pathが書き換わってしまうのは想像できない挙動かも?
|
||||||
|
this.createFileInternal(this.resolveStringPath(fileName));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager createFileIfNotExist() throws IOException {
|
||||||
|
if (!this.isExist()) {
|
||||||
|
this.createFile();
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager createFileIfNotExist(String fileName) throws IOException {
|
||||||
|
if (!this.isExist(fileName)) {
|
||||||
|
this.createFile(fileName);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internal
|
||||||
|
private void createDirectoryInternal(Path path) throws IOException {
|
||||||
|
try {
|
||||||
|
if (this.autoCreateParent) {
|
||||||
|
if (!path.getParent().toFile().exists()) {
|
||||||
|
Files.createDirectories(path.getParent());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Files.createDirectory(path);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("createDirectoryInternal", e.getMessage());
|
||||||
|
throw new IOException("Failed to create directory");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager createDirectory() throws IOException {
|
||||||
|
this.createDirectoryInternal(this.path);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager createDirectory(String directoryName) throws IOException {
|
||||||
|
this.createDirectoryInternal(this.resolveStringPath(directoryName));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager createDirectoryIfNotExist() throws IOException {
|
||||||
|
if (!this.isExist()) {
|
||||||
|
this.createDirectory();
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileManager createDirectoryIfNotExist(String directoryName) throws IOException {
|
||||||
|
if (!this.isExist(directoryName)) {
|
||||||
|
this.createDirectory(directoryName);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internal
|
||||||
|
private void saveXmlInternal(Document document, Path path) throws IOException {
|
||||||
try {
|
try {
|
||||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||||
Transformer transformer = transformerFactory.newTransformer();
|
Transformer transformer = transformerFactory.newTransformer();
|
||||||
DOMSource source = new DOMSource(document);
|
DOMSource source = new DOMSource(document);
|
||||||
File file = createFile(fileName);
|
|
||||||
StreamResult result = new StreamResult(file);
|
StreamResult result = new StreamResult(path.toFile());
|
||||||
|
|
||||||
transformer.transform(source, result);
|
transformer.transform(source, result);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
logger.error("saveXmlInternal", e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
throw new IOException("Failed to save xml");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private void saveBitmapInternal(Bitmap bitmap, Path path) throws IOException {
|
||||||
@Override
|
try {
|
||||||
public void saveDocument(Document document, Path path) {
|
logger.debug("saveBitmapInternal", "path: " + path);
|
||||||
// TODO-rca 実装する
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, Files.newOutputStream(path));
|
||||||
}
|
} catch (Exception e) {
|
||||||
|
logger.error("saveBitmapInternal", e.getMessage());
|
||||||
@Override
|
throw new IOException("Failed to save bitmap");
|
||||||
public Document loadDocument(String fileName) {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveXml(Document document, String fileName) throws IOException {
|
||||||
|
this.saveXmlInternal(document, this.resolveStringPath(fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveXml(Document document) throws IOException {
|
||||||
|
this.saveXmlInternal(document, this.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveBitmap(Bitmap bitmap, String fileName) throws IOException {
|
||||||
|
this.saveBitmapInternal(bitmap, this.resolveStringPath(fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveBitmap(Bitmap bitmap) throws IOException {
|
||||||
|
this.saveBitmapInternal(bitmap, this.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internal
|
||||||
|
private Document loadXmlInternal(Path path) throws IOException {
|
||||||
try {
|
try {
|
||||||
File file = getFile(fileName);
|
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||||
Document document = builder.parse(file);
|
return builder.parse(Files.newInputStream(path));
|
||||||
return document;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
logger.error("loadXmlInternal", e.getMessage());
|
||||||
return null;
|
throw new IOException("Failed to load xml");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private Bitmap loadBitmapInternal(Path path) throws IOException {
|
||||||
@Override
|
|
||||||
public Document loadDocument(Path path) {
|
|
||||||
// TODO-rca 実装する
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isExist(Path path) {
|
|
||||||
logger.debug("isExist", "called");
|
|
||||||
return Files.exists(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isExist(String fileName) {
|
|
||||||
logger.debug("isExist", "called");
|
|
||||||
return Files.exists(currentDir.resolve(fileName));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void autoCreateDir(Path path) {
|
|
||||||
logger.debug("autoCreateDir", "called");
|
|
||||||
if (!Files.exists(path)) {
|
|
||||||
try {
|
try {
|
||||||
Files.createDirectories(path);
|
return BitmapFactory.decodeFile(path.toString());
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
logger.error("loadBitmapInternal", e.getMessage());
|
||||||
}
|
throw new IOException("Failed to load bitmap");
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void autoCreateDir(String dirName) {
|
|
||||||
logger.debug("autoCreateDir", "called");
|
|
||||||
if (!Files.exists(currentDir.resolve(dirName))) {
|
|
||||||
try {
|
|
||||||
Files.createDirectories(currentDir.resolve(dirName));
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void autoCreateToCurrentDir() {
|
|
||||||
logger.debug("autoGenerateToCurrentDir", "called");
|
|
||||||
if (isExist(currentDir)) {
|
|
||||||
logger.debug("autoGenerateToCurrentDir", "currentDir already exists");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
try {
|
|
||||||
Files.createDirectories(currentDir);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void saveBitmapAtCurrent(Bitmap bitmap, String fileName) { // TODO-rca: ファイル形式を変更できるようにする?
|
|
||||||
logger.debug("saveBitmapAtCurrent", "called");
|
|
||||||
try {
|
|
||||||
File file = currentDir.resolve(fileName).toFile();
|
|
||||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, Files.newOutputStream(file.toPath()));
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Bitmap loadBitmap(Path path) {
|
public Document loadXml(String fileName) throws IOException {
|
||||||
return null;
|
return this.loadXmlInternal(this.resolveStringPath(fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeBitmap(Path path) {
|
public Document loadXml() throws IOException {
|
||||||
|
return this.loadXmlInternal(this.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Bitmap loadBitmap(String fileName) throws IOException {
|
||||||
|
return this.loadBitmapInternal(this.resolveStringPath(fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Bitmap loadBitmap() throws IOException {
|
||||||
|
return this.loadBitmapInternal(this.path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user