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.commons.vfs2.provider.res; 018 019import org.apache.commons.lang3.StringUtils; 020import org.apache.commons.vfs2.FileName; 021import org.apache.commons.vfs2.FileSystemException; 022import org.apache.commons.vfs2.FileType; 023import org.apache.commons.vfs2.provider.local.GenericFileNameParser; 024 025/** 026 * Slightly modified file name parser for resource URIs. 027 */ 028public class ResourceFileNameParser extends GenericFileNameParser { 029 030 private static final ResourceFileNameParser INSTANCE = new ResourceFileNameParser(); 031 032 /** 033 * Gets the singleton instance. 034 * 035 * @return the singleton instance. 036 */ 037 public static GenericFileNameParser getInstance() { 038 return INSTANCE; 039 } 040 041 /** 042 * Constructs a new instance. 043 */ 044 public ResourceFileNameParser() { 045 // empty 046 } 047 048 @Override 049 protected FileName createFileName(final String scheme, final String rootFile, final String path, 050 final FileType type) { 051 return new ResourceFileName(scheme, path, type); 052 } 053 054 @Override 055 protected String extractRootPrefix(final String uri, final StringBuilder name) throws FileSystemException { 056 // Resource URI (as used by ClassLoader.getResource()) are assumed to be absolute despite 057 // lacking a leading '/'. All leading '/' will be stripped from the name. 058 059 int index = 0; 060 while (index < name.length() && name.charAt(index) == '/') { 061 ++index; 062 } 063 if (index > 0) { 064 name.delete(0, index); 065 } 066 067 if (StringUtils.isEmpty(name)) { 068 throw new FileSystemException("vfs.provider.res/not-valid-resource-location.error", uri); 069 } 070 071 return "/"; 072 } 073}