1 19 20 package org.netbeans.modules.apisupport.project.ui.wizard.librarydescriptor; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.net.URL ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.zip.ZipEntry ; 32 import java.util.zip.ZipOutputStream ; 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 48 final class CreatedModifiedFilesProvider { 49 50 private static final String VOLUME_CLASS = "classpath"; private static final String VOLUME_SRC = "src"; private static final String VOLUME_JAVADOC = "javadoc"; 54 private static final String LIBRARY_LAYER_ENTRY = "org-netbeans-api-project-libraries/Libraries"; 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 template; 66 Map tokens; 67 68 template = CreatedModifiedFilesProvider.class.getResource("libdescriptemplate.xml"); tokens = getTokens(fileSupport, data.getProject(), data); 70 String 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 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 bundle = mm.getLocalizingBundle().replace('/', '.'); 86 if (bundle.endsWith(".properties")) { bundle = bundle.substring(0, bundle.length() - 11); 88 } 89 90 return bundle; 91 } 92 93 static String getLibraryDescriptorEntryPath(String libraryName) { 94 StringBuffer sb = new StringBuffer (); 95 96 sb.append(LIBRARY_LAYER_ENTRY).append("/").append(libraryName).append(".xml"); 98 return sb.toString(); } 100 101 102 private static String transformURL(final String cnb, final String pathPrefix, final String archiveName) { 103 StringBuffer sb = new StringBuffer (); 104 105 sb.append("jar:nbinst://").append(cnb).append("/"); sb.append(pathPrefix).append(archiveName).append("!/"); 108 return sb.toString(); 109 } 110 111 private static Map getTokens(CreatedModifiedFiles fileSupport, Project project, NewLibraryDescriptor.DataModel data) { 112 Map retval = new HashMap (); 113 Library library = data.getLibrary(); 114 retval.put("name_to_substitute",data.getLibraryName()); retval.put("bundle_to_substitute",getPackagePlusBundle(project).replace('/','.')); 117 Iterator it = library.getContent(VOLUME_CLASS).iterator(); 118 retval.put("classpath_to_substitute",getTokenSubstitution(it, fileSupport, data, "libs/")); 120 it = library.getContent(VOLUME_SRC).iterator(); 121 retval.put("src_to_substitute",getTokenSubstitution(it, fileSupport, data, "sources/")); 123 it = library.getContent(VOLUME_JAVADOC).iterator(); 124 retval.put("javadoc_to_substitute",getTokenSubstitution(it, fileSupport, data, "docs/")); 126 return retval; 127 } 128 129 private static String getTokenSubstitution(Iterator it, CreatedModifiedFiles fileSupport, 130 NewLibraryDescriptor.DataModel data, String pathPrefix) { 131 StringBuffer sb = new StringBuffer (); 132 while (it.hasNext()) { 133 URL originalURL = (URL )it.next(); 134 String archiveName; 135 archiveName = addArchiveToCopy(fileSupport, data, originalURL, "release/"+pathPrefix); if (archiveName != null) { 137 String codeNameBase = data.getModuleInfo().getCodeNameBase(); 138 String urlToString = transformURL(codeNameBase, pathPrefix, archiveName); sb.append("<resource>"); sb.append(urlToString); 141 if (it.hasNext()) { 142 sb.append("</resource>\n"); } else { 144 sb.append("</resource>"); } 146 } 147 } 148 return sb.toString(); 149 } 150 151 152 private static String addArchiveToCopy(CreatedModifiedFiles fileSupport,NewLibraryDescriptor.DataModel data, URL originalURL, String pathPrefix) { 153 String retval = null; 154 155 URL 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 sb = new StringBuffer (); 161 sb.append(pathPrefix).append(retval); 162 fileSupport.add(fileSupport.createFile(sb.toString(),archivURL)); 163 } else { 164 if ("file".equals(originalURL.getProtocol())) { FileObject folderToZip; 166 folderToZip = URLMapper.findFileObject(originalURL); 167 if (folderToZip != null) { 168 retval = data.getLibraryName()+".zip"; 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 relativePath; 181 ZipAndCopyOperation(Project prj, FileObject folderToZip, String 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 { 189 Collection 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 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 target, FileObject root, Collection files) throws IOException { 219 ZipOutputStream str = null; 220 try { 221 str = new ZipOutputStream (target); 222 Iterator it = files.iterator(); 223 while (it.hasNext()) { 224 FileObject fo = (FileObject)it.next(); 225 String 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 entry = new ZipEntry (relativePath); 234 str.putNextEntry(entry); 235 if (fo.isData()) { 236 InputStream 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 |