1 9 10 package org.netbeans.modules.xml.catalogsupport; 11 12 import java.io.File ; 13 import java.io.IOException ; 14 import java.net.URI ; 15 import java.net.URISyntaxException ; 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.Set ; 19 import java.util.StringTokenizer ; 20 import org.netbeans.api.java.project.JavaProjectConstants; 21 import org.netbeans.api.project.FileOwnerQuery; 22 import org.netbeans.api.project.Project; 23 import org.netbeans.api.project.ProjectUtils; 24 import org.netbeans.api.project.SourceGroup; 25 import org.netbeans.api.project.Sources; 26 import org.netbeans.modules.web.api.webmodule.WebProjectConstants; 27 import org.netbeans.modules.xml.catalogsupport.util.ProjectReferenceUtility; 28 import org.netbeans.modules.xml.retriever.catalog.CatalogEntry; 29 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel; 30 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory; 31 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 32 import org.netbeans.spi.project.SubprojectProvider; 33 import org.netbeans.spi.project.support.ant.AntProjectHelper; 34 import org.netbeans.spi.project.support.ant.PropertyUtils; 35 import org.netbeans.spi.project.support.ant.ReferenceHelper; 36 import org.openide.filesystems.FileObject; 37 import org.netbeans.modules.xml.retriever.catalog.ProjectCatalogSupport; 38 import org.openide.filesystems.FileUtil; 39 40 44 public class DefaultProjectCatalogSupport extends ProjectCatalogSupport { 45 46 public static final String NBURI_SCHEME = "nb-uri"; 47 48 public Project project; 49 public AntProjectHelper helper; 50 public ReferenceHelper refHelper; 51 54 public DefaultProjectCatalogSupport(Project project) { 55 this(project,null,null); 56 } 57 58 public DefaultProjectCatalogSupport(Project project, AntProjectHelper helper, 59 ReferenceHelper refHelper) { 60 this.project = project; 61 this.helper = helper; 62 this.refHelper = refHelper; 63 } 64 65 public static DefaultProjectCatalogSupport getInstance(FileObject source) { 66 Project owner = FileOwnerQuery.getOwner(source); 67 if(owner!=null) { 68 DefaultProjectCatalogSupport support = (DefaultProjectCatalogSupport)owner. 69 getLookup().lookup(DefaultProjectCatalogSupport.class); 70 if(support!=null) return support; 71 } 72 return new DefaultProjectCatalogSupport(owner); 73 } 74 75 public boolean supportsCrossProject() { 76 return helper!=null; 77 } 78 79 public URI constructProjectProtocol(FileObject foTobeAddedInCat) { 80 Project owner = FileOwnerQuery.getOwner(foTobeAddedInCat); 81 if(owner!=null) { 82 String ssp = getUsableProjectName(owner); 83 String fragment = getRelativePath(owner.getProjectDirectory(),foTobeAddedInCat); 84 try { 85 return new URI (NBURI_SCHEME,ssp,fragment); 86 } catch (URISyntaxException ex) { 87 } 88 } 89 return null; 90 } 91 92 public boolean isProjectProtocol(URI uriStoredInCatFile) { 93 return NBURI_SCHEME.equals(uriStoredInCatFile.getScheme()); 94 } 95 96 public FileObject resolveProjectProtocol(URI uriToBeResolved) { 97 if(supportsCrossProject() && isProjectProtocol(uriToBeResolved)) { 98 String ssp = uriToBeResolved.getSchemeSpecificPart(); 99 String targetPrjRelativeRoot = helper.getProperties( 100 AntProjectHelper.PROJECT_PROPERTIES_PATH). 101 getProperty("project.".concat(ssp)); 102 if(targetPrjRelativeRoot!=null){ 103 File myPrjRoot = FileUtil.toFile(project.getProjectDirectory()); 104 File tgtPrjRoot = new File (myPrjRoot.toURI().resolve(targetPrjRelativeRoot)); 105 FileObject tgtPrjFobj = FileUtil.toFileObject(FileUtil.normalizeFile(tgtPrjRoot)); 106 return tgtPrjFobj.getFileObject(uriToBeResolved.getFragment()); 107 } 108 } 109 return null; 110 } 111 112 public boolean needsCatalogEntry(FileObject source, FileObject target) { 113 assert source !=null && target !=null; 114 if(project!=FileOwnerQuery.getOwner(target)) { 116 return false; 117 } 118 119 FileObject folder = getSourceFolder(source); 120 if (folder != null && !FileUtil.isParentOf(folder,target)) { 121 return true; 122 } 123 124 return false; 125 } 126 127 public URI createCatalogEntry(FileObject source, FileObject target) throws 128 IOException , CatalogModelException { 129 assert source !=null && target !=null; 130 CatalogWriteModel cwm = CatalogWriteModelFactory.getInstance(). 131 getCatalogWriteModelForProject(project.getProjectDirectory()); 132 assert cwm!= null; 133 Project targetProject = FileOwnerQuery.getOwner(target); 134 URI targetURI = null; 135 URI sourceURI = null; 136 try { 137 sourceURI = getReferenceURI(source, target); 138 } catch (URISyntaxException ex) { 139 return null; 140 } 141 if(project!=targetProject) { 142 if(!getProjectReferences().contains(targetProject) && 143 supportsCrossProject()) { 144 ProjectReferenceUtility.addProjectReference(refHelper,targetProject); 145 }; 146 targetURI = constructProjectProtocol(target); 147 } else { 148 try { 149 targetURI = new URI (getRelativePath(cwm.getCatalogFileObject(),target)); 150 } catch (URISyntaxException ex) { 151 return null; 152 } 153 } 154 cwm.addURI(sourceURI,targetURI); 155 return sourceURI; 156 } 157 158 public URI getReferenceURI(FileObject source, FileObject target) throws URISyntaxException { 159 Project targetProject = FileOwnerQuery.getOwner(target); 160 FileObject sourceFolder = getSourceFolder(source); 161 if (sourceFolder == null) { 162 throw new IllegalArgumentException (source.getPath()+" is not in project source"); } 164 String relPathToSrcGroup = getRelativePath(source.getParent(), sourceFolder); 165 String relPathToSrcGroupWithSlash = relPathToSrcGroup.trim().equals("")? "" : 166 relPathToSrcGroup.concat("/"); 167 if(project!=targetProject) { 168 FileObject folder = getSourceFolder(targetProject,target); 169 if (folder == null) { 170 throw new IllegalArgumentException (target.getPath()+" is not in target project source"); } 172 String relPathFromTgtGroup = getRelativePath(folder,target); 173 return new URI (relPathToSrcGroupWithSlash.concat( 174 getUsableProjectName(targetProject)). 175 concat("/").concat(relPathFromTgtGroup)); 176 } else { 177 FileObject targetSourceFolder = getSourceFolder(target); 178 if (targetSourceFolder == null) { 179 throw new IllegalArgumentException (target.getPath()+" is not in project source"); } 181 String relPathFromTgtGroup = 182 getRelativePath(targetSourceFolder,target); 183 return new URI (relPathToSrcGroupWithSlash.concat(relPathFromTgtGroup)); 184 } 185 } 186 187 public Set getProjectReferences() { 188 SubprojectProvider provider = (SubprojectProvider)project.getLookup(). 189 lookup(SubprojectProvider.class); 190 return provider.getSubprojects(); 191 } 192 193 private FileObject getSourceFolder(FileObject source) { 194 return getSourceFolder(project,source); 195 } 196 197 private static String [] sourceTypes = new String [] { 198 JavaProjectConstants.SOURCES_TYPE_JAVA, 199 WebProjectConstants.TYPE_DOC_ROOT, 200 WebProjectConstants.TYPE_WEB_INF 201 }; 202 203 private static FileObject getSourceFolder(Project project, FileObject source) { 204 Sources sources = ProjectUtils.getSources(project); 205 assert sources !=null; 206 ArrayList <SourceGroup> sourceGroups = new ArrayList <SourceGroup>(); 207 for (String type : sourceTypes) { 208 SourceGroup[] groups = sources.getSourceGroups(type); 209 if (groups != null) { 210 sourceGroups.addAll(Arrays.asList(groups)); 211 } 212 } 213 214 assert sourceGroups.size()>0; 215 for(SourceGroup sourceGroup:sourceGroups) { 216 if(FileUtil.isParentOf(sourceGroup.getRootFolder(),source)) 217 return sourceGroup.getRootFolder(); 218 } 219 return null; 220 } 221 222 private static String getRelativePath(FileObject source, FileObject target) { 223 File sourceLocationFile = FileUtil.toFile(source); 224 File targetLocationFile = FileUtil.toFile(target); 225 String sourceLocation = sourceLocationFile.toURI().toString(); 226 String targetLocation = targetLocationFile.toURI().toString(); 227 StringTokenizer st1 = new StringTokenizer (sourceLocation,"/"); 228 StringTokenizer st2 = new StringTokenizer (targetLocation,"/"); 229 String relativeLoc = ""; 230 while (st1.hasMoreTokens() && st2.hasMoreTokens()) { 231 relativeLoc = st2.nextToken(); 232 if (!st1.nextToken().equals(relativeLoc)) { 233 break; 234 } 235 if(!st1.hasMoreTokens() || !st2.hasMoreTokens()) { 236 if(st1.hasMoreElements()) { 238 relativeLoc = ".."; 240 st1.nextToken(); 241 } else if(st2.hasMoreTokens()) { 242 relativeLoc = st2.nextToken(); 244 } else { 245 relativeLoc = ""; 247 } 248 } 249 } 250 while (st1.hasMoreTokens()) { 251 relativeLoc = "../".concat(relativeLoc); 252 st1.nextToken(); 253 } 254 while(st2.hasMoreTokens()) { 255 relativeLoc = relativeLoc.concat("/").concat(st2.nextToken()); 256 } 257 return relativeLoc; 258 } 259 260 261 private static String getUsableProjectName(Project project) { 262 return PropertyUtils.getUsablePropertyName(ProjectUtils.getInformation 263 (project).getName()).replace('.','_'); 264 } 265 266 public boolean removeCatalogEntry(URI uri) throws IOException { 267 CatalogWriteModel cwm = null; 268 try { 269 cwm = CatalogWriteModelFactory.getInstance(). 270 getCatalogWriteModelForProject(project.getProjectDirectory()); 271 } catch (CatalogModelException ex) { 272 return false; 273 } 274 boolean entryFound = false; 275 for(CatalogEntry ce : cwm.getCatalogEntries()){ 276 URI src = null; 277 try { 278 src = new URI (ce.getSource()); 279 } catch (URISyntaxException ex) { 280 continue; 281 } 282 if(src.equals(uri)){ 283 entryFound = true; 284 break; 285 } 286 } 287 if(entryFound){ 288 cwm.removeURI(uri); 289 return true; 290 } 291 return false; 292 } 293 294 } 295 | Popular Tags |