Java tutorial
/* * Copyright (C) 2012 NS Solutions Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.htmlhifive.tools.wizard.utils; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.eclipse.core.internal.resources.ICoreConstants; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.swt.widgets.Shell; import com.htmlhifive.tools.wizard.log.messages.Messages; /** * <H3>Eclipse.</H3> * * @author fkubo */ public abstract class H5IOUtils { // // /** ?. */ // private static final int BUFFER = 4096; /** PROJECT_NAME. */ private static final String PROJECT_NAME = "PROJECT_NAME"; /** * ????. * * @param parent * @param monitor * @throws CoreException */ public static void createParentFolder(IContainer parent, IProgressMonitor monitor) throws CoreException { if (!ResourcesPlugin.getWorkspace().getRoot().exists(parent.getFullPath())) { createParentFolder(parent.getParent(), monitor); ((IFolder) parent).create(true, true, monitor); ((IFolder) parent).refreshLocal(IResource.DEPTH_ZERO, monitor); } } // // /** // * ?zip???. // * // * @param shell // * @param is // * @return zip? // */ // public static File createTemporaryZipFile(Shell shell, InputStream is) { // // File outputFile = null; // BufferedInputStream input = null; // BufferedOutputStream output = null; // try { // input = new BufferedInputStream(is); // outputFile = File.createTempFile("structure", ".zip"); // // VM????. // outputFile.deleteOnExit(); // output = new BufferedOutputStream(new FileOutputStream(outputFile)); // final byte[] buf = new byte[BUFFER]; // int len; // while ((len = input.read(buf)) != -1) { // output.write(buf, 0, len); // } // output.flush(); // } catch (IOException e) { // log(shell, e, Messages.SE0022.format(), Messages.SE0024); // } finally { // IOUtils.closeQuietly(is); // IOUtils.closeQuietly(input); // IOUtils.closeQuietly(output); // } // return outputFile; // } /** * ????. ${PROJECT_NAME}???????. * * @param shell * @param projectHandle ? * @param fileName ?? */ public static void convertProjectName(Shell shell, IProject projectHandle, String fileName) { convertName(shell, projectHandle, fileName, new String[] { PROJECT_NAME }, new String[] { projectHandle.getName() }); } /** * ????. ${PROJECT_NAME}???????. * * @param shell * @param projectHandle ? * @param fileName ?? * @param varNames ?? */ private static void convertName(Shell shell, IProject projectHandle, String fileName, String[] varNames, String[] varValues) { final IFile file = projectHandle.getFile(new Path(fileName)); if (!file.isAccessible()) { return; } InputStream is = null; InputStream is2 = null; try { is = file.getContents(); String text = IOUtils.toString(is, file.getCharset()); for (int i = 0; i < varNames.length; i++) { text = StringUtils.replace(text, "${" + varNames[i] + "}", varValues[i]); } is2 = IOUtils.toInputStream(text, file.getCharset()); file.setContents(is2, true, true, null); } catch (IOException e) { H5LogUtils.putLog(e, Messages.SE0024, fileName); //H5LogUtils.showLog(e, Messages.SE0022, Messages.SE0024, fileName); } catch (CoreException e) { H5LogUtils.putLog(e, Messages.SE0024, fileName); //H5LogUtils.showLog(e, Messages.SE0022, Messages.SE0024, fileName); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(is2); } } /** * ?????. * * @param path * @return ??????? */ public static boolean isValidWorkspacePath(IPath path) { // ???Workspace.newResource?. return path.isValidPath(path.toString()) && path.segmentCount() >= ICoreConstants.MINIMUM_FOLDER_SEGMENT_LENGTH; } /** * URL???????????. * * @param url URL * @return URL????????? */ public static boolean isClassResources(String url) { return url.startsWith("/"); } /** * ???. * * @param siteUrl URL * @return ?? */ public static String getURLPath(String siteUrl) { try { if (!H5IOUtils.isClassResources(siteUrl)) { // ??. return new URL(siteUrl).getPath(); } return siteUrl; } catch (MalformedURLException e) { } return null; } }