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