001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.catalog.util;
018:
019: import java.io.IOException;
020: import java.net.URL;
021: import java.util.LinkedList;
022: import java.util.List;
023:
024: import net.refractions.udig.catalog.IResolve;
025: import net.refractions.udig.catalog.IResolveChangeEvent;
026: import net.refractions.udig.catalog.IResolveDelta;
027: import net.refractions.udig.catalog.IResolveDeltaVisitor;
028: import net.refractions.udig.catalog.URLUtils;
029: import net.refractions.udig.catalog.IResolveDelta.Kind;
030:
031: /**
032: * Easy of use visitor for search IResolveDeltas.
033: * <p>
034: * After a run found == Delta that best matches the provided handle.
035: * <p>
036: * Will be null if no matches were found that were interesting. Where: interesting != NO_CHANGE
037: * </p>
038: * <p>
039: * You can use this code as an example of a good IResolveDeltaVisitor.
040: * </p>
041: *
042: * @author jgarnett
043: * @since 0.6.0
044: */
045: public class SearchIDDeltaVisitor implements IResolveDeltaVisitor {
046: private IResolveDelta found;
047: private URL id;
048:
049: public SearchIDDeltaVisitor(URL id) {
050: this .id = id;
051: found = null;
052: }
053:
054: /** Find available parents if provided handle */
055: static List<URL> path(IResolve handle) {
056: IResolve handle2 = handle;
057: LinkedList<URL> path = new LinkedList<URL>();
058: while (handle2 != null) {
059: path.addFirst(handle2.getIdentifier());
060: try {
061: handle2 = handle2.parent(null);
062: } catch (IOException e) {
063: handle2 = null; // no more parents
064: }
065: }
066: return path;
067: }
068:
069: /**
070: * Best match IResolveDelta for handle, may be null if search came up empty.
071: *
072: * @return Best match IResolveDelta for handle
073: */
074: public IResolveDelta getFound() {
075: return found;
076: }
077:
078: public boolean visit(IResolveDelta delta) {
079: if (delta.getKind() == Kind.NO_CHANGE) {
080: return true; // no match here visit children
081: }
082: URL here = delta.getResolve().getIdentifier();
083: if (here == null)
084: return true; // visit children
085:
086: if (URLUtils.urlEquals(here, id, false)
087: || here.toExternalForm()
088: .startsWith(id.toExternalForm())) {
089: found = delta;
090: return false; // found - it we don't need to find children
091: }
092: return true;
093: }
094:
095: /**
096: * Quick method that uses this visitor to search an event.
097: * <p>
098: * This serves as a good example of using a visitor.
099: * </p>
100: */
101: public static IResolveDelta search(URL id, IResolveChangeEvent event) {
102: if (id == null || event == null)
103: return null;
104:
105: IResolveDelta delta = event.getDelta();
106: if (delta == null)
107: return null;
108:
109: SearchIDDeltaVisitor visitor = new SearchIDDeltaVisitor(id);
110: try {
111: delta.accept(visitor);
112: return visitor.getFound();
113: } catch (IOException e) {
114: return null; // visitor obviously could not find anything
115: }
116: }
117: }
|