Skip to content

Commit

Permalink
Fix quality flaws
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brandhof committed Jan 6, 2015
1 parent 6387879 commit dacd73d
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 31 deletions.
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.process.monitor;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -49,7 +50,7 @@ class StreamGobbler extends Thread {

@Override
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedReader br = new BufferedReader(new InputStreamReader(is, Charsets.UTF_8));
try {
String line;
while ((line = br.readLine()) != null) {
Expand Down
Expand Up @@ -19,12 +19,15 @@
*/
package org.sonar.process;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.text.StrSubstitutor;

import java.io.File;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -55,9 +58,9 @@ static Props loadPropsFromCommandLineArgs(String[] args) {

File propertyFile = new File(args[0]);
Properties properties = new Properties();
FileReader reader = null;
Reader reader = null;
try {
reader = new FileReader(propertyFile);
reader = new InputStreamReader(new FileInputStream(propertyFile), Charsets.UTF_8);
properties.load(reader);
} catch (Exception e) {
throw new IllegalStateException("Could not read properties from file: " + args[0], e);
Expand Down
Expand Up @@ -22,6 +22,7 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps;
import org.apache.commons.io.Charsets;
import org.picocontainer.Startable;
import org.sonar.api.Plugin;
import org.sonar.api.ServerExtension;
Expand Down Expand Up @@ -123,11 +124,11 @@ public Collection<String> getContributingPluginList() {
public Reader createReaderForXMLFile(String pluginKey) {
ClassLoader classLoader = contributingPluginKeyToClassLoader.get(pluginKey);
String xmlFilePath = getXMLFilePath(pluginKey);
return new InputStreamReader(classLoader.getResourceAsStream(xmlFilePath));
return new InputStreamReader(classLoader.getResourceAsStream(xmlFilePath), Charsets.UTF_8);
}

@VisibleForTesting
Map<String, ClassLoader> getContributingPluginKeyToClassLoader(){
Map<String, ClassLoader> getContributingPluginKeyToClassLoader() {
return contributingPluginKeyToClassLoader;
}

Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import org.apache.commons.io.Charsets;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.ServerComponent;
Expand Down Expand Up @@ -165,12 +166,10 @@ private String prettyFormatXml(String xml) {
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT);
Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8))));
StreamResult res = new StreamResult(new ByteArrayOutputStream());
serializer.transform(xmlSource, res);
return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());
} catch (TransformerConfigurationException ignored) {
// Ignore, raw XML will be returned
return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(), Charsets.UTF_8);
} catch (TransformerException ignored) {
// Ignore, raw XML will be returned
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.server.qualityprofile;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -87,14 +88,14 @@ private void verify(QualityProfileDto fromProfile, QProfileName toProfileName) {
}
if (fromProfile.getName().equals(toProfileName.getName())) {
throw new IllegalArgumentException(String.format("Source and target profiles are equal: %s",
fromProfile.getName(), toProfileName.getName()));
fromProfile.getName()));
}
}

