Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging.util

File PathSplitter.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

16
24
7
1
132
51
16
0.67
3.43
7
2.29

Classes

Class Line # Actions
PathSplitter 51 24 0% 16 0
1.0100%
 

Contributing tests

This file is covered by 21 tests. .

Source view

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  19 toggle public PathSplitter(String source) {
62  19 this(source, true);
63    }
64   
65    /**
66    * Splits with the '/' character (NOT the system-specific file separator).
67    */
 
68  24 toggle public PathSplitter(String source, boolean trimExtension) {
69  24 this(source, '/', trimExtension);
70    }
71   
 
72  29 toggle public PathSplitter(String source, char separator, boolean trimExtension) {
73  29 this.separator = separator;
74  29 final String sep = String.valueOf(separator);
75  29 if (source == null) {
76  4 source = "";
77    }
78  29 if (source.startsWith(sep)) {
79  18 source = source.substring(1);
80    }
81  29 if (source.endsWith(sep)) {
82  6 source = source.substring(0, source.length() - 1);
83    }
84    // trim extension
85  29 int dot = source.lastIndexOf('.');
86  29 if (dot < source.lastIndexOf(separator)) { // it isn't the extension but a part of the name
87  11 dot = -1;
88    }
89   
90  29 if (trimExtension && dot >= 0) {
91  6 source = source.substring(0, dot);
92    }
93   
94  29 this.pathElements = source.length() == 0 ? EMPTY : source.split(sep, -1); // include trailing empty matches
95  29 this.position = -1;
96    }
97   
98    /**
99    * Returns the number of path elements in the source string.
100    */
 
101  18 toggle public int count() {
102  18 return pathElements.length;
103    }
104   
105    /**
106    * Returns the next path element from the internal iterator.
107    */
 
108  22 toggle public String next() {
109  22 position++;
110  22 return position < pathElements.length ? pathElements[position] : "";
111    }
112   
113    /**
114    * Returns the element at the given 0-based index.
115    */
 
116  23 toggle public String skipTo(int index) {
117  23 position = index;
118  23 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  14 toggle public String remaining() {
129  14 position++;
130  14 return StringUtils.join(pathElements, separator, position, pathElements.length);
131    }
132    }