| 1 7 package fr.jayasoft.ivy.parser; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.net.URL ; 13 import java.text.ParseException ; 14 import java.util.Iterator ; 15 import java.util.LinkedList ; 16 import java.util.List ; 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 _parsers = new LinkedList (); 33 private ModuleDescriptorParserRegistry() { 34 _parsers.add(PomModuleDescriptorParser.getInstance()); 35 _parsers.add(XmlModuleDescriptorParser.getInstance()); 36 } 37 38 43 public void addParser(ModuleDescriptorParser parser) { 44 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 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 descriptorURL, Resource res, boolean validate) throws ParseException , IOException { 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 is, Resource res, File destFile, ModuleDescriptor md) throws ParseException , IOException { 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 |