private void backup(String profileKey, File backupFile) {
Writer writer = null;
try {
writer = new OutputStreamWriter(FileUtils.openOutputStream(backupFile));
writer = new OutputStreamWriter(FileUtils.openOutputStream(backupFile), Charsets.UTF_8);
backuper.backup(profileKey, writer);
} catch (IOException e) {
throw new IllegalStateException("Fail to open temporary backup file: " + backupFile, e);
Expand All @@ -106,7 +107,7 @@ private void backup(String profileKey, File backupFile) {
private void restore(File backupFile, QProfileName profileName) {
Reader reader = null;
try {
reader = new InputStreamReader(FileUtils.openInputStream(backupFile));
reader = new InputStreamReader(FileUtils.openInputStream(backupFile), Charsets.UTF_8);
backuper.restore(reader, profileName);
} catch (IOException e) {
throw new IllegalStateException("Fail to create temporary backup file: " + backupFile, e);
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.sonar.server.rule.ws;

import com.google.common.base.Strings;
import org.apache.commons.io.Charsets;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
Expand Down Expand Up @@ -167,7 +168,7 @@ private void write409(Response response, RuleKey ruleKey) {
Response.Stream stream = response.stream();
stream.setStatus(409);
stream.setMediaType(MimeTypes.JSON);
JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output())).beginObject().name("rule");
JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output(), Charsets.UTF_8)).beginObject().name("rule");
mapping.write(rule, json, null /* TODO replace by SearchOptions immutable constant */);
json.endObject().close();
}
Expand Down
Expand Up @@ -31,6 +31,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
* @since 2.11
Expand All @@ -51,7 +52,7 @@ public void start() throws IOException {

void writeIndex(File indexFile) throws IOException {
FileUtils.forceMkdir(indexFile.getParentFile());
FileWriter writer = new FileWriter(indexFile, false);
Writer writer = new FileWriter(indexFile, false);
try {
for (PluginMetadata metadata : repository.getMetadata()) {
writer.append(RemotePlugin.create((DefaultPluginMetadata) metadata).marshal());
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.server.ws;

import com.google.common.base.Charsets;
import org.sonar.api.server.ws.Response;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.api.utils.text.XmlWriter;
Expand Down Expand Up @@ -64,7 +65,7 @@ public OutputStream output() {
}

public String outputAsString() {
return output.toString();
return new String(output.toByteArray(), Charsets.UTF_8);
}

public ServletStream reset() {
Expand All @@ -78,13 +79,13 @@ public ServletStream reset() {
@Override
public JsonWriter newJsonWriter() {
stream.setMediaType(MimeTypes.JSON);
return JsonWriter.of(new OutputStreamWriter(stream.output()));
return JsonWriter.of(new OutputStreamWriter(stream.output(), Charsets.UTF_8));
}

@Override
public XmlWriter newXmlWriter() {
stream.setMediaType(MimeTypes.XML);
return XmlWriter.of(new OutputStreamWriter(stream.output()));
return XmlWriter.of(new OutputStreamWriter(stream.output(), Charsets.UTF_8));
}

@Override
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.server.ws;

import com.google.common.base.Charsets;
import org.picocontainer.Startable;
import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent;
Expand Down Expand Up @@ -120,7 +121,7 @@ private void sendErrors(ServletResponse response, int status, Errors errors) {
stream.reset();
stream.setStatus(status);
stream.setMediaType(MimeTypes.JSON);
JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output()));
JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output(), Charsets.UTF_8));

try {
json.beginObject();
Expand Down
Expand Up @@ -21,6 +21,7 @@

import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;
import org.apache.commons.io.Charsets;
import org.sonar.channel.CodeReader;

import java.io.IOException;
Expand Down Expand Up @@ -98,7 +99,7 @@ public static String getCss() {
InputStream input = null;
try {
input = HtmlRenderer.class.getResourceAsStream(CSS_PATH);
return new String(ByteStreams.toByteArray(input));
return new String(ByteStreams.toByteArray(input), Charsets.UTF_8);

} catch (IOException e) {
throw new SynhtaxHighlightingException("SonarQube Colorizer CSS file not found: " + CSS_PATH, e);
Expand Down
Expand Up @@ -23,6 +23,7 @@
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex;
import org.apache.commons.io.Charsets;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
Expand Down Expand Up @@ -548,7 +549,7 @@ private static String determineType(Object value) {
* Creates a vertex from GraphSON using settings supplied in the constructor.
*/
Vertex vertexFromJson(InputStream json) throws ParseException, IOException {
return this.vertexFromJson((JSONObject) parser.parse(new InputStreamReader(json)));
return this.vertexFromJson((JSONObject) parser.parse(new InputStreamReader(json, Charsets.UTF_8)));
}

/**
Expand All @@ -562,7 +563,7 @@ Edge edgeFromJson(String json, Vertex out, Vertex in) throws IOException, ParseE
* Creates an edge from GraphSON using settings supplied in the constructor.
*/
Edge edgeFromJson(InputStream json, Vertex out, Vertex in) throws IOException, ParseException {
return this.edgeFromJson((JSONObject) parser.parse(new InputStreamReader(json)), out, in);
return this.edgeFromJson((JSONObject) parser.parse(new InputStreamReader(json, Charsets.UTF_8)), out, in);
}

/**
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.api.utils;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -42,6 +43,7 @@
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -96,7 +98,7 @@ public void parse(@Nullable File file) {

BufferedReader buffer = null;
try {
buffer = new BufferedReader(new FileReader(file));
buffer = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8));
parse(buffer);

} catch (IOException e) {
Expand All @@ -110,7 +112,7 @@ public void parse(@Nullable File file) {
public void parse(InputStream stream) {
BufferedReader buffer = null;
try {
buffer = new BufferedReader(new InputStreamReader(stream));
buffer = new BufferedReader(new InputStreamReader(stream, Charsets.UTF_8));
parse(buffer);

} catch (IOException e) {
Expand All @@ -127,14 +129,12 @@ private void parse(BufferedReader buffer) throws IOException {

public void parse(String xml) {
try {
xml = fixUnicodeChar(xml);
doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
String fixedXml = fixUnicodeChar(xml);
doc = builder.parse(new ByteArrayInputStream(fixedXml.getBytes(Charsets.UTF_8)));
XPathFactory factory = XPathFactory.newInstance();
xpath = factory.newXPath();

} catch (SAXException e) {
throw new XmlParserException(CAN_NOT_PARSE_XML + xml, e);
} catch (IOException e) {
} catch (IOException | SAXException e) {
throw new XmlParserException(CAN_NOT_PARSE_XML + xml, e);
}
}
Expand Down
Expand Up @@ -19,14 +19,19 @@
*/
package org.sonar.test.i18n;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Properties;
import java.util.SortedMap;
Expand Down Expand Up @@ -118,9 +123,9 @@ private void printReport(File dumpFile, String details) {
dumpFile.delete();
}
dumpFile.getParentFile().mkdirs();
FileWriter writer = null;
Writer writer = null;
try {
writer = new FileWriter(dumpFile);
writer = new OutputStreamWriter(new FileOutputStream(dumpFile), Charsets.UTF_8);
writer.write(details);
} catch (IOException e) {
throw new IllegalStateException("Unable to write the report to 'target/l10n/" + bundleName + ".report.txt'", e);
Expand Down

0 comments on commit dacd73d

Please sign in to comment.