Skip to content

Commit

Permalink
style: apply code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-godoy authored and paodb committed Feb 9, 2022
1 parent fbfc44a commit 386bf34
Showing 1 changed file with 81 additions and 90 deletions.
171 changes: 81 additions & 90 deletions src/codegen/BuildSprites.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,116 +25,107 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.NoSuchElementException;
import java.util.Optional;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.TransformerFactoryImpl;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.StringBuilderWriter;
import org.apache.commons.lang3.StringUtils;
import org.kohsuke.github.GHException;
import org.kohsuke.github.GHFileNotFoundException;
import org.kohsuke.github.GHIOException;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;

import net.sf.saxon.TransformerFactoryImpl;
/**
* @author Javier Godoy / Flowing Code
*/
public class BuildSprites {

private static String tagName;

private static String repositoryName;

private static File spritesDirectory;

private static File iconsDirectory;


public static void main(String[] args) throws IOException {

iconsDirectory = new File(getRequiredProperty("codegen.icons")); //the location of the icons
spritesDirectory = new File(getRequiredProperty("codegen.sprites")); //the location of the sprites

if (!iconsDirectory.exists()) {
System.out.println("Skipping build-sprites phase");
return;
}

createDirectory(spritesDirectory);

execute();

}

private static String getRequiredProperty(String name) {
return Optional.ofNullable(System.getProperty(name)).orElseThrow(()->new NoSuchElementException("Missing system property -D"+name));
private static String tagName;

private static String repositoryName;

private static File spritesDirectory;

private static File iconsDirectory;


public static void main(String[] args) throws IOException {

iconsDirectory = new File(getRequiredProperty("codegen.icons")); // the location of the icons
spritesDirectory = new File(getRequiredProperty("codegen.sprites")); // the location of the sprites

if (!iconsDirectory.exists()) {
System.out.println("Skipping build-sprites phase");
return;
}

private static File createDirectory(File directory) {
if (!directory.isDirectory()&&!directory.mkdir()) {
System.err.println("Output directory "+directory+" does not exist and cannot be created");
System.exit(1);
}
return directory;

createDirectory(spritesDirectory);
execute();

}

private static String getRequiredProperty(String name) {
return Optional.ofNullable(System.getProperty(name))
.orElseThrow(() -> new NoSuchElementException("Missing system property -D" + name));
}

private static File createDirectory(File directory) {
if (!directory.isDirectory() && !directory.mkdir()) {
System.err.println("Output directory " + directory + " does not exist and cannot be created");
System.exit(1);
}

private static void execute() throws IOException {
for (File directory : iconsDirectory.listFiles(File::isDirectory)) {
String family = directory.getName();
File files[] = directory.listFiles(file->file.getName().endsWith(".svg"));
if (files.length==0) continue;

System.out.println("Creating sprite for " + family+ " ("+files.length+" icons)");

StringBuilderWriter out = new StringBuilderWriter();
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
out.write("<svg xmlns=\"http://www.w3.org/2000/svg\">\r\n");

for (File file: files) {
String data = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
data = data.replace("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">", "");
data = data.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>", "");
String name = FilenameUtils.getBaseName(file.getName());
out.write("<symbol id=\""+ name +"\">\r\n");
out.write(data);
out.write("\r\n</symbol>\r\n");
}
out.write("</svg>");

File iconset = new File(spritesDirectory, family+".svg");
FileUtils.writeStringToFile(iconset, out.toString(), StandardCharsets.UTF_8);
transform(iconset);
}
}

private static void transform(File file) throws IOException {
File dst = new File(file.getParent(), file.getName()+".2");
try (
InputStream in = new FileInputStream(file);
InputStream xslt = new FileInputStream("build-sprites.xslt");
OutputStream out = new FileOutputStream(dst)
) {
Transformer t = new TransformerFactoryImpl().newTransformer(new StreamSource(xslt));
Result outputTarget = new StreamResult(out);
t.transform(new StreamSource(in), outputTarget);
} catch (TransformerException e) {
throw new IOException(e);
return directory;
}

private static void execute() throws IOException {
for (File directory : iconsDirectory.listFiles(File::isDirectory)) {
String family = directory.getName();
File files[] = directory.listFiles(file -> file.getName().endsWith(".svg"));
if (files.length == 0) {
continue;
}
file.delete();
dst.renameTo(file);

System.out.println("Creating sprite for " + family + " (" + files.length + " icons)");

StringBuilderWriter out = new StringBuilderWriter();
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
out.write("<svg xmlns=\"http://www.w3.org/2000/svg\">\r\n");

for (File file : files) {
String data = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
data = data.replace(
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">",
"");
data = data.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>", "");
String name = FilenameUtils.getBaseName(file.getName());
out.write("<symbol id=\"" + name + "\">\r\n");
out.write(data);
out.write("\r\n</symbol>\r\n");
}
out.write("</svg>");

File iconset = new File(spritesDirectory, family + ".svg");
FileUtils.writeStringToFile(iconset, out.toString(), StandardCharsets.UTF_8);
transform(iconset);
}
}

private static void transform(File file) throws IOException {
File dst = new File(file.getParent(), file.getName() + ".2");
try (InputStream in = new FileInputStream(file);
InputStream xslt = new FileInputStream("build-sprites.xslt");
OutputStream out = new FileOutputStream(dst)) {
Transformer t = new TransformerFactoryImpl().newTransformer(new StreamSource(xslt));
Result outputTarget = new StreamResult(out);
t.transform(new StreamSource(in), outputTarget);
} catch (TransformerException e) {
throw new IOException(e);
}
file.delete();
dst.renameTo(file);
}

}

0 comments on commit 386bf34

Please sign in to comment.