View Javadoc
1   /**
2    * This file Copyright (c) 2009-2015 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   * @version $Id$
52   */
53  public class PathSplitter {
54      private static final String[] EMPTY = new String[0];
55  
56      private final char separator;
57      private final String[] pathElements;
58      private int position;
59  
60      /**
61       * Splits with the '/' character (NOT the system-specific file separator) and trims extensions.
62       */
63      public PathSplitter(String source) {
64          this(source, true);
65      }
66  
67      /**
68       * Splits with the '/' character (NOT the system-specific file separator).
69       */
70      public PathSplitter(String source, boolean trimExtension) {
71          this(source, '/', trimExtension);
72      }
73  
74      public PathSplitter(String source, char separator, boolean trimExtension) {
75          this.separator = separator;
76          final String sep = String.valueOf(separator);
77          if (source == null) {
78              source = "";
79          }
80          if (source.startsWith(sep)) {
81              source = source.substring(1);
82          }
83          if (source.endsWith(sep)) {
84              source = source.substring(0, source.length() - 1);
85          }
86          // trim extension
87          int dot = source.lastIndexOf('.');
88          if (dot < source.lastIndexOf(separator)) { //it isn't the extension but a part of the name
89              dot = -1;
90          }
91  
92          if (trimExtension && dot >= 0) {
93              source = source.substring(0, dot);
94          }
95  
96          this.pathElements = source.length() == 0 ? EMPTY : source.split(sep, -1); // include trailing empty matches
97          this.position = -1;
98      }
99  
100     /**
101      * Returns the number of path elements in the source string.
102      */
103     public int count() {
104         return pathElements.length;
105     }
106 
107     /**
108      * Returns the next path element from the internal iterator.
109      */
110     public String next() {
111         position++;
112         return position < pathElements.length ? pathElements[position] : "";
113     }
114 
115     /**
116      * Returns the element at the given 0-based index.
117      * @throws ArrayIndexOutOfBoundsException
118      */
119     public String skipTo(int index) {
120         position = index;
121         return position < pathElements.length ? pathElements[position] : "";
122     }
123 
124     /**
125      * Returns all the elements lefts after the last call to at() or next(),
126      * *without* a leading separator. This returns an empty string if there
127      * are not remaining elements.
128      * Calls to next(), skipTo() or remaining() after a call to remaining will
129      * lead to unexpected results.
130      */
131     public String remaining() {
132         position++;
133         return StringUtils.join(pathElements, separator, position, pathElements.length);
134     }
135 }