Java tutorial
/* * Copyright (c) 2016 Washington State Department of Transportation * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/> * */ package gov.wa.wsdot.cms.utils; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import org.apache.commons.io.FileUtils; public class Migration { /** * Remove weird HTML markup * * Remove weird HTML markup which has been put there by authors, browsers or the * RAD placeholder editor itself over the years. * * @param content * @return */ public static String sanitizeContent(String content) { // Regular expressions sanitizing rules. if (content != null) { content = content.replaceAll("<p>(<br>)*</p>", ""); // Remove empty repeating <p> tags. content = content.replaceAll("(<strong(.*?)>)+(.*?)(</strong>)+", "<strong>$3</strong>"); // Remove nested <strong> tags. // (360) 123-4567 // 360-123-4567 // 360.123.4567 // 1-888-123-4567 content = content.replaceAll( "<span class=\"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\".+?>" + "((\\d{1}[-.\\s])?\\(?\\d{3}\\)?[-.\\s]\\d{3}[-.]\\d{4})\\s*?" + "<a.+?><img.+?src=\"data:image/png;base64,iVBOR.+?uQmCC\"\\s?/?>\\s?</a></span>", "$1"); // Remove blue phone icon and surrounding span and anchor tags. Leave just the phone number. content = content.replaceAll("(?i)(http://(www|edit).wsdot.wa.gov)?/public/images/uparrow.gif", "/sites/default/files/images/uparrow.gif"); // Rewrite location of uparrow.gif image } return content; } /** * Renames resources to original filename which are not GUID based. * * @param oldFilename * @param newFilename */ public static void resourceCopy(String archiveFolder, String oldFilename, String newFilename) { File destinationPath = new File("export/" + archiveFolder); File oldFile = new File(archiveFolder + File.separator + oldFilename); File newFile = new File(destinationPath + File.separator + newFilename); if (!destinationPath.exists()) { if (destinationPath.mkdirs()) { try { FileUtils.copyFile(oldFile, newFile); } catch (IOException e) { System.out.println("Copy failed: " + oldFilename + " to " + newFilename); e.printStackTrace(); } } } else { try { FileUtils.copyFile(oldFile, newFile); } catch (IOException e) { System.out.println("Copy failed: " + oldFilename + " to " + newFilename); e.printStackTrace(); } } } /** * Delete resource files named after GUIDs. */ public static void deleteResources(String archiveFolder) { File files[] = (new File(archiveFolder)).listFiles(new FilenameFilter() { @Override public boolean accept(File archiveFolder, String name) { return name.matches("res[A-Z0-9]{32}.*"); } }); System.out.println("Deleting resources."); for (File file : files) { if (!file.delete()) { System.out.println("Can't remove: " + file.getAbsolutePath()); } else { System.out.println("Removed: " + file.getAbsolutePath()); } } System.out.println("Done."); } /** * Match all files in directory which are not xml files. */ public static void matchResources(String archiveFolder) { File files[] = (new File(archiveFolder)).listFiles(new FilenameFilter() { @Override public boolean accept(File archiveFolder, String name) { return name.matches(".*(?<!\\.xml)$"); } }); for (File file : files) { System.out.println("Found: " + file); } } /** * Calculate number of days since the 1900 epoch. * * @param daysSinceEpoch days since epoch in milliseconds * @return converted date in the form "Tue Apr 09 07:56:37 PDT 2013" */ public static String convertDays(double daysSinceEpoch) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); // Set TimeZone to UTC Calendar startDate = Calendar.getInstance(); startDate.set(1899, 11, 31, 16, 0, 0); long MILLIS_PER_DAY = 86400000; long createdWhen = (long) (daysSinceEpoch * MILLIS_PER_DAY + startDate.getTimeInMillis()) - 2 * MILLIS_PER_DAY; return dateFormat.format(new Date(createdWhen)); } }