import com.thoughtworks.xstream.io.xml.XppDriver;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.CharEncoding;
+import org.apache.commons.lang.StringUtils;
import org.sonar.api.database.DatabaseSession;
import org.sonar.jpa.entity.SchemaMigration;
@Override
protected void writeText(QuickWriter writer, String text) {
writer.write("<![CDATA[");
+ /*
+ * See http://jira.codehaus.org/browse/SONAR-1605
+ * According to XML specification ( http://www.w3.org/TR/REC-xml/#sec-cdata-sect )
+ * CData section may contain everything except of sequence ']]>'
+ * so we will split all occurrences of this sequence into two CDATA
+ * first one would contain ']]' and second '>'
+ */
+ text = StringUtils.replace(text, "]]>", "]]]]><![CDATA[>");
writer.write(text);
writer.write("]]>");
}
};
}
- }
- );
+ });
xStream.processAnnotations(SonarConfig.class);
xStream.addDefaultImplementation(ArrayList.class, Collection.class);
- xStream.registerConverter(new DateConverter(DATE_FORMAT, new String[]{}));
+ xStream.registerConverter(new DateConverter(DATE_FORMAT, new String[] {}));
for (Backupable backupable : backupables) {
backupable.configure(xStream);
assertThat(metric.getId(), nullValue());
}
+ @Test
+ public void shouldExportAndImportInnerCDATA() throws Exception {
+ SonarConfig sonarConfig = getSonarConfig();
+ sonarConfig.setProperties(getPropertiesWithCDATA());
+
+ Backup backup = new Backup(Arrays.asList(new MetricsBackup(null), new PropertiesBackup(null)));
+ String xml = backup.getXmlFromSonarConfig(sonarConfig);
+ assertXmlAreSimilar(xml, "backup-with-splitted-cdata.xml");
+
+ sonarConfig = backup.getSonarConfigFromXml(xml);
+ assertTrue(CollectionUtils.isEqualCollection(sonarConfig.getProperties(), getPropertiesWithCDATA()));
+ }
+
private SonarConfig getSonarConfig() throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(Backup.DATE_FORMAT);
Date date = dateFormat.parse("2008-11-18");
return properties;
}
+ private List<Property> getPropertiesWithCDATA() {
+ List<Property> properties = new ArrayList<Property>();
+ properties.add(new Property("key1", "<![CDATA[value1]]>"));
+ properties.add(new Property("key2", "]]>value2"));
+ properties.add(new Property("key3", "prefix]]>value3"));
+ properties.add(new Property("key4", "<name><![CDATA[Forges]]></name>"));
+ return properties;
+ }
+
private List<Property> getPropertiesWithXmlIlliciteCharacters() {
List<Property> properties = new ArrayList<Property>();
properties.add(new Property("key", "<value>"));
--- /dev/null
+<sonar-config>
+ <version><![CDATA[54]]></version>
+ <date><![CDATA[2008-11-18]]></date>
+ <properties>
+ <property>
+ <key><![CDATA[key1]]></key>
+ <value><![CDATA[<![CDATA[value1]]]]><![CDATA[>]]></value>
+ </property>
+ <property>
+ <key><![CDATA[key2]]></key>
+ <value><![CDATA[]]]]><![CDATA[>value2]]></value>
+ </property>
+ <property>
+ <key><![CDATA[key3]]></key>
+ <value><![CDATA[prefix]]]]><![CDATA[>value3]]></value>
+ </property>
+ <property>
+ <key><![CDATA[key4]]></key>
+ <value><![CDATA[<name><![CDATA[Forges]]]]><![CDATA[></name>]]></value>
+ </property>
+ </properties>
+</sonar-config>