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);
|
||||
Toast.makeText(getActivity(), "documentProcessor created", Toast.LENGTH_LONG).show();
|
||||
try {
|
||||
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();
|
||||
});
|
||||
|
||||
|
@ -185,9 +190,21 @@ public class ScannerDataManagerStubFragment extends Fragment {
|
|||
bitmaps[i] = results.get(i).getBitmap();
|
||||
}
|
||||
|
||||
try {
|
||||
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();
|
||||
} 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);
|
||||
String dirName = editText.getText().toString();
|
||||
|
||||
FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory());
|
||||
fileManager.createDir(dirName);
|
||||
// FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory());
|
||||
// fileManager.createDir(dirName);
|
||||
});
|
||||
|
||||
view.findViewById(R.id.button_save_item).setOnClickListener(v -> {
|
||||
FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory());
|
||||
fileManager.createDir("test");
|
||||
fileManager.changeDir("test");
|
||||
Bitmap bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
|
||||
// Bitmapに描画処理を行う
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
// 大きな山の形状を作成
|
||||
android.graphics.Path bigMountainPath = new android.graphics.Path();
|
||||
bigMountainPath.moveTo(100, 800); // 左下の開始点
|
||||
bigMountainPath.lineTo(500, 300); // 頂点
|
||||
bigMountainPath.lineTo(900, 800); // 右下
|
||||
bigMountainPath.close(); // パスを閉じる
|
||||
|
||||
// 山の描画設定
|
||||
Paint mountainPaint = new Paint();
|
||||
mountainPaint.setColor(Color.GREEN);
|
||||
mountainPaint.setStyle(Paint.Style.FILL);
|
||||
|
||||
// 大きな山を描画
|
||||
canvas.drawPath(bigMountainPath, mountainPaint);
|
||||
|
||||
// 小さな山の形状を作成
|
||||
android.graphics.Path smallMountainPath = new android.graphics.Path();
|
||||
smallMountainPath.moveTo(400, 800); // 左下の開始点
|
||||
smallMountainPath.lineTo(650, 400); // 頂点
|
||||
smallMountainPath.lineTo(900, 800); // 右下
|
||||
smallMountainPath.close(); // パスを閉じる
|
||||
|
||||
Paint smallMountainPaint = new Paint();
|
||||
smallMountainPaint.setColor(Color.parseColor("#006e54"));
|
||||
smallMountainPaint.setStyle(Paint.Style.FILL);
|
||||
|
||||
// 小さな山を描画
|
||||
canvas.drawPath(smallMountainPath, smallMountainPaint);
|
||||
fileManager.saveBitmapAtCurrent(bitmap, "test.png");
|
||||
// FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory());
|
||||
// fileManager.createDir("test");
|
||||
// fileManager.changeDir("test");
|
||||
// Bitmap bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
|
||||
// // Bitmapに描画処理を行う
|
||||
// Canvas canvas = new Canvas(bitmap);
|
||||
// // 大きな山の形状を作成
|
||||
// android.graphics.Path bigMountainPath = new android.graphics.Path();
|
||||
// bigMountainPath.moveTo(100, 800); // 左下の開始点
|
||||
// bigMountainPath.lineTo(500, 300); // 頂点
|
||||
// bigMountainPath.lineTo(900, 800); // 右下
|
||||
// bigMountainPath.close(); // パスを閉じる
|
||||
//
|
||||
// // 山の描画設定
|
||||
// Paint mountainPaint = new Paint();
|
||||
// mountainPaint.setColor(Color.GREEN);
|
||||
// mountainPaint.setStyle(Paint.Style.FILL);
|
||||
//
|
||||
// // 大きな山を描画
|
||||
// canvas.drawPath(bigMountainPath, mountainPaint);
|
||||
//
|
||||
// // 小さな山の形状を作成
|
||||
// android.graphics.Path smallMountainPath = new android.graphics.Path();
|
||||
// smallMountainPath.moveTo(400, 800); // 左下の開始点
|
||||
// smallMountainPath.lineTo(650, 400); // 頂点
|
||||
// smallMountainPath.lineTo(900, 800); // 右下
|
||||
// smallMountainPath.close(); // パスを閉じる
|
||||
//
|
||||
// Paint smallMountainPaint = new Paint();
|
||||
// smallMountainPaint.setColor(Color.parseColor("#006e54"));
|
||||
// smallMountainPaint.setStyle(Paint.Style.FILL);
|
||||
//
|
||||
// // 小さな山を描画
|
||||
// canvas.drawPath(smallMountainPath, smallMountainPaint);
|
||||
// fileManager.saveBitmapAtCurrent(bitmap, "test.png");
|
||||
});
|
||||
|
||||
return view;
|
||||
|
|
|
@ -5,8 +5,8 @@ import android.graphics.Bitmap;
|
|||
public interface DocumentProcessor {
|
||||
|
||||
// ページ操作
|
||||
void addNewPageToLast(Bitmap bitmap);
|
||||
void addNewPagesToLast(Bitmap[] bitmaps);
|
||||
void addNewPageToLast(Bitmap bitmap) throws Exception;
|
||||
void addNewPagesToLast(Bitmap[] bitmaps) throws Exception;
|
||||
void addNewPageAfterIndex(Bitmap bitmap, int index);
|
||||
void addNewPageBeforeIndex(Bitmap bitmap, int index);
|
||||
void removePageAtIndex(int index);
|
||||
|
@ -18,7 +18,7 @@ public interface DocumentProcessor {
|
|||
Bitmap getPageAtIndex(int index);
|
||||
int getPageCount();
|
||||
|
||||
void close();
|
||||
void init();
|
||||
void close() throws Exception;
|
||||
void init() throws Exception; // TODO-rca: 例外処理
|
||||
|
||||
}
|
||||
|
|
|
@ -49,25 +49,24 @@ public class DocumentProcessorImpl implements DocumentProcessor{
|
|||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
public void init() throws Exception{
|
||||
logger.debug("init", "called");
|
||||
// Init Variables
|
||||
this.documentRootPath = this.documentDetail.getPath().getFullPath();
|
||||
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");
|
||||
|
||||
this.fileManager.autoCreateDir(this.documentRootPath);
|
||||
|
||||
// rawディレクトリInit
|
||||
this.fileManager.autoCreateDir(DEFAULT_SAVE_DIR);
|
||||
|
||||
// xmlファイルの読み込み
|
||||
if (fileManager.isExist("meta.xml")) {
|
||||
if (initFileManager.isExist("meta.xml")) {
|
||||
logger.debug("init", "meta.xml found");
|
||||
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) {
|
||||
logger.debug("init", "meta.xml parse failed");
|
||||
logger.trace("init", e.getMessage());
|
||||
|
@ -83,7 +82,7 @@ public class DocumentProcessorImpl implements DocumentProcessor{
|
|||
xmlMetaModel.setPages(new ArrayList<>());
|
||||
|
||||
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");
|
||||
} catch (Exception e) {
|
||||
logger.error("init", "meta.xml save failed");
|
||||
|
@ -95,33 +94,23 @@ public class DocumentProcessorImpl implements DocumentProcessor{
|
|||
}
|
||||
|
||||
@Override
|
||||
public void addNewPageToLast(Bitmap bitmap) {
|
||||
public void addNewPageToLast(Bitmap bitmap) throws Exception{
|
||||
logger.debug("addNewPageToLast", "called");
|
||||
String filename = UUID.randomUUID().toString() + ".png"; // TODO-rca: 拡張子を動的にする
|
||||
|
||||
// FileManager
|
||||
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");
|
||||
this.fileManager.getNewInstance().createDirectoryIfNotExist(DEFAULT_SAVE_DIR).resolve(DEFAULT_SAVE_DIR).saveBitmap(bitmap, filename);
|
||||
|
||||
// Save file
|
||||
this.fileManager.saveBitmapAtCurrent(bitmap, filename);
|
||||
XmlMetaPageModel xmlMetaPageModel = new XmlMetaPageModel();
|
||||
xmlMetaPageModel.setFilename(filename);
|
||||
xmlMetaPageModel.setIndex(xmlMetaModel.getPages().size() + 1);
|
||||
xmlMetaModel.getPages().add(xmlMetaPageModel);
|
||||
|
||||
// Update meta
|
||||
XmlMetaPageModel page = new XmlMetaPageModel();
|
||||
page.setIndex(xmlMetaModel.getPages().size() + 1);
|
||||
page.setFilename(filename);
|
||||
xmlMetaModel.addPage(page);
|
||||
logger.info("addNewPageToLast", "finished");
|
||||
logger.info("addNewPageToLast", "filename: " + filename + ", index: " + xmlMetaPageModel.getIndex());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addNewPagesToLast(Bitmap[] bitmaps) {
|
||||
public void addNewPagesToLast(Bitmap[] bitmaps) throws Exception{
|
||||
logger.debug("addNewPagesToLast", "called");
|
||||
|
||||
for (Bitmap bitmap : bitmaps) {
|
||||
|
@ -160,18 +149,14 @@ public class DocumentProcessorImpl implements DocumentProcessor{
|
|||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
public void close() throws Exception{
|
||||
logger.debug("close", "called");
|
||||
// TODO-rca: ここでxmlファイルを保存する
|
||||
this.fileManager.backRootDir();
|
||||
|
||||
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");
|
||||
} catch (Exception e) {
|
||||
logger.error("close", "meta.xml save failed");
|
||||
logger.trace("close", e.getMessage());
|
||||
}
|
||||
logger.info("close", "finished");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@ import android.graphics.Bitmap;
|
|||
import org.w3c.dom.Document;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -12,48 +14,52 @@ import java.util.List;
|
|||
/** @noinspection unused*/
|
||||
public interface FileManager {
|
||||
|
||||
Path getRootDir();
|
||||
Path getCurrentDir();
|
||||
File getFileRef();
|
||||
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
|
||||
void changeDir(Path path); //cd
|
||||
void backDir(); //cd ..
|
||||
void backRootDir(); //cd /
|
||||
List<Path> getList();
|
||||
void createDir(String dirName);
|
||||
void createDir(Path path);
|
||||
void removeDir(String dirName);
|
||||
void removeDir(Path path);
|
||||
// Configure
|
||||
FileManager enableAutoCreateParent();
|
||||
FileManager disableRootDirCheck();
|
||||
|
||||
File createFile(String fileName);
|
||||
void removeFile(String fileName);
|
||||
FileManager setRootDir(Path rootDir);
|
||||
FileManager setPath(Path path);
|
||||
FileManager resolve(String path) throws IOException;
|
||||
|
||||
File getFile(String fileName);
|
||||
File getFile(Path path);
|
||||
// Create
|
||||
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);
|
||||
String loadText(Path path);
|
||||
// Save
|
||||
// XML
|
||||
void saveXml(Document document, String fileName) throws IOException;
|
||||
void saveXml(Document document) throws IOException;
|
||||
|
||||
void saveText(String text, String fileName);
|
||||
void saveText(String text, Path path);
|
||||
|
||||
void saveDocument(Document document, String fileName);
|
||||
void saveDocument(Document document, Path path);
|
||||
|
||||
Document loadDocument(String fileName);
|
||||
Document loadDocument(Path path);
|
||||
// Bitmap
|
||||
void saveBitmap(Bitmap bitmap, String fileName) throws IOException; // TODO-rca: パラメータに対応させる
|
||||
void saveBitmap(Bitmap bitmap) throws IOException; // TODO-rca: パラメータに対応させる
|
||||
|
||||
|
||||
boolean isExist(Path path);
|
||||
boolean isExist(String fileName);
|
||||
// Load
|
||||
// XML
|
||||
Document loadXml(String fileName) throws IOException;
|
||||
Document loadXml() throws IOException;
|
||||
|
||||
void autoCreateDir(Path path);
|
||||
void autoCreateDir(String dirName);
|
||||
|
||||
void autoCreateToCurrentDir();
|
||||
|
||||
void saveBitmapAtCurrent(Bitmap bitmap, String fileName);
|
||||
Bitmap loadBitmap(Path path);
|
||||
void removeBitmap(Path path);
|
||||
// Bitmap
|
||||
Bitmap loadBitmap(String fileName) throws IOException;
|
||||
Bitmap loadBitmap() throws IOException;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,17 +1,14 @@
|
|||
package one.nem.lacerta.source.file.impl;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
@ -28,320 +25,340 @@ import one.nem.lacerta.utils.LacertaLogger;
|
|||
|
||||
public class FileManagerImpl implements FileManager {
|
||||
|
||||
// RootDir
|
||||
// variables
|
||||
private Path rootDir;
|
||||
private Path path;
|
||||
private boolean autoCreateParent = false;
|
||||
private boolean disableRootDirCheck = false;
|
||||
|
||||
// CurrentDir
|
||||
private Path currentDir;
|
||||
|
||||
// Internal Methods
|
||||
private Path convertPath(String path) {
|
||||
Path convertedPath = currentDir.resolve(path);
|
||||
if (convertedPath.startsWith(rootDir)) { // 異常なパスの場合はnullを返す // TODO-rca: エラーハンドリング
|
||||
return convertedPath;
|
||||
// Injection
|
||||
private final LacertaLogger logger;
|
||||
@AssistedInject
|
||||
public FileManagerImpl(LacertaLogger logger, @Assisted Path rootDir) {
|
||||
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 {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Injection
|
||||
private LacertaLogger logger;
|
||||
|
||||
@AssistedInject
|
||||
public FileManagerImpl(LacertaLogger logger, @Assisted Path rootDir) {
|
||||
this.logger = logger;
|
||||
this.rootDir = rootDir;
|
||||
this.currentDir = rootDir;
|
||||
@Override
|
||||
public boolean isExist(String name) throws IOException {
|
||||
Path resolvedPath = this.resolveStringPath(name);
|
||||
return Files.exists(resolvedPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path getRootDir() {
|
||||
return rootDir;
|
||||
public boolean isExist(){
|
||||
return Files.exists(this.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path getCurrentDir() {
|
||||
return currentDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeDir(String dirName) {
|
||||
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: 例外を投げる
|
||||
public boolean isDirectory() {
|
||||
if (this.isExist()) {
|
||||
File file = this.path.toFile();
|
||||
return file.isDirectory();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void backDir() {
|
||||
this.currentDir = currentDir.getParent();
|
||||
public boolean isFile() {
|
||||
if (this.isExist()) {
|
||||
File file = this.path.toFile();
|
||||
return file.isFile();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void backRootDir() {
|
||||
this.currentDir = rootDir;
|
||||
public boolean isReadable() {
|
||||
if (this.isExist()) {
|
||||
File file = this.path.toFile();
|
||||
return file.canRead();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Path> getList() {
|
||||
List<Path> list = new ArrayList<>();
|
||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(currentDir)) {
|
||||
for (Path entry : stream) { // TODO-rca: エラーハンドリング, 効率化
|
||||
list.add(entry);
|
||||
public boolean isWritable() {
|
||||
if (this.isExist()) {
|
||||
File file = this.path.toFile();
|
||||
return file.canWrite();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createDir(String dirName) {
|
||||
//ディレクトリ作成
|
||||
logger.debug("createDir", "called");
|
||||
public FileManager getCurrentInstance() {
|
||||
return this;
|
||||
}
|
||||
|
||||
Path path = currentDir.resolve(dirName);
|
||||
logger.debug("createDir", "path: " + path);
|
||||
@Override
|
||||
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 {
|
||||
Files.createDirectory(path);
|
||||
resolvedPath = resolveStringPath(path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.error("resolve", e.getMessage());
|
||||
throw new IOException("Invalid path: " + path);
|
||||
}
|
||||
return this.setPath(resolvedPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createDir(Path path) {
|
||||
logger.debug("createDir", "called");
|
||||
// Internal
|
||||
private void createFileInternal(Path path) throws IOException {
|
||||
try {
|
||||
Files.createDirectory(path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
if (this.autoCreateParent) {
|
||||
if (!path.getParent().toFile().exists()) {
|
||||
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.write(path, text.getBytes());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("createFileInternal", e.getMessage());
|
||||
throw new IOException("Failed to create file");
|
||||
}
|
||||
}
|
||||
|
||||
@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 {
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMSource source = new DOMSource(document);
|
||||
File file = createFile(fileName);
|
||||
StreamResult result = new StreamResult(file);
|
||||
|
||||
StreamResult result = new StreamResult(path.toFile());
|
||||
|
||||
transformer.transform(source, result);
|
||||
} catch (Exception e) {
|
||||
logger.error("saveXmlInternal", e.getMessage());
|
||||
e.printStackTrace();
|
||||
throw new IOException("Failed to save xml");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveDocument(Document document, Path path) {
|
||||
// TODO-rca 実装する
|
||||
}
|
||||
|
||||
@Override
|
||||
public Document loadDocument(String fileName) {
|
||||
private void saveBitmapInternal(Bitmap bitmap, Path path) throws IOException {
|
||||
try {
|
||||
logger.debug("saveBitmapInternal", "path: " + path);
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, Files.newOutputStream(path));
|
||||
} catch (Exception e) {
|
||||
logger.error("saveBitmapInternal", e.getMessage());
|
||||
throw new IOException("Failed to save bitmap");
|
||||
}
|
||||
}
|
||||
|
||||
@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 {
|
||||
File file = getFile(fileName);
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document document = builder.parse(file);
|
||||
return document;
|
||||
return builder.parse(Files.newInputStream(path));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
logger.error("loadXmlInternal", e.getMessage());
|
||||
throw new IOException("Failed to load xml");
|
||||
}
|
||||
}
|
||||
|
||||
@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)) {
|
||||
private Bitmap loadBitmapInternal(Path path) throws IOException {
|
||||
try {
|
||||
Files.createDirectories(path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
return BitmapFactory.decodeFile(path.toString());
|
||||
} catch (Exception e) {
|
||||
logger.error("loadBitmapInternal", e.getMessage());
|
||||
throw new IOException("Failed to load bitmap");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap loadBitmap(Path path) {
|
||||
return null;
|
||||
public Document loadXml(String fileName) throws IOException {
|
||||
return this.loadXmlInternal(this.resolveStringPath(fileName));
|
||||
}
|
||||
|
||||
@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