001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.checks.templates;
021    
022    import org.apache.commons.io.IOUtils;
023    import org.apache.commons.lang.CharEncoding;
024    import org.sonar.api.rules.Rule;
025    import org.sonar.api.rules.RuleParam;
026    import org.sonar.api.rules.StandardRulesXmlParser;
027    import org.sonar.api.utils.SonarException;
028    
029    import java.io.IOException;
030    import java.io.InputStream;
031    import java.io.Reader;
032    import java.util.ArrayList;
033    import java.util.List;
034    
035    /**
036     * EXPERIMENTAL - will be used in version 2.2
037     * @since 2.1
038     */
039    public class XmlCheckTemplateFactory {
040    
041      public List<CheckTemplate> parseXml(String xml) {
042        InputStream input = null;
043        try {
044          input = IOUtils.toInputStream(xml, CharEncoding.UTF_8);
045          return parse(input);
046    
047        } catch (IOException e) {
048          throw new SonarException("Can't parse xml file", e);
049    
050        } finally {
051          IOUtils.closeQuietly(input);
052        }
053      }
054    
055      public List<CheckTemplate> parse(Reader reader) {
056        StandardRulesXmlParser parser = new StandardRulesXmlParser();
057        List<Rule> rules = parser.parse(reader);
058        return toCheckTemplates(rules);
059    
060      }
061    
062      public List<CheckTemplate> parse(InputStream input) {
063        StandardRulesXmlParser parser = new StandardRulesXmlParser();
064        List<Rule> rules = parser.parse(input);
065        return toCheckTemplates(rules);
066    
067      }
068    
069      private List<CheckTemplate> toCheckTemplates(List<Rule> rules) {
070        List<CheckTemplate> templates = new ArrayList<CheckTemplate>();
071        if (rules != null) {
072          for (Rule rule : rules) {
073            DefaultCheckTemplate template = new DefaultCheckTemplate(rule.getKey());
074            templates.add(template);
075    
076            template.setConfigKey(rule.getConfigKey());
077            template.setDescription(rule.getDescription());
078            template.setIsoCategory(rule.getRulesCategory().toIsoCategory());
079            template.setPriority(rule.getPriority().toCheckPriority());
080            template.setTitle(rule.getName());
081    
082            if (rule.getParams() != null) {
083              for (RuleParam param : rule.getParams()) {
084                template.addProperty(toProperty(param));
085              }
086            }
087          }
088        }
089        return templates;
090      }
091    
092      private CheckTemplateProperty toProperty(RuleParam param) {
093        DefaultCheckTemplateProperty property = new DefaultCheckTemplateProperty();
094        property.setKey(param.getKey());
095        property.setTitle(param.getKey());
096        property.setDescription(param.getDescription());
097        return property;
098      }
099    
100    }