001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 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 * 038 * @since 2.1 039 */ 040 public class XmlCheckTemplateFactory { 041 042 public List<CheckTemplate> parseXml(String xml) { 043 InputStream input = null; 044 try { 045 input = IOUtils.toInputStream(xml, CharEncoding.UTF_8); 046 return parse(input); 047 048 } catch (IOException e) { 049 throw new SonarException("Can't parse xml file", e); 050 051 } finally { 052 IOUtils.closeQuietly(input); 053 } 054 } 055 056 public List<CheckTemplate> parse(Reader reader) { 057 StandardRulesXmlParser parser = new StandardRulesXmlParser(); 058 List<Rule> rules = parser.parse(reader); 059 return toCheckTemplates(rules); 060 061 } 062 063 public List<CheckTemplate> parse(InputStream input) { 064 StandardRulesXmlParser parser = new StandardRulesXmlParser(); 065 List<Rule> rules = parser.parse(input); 066 return toCheckTemplates(rules); 067 068 } 069 070 private List<CheckTemplate> toCheckTemplates(List<Rule> rules) { 071 List<CheckTemplate> templates = new ArrayList<CheckTemplate>(); 072 if (rules != null) { 073 for (Rule rule : rules) { 074 DefaultCheckTemplate template = new DefaultCheckTemplate(rule.getKey()); 075 templates.add(template); 076 077 template.setConfigKey(rule.getConfigKey()); 078 template.setDescription(rule.getDescription()); 079 template.setPriority(rule.getSeverity().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 }