KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > wizard > librarydescriptor > CreatedModifiedFilesProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.ui.wizard.librarydescriptor;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32 import java.util.zip.ZipOutputStream JavaDoc;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.api.project.libraries.Library;
35 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles;
36 import org.netbeans.modules.apisupport.project.CreatedModifiedFilesFactory;
37 import org.netbeans.modules.apisupport.project.ManifestManager;
38 import org.netbeans.modules.apisupport.project.NbModuleProject;
39 import org.openide.filesystems.FileLock;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.filesystems.URLMapper;
43
44 /**
45  *
46  * @author Radek Matous
47  */

48 final class CreatedModifiedFilesProvider {
49     
50     private static final String JavaDoc VOLUME_CLASS = "classpath";//NOI18N
51
private static final String JavaDoc VOLUME_SRC = "src";//NOI18N
52
private static final String JavaDoc VOLUME_JAVADOC = "javadoc";//NOI18N
53

54     private static final String JavaDoc LIBRARY_LAYER_ENTRY = "org-netbeans-api-project-libraries/Libraries";//NOI18N
55

56     static CreatedModifiedFiles createInstance(NewLibraryDescriptor.DataModel data) {
57         
58         CreatedModifiedFiles retval = new CreatedModifiedFiles(data.getProject());
59         addOperations(retval, data);
60         
61         return retval;
62     }
63     
64     private static void addOperations(CreatedModifiedFiles fileSupport, NewLibraryDescriptor.DataModel data) {
65         URL JavaDoc template;
66         Map JavaDoc tokens;
67         
68         template = CreatedModifiedFilesProvider.class.getResource("libdescriptemplate.xml");//NOI18N
69
tokens = getTokens(fileSupport, data.getProject(), data);
70         String JavaDoc layerEntry = getLibraryDescriptorEntryPath(data.getLibraryName());
71         
72         fileSupport.add(
73                 fileSupport.createLayerEntry(layerEntry, template, tokens, null, null));
74         
75         fileSupport.add(
76                 fileSupport.bundleKeyDefaultBundle(data.getLibraryName(), data.getLibraryDisplayName()));
77     }
78     
79     
80     private static String JavaDoc getPackagePlusBundle(Project project) {
81         NbModuleProject nbproj = project.getLookup().lookup(NbModuleProject.class);
82         assert nbproj != null : "this template works only with default netbeans modules.";
83         ManifestManager mm = ManifestManager.getInstance(nbproj.getManifest(), false);
84         
85         String JavaDoc bundle = mm.getLocalizingBundle().replace('/', '.');
86         if (bundle.endsWith(".properties")) { // NOI18N
87
bundle = bundle.substring(0, bundle.length() - 11);
88         }
89         
90         return bundle;
91     }
92     
93     static String JavaDoc getLibraryDescriptorEntryPath(String JavaDoc libraryName) {
94         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
95         
96         sb.append(LIBRARY_LAYER_ENTRY).append("/").append(libraryName).append(".xml");//NOI18N
97

98         return sb.toString();//NOI18N
99
}
100     
101     
102     private static String JavaDoc transformURL(final String JavaDoc cnb, final String JavaDoc pathPrefix, final String JavaDoc archiveName) {
103         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
104         
105         sb.append("jar:nbinst://").append(cnb).append("/");//NOI18N
106
sb.append(pathPrefix).append(archiveName).append("!/");//NOI18N
107

108         return sb.toString();
109     }
110     
111     private static Map JavaDoc getTokens(CreatedModifiedFiles fileSupport, Project project, NewLibraryDescriptor.DataModel data) {
112         Map JavaDoc retval = new HashMap JavaDoc();
113         Library library = data.getLibrary();
114         retval.put("name_to_substitute",data.getLibraryName());//NOI18N
115
retval.put("bundle_to_substitute",getPackagePlusBundle(project).replace('/','.'));//NOI18N
116

117         Iterator JavaDoc it = library.getContent(VOLUME_CLASS).iterator();
118         retval.put("classpath_to_substitute",getTokenSubstitution(it, fileSupport, data, "libs/"));//NOI18N
119

120         it = library.getContent(VOLUME_SRC).iterator();
121         retval.put("src_to_substitute",getTokenSubstitution(it, fileSupport, data, "sources/"));//NOI18N
122

123         it = library.getContent(VOLUME_JAVADOC).iterator();
124         retval.put("javadoc_to_substitute",getTokenSubstitution(it, fileSupport, data, "docs/"));//NOI18N
125

126         return retval;
127     }
128     
129     private static String JavaDoc getTokenSubstitution(Iterator JavaDoc it, CreatedModifiedFiles fileSupport,
130             NewLibraryDescriptor.DataModel data, String JavaDoc pathPrefix) {
131         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
132         while (it.hasNext()) {
133             URL JavaDoc originalURL = (URL JavaDoc)it.next();
134             String JavaDoc archiveName;
135             archiveName = addArchiveToCopy(fileSupport, data, originalURL, "release/"+pathPrefix);//NOI18N
136
if (archiveName != null) {
137                 String JavaDoc codeNameBase = data.getModuleInfo().getCodeNameBase();
138                 String JavaDoc urlToString = transformURL(codeNameBase, pathPrefix, archiveName);//NOI18N
139
sb.append("<resource>");//NOI18N
140
sb.append(urlToString);
141                 if (it.hasNext()) {
142                     sb.append("</resource>\n");//NOI18N
143
} else {
144                     sb.append("</resource>");//NOI18N
145
}
146             }
147         }
148         return sb.toString();
149     }
150     
151     /** returns archive name or temporarily null cause there is no zip support for file protocol */
152     private static String JavaDoc addArchiveToCopy(CreatedModifiedFiles fileSupport,NewLibraryDescriptor.DataModel data, URL JavaDoc originalURL, String JavaDoc pathPrefix) {
153         String JavaDoc retval = null;
154         
155         URL JavaDoc archivURL = FileUtil.getArchiveFile(originalURL);
156         if (archivURL != null && FileUtil.isArchiveFile(archivURL)) {
157             FileObject archiv = URLMapper.findFileObject(archivURL);
158             assert archiv != null;
159             retval = archiv.getNameExt();
160             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
161             sb.append(pathPrefix).append(retval);
162             fileSupport.add(fileSupport.createFile(sb.toString(),archivURL));
163         } else {
164             if ("file".equals(originalURL.getProtocol())) {//NOI18N
165
FileObject folderToZip;
166                 folderToZip = URLMapper.findFileObject(originalURL);
167                 if (folderToZip != null) {
168                     retval = data.getLibraryName()+".zip";//NOI18N
169
pathPrefix += retval;
170                     fileSupport.add(new ZipAndCopyOperation(data.getProject(),
171                             folderToZip, pathPrefix));
172                 }
173             }
174         }
175         return retval;
176     }
177     
178     private static class ZipAndCopyOperation extends CreatedModifiedFilesFactory.OperationBase {
179         private FileObject folderToZip;
180         private String JavaDoc relativePath;
181         ZipAndCopyOperation(Project prj, FileObject folderToZip, String JavaDoc relativePath) {
182             super(prj);
183             this.folderToZip = folderToZip;
184             this.relativePath = relativePath;
185             addCreatedOrModifiedPath(relativePath, false);
186         }
187         
188         public void run() throws java.io.IOException JavaDoc {
189             Collection JavaDoc files = Collections.list(folderToZip.getChildren(true));
190             if (files.isEmpty()) return;
191             FileObject prjDir = getProject().getProjectDirectory();
192             assert prjDir != null;
193             
194             FileObject zipedTarget = prjDir.getFileObject(relativePath);
195             if (zipedTarget == null) {
196                 zipedTarget = FileUtil.createData(prjDir, relativePath);
197             }
198             
199             assert zipedTarget != null;
200             FileLock fLock = null;
201             OutputStream JavaDoc os = null;
202             
203             try {
204                 fLock = zipedTarget.lock();
205                 os = zipedTarget.getOutputStream(fLock);
206                 createZipFile(os, folderToZip, files);
207             } finally {
208                 if (os != null) {
209                     os.close();
210                 }
211                 
212                 if (fLock != null) {
213                     fLock.releaseLock();
214                 }
215             }
216         }
217         
218         private static void createZipFile(OutputStream JavaDoc target, FileObject root, Collection JavaDoc /* FileObject*/ files) throws IOException JavaDoc {
219             ZipOutputStream JavaDoc str = null;
220             try {
221                 str = new ZipOutputStream JavaDoc(target);
222                 Iterator JavaDoc it = files.iterator();
223                 while (it.hasNext()) {
224                     FileObject fo = (FileObject)it.next();
225                     String JavaDoc relativePath = FileUtil.getRelativePath(root, fo);
226                     if (fo.isFolder()) {
227                         if (fo.getChildren().length > 0) {
228                             continue;
229                         } else if (!relativePath.endsWith("/")) {
230                             relativePath += "/";
231                         }
232                     }
233                     ZipEntry JavaDoc entry = new ZipEntry JavaDoc(relativePath);
234                     str.putNextEntry(entry);
235                     if (fo.isData()) {
236                         InputStream JavaDoc in = null;
237                         try {
238                             in = fo.getInputStream();
239                             FileUtil.copy(in, str);
240                         } finally {
241                             if (in != null) {
242                                 in.close();
243                             }
244                         }
245                     }
246                     str.closeEntry();
247                 }
248             } finally {
249                 if (str != null) {
250                     str.close();
251                 }
252             }
253         }
254     }
255 }
256
Popular Tags