/* * Copyright 2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.crawler.kiss; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.wamblee.conditions.Condition; import org.wamblee.conditions.OrCondition; /** * Parse the configuration of desired programs. */ public class ProgramConfigurationParser { private static final String ELEM_PROGRAM = "program"; private static final String ELEM_PATTERN = "name"; /** * Parses the condition used to match the desired programs. * * @param aStream * Input stream to parse from. * @return Condition. */ Condition parse(InputStream aStream) { try { SAXReader reader = new SAXReader(); Document document = reader.read(aStream); Element root = document.getRootElement(); List> conditions = new ArrayList>(); for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) { Element program = (Element) i.next(); String pattern = ".*" + program.element(ELEM_PATTERN).getText() + ".*"; conditions.add(new ProgramNameMatcher(pattern)); } return new OrCondition(conditions); } catch (DocumentException e) { throw new RuntimeException("Error parsing program configuraiton", e); } } }