001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.parser;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.net.URL;
024: import java.text.ParseException;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027: import java.util.List;
028:
029: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
030: import org.apache.ivy.plugins.parser.m2.PomModuleDescriptorParser;
031: import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParser;
032: import org.apache.ivy.plugins.repository.Resource;
033: import org.apache.ivy.util.Message;
034:
035: public final class ModuleDescriptorParserRegistry extends
036: AbstractModuleDescriptorParser {
037: private static final ModuleDescriptorParserRegistry INSTANCE = new ModuleDescriptorParserRegistry();
038:
039: public static ModuleDescriptorParserRegistry getInstance() {
040: return INSTANCE;
041: }
042:
043: private List parsers = new LinkedList();
044:
045: private ModuleDescriptorParserRegistry() {
046: parsers.add(PomModuleDescriptorParser.getInstance());
047: parsers.add(XmlModuleDescriptorParser.getInstance());
048: }
049:
050: /**
051: * Adds a the given parser to this registry.
052: *
053: * @param parser
054: * the parser to add
055: */
056: public void addParser(ModuleDescriptorParser parser) {
057: /*
058: * The parser is added in the front of the list of parsers. This is necessary because the
059: * XmlModuleDescriptorParser accepts all resources!
060: */
061: parsers.add(0, parser);
062: }
063:
064: public ModuleDescriptorParser[] getParsers() {
065: return (ModuleDescriptorParser[]) parsers
066: .toArray(new ModuleDescriptorParser[parsers.size()]);
067: }
068:
069: public ModuleDescriptorParser getParser(Resource res) {
070: for (Iterator iter = parsers.iterator(); iter.hasNext();) {
071: ModuleDescriptorParser parser = (ModuleDescriptorParser) iter
072: .next();
073: if (parser.accept(res)) {
074: return parser;
075: }
076: }
077: return null;
078: }
079:
080: public ModuleDescriptor parseDescriptor(ParserSettings settings,
081: URL descriptorURL, Resource res, boolean validate)
082: throws ParseException, IOException {
083: ModuleDescriptorParser parser = getParser(res);
084: if (parser == null) {
085: Message
086: .warn("no module descriptor parser found for "
087: + res);
088: return null;
089: }
090: return parser.parseDescriptor(settings, descriptorURL, res,
091: validate);
092: }
093:
094: public boolean accept(Resource res) {
095: return getParser(res) != null;
096: }
097:
098: public void toIvyFile(InputStream is, Resource res, File destFile,
099: ModuleDescriptor md) throws ParseException, IOException {
100: ModuleDescriptorParser parser = getParser(res);
101: if (parser == null) {
102: Message
103: .warn("no module descriptor parser found for "
104: + res);
105: } else {
106: parser.toIvyFile(is, res, destFile, md);
107: }
108: }
109:
110: }
|