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.local; 018 019import java.net.URI; 020 021import org.apache.commons.vfs2.FileName; 022import org.apache.commons.vfs2.FileSystemException; 023import org.apache.commons.vfs2.FileType; 024import org.apache.commons.vfs2.provider.AbstractFileNameParser; 025import org.apache.commons.vfs2.provider.UriParser; 026import org.apache.commons.vfs2.provider.VfsComponentContext; 027 028/** 029 * A name parser. 030 */ 031public abstract class LocalFileNameParser extends AbstractFileNameParser { 032 033 /** 034 * Constructs a new instance. 035 */ 036 public LocalFileNameParser() { 037 // empty 038 } 039 040 /** 041 * Creates a FileName. 042 * 043 * @param scheme The scheme. 044 * @param rootFile the root file. 045 * @param path the path. 046 * @param fileType the file type. 047 * @return a FileName. 048 */ 049 protected abstract FileName createFileName(String scheme, String rootFile, String path, FileType fileType); 050 051 /** 052 * Pops the root prefix off a URI, which has had the scheme removed. 053 * 054 * @param name the URI to modify. 055 * @param uri the whole URI for error reporting. 056 * @return the root prefix extracted. 057 * @throws FileSystemException if an error occurs. 058 */ 059 protected abstract String extractRootPrefix(String uri, StringBuilder name) throws FileSystemException; 060 061 private String[] getSchemes(final VfsComponentContext context, final FileName base, final String uri) { 062 if (context == null) { 063 return new String[] {base != null ? base.getScheme() : URI.create(uri).getScheme()}; 064 } 065 return context.getFileSystemManager().getSchemes(); 066 } 067 068 /** 069 * Determines if a name is an absolute file name. 070 * 071 * @param name The file name. 072 * @return true if the name is absolute, false otherwise. 073 */ 074 public boolean isAbsoluteName(final String name) { 075 // TODO - this is yucky 076 final StringBuilder b = new StringBuilder(name); 077 try { 078 UriParser.fixSeparators(b); 079 extractRootPrefix(name, b); 080 return true; 081 } catch (final FileSystemException e) { 082 return false; 083 } 084 } 085 086 @Override 087 public FileName parseUri(final VfsComponentContext context, final FileName base, final String uri) 088 throws FileSystemException { 089 final StringBuilder nameBuilder = new StringBuilder(); 090 091 // Extract the scheme 092 String scheme = UriParser.extractScheme(getSchemes(context, base, uri), uri, nameBuilder); 093 if (scheme == null && base != null) { 094 scheme = base.getScheme(); 095 } 096 if (scheme == null) { 097 scheme = "file"; 098 } 099 100 // Remove encoding, and adjust the separators 101 UriParser.canonicalizePath(nameBuilder, 0, nameBuilder.length(), this); 102 103 UriParser.fixSeparators(nameBuilder); 104 105 // Extract the root prefix 106 final String rootFile = extractRootPrefix(uri, nameBuilder); 107 108 // Normalise the path 109 final FileType fileType = UriParser.normalisePath(nameBuilder); 110 111 final String path = nameBuilder.toString(); 112 113 return createFileName(scheme, rootFile, path, fileType); 114 } 115}