Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package css.variable.converter; import java.awt.HeadlessException; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JComboBox; import javax.swing.JOptionPane; import org.apache.commons.io.FileUtils; /** * * @author Ben Jenkins This program will find all of the CSS files in it's * immediate sub directories Asks for the main file(This file must contain all * variables under the ":root" label. And then it writes over your previous * code, replacing them with release versions. */ public class CSSVariableConverter { // Will contain every CSS file I find in my subdirectories private static ArrayList<File> theCSSList = new ArrayList<File>(); public static void start(File sourceDirectory, File releaseDirectory) { try { FileUtils.copyDirectory(sourceDirectory, releaseDirectory); loadCSSFiles(releaseDirectory);// Find those CSS files File mainFile = getMainCSSFile();// Ask which file is the main file(so we can get variables) ArrayList<CSSVar> rootCSSVars = getCSSVars(mainFile, ":root");// Collect variables from main file editForRelease(rootCSSVars);// Go through existing CSS Files and replace variable access with variable values } catch (IOException ex) { Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex); } } /** * Ask which file is the main file(so we can get variables) * * @return The Main File that has the ":root" identifier for variables. * @throws HeadlessException */ private static File getMainCSSFile() throws HeadlessException { // Convert CSS Filenames into a string array String[] cssStrNames = new String[theCSSList.size()]; for (int i = 0; i < theCSSList.size(); i++) { cssStrNames[i] = theCSSList.get(i).getName(); } // Create JComboBox and prompt to choose main file RootFilePanel getCSSRoot = new RootFilePanel(cssStrNames); JOptionPane.showConfirmDialog(null, getCSSRoot, "Select your CSS File with root variables", JOptionPane.OK_CANCEL_OPTION); int index = getCSSRoot.getSelectedIndex(); if (index == -1) { return null; } return theCSSList.get(index); } private static void loadCSSFiles(File releaseDirectory) { CSSFilter theCSSFilter = new CSSFilter(); DirectoryFilter theDirectoryFilter = new DirectoryFilter(); ArrayList<String> theDirectoryList = new ArrayList<String>(); // We're about to go look for every CSS file in a sub directory theDirectoryList.add(releaseDirectory.getAbsolutePath());// Add the current directory while (!theDirectoryList.isEmpty()) { File currentDirectory = new File(theDirectoryList.get(0)); File[] currentCSSFiles = currentDirectory.listFiles(theCSSFilter); File[] subDirectories = currentDirectory.listFiles(theDirectoryFilter); // Add any css files found to theCSSList if (currentCSSFiles != null) { for (File cssFile : currentCSSFiles) { theCSSList.add(cssFile); } } // Add any more subdirectories found to the directory list. if (subDirectories != null) { for (File dir : subDirectories) { theDirectoryList.add(dir.getPath()); } } // Remove current directory. theDirectoryList.remove(0); } } /** * Collect variables from main file * * @param mainFile File with ":root" identifier for variables. * @return */ private static ArrayList<CSSVar> getCSSVars(File mainFile, String identifier) { ArrayList<CSSVar> cSSVarsList = new ArrayList<CSSVar>(); boolean isInVarDefinition = false; boolean isFinished = false; try { Scanner fileReader = new Scanner(mainFile); // Loop until end of text file, or outside of :root while (fileReader.hasNextLine() && !isFinished) { String currentLine = fileReader.nextLine(); if (isInVarDefinition) { // I identify all of my variables with -- if (currentLine.contains("--")) { int varNameBegIndex = currentLine.indexOf("--"); int varNameEndIndex = currentLine.indexOf(":"); String varName = currentLine.substring(varNameBegIndex, varNameEndIndex); int varValueBegIndex = currentLine.indexOf(":") + 1; int varValueEndIndex = currentLine.indexOf(";"); String varValue = currentLine.substring(varValueBegIndex, varValueEndIndex); varName = varName.trim(); varValue = varValue.trim(); cSSVarsList.add(new CSSVar(varName, varValue)); } if (currentLine.contains("}")) { isInVarDefinition = false; isFinished = true; } } else { if (currentLine.contains(identifier)) { isInVarDefinition = true; } } } fileReader.close(); } catch (FileNotFoundException ex) { Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex); } return cSSVarsList; } /** * Go through existing CSS Files and replace variable access with variable * values * * @param rootCSSVars All css variables you want to convert from name to value */ private static void editForRelease(ArrayList<CSSVar> rootCSSVars) { for (File cssFile : theCSSList) { ArrayList<CSSVar> localCSSVars = getCSSVars(cssFile, ":local"); for (CSSVar cssVar : rootCSSVars) { if (!localCSSVars.contains(cssVar)) { localCSSVars.add((CSSVar) (cssVar.clone())); } } // This will store info the new text for the file we will write below ArrayList<String> filesNewInfo = new ArrayList<String>(); try { Scanner fileReader = new Scanner(cssFile); while (fileReader.hasNextLine()) { String currentLine = fileReader.nextLine(); // If we find variables, replace them, with their value if (currentLine.contains("var(")) { for (CSSVar var : localCSSVars) { while (currentLine.contains(var.getName())) { currentLine = currentLine.replace("var(" + var.getName() + ")", var.getValue()); } } } // Add new currentLine to be written filesNewInfo.add(currentLine); } fileReader.close(); } catch (FileNotFoundException ex) { Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex); } // Write the new files below try { BufferedWriter writer = new BufferedWriter(new FileWriter(cssFile.getPath())); while (!filesNewInfo.isEmpty()) { writer.write(filesNewInfo.get(0)); writer.newLine(); filesNewInfo.remove(0); } writer.close(); } catch (IOException ex) { Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex); } } } }