• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.jarutils;
18 
19 import com.android.jarutils.SignedJarBuilder.IZipEntryFilter;
20 
21 /**
22  * A basic implementation of {@link IZipEntryFilter} to filter out anything that is not a
23  * java resource.
24  */
25 public class JavaResourceFilter implements IZipEntryFilter {
26 
checkEntry(String name)27     public boolean checkEntry(String name) {
28         // split the path into segments.
29         String[] segments = name.split("/");
30 
31         // empty path? skip to next entry.
32         if (segments.length == 0) {
33             return false;
34         }
35 
36         // Check each folders to make sure they should be included.
37         // Folders like CVS, .svn, etc.. should already have been excluded from the
38         // jar file, but we need to exclude some other folder (like /META-INF) so
39         // we check anyway.
40         for (int i = 0 ; i < segments.length - 1; i++) {
41             if (checkFolderForPackaging(segments[i]) == false) {
42                 return false;
43             }
44         }
45 
46         // get the file name from the path
47         String fileName = segments[segments.length-1];
48 
49         return checkFileForPackaging(fileName);
50     }
51 
52     /**
53      * Checks whether a folder and its content is valid for packaging into the .apk as
54      * standard Java resource.
55      * @param folderName the name of the folder.
56      */
checkFolderForPackaging(String folderName)57     public static boolean checkFolderForPackaging(String folderName) {
58         return folderName.equals("CVS") == false &&
59             folderName.equals(".svn") == false &&
60             folderName.equals("SCCS") == false &&
61             folderName.equals("META-INF") == false &&
62             folderName.startsWith("_") == false;
63     }
64 
65     /**
66      * Checks a file to make sure it should be packaged as standard resources.
67      * @param fileName the name of the file (including extension)
68      * @return true if the file should be packaged as standard java resources.
69      */
checkFileForPackaging(String fileName)70     public static boolean checkFileForPackaging(String fileName) {
71         String[] fileSegments = fileName.split("\\.");
72         String fileExt = "";
73         if (fileSegments.length > 1) {
74             fileExt = fileSegments[fileSegments.length-1];
75         }
76 
77         return checkFileForPackaging(fileName, fileExt);
78     }
79 
80     /**
81      * Checks a file to make sure it should be packaged as standard resources.
82      * @param fileName the name of the file (including extension)
83      * @param extension the extension of the file (excluding '.')
84      * @return true if the file should be packaged as standard java resources.
85      */
checkFileForPackaging(String fileName, String extension)86     public  static boolean checkFileForPackaging(String fileName, String extension) {
87         // Note: this method is used by com.android.ide.eclipse.adt.internal.build.ApkBuilder
88         if (fileName.charAt(0) == '.') { // ignore hidden files.
89             return false;
90         }
91 
92         return "aidl".equalsIgnoreCase(extension) == false &&       // Aidl files
93             "java".equalsIgnoreCase(extension) == false &&          // Java files
94             "class".equalsIgnoreCase(extension) == false &&         // Java class files
95             "scc".equalsIgnoreCase(extension) == false &&           // VisualSourceSafe
96             "swp".equalsIgnoreCase(extension) == false &&           // vi swap file
97             "package.html".equalsIgnoreCase(fileName) == false &&   // Javadoc
98             "overview.html".equalsIgnoreCase(fileName) == false &&  // Javadoc
99             ".cvsignore".equalsIgnoreCase(fileName) == false &&     // CVS
100             ".DS_Store".equals(fileName) == false &&                // Mac resources
101             fileName.charAt(fileName.length()-1) != '~';            // Backup files
102     }
103 }
104