1 /** 2 * This file Copyright (c) 2009-2018 Magnolia International 3 * Ltd. (http://www.magnolia-cms.com). All rights reserved. 4 * 5 * 6 * This file is dual-licensed under both the Magnolia 7 * Network Agreement and the GNU General Public License. 8 * You may elect to use one or the other of these licenses. 9 * 10 * This file is distributed in the hope that it will be 11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the 12 * implied warranty of MERCHANTABILITY or FITNESS FOR A 13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. 14 * Redistribution, except as permitted by whichever of the GPL 15 * or MNA you select, is prohibited. 16 * 17 * 1. For the GPL license (GPL), you can redistribute and/or 18 * modify this file under the terms of the GNU General 19 * Public License, Version 3, as published by the Free Software 20 * Foundation. You should have received a copy of the GNU 21 * General Public License, Version 3 along with this program; 22 * if not, write to the Free Software Foundation, Inc., 51 23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24 * 25 * 2. For the Magnolia Network Agreement (MNA), this file 26 * and the accompanying materials are made available under the 27 * terms of the MNA which accompanies this distribution, and 28 * is available at http://www.magnolia-cms.com/mna.html 29 * 30 * Any modifications to this file must keep this entire header 31 * intact. 32 * 33 */ 34 package info.magnolia.imaging.util; 35 36 import org.apache.commons.lang3.StringUtils; 37 38 /** 39 * Splits a String representation path where elements are separated by defined or default ('/') separator. 40 * Leading and trailing separators are ignored. Empty elements are not 41 * ("/a//c/" as three elements, "a", "" and "c") 42 * 43 * This keeps an internal iterator, such that for an input of "/foo/bar/baz/a/b/c/d/e", 44 * following sequence of calls: 45 * <ul> 46 * <li>skipTo(2) : return "baz" 47 * <li>next() : return "a" 48 * <li>remaining() : "b/c/d/e" 49 * </ul> 50 */ 51 public class PathSplitter { 52 private static final String[] EMPTY = new String[0]; 53 54 private final char separator; 55 private final String[] pathElements; 56 private int position; 57 58 /** 59 * Splits with the '/' character (NOT the system-specific file separator) and trims extensions. 60 */ 61 public PathSplitter(String source) { 62 this(source, true); 63 } 64 65 /** 66 * Splits with the '/' character (NOT the system-specific file separator). 67 */ 68 public PathSplitter(String source, boolean trimExtension) { 69 this(source, '/', trimExtension); 70 } 71 72 public PathSplitter(String source, char separator, boolean trimExtension) { 73 this.separator = separator; 74 final String sep = String.valueOf(separator); 75 if (source == null) { 76 source = ""; 77 } 78 if (source.startsWith(sep)) { 79 source = source.substring(1); 80 } 81 if (source.endsWith(sep)) { 82 source = source.substring(0, source.length() - 1); 83 } 84 // trim extension 85 int dot = source.lastIndexOf('.'); 86 if (dot < source.lastIndexOf(separator)) { // it isn't the extension but a part of the name 87 dot = -1; 88 } 89 90 if (trimExtension && dot >= 0) { 91 source = source.substring(0, dot); 92 } 93 94 this.pathElements = source.length() == 0 ? EMPTY : source.split(sep, -1); // include trailing empty matches 95 this.position = -1; 96 } 97 98 /** 99 * Returns the number of path elements in the source string. 100 */ 101 public int count() { 102 return pathElements.length; 103 } 104 105 /** 106 * Returns the next path element from the internal iterator. 107 */ 108 public String next() { 109 position++; 110 return position < pathElements.length ? pathElements[position] : ""; 111 } 112 113 /** 114 * Returns the element at the given 0-based index. 115 */ 116 public String skipTo(int index) { 117 position = index; 118 return position < pathElements.length ? pathElements[position] : ""; 119 } 120 121 /** 122 * Returns all the elements lefts after the last call to at() or next(), 123 * *without* a leading separator. This returns an empty string if there 124 * are not remaining elements. 125 * Calls to next(), skipTo() or remaining() after a call to remaining will 126 * lead to unexpected results. 127 */ 128 public String remaining() { 129 position++; 130 return StringUtils.join(pathElements, separator, position, pathElements.length); 131 } 132 }