001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.xbean.finder.util; 018 019import org.apache.xbean.finder.archive.FileArchive; 020 021import java.io.File; 022import java.net.MalformedURLException; 023import java.net.URL; 024 025public final class Files { 026 public static File toFile(final URL url) { 027 if ("jar".equals(url.getProtocol())) { 028 try { 029 final String spec = url.getFile(); 030 final int separator = spec.indexOf('!'); 031 if (separator == -1) { 032 return null; 033 } 034 return toFile(new URL(spec.substring(0, separator + 1))); 035 } catch (final MalformedURLException e) { 036 return null; 037 } 038 } else if ("file".equals(url.getProtocol())) { 039 String path = FileArchive.decode(url.getFile()); 040 if (path.endsWith("!")) { 041 path = path.substring(0, path.length() - 1); 042 } 043 return new File(path); 044 } 045 return null; 046 } 047 048 private Files() { 049 // no-op 050 } 051}