001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.gen;
021
022import gwt.material.design.client.constants.IconType;
023
024import java.io.File;
025import java.io.FileNotFoundException;
026import java.io.IOException;
027import java.nio.file.Files;
028import java.nio.file.Paths;
029import java.text.DateFormat;
030import java.text.SimpleDateFormat;
031import java.util.Date;
032import java.util.TimeZone;
033import java.util.regex.Matcher;
034import java.util.regex.Pattern;
035
036/**
037 * Generator for the {@link IconType} class. It parses the MaterialIcons-Regular.ijmap file and creates
038 * the IconType.java file.
039 *
040 * @author gilberto-torrezan
041 */
042public class IconTypeGenerator {
043
044    public static void main(String[] args) throws IOException {
045        System.out.println("Generating the IconType class...");
046
047        File ijmap = new File("src/main/resources/gwt/material/design/public/font/material-icons/MaterialIcons-Regular.ijmap");
048        if (!ijmap.isFile()) {
049            throw new FileNotFoundException(ijmap.getAbsolutePath());
050        }
051        File template = new File("src/main/java/gwt/material/design/gen/IconTypeTemplate.txt");
052        if (!template.isFile()) {
053            throw new FileNotFoundException(template.getAbsolutePath());
054        }
055
056        //parses the ijmap file by using a regex
057        //to parse it as a json, on Java 7, an external library should be used
058        String iconsFile = new String(Files.readAllBytes(Paths.get(ijmap.toURI())), "UTF-8");
059        Pattern pattern = Pattern.compile("\\{\\s*\"name\"\\s*:\\s*\"([^\"]*)\"\\s*\\}");
060        Matcher matcher = pattern.matcher(iconsFile);
061
062        StringBuilder builder = new StringBuilder();
063
064        //adds the default constant
065        builder.append("DEFAULT(\"\")");
066        int count = 1;
067
068        while (matcher.find()) {
069            String name = matcher.group(1);
070            String cssName = name.toLowerCase().replace(' ', '_');
071
072            //when a name starts with a number, the type name must be changed
073            while (name.matches("\\d.*")) { //starts with a number
074                int firstSpace = name.indexOf(' ');
075                String num = name.substring(0, firstSpace);
076                name = name.substring(firstSpace + 1) + '_' + num;
077            }
078            String typeName = name.toUpperCase().replace(' ', '_');
079
080            //appends to the document
081            builder.append(",\n    ").append(typeName).append("(\"").append(cssName).append("\")");
082            count++;
083        }
084        builder.append(";");
085
086        //loading the template java file
087        String templateString = new String(Files.readAllBytes(Paths.get(template.toURI())), "UTF-8");
088        templateString = templateString.replace("${generationDate}", getDateAsISO8601()).replace("${enumValues}", builder.toString());
089
090        //time to write it to the destination
091        File target = new File("src/main/java/gwt/material/design/client/constants/IconType.java");
092        Files.write(Paths.get(target.toURI()), templateString.getBytes("UTF-8"));
093
094        System.out.println("Generation complete with " + count + " IconType constants.");
095    }
096
097    /*
098     * Needed by the @Generated annotation 
099     */
100    private static String getDateAsISO8601() {
101        TimeZone tz = TimeZone.getTimeZone("UTC");
102        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
103        df.setTimeZone(tz);
104        return df.format(new Date());
105    }
106
107}