KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > parser > ModuleDescriptorParserRegistry


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy.parser;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.text.ParseException JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.LinkedList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import fr.jayasoft.ivy.Ivy;
19 import fr.jayasoft.ivy.ModuleDescriptor;
20 import fr.jayasoft.ivy.external.m2.PomModuleDescriptorParser;
21 import fr.jayasoft.ivy.repository.Resource;
22 import fr.jayasoft.ivy.util.Message;
23 import fr.jayasoft.ivy.xml.XmlModuleDescriptorParser;
24
25 public class ModuleDescriptorParserRegistry extends AbstractModuleDescriptorParser {
26     private static ModuleDescriptorParserRegistry INSTANCE = new ModuleDescriptorParserRegistry();
27     
28     public static ModuleDescriptorParserRegistry getInstance() {
29         return INSTANCE;
30     }
31
32     private List JavaDoc _parsers = new LinkedList JavaDoc();
33     private ModuleDescriptorParserRegistry() {
34         _parsers.add(PomModuleDescriptorParser.getInstance());
35         _parsers.add(XmlModuleDescriptorParser.getInstance());
36     }
37     
38     /**
39      * Adds a the given parser to this registry.
40      *
41      * @param parser the parser to add
42      */

43     public void addParser(ModuleDescriptorParser parser) {
44         /*
45          * The parser is added in the front of the list of parsers. This is necessary because
46          * the XmlModuleDescriptorParser accepts all resources!
47          */

48         _parsers.add(0, parser);
49     }
50     
51     public ModuleDescriptorParser[] getParsers() {
52         return (ModuleDescriptorParser[])_parsers.toArray(new ModuleDescriptorParser[_parsers.size()]);
53     }
54     
55     public ModuleDescriptorParser getParser(Resource res) {
56         for (Iterator JavaDoc iter = _parsers.iterator(); iter.hasNext();) {
57             ModuleDescriptorParser parser = (ModuleDescriptorParser)iter.next();
58             if (parser.accept(res)) {
59                 return parser;
60             }
61         }
62         return null;
63     }
64     
65     public ModuleDescriptor parseDescriptor(Ivy ivy, URL JavaDoc descriptorURL, Resource res, boolean validate) throws ParseException JavaDoc, IOException JavaDoc {
66         ModuleDescriptorParser parser = getParser(res);
67         if (parser == null) {
68             Message.warn("no module descriptor parser found for "+res);
69             return null;
70         }
71         return parser.parseDescriptor(ivy, descriptorURL, res, validate);
72     }
73
74     public boolean accept(Resource res) {
75         return getParser(res) != null;
76     }
77
78     public void toIvyFile(InputStream JavaDoc is, Resource res, File JavaDoc destFile, ModuleDescriptor md) throws ParseException JavaDoc, IOException JavaDoc {
79         ModuleDescriptorParser parser = getParser(res);
80         if (parser == null) {
81             Message.warn("no module descriptor parser found for "+res);
82         } else {
83             parser.toIvyFile(is, res, destFile, md);
84         }
85     }
86
87 }
88
Popular Tags