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: /*
018: * Created on Aug 31, 2004
019: *
020: * TODO To change the template for this generated file go to
021: * Window - Preferences - Java - Code Generation - Code and Comments
022: */
023: package org.apache.jetspeed.page.document.psml;
024:
025: import java.util.Comparator;
026: import java.util.List;
027:
028: /**
029: * <p>
030: * DocumentOrderCompartaor
031: * </p>
032: * <p>
033: *
034: * </p>
035: *
036: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
037: * @version $Id: NodeOrderCompartaor.java 516881 2007-03-11 10:34:21Z ate $
038: *
039: */
040: public class NodeOrderCompartaor implements Comparator {
041: private List nodeIndex;
042: private String relativePath = "";
043:
044: /**
045: *
046: */
047: public NodeOrderCompartaor(List nodeIndex, String relativePath) {
048: super ();
049: this .nodeIndex = nodeIndex;
050: this .relativePath = relativePath;
051: }
052:
053: /**
054: * <p>
055: * compare
056: * </p>
057: *
058: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
059: * @param o1
060: * @param o2
061: * @return
062: */
063: public int compare(Object o1, Object o2) {
064:
065: String node1 = null;
066: String node2 = null;
067:
068: if (relativePath.length() < o1.toString().length()) {
069: node1 = o1.toString().substring(relativePath.length());
070: } else {
071: node1 = o1.toString();
072: }
073:
074: if (relativePath.length() < o2.toString().length()) {
075: node2 = o2.toString().substring(relativePath.length());
076: } else {
077: node2 = o2.toString();
078: }
079:
080: String c1 = null;
081: String c2 = null;
082:
083: if (nodeIndex != null) {
084: int index1 = nodeIndex.indexOf(node1);
085: int index2 = nodeIndex.indexOf(node2);
086:
087: if (index1 > -1) {
088: c1 = String.valueOf(index1);
089: } else {
090: c1 = node1;
091: }
092:
093: if (index2 > -1) {
094: c2 = String.valueOf(index2);
095: } else {
096: c2 = node2;
097: }
098: } else {
099: c1 = node1;
100: c2 = node2;
101: }
102:
103: return c1.compareTo(c2);
104:
105: }
106:
107: }
|