1 package com.atlassian.plugin.spring.scanner.core.vfs;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileReader;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.util.Collection;
9 import java.util.Collections;
10 import java.util.Properties;
11
12
13
14 class FileBasedVirtualFile implements VirtualFile {
15 private final File file;
16
17 FileBasedVirtualFile(final File file) {
18 this.file = file;
19 }
20
21 @Override
22 public Collection<String> readLines() throws IOException {
23 FileReader reader;
24 try {
25 reader = new FileReader(file);
26 } catch (FileNotFoundException e) {
27 return Collections.emptyList();
28 }
29 return CommonIO.readLines(reader);
30 }
31
32 @Override
33 public void writeLines(final Iterable<String> lines) throws IOException {
34 mkdirs(file);
35 CommonIO.writeLines(new FileWriter(file), lines);
36 }
37
38 @Override
39 public void writeProperties(final Properties properties, final String comment) throws IOException {
40 mkdirs(file);
41 CommonIO.writeProperties(new FileWriter(file), properties, comment);
42 }
43
44 private void mkdirs(final File file) throws IOException {
45 File parentDir = file.getParentFile();
46
47 parentDir.mkdirs();
48 if (!parentDir.exists()) {
49 throw new IOException("Unable to create output directory " + parentDir);
50 }
51 }
52 }