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: */
017: package org.apache.wicket.util.resource.locator;
018:
019: import java.util.Iterator;
020:
021: import org.apache.wicket.util.string.Strings;
022:
023: /**
024: * Contains the logic to build the various combinations of file path, style and
025: * locale required while searching for Wicket resources. The full filename will
026: * be built like: <path>_<style>_<locale>.<extension>.
027: * <p>
028: * Resource matches will be attempted in the following order:
029: * <ol>
030: * <li>1. <path>_<style>_<locale>.<extension></li>
031: * <li>2. <path>_<locale>.<extension></li>
032: * <li>3. <path>_<style>.<extension></li>
033: * <li>4. <path>.<extension></li>
034: * </ol>
035: * <p>
036: * Locales may contain a language, a country and a region or variant.
037: * Combinations of these components will be attempted in the following order:
038: * <ol>
039: * <li>locale.toString() see javadoc for Locale for more details</li>
040: * <li><language>_<country></li>
041: * <li><language></li>
042: * </ol>
043: *
044: * @author Juergen Donnerstag
045: * @author Jonathan Locke
046: */
047: public class ExtensionResourceNameIterator implements Iterator {
048: /** The base path */
049: private final String path;
050:
051: private final String[] extensions;
052:
053: private int index;
054:
055: /**
056: * Construct.
057: *
058: * @param path
059: * @param extension
060: */
061: public ExtensionResourceNameIterator(String path,
062: final String extension) {
063: if (extension == null) {
064: // Get the extension from the path provided
065: extensions = new String[] { "."
066: + Strings.lastPathComponent(path, '.') };
067: path = Strings.beforeLastPathComponent(path, '.');
068: } else {
069: // Extension can be a comma separated list
070: extensions = Strings.split(extension, ',');
071: for (int i = extensions.length - 1; i >= 0; i--) {
072: extensions[i] = extensions[i].trim();
073: if (!extensions[i].startsWith(".")) {
074: extensions[i] = "." + extensions[i];
075: }
076: }
077: }
078:
079: this .path = path;
080: this .index = 0;
081: }
082:
083: /**
084: *
085: * @see java.util.Iterator#hasNext()
086: */
087: public boolean hasNext() {
088: return (this .index < this .extensions.length);
089: }
090:
091: /**
092: *
093: * @see java.util.Iterator#next()
094: */
095: public Object next() {
096: return path + this .extensions[this .index++];
097: }
098:
099: /**
100: *
101: * @see java.util.Iterator#remove()
102: */
103: public void remove() {
104: }
105: }
|