001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.visualweb.insync;
029:
030: import java.io.File;
031: import java.io.FileInputStream;
032: import java.io.IOException;
033: import java.io.OutputStream;
034: import java.util.Arrays;
035: import java.util.zip.ZipEntry;
036: import java.util.zip.ZipInputStream;
037: import org.netbeans.api.project.Project;
038: import org.netbeans.api.project.ProjectManager;
039: import org.netbeans.api.project.ui.OpenProjects;
040: import org.openide.filesystems.FileLock;
041: import org.openide.filesystems.FileObject;
042: import org.openide.filesystems.FileUtil;
043:
044: /**
045: *
046: * @author jdeva
047: */
048: public class ProjectUtils {
049: public static Project openProject(File workDir, String zipPath,
050: String projectName) throws IOException {
051: assert (zipPath != null);
052: File archiveFile = new File(zipPath);
053:
054: FileObject destFileObj = FileUtil.toFileObject(workDir);
055: unZipFile(archiveFile, destFileObj);
056:
057: //Unzip results in adding the valid project directory as no such project because of
058: //FileBuiltQuery implementation in insync, therefore we cannot open multiple zipped projects
059: //The following statement should clear the cache but is not public
060: //ProjectManager.getDefault().reset();
061:
062: assert (destFileObj.isValid() == true);
063: FileObject testApp = destFileObj.getFileObject(projectName);
064:
065: assert (testApp != null);
066: System.out.println("Children of " + projectName + ":"
067: + Arrays.toString(testApp.getChildren()));
068: //assertTrue( ProjectManager.getDefault().isProject(testApp));
069: Project project = ProjectManager.getDefault().findProject(
070: testApp);
071: assert (project != null);
072: OpenProjects.getDefault()
073: .open(new Project[] { project }, false);
074: return project;
075: }
076:
077: public static void destroyProject(Project project)
078: throws IOException {
079: OpenProjects.getDefault().close(new Project[] { project });
080: project.getProjectDirectory().delete();
081: //Need to clear the cache, otherwise subsequent call to findProject will fail
082: ProjectManager.getDefault().clearNonProjectCache();
083: }
084:
085: private static void unZipFile(File archiveFile, FileObject destDir)
086: throws IOException {
087: FileInputStream fis = new FileInputStream(archiveFile);
088: try {
089: ZipInputStream str = new ZipInputStream(fis);
090: ZipEntry entry;
091: while ((entry = str.getNextEntry()) != null) {
092: if (entry.isDirectory()) {
093: FileUtil.createFolder(destDir, entry.getName());
094: } else {
095: FileObject fo = FileUtil.createData(destDir, entry
096: .getName());
097: FileLock lock = fo.lock();
098: try {
099: OutputStream out = fo.getOutputStream(lock);
100: try {
101: FileUtil.copy(str, out);
102: } finally {
103: out.close();
104: }
105: } finally {
106: lock.releaseLock();
107: }
108: }
109: }
110: } finally {
111: fis.close();
112: }
113: }
114: }
|