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; 018 019import org.apache.commons.vfs2.FileName; 020import org.apache.commons.vfs2.FileSystemException; 021import org.apache.commons.vfs2.FileType; 022 023/** 024 * Implementation for layered file systems. 025 * <p> 026 * Additionally encodes the '!' character. 027 * </p> 028 */ 029public class LayeredFileNameParser extends AbstractFileNameParser { 030 031 private static final LayeredFileNameParser INSTANCE = new LayeredFileNameParser(); 032 033 /** 034 * Gets the singleton instance. 035 * 036 * @return the singleton instance. 037 */ 038 public static LayeredFileNameParser getInstance() { 039 return INSTANCE; 040 } 041 042 /** 043 * Constructs a new instance. 044 */ 045 public LayeredFileNameParser() { 046 // empty 047 } 048 049 /** 050 * Determines if a character should be encoded. 051 * 052 * @param ch The character to check. 053 * @return true if the character should be encoded. 054 */ 055 @Override 056 public boolean encodeCharacter(final char ch) { 057 return super.encodeCharacter(ch) || ch == LayeredFileName.LAYER_SEPARATOR; 058 } 059 060 /** 061 * Pops the root prefix off a URI, which has had the scheme removed. 062 * 063 * @param uri string builder which gets modified. 064 * @return the extracted root name. 065 */ 066 protected String extractRootName(final StringBuilder uri) { 067 // Looking for <name>!<abspath> (staring at the end) 068 final int maxlen = uri.length(); 069 int pos = maxlen - 1; 070 while (pos > 0 && uri.charAt(pos) != LayeredFileName.LAYER_SEPARATOR) { 071 pos--; 072 } 073 074 if (pos == 0 && uri.charAt(pos) != LayeredFileName.LAYER_SEPARATOR) { 075 // not ! found, so take the whole path a root 076 // e.g. zip:/my/zip/file.zip 077 pos = maxlen; 078 } 079 080 // Extract the name 081 final String prefix = uri.substring(0, pos); 082 if (pos < maxlen) { 083 uri.delete(0, pos + 1); 084 } else { 085 uri.setLength(0); 086 } 087 088 return prefix; 089 } 090 091 /** 092 * Parses the base and name into a FileName. 093 * 094 * @param context The component context. 095 * @param baseFileName The base FileName. 096 * @param fileName name The target file name. 097 * @return The constructed FileName. 098 * @throws FileSystemException if an error occurs. 099 */ 100 @Override 101 public FileName parseUri(final VfsComponentContext context, final FileName baseFileName, final String fileName) 102 throws FileSystemException { 103 final StringBuilder name = new StringBuilder(); 104 105 // Extract the scheme 106 final String scheme = UriParser.extractScheme(context.getFileSystemManager().getSchemes(), fileName, name); 107 108 // Extract the Layered file URI 109 final String rootUriName = extractRootName(name); 110 FileName rootUri = null; 111 if (rootUriName != null) { 112 rootUri = context.parseURI(rootUriName); 113 } 114 115 // Decode and normalise the path 116 UriParser.canonicalizePath(name, 0, name.length(), this); 117 UriParser.fixSeparators(name); 118 final FileType fileType = UriParser.normalisePath(name); 119 final String path = name.toString(); 120 121 return new LayeredFileName(scheme, rootUri, path, fileType); 122 } 123 124}