Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.android.builder.internal.packaging;
 
 
 
 import java.io.File;
 
 public class JavaResourceProcessor {
 
     private final IArchiveBuilder mBuilder;
 
     public interface IArchiveBuilder {

        
Adds a file to the archive at a given path

Parameters:
file the file to add
archivePath the path of the file inside the APK archive.
Throws:
com.android.builder.packaging.PackagerException if an error occurred
com.android.builder.packaging.SealedPackageException if the archive is already sealed.
com.android.builder.packaging.DuplicateFileException if a file conflicts with another already added to the APK at the same location inside the APK archive.
 
         void addFile(File fileString archivePaththrows PackagerException,
                 SealedPackageExceptionDuplicateFileException;
     }
 
 
     public JavaResourceProcessor(IArchiveBuilder builder) {
          = builder;
     }

    
Adds the resources from a source folder to a given JavaResourceProcessor.IArchiveBuilder

Parameters:
sourceLocation the source folder.
Throws:
com.android.builder.packaging.PackagerException if an error occurred
com.android.builder.packaging.SealedPackageException if the APK is already sealed.
com.android.builder.packaging.DuplicateFileException if a file conflicts with another already added to the APK at the same location inside the APK archive.
 
     public void addSourceFolder(String sourceLocation)
             throws PackagerExceptionDuplicateFileExceptionSealedPackageException {
         File sourceFolder = new File(sourceLocation);
         if (sourceFolder.isDirectory()) {
             try {
                 // file is a directory, process its content.
                 File[] files = sourceFolder.listFiles();
                 for (File file : files) {
                     processFileForResource(filenull);
                 }
             } catch (DuplicateFileException e) {
                 throw e;
             } catch (SealedPackageException e) {
                 throw e;
             } catch (Exception e) {
                 throw new PackagerException(e"Failed to add %s"sourceFolder);
             }
         } else {
             // not a directory? check if it's a file or doesn't exist
             if (sourceFolder.exists()) {
                 throw new PackagerException("%s is not a folder"sourceFolder);
             }
         }
     }


    
Processes a java.io.File that could be an APK java.io.File, or a folder containing java resources.

Parameters:
file the java.io.File to process.
path the relative path of this file to the source folder. Can be null to identify a root file.
Throws:
java.io.IOException
com.android.builder.packaging.DuplicateFileException if a file conflicts with another already added to the APK at the same location inside the APK archive.
com.android.builder.packaging.PackagerException if an error occurred
com.android.builder.packaging.SealedPackageException if the APK is already sealed.
 
     private void processFileForResource(File fileString path)
        if (file.isDirectory()) {
            // a directory? we check it
            if (PackagingUtils.checkFolderForPackaging(file.getName())) {
                // if it's valid, we append its name to the current path.
                if (path == null) {
                    path = file.getName();
                } else {
                    path = path + "/" + file.getName();
                }
                // and process its content.
                File[] files = file.listFiles();
                for (File contentFile : files) {
                    processFileForResource(contentFilepath);
                }
            }
        } else {
            // a file? we check it to make sure it should be added
            if (PackagingUtils.checkFileForPackaging(file.getName())) {
                // we append its name to the current path
                if (path == null) {
                    path = file.getName();
                } else {
                    path = path + "/" + file.getName();
                }
                // and add it to the apk
                .addFile(filepath);
            }
        }
    }
New to GrepCode? Check out our FAQ X