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