Skip to content

Commit

Permalink
Add logs which files are converted and fix a bug causing the output d…
Browse files Browse the repository at this point in the history
…irectory not to be skipped
  • Loading branch information
Christian Würthner committed Sep 7, 2021
1 parent 80473d7 commit 6cbd63f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 95 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ to parse svg and convert to xml file.
If you directly want to use the jar , use as below:

```bash
java -jar Svg2VectorAndroid-1.0.1.jar "SourceDirectoryPath"
java -jar Svg2VectorAndroid-1.0.3.jar "SourceDirectoryPath"
```

## Build instructions
Expand Down
Binary file added Svg2VectorAndroid-1.0.3.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'geekyappz'
version '1.0.2'
version '1.0.3'

apply plugin: 'java'

Expand Down
187 changes: 94 additions & 93 deletions src/main/java/com/vector/svg2vectorandroid/SvgFilesProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,104 +19,105 @@

public class SvgFilesProcessor {

private Path sourceSvgPath;
private Path destinationVectorPath;
private String extension;
private String extensionSuffix;
private Path sourceSvgPath;
private Path destinationVectorPath;
private String extension;
private String extensionSuffix;

public SvgFilesProcessor(String sourceSvgDirectory) {
this(sourceSvgDirectory, new File(new File(sourceSvgDirectory), "ProcessedSVG").getAbsolutePath(), "xml", "");
}
public SvgFilesProcessor(String sourceSvgDirectory) {
this(sourceSvgDirectory, new File(new File(sourceSvgDirectory), "ProcessedSVG").getAbsolutePath(), "xml", "");
}

public SvgFilesProcessor(String sourceSvgDirectory, String destinationVectorDirectory) {
this(sourceSvgDirectory, destinationVectorDirectory, "xml", "");
}
public SvgFilesProcessor(String sourceSvgDirectory, String destinationVectorDirectory) {
this(sourceSvgDirectory, destinationVectorDirectory, "xml", "");
}

public SvgFilesProcessor(String sourceSvgDirectory, String destinationVectorDirectory, String extension,
public SvgFilesProcessor(String sourceSvgDirectory, String destinationVectorDirectory, String extension,
String extensionSuffix) {
this.sourceSvgPath = Paths.get(sourceSvgDirectory);
this.destinationVectorPath = Paths.get(destinationVectorDirectory);
this.extension = extension;
this.extensionSuffix = extensionSuffix;
}

public void process(){
try{
EnumSet<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
//check first if source is a directory
if(Files.isDirectory(sourceSvgPath)){
Files.walkFileTree(sourceSvgPath, options, Integer.MAX_VALUE, new FileVisitor<Path>() {

public FileVisitResult postVisitDirectory(Path dir,
IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}

public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) {
// Skip folder which is processing svgs to xml
if(dir.equals(destinationVectorPath)){
return FileVisitResult.SKIP_SUBTREE;
}

CopyOption[] opt = new CopyOption[]{COPY_ATTRIBUTES, REPLACE_EXISTING};
Path newDirectory = destinationVectorPath.resolve(sourceSvgPath.relativize(dir));
try{
Files.copy(dir, newDirectory,opt);
} catch(FileAlreadyExistsException ex){
System.out.println("FileAlreadyExistsException "+ex.toString());
} catch(IOException x){
return FileVisitResult.SKIP_SUBTREE;
}
return CONTINUE;
}


public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
convertToVector(file, destinationVectorPath.resolve(sourceSvgPath.relativize(file)));
return CONTINUE;
}


public FileVisitResult visitFileFailed(Path file,
IOException exc) throws IOException {
return CONTINUE;
}
});
} else {
System.out.println("source not a directory");
}

} catch (IOException e){
System.out.println("IOException "+e.getMessage());
}

}

private void convertToVector(Path source, Path target) throws IOException{
// convert only if it is .svg
if(source.getFileName().toString().endsWith(".svg")){
File targetFile = getFileWithXMlExtension(target, extension, extensionSuffix);
FileOutputStream fous = new FileOutputStream(targetFile);
Svg2Vector.parseSvgToXml(source.toFile(), fous);
} else {
System.out.println("Skipping file as its not svg "+source.getFileName().toString());
}
this.sourceSvgPath = Paths.get(sourceSvgDirectory);
this.destinationVectorPath = Paths.get(destinationVectorDirectory);
this.extension = extension;
this.extensionSuffix = extensionSuffix;
}

public void process() {
try {
EnumSet<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
//check first if source is a directory
if (Files.isDirectory(sourceSvgPath)) {
Files.walkFileTree(sourceSvgPath, options, Integer.MAX_VALUE, new FileVisitor<Path>() {

public FileVisitResult postVisitDirectory(Path dir,
IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}

public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) {
// Skip folder which is processing svgs to xml
if (dir.toAbsolutePath().equals(destinationVectorPath.toAbsolutePath())) {
return FileVisitResult.SKIP_SUBTREE;
}

CopyOption[] opt = new CopyOption[]{COPY_ATTRIBUTES, REPLACE_EXISTING};
Path newDirectory = destinationVectorPath.resolve(sourceSvgPath.relativize(dir));
try {
Files.copy(dir, newDirectory, opt);
} catch (FileAlreadyExistsException ex) {
System.out.println("FileAlreadyExistsException " + ex.toString());
} catch (IOException x) {
return FileVisitResult.SKIP_SUBTREE;
}
return CONTINUE;
}


public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
convertToVector(file, destinationVectorPath.resolve(sourceSvgPath.relativize(file)));
return CONTINUE;
}


public FileVisitResult visitFileFailed(Path file,
IOException exc) throws IOException {
return CONTINUE;
}
});
} else {
System.out.println("source not a directory");
}

} catch (IOException e) {
System.out.println("IOException " + e.getMessage());
}

}

private void convertToVector(Path source, Path target) throws IOException {
// convert only if it is .svg
if (source.getFileName().toString().endsWith(".svg")) {
System.out.println("Converting " + source.getFileName().toString());
File targetFile = getFileWithXMlExtension(target, extension, extensionSuffix);
FileOutputStream fous = new FileOutputStream(targetFile);
Svg2Vector.parseSvgToXml(source.toFile(), fous);
} else {
System.out.println("Skipping file as its not svg " + source.getFileName().toString());
}
}

private File getFileWithXMlExtension(Path target, String extension, String extensionSuffix){
String svgFilePath = target.toFile().getAbsolutePath();
StringBuilder svgBaseFile = new StringBuilder();
int index = svgFilePath.lastIndexOf(".");
if(index != -1){
String subStr = svgFilePath.substring(0, index);
svgBaseFile.append(subStr);
}
svgBaseFile.append(null != extensionSuffix ? extensionSuffix : "");
svgBaseFile.append(".");
svgBaseFile.append(extension);
return new File(svgBaseFile.toString());
}
private File getFileWithXMlExtension(Path target, String extension, String extensionSuffix) {
String svgFilePath = target.toFile().getAbsolutePath();
StringBuilder svgBaseFile = new StringBuilder();
int index = svgFilePath.lastIndexOf(".");
if (index != -1) {
String subStr = svgFilePath.substring(0, index);
svgBaseFile.append(subStr);
}
svgBaseFile.append(null != extensionSuffix ? extensionSuffix : "");
svgBaseFile.append(".");
svgBaseFile.append(extension);
return new File(svgBaseFile.toString());
}

}

0 comments on commit 6cbd63f

Please sign in to comment.