private ResourceProxy findSerializationDataFromCoveringParent()

in shared/api/src/main/java/org/apache/sling/ide/sync/content/impl/DefaultSyncCommandFactory.java [131:205]


   private ResourceProxy findSerializationDataFromCoveringParent(WorkspaceResource localFile,
           String resourceLocation, WorkspacePath serializationFilePath) throws IOException {

       logger.trace("Found plain nt:folder candidate at {0}, trying to find a covering resource for it",
localFile);
       
       WorkspacePath orig = serializationFilePath;

       WorkspacePath syncDirPath = localFile.getProject().getSyncDirectory().getLocalPath();
       serializationFilePath = localFile.getProject().getLocalPath().relativize(serializationFilePath);
       if ( serializationFilePath == null )
           throw new RuntimeException("Unable to get relative path from " + localFile.getProject().getLocalPath() + " to " + orig);
       serializationFilePath = serializationFilePath.absolute();
       
       while (!syncDirPath.equals(serializationFilePath)) {
           serializationFilePath = serializationFilePath.getParent();
           // TODO - better check for path going outside the sync directory
           if ( serializationFilePath.asPortableString().lastIndexOf('/') == 0 ) {
               break;
           }
           WorkspaceDirectory folderWithPossibleSerializationFile = localFile.getProject().getDirectory(serializationFilePath);
           
           
           if (!folderWithPossibleSerializationFile.exists()) {
               logger.trace("No folder found at {0}, moving up to the next level", serializationFilePath);
               continue;
           }

           // it's safe to use a specific SerializationKind since this scenario is only valid for METADATA_PARTIAL
           // coverage
           String possibleSerializationFilePath = serializationManager.getSerializationFilePath(
                   folderWithPossibleSerializationFile.getOSPath().toString(),
                   SerializationKind.METADATA_PARTIAL);

           logger.trace("Looking for serialization data in {0}", possibleSerializationFilePath);
           if (serializationManager.isSerializationFile(possibleSerializationFilePath)) {
               
               Path parentFileSerializationOSPath = localFile.getProject().getSyncDirectory().getOSPath().
                       relativize(Paths.get(possibleSerializationFilePath));

               WorkspacePath parentSerializationFilePath = new WorkspacePath(parentFileSerializationOSPath.toString())
                       .absolute();
               
               WorkspaceFile possibleSerializationFile = localFile.getProject().getSyncDirectory().getFile(parentSerializationFilePath);
               if (!possibleSerializationFile.exists()) {
                   logger.trace("Potential serialization data file {0} does not exist, moving up to the next level",
                           possibleSerializationFile.getLocalPath());
                   continue;
               }
               
               ResourceProxy serializationData;
               try (InputStream contents = possibleSerializationFile.getContents()) {
                   serializationData = serializationManager.readSerializationData(
                           parentSerializationFilePath.asPortableString(), contents);
               }

               String repositoryPath = serializationManager.getRepositoryPath(resourceLocation);
               String potentialPath = serializationData.getPath();
               boolean covered = serializationData.covers(repositoryPath);

               logger.trace(
                       "Found possible serialization data at {0}. Resource :{1} ; our resource: {2}. Covered: {3}",
                       parentSerializationFilePath, potentialPath, repositoryPath, covered);
               // note what we don't need to normalize the children here since this resource's data is covered by
               // another resource
               if (covered) {
                   return serializationData.getChild(repositoryPath);
               }

               break;
           }
       }

       return null;
   }