1 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 ; 34 35 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 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 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 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 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 classname = extractorConfig.getAttribute("classname"); 108 String uri = extractorConfig.getAttribute("uri", null); 109 String contentType = extractorConfig.getAttribute("content-type", null); 110 String namespace = extractorConfig.getAttribute("namespace", null); 111 try { 112 Class extractorClass = Class.forName(classname); 113 Extractor extractor = null; 114 try { 115 Constructor extractorConstructor = extractorClass.getConstructor(new Class [] { String .class, String .class, String .class } ); 116 extractor = (Extractor) extractorConstructor.newInstance(new String [] { uri, contentType, namespace }); 117 } 118 catch (NoSuchMethodException e) { 119 Constructor extractorConstructor = extractorClass.getConstructor(new Class [] { String .class, String .class } ); 120 extractor = (Extractor) extractorConstructor.newInstance(new String [] { uri, contentType }); 121 } 122 if ( extractor instanceof Configurable ) { 123 ((Configurable)extractor).configure(extractorConfig.getConfiguration("configuration")); 124 } 125 addExtractor(extractor); 126 } catch (ClassCastException e) { 127 throw new ConfigurationException("Extractor '"+classname+"' is not of type Extractor", config); 128 } catch (ConfigurationException e) { 129 throw e; 130 } catch (Exception e) { 131 throw new ConfigurationException("Extractor '"+classname+"' could not be loaded", config); 132 } 133 } 134 } 135 } | Popular Tags |