1 2 3 4 package net.nutch.parse; 5 6 import java.util.Hashtable ; 7 8 import net.nutch.plugin.*; 9 10 import java.util.logging.Logger ; 11 import net.nutch.util.LogFormatter; 12 13 14 public class ParserFactory { 15 16 public static final Logger LOG = LogFormatter 17 .getLogger(ParserFactory.class.getName()); 18 19 private static final ExtensionPoint X_POINT = PluginRepository.getInstance() 20 .getExtensionPoint(Parser.X_POINT_ID); 21 22 static { 23 if (X_POINT == null) { 24 throw new RuntimeException ("x point "+Parser.X_POINT_ID+" not found."); 25 } 26 } 27 28 private static final Hashtable CACHE = new Hashtable (); 29 30 private ParserFactory() {} 32 42 public static Parser getParser(String contentType, String url) 43 throws ParserNotFound { 44 45 try { 46 Extension extension = getExtension(contentType, getSuffix(url)); 47 if (extension == null) 48 throw new ParserNotFound(url, contentType); 49 50 return (Parser)extension.getExtensionInstance(); 51 52 } catch (PluginRuntimeException e) { 53 throw new ParserNotFound(url, contentType, e.toString()); 54 } 55 } 56 57 private static String getSuffix(String url) { 58 int i = url.lastIndexOf('.'); 59 int j = url.lastIndexOf('/'); 60 if (i == -1 || i == url.length()-1 || i < j) 61 return null; 62 return url.substring(i+1); 63 } 64 65 66 private static Extension getExtension(String contentType, String suffix) 67 throws PluginRuntimeException { 68 69 71 String key = contentType + "+" + suffix; 72 73 if (CACHE.containsKey(key)) 74 return (Extension)CACHE.get(key); 75 76 Extension extension = findExtension(contentType, suffix); 77 78 CACHE.put(key, extension); 79 80 return extension; 81 } 82 83 private static Extension findExtension(String contentType, String suffix) 84 throws PluginRuntimeException{ 85 86 88 Extension[] extensions = X_POINT.getExtentens(); 89 90 if (contentType != null) { 92 for (int i = 0; i < extensions.length; i++) { 93 Extension extension = extensions[i]; 94 if (contentType.startsWith(extension.getAttribute("contentType"))) 95 return extension; } 97 } 98 99 if (suffix != null) { 101 for (int i = 0; i < extensions.length; i++) { 102 Extension extension = extensions[i]; 103 if (suffix.equals(extension.getAttribute("pathSuffix"))) 104 return extension; } 106 } 107 108 for (int i = 0; i < extensions.length; i++) { 110 Extension extension = extensions[i]; 111 if ("".equals(extension.getAttribute("pathSuffix"))) return extension; 113 } 114 115 return null; 116 } 117 } 118 | Popular Tags |