01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.lenya.cms.publication.templating;
20:
21: import org.apache.excalibur.source.Source;
22: import org.apache.excalibur.source.SourceResolver;
23:
24: /**
25: * Source visitor to obtain the first existing source.
26: *
27: * @version $Id: ExistingSourceResolver.java 179568 2005-06-02 09:27:26Z jwkaltz $
28: */
29: public class ExistingAncestorSourceResolver implements
30: VisitingSourceResolver {
31:
32: private Source source;
33:
34: /**
35: * Ctor.
36: */
37: public ExistingAncestorSourceResolver() {
38: super ();
39: }
40:
41: /**
42: * @return the ancestor of the first existing source.
43: */
44: public Source getSource() {
45: return this .source;
46: }
47:
48: private int matches = 0;
49:
50: public void visit(SourceResolver resolver, String sourceUri) {
51: Source source = null;
52: try {
53: source = resolver.resolveURI(sourceUri);
54: if (source.exists()) {
55: matches++;
56: if (matches == 2) {
57: this .source = source;
58: }
59: }
60: } catch (Exception e) {
61: throw new RuntimeException(e);
62: } finally {
63: if (source != null) {
64: resolver.release(source);
65: }
66: }
67: }
68:
69: }
|