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: */package org.apache.cocoon.components.search.utils;
17:
18: /**
19: * Utility class
20: *
21: * @author Maisonneuve Nicolas
22: *
23: */
24: import java.io.IOException;
25: import java.util.HashMap;
26:
27: import org.apache.avalon.framework.configuration.Configuration;
28: import org.apache.avalon.framework.configuration.ConfigurationException;
29: import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
30: import org.apache.excalibur.source.Source;
31: import org.apache.excalibur.source.SourceValidity;
32: import org.xml.sax.SAXException;
33:
34: public class SourceHelper {
35:
36: static final private HashMap sources = new HashMap();
37:
38: static final private DefaultConfigurationBuilder confBuilder = new DefaultConfigurationBuilder();
39:
40: static final public void registerSource(Source source) {
41: if (!sources.containsKey(source)) {
42: SourceValidity refValidity = source.getValidity();
43: sources.put(source, refValidity);
44: }
45: }
46:
47: /**
48: * Check the validity of the source with the registered source
49: *
50: * @return true if the source didn't changed
51: */
52: static final public boolean checkSourceValidity(Source source) {
53: SourceValidity newValidity = source.getValidity();
54: SourceValidity refValidity = (SourceValidity) sources
55: .get(source);
56: return checkSourceValidity(newValidity, refValidity);
57: }
58:
59: /**
60: * Compare two sources
61: *
62: * @return true if the source didn't changed
63: */
64: static final public boolean checkSourceValidity(
65: SourceValidity s1Validity, SourceValidity s2Validity) {
66:
67: int valid = s2Validity.isValid();
68: boolean isValid;
69: if (valid == 0) {
70: valid = s2Validity.isValid(s1Validity);
71: isValid = (valid == 1);
72: } else {
73: isValid = (valid == 1);
74: }
75: return isValid;
76: }
77:
78: static final public Configuration build(Source source)
79: throws ConfigurationException {
80: try {
81: return confBuilder.build(source.getInputStream());
82: } catch (IOException ex) {
83: throw new ConfigurationException("File " + source.getURI(),
84: ex);
85: } catch (SAXException ex) {
86: throw new ConfigurationException(
87: "SAX Error in the configuration File", ex);
88: }
89: }
90:
91: }
|