001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.deployment.scanner;
023:
024: import org.jboss.deployment.DefaultDeploymentSorter;
025:
026: import java.util.Comparator;
027: import java.net.URL;
028:
029: /**
030: * This is simialr to the PrefixDeploymentSorter in that it will order
031: * files that do not start with a numeric value before those that do.
032: * If two files begin with a number, will compare the numeric values.
033: * However, if the two files do not have numeric prefixes, will
034: * compare them using compareToIgnoreCase.
035: *
036: * @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
037: */
038: public class AlphaNumericDeploymentSorter implements Comparator,
039: DefaultDeploymentSorter {
040:
041: private PrefixDeploymentSorter sorter = new PrefixDeploymentSorter();
042:
043: public String[] getSuffixOrder() {
044: return sorter.getSuffixOrder();
045: }
046:
047: public void setSuffixOrder(String[] suffixOrder) {
048: sorter.setSuffixOrder(suffixOrder);
049: }
050:
051: public int compare(Object o1, Object o2) {
052: int comp = sorter.compare(o1, o2);
053:
054: return comp == 0 ? alphaCompare(o1, o2) : comp;
055: }
056:
057: private String convertURLToString(URL url) {
058: String path = url.getPath();
059: int nameEnd = path.length() - 1;
060: if (nameEnd <= 0) {
061: return "";
062: }
063:
064: // ignore a trailing '/'
065: if (path.charAt(nameEnd) == '/') {
066: nameEnd--;
067: }
068:
069: // find the previous URL separator: '/'
070: int nameStart = path.lastIndexOf('/', nameEnd) + 1;
071:
072: return path.substring(nameStart);
073:
074: }
075:
076: public int alphaCompare(Object o1, Object o2) {
077: String s1 = convertURLToString((URL) o1);
078: boolean s1IsDigit = Character.isDigit(s1.charAt(0));
079: String s2 = convertURLToString((URL) o2);
080: boolean s2IsDigit = Character.isDigit(s2.charAt(0));
081:
082: if (s1IsDigit && !s2IsDigit) {
083: return 1; // o1 is greater than o2, since has number and o2 does not
084: } else if (!s1IsDigit && s2IsDigit) {
085: return -1; //o1 is less than o2, since does not have number and o2 does
086: }
087: if (s1IsDigit && s2IsDigit) // numeric comapre
088: {
089: int num1 = getNumericPrefix(s1);
090: int num2 = getNumericPrefix(s2);
091: int diff = num1 - num2;
092: if (diff != 0) // do not begin with same number
093: {
094: return diff;
095: } else //numbers are the same, so have to compare rest of the string
096: {
097: String s1Suf = getAlphaSuffix(s1);
098: String s2Sef = getAlphaSuffix(s2);
099: return s1Suf.compareToIgnoreCase(s2Sef);
100: }
101: } else // alpha compare
102: {
103: return s1.compareToIgnoreCase(s2);
104: }
105: }
106:
107: private String getAlphaSuffix(String s1) {
108: int x = 0;
109: while (Character.isDigit(s1.charAt(x++)))
110: ;
111: return s1.substring(x);
112: }
113:
114: private int getNumericPrefix(String s1) {
115: int x = 0;
116: String numS1 = null;
117: while (Character.isDigit(s1.charAt(x++))) {
118: numS1 = s1.substring(0, x);
119: }
120: int number = Integer.parseInt(numS1);
121: return number;
122: }
123:
124: }
|