001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.schema.ldif.extractor.impl;
021
022
023 import java.io.File;
024 import java.io.IOException;
025 import java.util.Enumeration;
026 import java.util.HashMap;
027 import java.util.Map;
028 import java.util.regex.Pattern;
029 import java.util.zip.ZipEntry;
030 import java.util.zip.ZipException;
031 import java.util.zip.ZipFile;
032
033
034 /**
035 * Lists LDIF resources available from the classpath.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 * @version $Rev$, $Date$
039 */
040 public class ResourceMap
041 {
042 /**
043 * For all elements of java.class.path get a Map of resources
044 * Pattern pattern = Pattern.compile(".*"). The keys represent
045 * resource names and the boolean parameter indicates whether or
046 * not the resource is in a Jar file.
047 *
048 * @param pattern the pattern to match
049 * @return the resources with markers - true if resource is in Jar
050 */
051 public static Map<String,Boolean> getResources( Pattern pattern )
052 {
053 HashMap<String,Boolean> retval = new HashMap<String,Boolean>();
054 String classPath = System.getProperty( "java.class.path", "." );
055 String[] classPathElements = classPath.split( File.pathSeparator );
056
057 for ( String element : classPathElements )
058 {
059 getResources( retval, element, pattern );
060 }
061
062 return retval;
063 }
064
065
066 private static void getResources( HashMap<String,Boolean> map,
067 String element, Pattern pattern )
068 {
069 File file = new File( element );
070 if ( !file.exists() )
071 {
072 // this may happen if the class path contains an element that doesn't exist
073 return;
074 }
075
076 if ( file.isDirectory() )
077 {
078 getResourcesFromDirectory( map, file, pattern );
079 }
080 else
081 {
082 getResourcesFromJarFile( map, file, pattern );
083 }
084 }
085
086
087 private static void getResourcesFromJarFile( HashMap<String,Boolean> map,
088 File file, Pattern pattern )
089 {
090 ZipFile zf;
091
092 try
093 {
094 zf = new ZipFile( file );
095 }
096 catch ( ZipException e )
097 {
098 throw new Error( e );
099 }
100 catch ( IOException e )
101 {
102 throw new Error( e );
103 }
104
105 Enumeration<? extends ZipEntry> e = zf.entries();
106
107 while ( e.hasMoreElements() )
108 {
109 ZipEntry ze = e.nextElement();
110 String fileName = ze.getName();
111 boolean accept = pattern.matcher( fileName ).matches();
112
113 if ( accept )
114 {
115 map.put( fileName, Boolean.TRUE );
116 }
117 }
118 try
119 {
120 zf.close();
121 }
122 catch ( IOException e1 )
123 {
124 throw new Error( e1 );
125 }
126 }
127
128
129 private static void getResourcesFromDirectory(
130 HashMap<String,Boolean> map, File directory, Pattern pattern )
131 {
132 File[] fileList = directory.listFiles();
133
134 for ( File file : fileList )
135 {
136 if ( file.isDirectory() )
137 {
138 getResourcesFromDirectory( map, file, pattern );
139 }
140 else
141 {
142 try
143 {
144 String fileName = file.getCanonicalPath();
145 boolean accept = pattern.matcher( fileName ).matches();
146
147 if ( accept )
148 {
149 map.put( fileName, Boolean.FALSE );
150 }
151 }
152 catch ( IOException e )
153 {
154 throw new Error( e );
155 }
156 }
157 }
158 }
159 }