KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > extractor > ExtractorManager


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/extractor/ExtractorManager.java,v 1.4.2.2 2004/09/29 15:11:38 unico Exp $
3  * $Revision: 1.4.2.2 $
4  * $Date: 2004/09/29 15:11:38 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.extractor;
25
26 import org.apache.slide.util.conf.Configurable;
27 import org.apache.slide.util.conf.Configuration;
28 import org.apache.slide.util.conf.ConfigurationException;
29 import org.apache.slide.content.NodeRevisionDescriptors;
30 import org.apache.slide.content.NodeRevisionDescriptor;
31
32 import java.util.*;
33 import java.lang.reflect.Constructor JavaDoc;
34
35 /**
36  * The ExtractorManager class
37  */

38 public class ExtractorManager implements Configurable {
39     private final static ExtractorManager manager = new ExtractorManager();
40
41     private List extractors = new ArrayList();
42
43     private ExtractorManager() {}
44
45     public static ExtractorManager getInstance() {
46         return manager;
47     }
48
49     public void addExtractor(Extractor extractor) {
50         extractors.add(extractor);
51     }
52
53     public PropertyExtractor[] getPropertyExtractors(String JavaDoc namespace, NodeRevisionDescriptors descriptors, NodeRevisionDescriptor descriptor) {
54         List matchingExtractors = new ArrayList();
55         for ( Iterator i = extractors.iterator(); i.hasNext(); ) {
56             Extractor extractor = (Extractor)i.next();
57             if ( extractor instanceof PropertyExtractor && matches(extractor, namespace, descriptors, descriptor)) {
58                 matchingExtractors.add(extractor);
59             }
60         }
61         PropertyExtractor[] extractors = new PropertyExtractor[matchingExtractors.size()];
62         return (PropertyExtractor [])matchingExtractors.toArray(extractors);
63     };
64
65     public ContentExtractor[] getContentExtractors(String JavaDoc namespace, NodeRevisionDescriptors descriptors, NodeRevisionDescriptor descriptor) {
66         List matchingExtractors = new ArrayList();
67         for ( Iterator i = extractors.iterator(); i.hasNext(); ) {
68             Extractor extractor = (Extractor)i.next();
69             if ( extractor instanceof ContentExtractor && matches(extractor, namespace, descriptors, descriptor)) {
70                 matchingExtractors.add(extractor);
71             }
72         }
73         ContentExtractor[] extractors = new ContentExtractor[matchingExtractors.size()];
74         return (ContentExtractor [])matchingExtractors.toArray(extractors);
75     };
76
77     public Extractor[] getExtractors(String JavaDoc namespace, NodeRevisionDescriptors descriptors, NodeRevisionDescriptor descriptor) {
78         List matchingExtractors = new ArrayList();
79         for ( Iterator i = extractors.iterator(); i.hasNext(); ) {
80             Extractor extractor = (Extractor)i.next();
81             if ( matches(extractor, namespace, descriptors, descriptor)) {
82                 matchingExtractors.add(extractor);
83             }
84         }
85         Extractor[] extractors = new Extractor[matchingExtractors.size()];
86         return (Extractor [])matchingExtractors.toArray(extractors);
87     };
88
89     public boolean matches(Extractor extractor, String JavaDoc namespace, NodeRevisionDescriptors descriptors, NodeRevisionDescriptor descriptor) {
90         boolean matching = true;
91         if ( descriptor != null && extractor.getContentType() != null && !descriptor.getContentType().equals(extractor.getContentType()) ) {
92             matching = false;
93         }
94         if ( descriptors != null && extractor.getUri() != null && !descriptors.getUri().startsWith(extractor.getUri()) ) {
95             matching = false;
96         }
97         if ( descriptors != null && extractor.getNamespace() != null && !extractor.getNamespace().equals(namespace)) {
98             matching = false;
99         }
100         return matching;
101     }
102
103     public void configure(Configuration config) throws ConfigurationException {
104         Enumeration extractorConfigs = config.getConfigurations("extractor");
105         while (extractorConfigs.hasMoreElements()) {
106             Configuration extractorConfig = (Configuration)extractorConfigs.nextElement();
107             String JavaDoc classname = extractorConfig.getAttribute("classname");
108             String JavaDoc uri = extractorConfig.getAttribute("uri", null);
109             String JavaDoc contentType = extractorConfig.getAttribute("content-type", null);
110             String JavaDoc namespace = extractorConfig.getAttribute("namespace", null);
111             try {
112                 Class JavaDoc extractorClass = Class.forName(classname);
113                 Extractor extractor = null;
114                 try {
115                     Constructor JavaDoc extractorConstructor = extractorClass.getConstructor(new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class, String JavaDoc.class } );
116                     extractor = (Extractor) extractorConstructor.newInstance(new String JavaDoc[] { uri, contentType, namespace });
117                 }
118                 catch (NoSuchMethodException JavaDoc e) {
119                     Constructor JavaDoc extractorConstructor = extractorClass.getConstructor(new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class } );
120                     extractor = (Extractor) extractorConstructor.newInstance(new String JavaDoc[] { uri, contentType });
121                 }
122                 if ( extractor instanceof Configurable ) {
123                     ((Configurable)extractor).configure(extractorConfig.getConfiguration("configuration"));
124                 }
125                 addExtractor(extractor);
126             } catch (ClassCastException JavaDoc e) {
127                 throw new ConfigurationException("Extractor '"+classname+"' is not of type Extractor", config);
128             } catch (ConfigurationException e) {
129                 throw e;
130             } catch (Exception JavaDoc e) {
131                 throw new ConfigurationException("Extractor '"+classname+"' could not be loaded", config);
132             }
133         }
134     }
135 }
Popular Tags