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.rules; 021 022 import com.thoughtworks.xstream.XStream; 023 import com.thoughtworks.xstream.core.util.QuickWriter; 024 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 025 import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; 026 import com.thoughtworks.xstream.io.xml.XppDriver; 027 import org.apache.commons.io.IOUtils; 028 import org.apache.commons.lang.CharEncoding; 029 import org.sonar.api.utils.SonarException; 030 031 import java.io.*; 032 import java.util.ArrayList; 033 import java.util.List; 034 035 public class StandardRulesXmlParser { 036 037 /** 038 * see the XML format into the unit test src/test/java/.../StandardRulesXmlParserTest 039 */ 040 public List<Rule> parse(String xml) { 041 InputStream inputStream = null; 042 try { 043 inputStream = IOUtils.toInputStream(xml, CharEncoding.UTF_8); 044 return setDefaultRulePriorities((List<Rule>) getXStream().fromXML(inputStream)); 045 046 } catch (IOException e) { 047 throw new SonarException("Can't parse xml file", e); 048 049 } finally { 050 IOUtils.closeQuietly(inputStream); 051 } 052 } 053 054 public List<Rule> parse(Reader reader) { 055 return setDefaultRulePriorities((List<Rule>) getXStream().fromXML(reader)); 056 } 057 058 public List<Rule> parse(InputStream input) { 059 try { 060 return setDefaultRulePriorities((List<Rule>) getXStream().fromXML(new InputStreamReader(input, CharEncoding.UTF_8))); 061 062 } catch (UnsupportedEncodingException e) { 063 throw new SonarException("Can't parse xml file", e); 064 } 065 } 066 067 private List<Rule> setDefaultRulePriorities(List<Rule> rules) { 068 for (Rule rule : rules) { 069 if (rule.getPriority() == null) { 070 rule.setPriority(RulePriority.MAJOR); 071 } 072 } 073 return rules; 074 } 075 076 public String toXml(List<Rule> rules) { 077 return getXStream().toXML(rules); 078 } 079 080 private static class CDataXppDriver extends XppDriver { 081 @Override 082 public HierarchicalStreamWriter createWriter(Writer out) { 083 return new XDataPrintWriter(out); 084 } 085 } 086 087 private static class XDataPrintWriter extends PrettyPrintWriter { 088 public XDataPrintWriter(Writer writer) { 089 super(writer); 090 } 091 092 @Override 093 protected void writeText(QuickWriter writer, String text) { 094 writer.write("<![CDATA["); 095 writer.write(text); 096 writer.write("]]>"); 097 } 098 } 099 100 private XStream getXStream() { 101 XStream xstream = new XStream(new CDataXppDriver()); 102 xstream.alias("rules", ArrayList.class); 103 104 xstream.alias("categ", RulesCategory.class); 105 xstream.useAttributeFor(RulesCategory.class, "name"); 106 xstream.aliasField("category", Rule.class, "rulesCategory"); 107 108 xstream.alias("rule", Rule.class); 109 xstream.useAttributeFor(Rule.class, "key"); 110 xstream.useAttributeFor("priority", RulePriority.class); 111 112 xstream.addImplicitCollection(Rule.class, "params"); 113 114 xstream.alias("param", RuleParam.class); 115 xstream.useAttributeFor(RuleParam.class, "key"); 116 xstream.useAttributeFor(RuleParam.class, "type"); 117 118 // only for backward compatibility with sonar 1.4. 119 xstream.omitField(RuleParam.class, "defaultValue"); 120 return xstream; 121 } 122 }