KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > plugin > PluginManifestParser


1 /*
2  * Copyright (c) 2003 The Nutch Organization. All rights reserved. Use subject
3  * to the conditions in http://www.nutch.org/LICENSE.txt.
4  */

5 package net.nutch.plugin;
6 import java.io.File JavaDoc;
7 import java.io.UnsupportedEncodingException JavaDoc;
8 import java.net.MalformedURLException JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.net.URLDecoder JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.logging.Logger JavaDoc;
14 import net.nutch.util.NutchConf;
15 import org.dom4j.Attribute;
16 import org.dom4j.Document;
17 import org.dom4j.DocumentException;
18 import org.dom4j.Element;
19 import org.dom4j.io.SAXReader;
20 import java.util.regex.Pattern JavaDoc;
21
22 /**
23  * The <code>PluginManifestParser</code> parser just parse the manifest file in all plugin
24  * directories.
25  *
26  * @author joa23
27  */

28 public class PluginManifestParser {
29   public static final Logger JavaDoc LOG = PluginRepository.LOG;
30
31   private static final boolean WINDOWS
32     = System.getProperty("os.name").startsWith("Windows");
33
34   /**
35    * Returns a list with plugin descriptors.
36    *
37    * @return ArrayList
38    * @throws IOException
39    * @throws MalformedURLException
40    * @throws DocumentException
41    */

42   public static ArrayList JavaDoc parsePluginFolder() {
43     ArrayList JavaDoc list = new ArrayList JavaDoc();
44     String JavaDoc[] pluginFolders = NutchConf.getStrings("plugin.folders");
45     Pattern JavaDoc excludes = Pattern.compile(NutchConf.get("plugin.excludes", ""));
46     Pattern JavaDoc includes = Pattern.compile(NutchConf.get("plugin.includes", ""));
47     if (pluginFolders == null)
48       throw new IllegalArgumentException JavaDoc("plugin.folders is not defined");
49     for (int i = 0; i < pluginFolders.length; i++) {
50       String JavaDoc name = pluginFolders[i];
51       File JavaDoc directory = getPluginFolder(name);
52       if (directory == null)
53         continue;
54       LOG.info("Plugins: looking in: "+directory);
55       File JavaDoc[] files = directory.listFiles();
56       if (files == null)
57         continue;
58       for (int j = 0; j < files.length; j++) {
59         File JavaDoc oneSubFolder = files[j];
60         if (oneSubFolder.isDirectory()) {
61           
62           if (!includes.matcher(oneSubFolder.getName()).matches()) {
63             LOG.info("not including: "+oneSubFolder);
64             continue;
65           }
66
67           if (excludes.matcher(oneSubFolder.getName()).matches()) {
68             LOG.info("excluding: "+oneSubFolder);
69             continue;
70           }
71
72           String JavaDoc manifestPath = oneSubFolder.getAbsolutePath()
73             + File.separator + "plugin.xml";
74           try {
75             LOG.info("parsing: "+manifestPath);
76             list.add(parseManifestFile(manifestPath));
77           } catch (MalformedURLException JavaDoc e) {
78             LOG.info(e.toString());
79           } catch (DocumentException e) {
80             LOG.info(e.toString());
81           }
82         }
83       }
84     }
85     return list;
86   }
87
88   /** Return the named plugin folder. If the name is absolute then it is
89    * returned. Otherwise, for relative names, the classpath is scanned.*/

90   static File JavaDoc getPluginFolder(String JavaDoc name) {
91     File JavaDoc directory = new File JavaDoc(name);
92     if (!directory.isAbsolute()) {
93       URL JavaDoc url =PluginManifestParser.class.getClassLoader().getResource(name);
94       if (url == null) {
95         LOG.info("Plugins: directory not found: "+name);
96         return null;
97       } else if (!"file".equals(url.getProtocol())) {
98         LOG.info("Plugins: not a file: url. Can't load plugins from: "+url);
99         return null;
100       }
101       String JavaDoc path = url.getPath();
102       if (WINDOWS && path.startsWith("/")) // patch a windows bug
103
path = path.substring(1);
104       try {
105         path = URLDecoder.decode(path, "UTF-8"); // decode the url path
106
} catch (UnsupportedEncodingException JavaDoc e) {}
107       directory = new File JavaDoc(path);
108     }
109     return directory;
110   }
111
112   /**
113    * @param manifestPath
114    */

115   private static PluginDescriptor parseManifestFile(String JavaDoc pManifestPath)
116     throws MalformedURLException JavaDoc, DocumentException {
117     Document document = parseXML(new File JavaDoc(pManifestPath).toURL());
118     String JavaDoc pPath = new File JavaDoc(pManifestPath).getParent();
119     return parsePlugin(document, pPath);
120   }
121   /**
122    * @param url
123    * @return Document
124    * @throws DocumentException
125    */

126   private static Document parseXML(URL JavaDoc url) throws DocumentException {
127     SAXReader reader = new SAXReader();
128     Document document = reader.read(url);
129     return document;
130   }
131   /**
132    * @param document
133    */

134   private static PluginDescriptor parsePlugin(Document pDocument, String JavaDoc pPath)
135     throws MalformedURLException JavaDoc {
136     Element rootElement = pDocument.getRootElement();
137     String JavaDoc id = rootElement.attributeValue("id");
138     String JavaDoc name = rootElement.attributeValue("name");
139     String JavaDoc version = rootElement.attributeValue("version");
140     String JavaDoc providerName = rootElement.attributeValue("provider-name");
141     String JavaDoc pluginClazz = rootElement.attributeValue("class");
142     PluginDescriptor pluginDescriptor = new PluginDescriptor(id, version,
143                                                              name, providerName, pluginClazz, pPath);
144 // LOG.fine("plugin: id="+id+" name="+name+" version="+version
145
// +" provider="+providerName+"class="+pluginClazz);
146
parseExtension(rootElement, pluginDescriptor);
147     parseExtensionPoints(rootElement, pluginDescriptor);
148     parseLibraries(rootElement, pluginDescriptor);
149     return pluginDescriptor;
150   }
151   /**
152    * @param rootElement
153    * @param pluginDescriptor
154    */

155   private static void parseLibraries(Element pRootElement,
156                                      PluginDescriptor pDescriptor) throws MalformedURLException JavaDoc {
157     Element runtime = pRootElement.element("runtime");
158     if (runtime == null)
159       return;
160     List JavaDoc libraries = runtime.elements("library");
161     for (int i = 0; i < libraries.size(); i++) {
162       Element library = (Element) libraries.get(i);
163       String JavaDoc libName = library.attributeValue("name");
164       Element exportElement = library.element("extport");
165       if (exportElement != null)
166         pDescriptor.addExportedLibRelative(libName);
167       else
168         pDescriptor.addNotExportedLibRelative(libName);
169     }
170   }
171   /**
172    * @param rootElement
173    * @param pluginDescriptor
174    */

175   private static void parseExtensionPoints(Element pRootElement,
176                                            PluginDescriptor pPluginDescriptor) {
177     List JavaDoc list = pRootElement.elements("extension-point");
178     if (list != null) {
179       for (int i = 0; i < list.size(); i++) {
180         Element oneExtensionPoint = (Element) list.get(i);
181         String JavaDoc id = oneExtensionPoint.attributeValue("id");
182         String JavaDoc name = oneExtensionPoint.attributeValue("name");
183         String JavaDoc schema = oneExtensionPoint.attributeValue("schema");
184         ExtensionPoint extensionPoint = new ExtensionPoint(id, name,
185                                                            schema);
186         //LOG.fine("plugin: point="+id);
187
pPluginDescriptor.addExtensionPoint(extensionPoint);
188       }
189     }
190   }
191   /**
192    * @param rootElement
193    * @param pluginDescriptor
194    */

195   private static void parseExtension(Element pRootElement,
196                                      PluginDescriptor pPluginDescriptor) {
197     List JavaDoc extensions = pRootElement.elements("extension");
198     if (extensions != null) {
199       for (int i = 0; i < extensions.size(); i++) {
200         Element oneExtension = (Element) extensions.get(i);
201         String JavaDoc pointId = oneExtension.attributeValue("point");
202         List JavaDoc extensionImplementations = oneExtension.elements();
203         if (extensionImplementations != null) {
204           for (int j = 0; j < extensionImplementations.size(); j++) {
205             Element oneImplementation = (Element) extensionImplementations
206               .get(j);
207             String JavaDoc id = oneImplementation.attributeValue("id");
208             String JavaDoc extensionClass = oneImplementation.attributeValue("class");
209             LOG.fine("impl: point="+pointId+" class="+extensionClass);
210             Extension extension = new Extension(pPluginDescriptor,
211                                                 pointId, id, extensionClass);
212             List JavaDoc list = oneImplementation.attributes();
213             for (int k = 0; k < list.size(); k++) {
214               Attribute attribute = (Attribute) list.get(k);
215               String JavaDoc name = attribute.getName();
216               if (name.equals("id") || name.equals("class"))
217                 continue;
218               String JavaDoc value = attribute.getValue();
219               extension.addAttribute(name, value);
220             }
221             pPluginDescriptor.addExtension(extension);
222           }
223         }
224       }
225     }
226   }
227 }
228
Popular Tags