1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.eluder.coveralls.maven.plugin.json;
25
26 import com.fasterxml.jackson.core.JsonEncoding;
27 import com.fasterxml.jackson.core.JsonGenerator;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.MappingJsonFactory;
30
31 import org.apache.commons.lang3.StringUtils;
32 import org.eluder.coveralls.maven.plugin.ProcessingException;
33 import org.eluder.coveralls.maven.plugin.domain.Job;
34 import org.eluder.coveralls.maven.plugin.domain.Source;
35 import org.eluder.coveralls.maven.plugin.source.SourceCallback;
36
37 import java.io.Closeable;
38 import java.io.File;
39 import java.io.IOException;
40 import java.text.SimpleDateFormat;
41 import java.util.Date;
42 import java.util.Map.Entry;
43 import java.util.Properties;
44
45 public class JsonWriter implements SourceCallback, Closeable {
46
47 protected static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
48
49 private final Job job;
50 private final File coverallsFile;
51 private final JsonGenerator generator;
52
53 public JsonWriter(final Job job, final File coverallsFile) throws IOException {
54 File directory = coverallsFile.getParentFile();
55 if (!directory.exists()) {
56 directory.mkdirs();
57 }
58 this.job = job;
59 this.coverallsFile = coverallsFile;
60 this.generator = new MappingJsonFactory().createGenerator(coverallsFile, JsonEncoding.UTF8);
61 }
62
63 public final Job getJob() {
64 return job;
65 }
66
67 public final File getCoverallsFile() {
68 return coverallsFile;
69 }
70
71 @Override
72 public void onBegin() throws ProcessingException, IOException {
73 try {
74 generator.writeStartObject();
75 writeOptionalString("repo_token", job.getRepoToken());
76 writeOptionalString("service_name", job.getServiceName());
77 writeOptionalString("service_job_id", job.getServiceJobId());
78 writeOptionalString("service_number", job.getServiceBuildNumber());
79 writeOptionalString("service_build_url", job.getServiceBuildUrl());
80 writeOptionalString("service_branch", job.getBranch());
81 writeOptionalString("service_pull_request", job.getPullRequest());
82 writeOptionalBoolean("parallel", job.isParallel());
83 writeOptionalTimestamp("run_at", job.getTimestamp());
84 writeOptionalEnvironment("environment", job.getServiceEnvironment());
85 writeOptionalObject("git", job.getGit());
86 generator.writeArrayFieldStart("source_files");
87 } catch (JsonProcessingException ex) {
88 throw new ProcessingException(ex);
89 }
90 }
91
92 @Override
93 public void onSource(final Source source) throws ProcessingException, IOException {
94 try {
95 generator.writeObject(source);
96 } catch (JsonProcessingException ex) {
97 throw new ProcessingException(ex);
98 }
99 }
100
101 @Override
102 public void onComplete() throws ProcessingException, IOException {
103 try {
104 generator.writeEndArray();
105 generator.writeEndObject();
106 } catch (JsonProcessingException ex) {
107 throw new ProcessingException(ex);
108 }
109 }
110
111 @Override
112 public void close() throws IOException {
113 generator.close();
114 }
115
116 private void writeOptionalString(final String field, final String value) throws IOException {
117 if (StringUtils.isNotBlank(value)) {
118 generator.writeStringField(field, value);
119 }
120 }
121
122 private void writeOptionalBoolean(final String field, final boolean value) throws IOException {
123 if (value) {
124 generator.writeBooleanField(field, value);
125 }
126 }
127
128 private void writeOptionalObject(final String field, final Object value) throws IOException {
129 if (value != null) {
130 generator.writeObjectField(field, value);
131 }
132 }
133
134 private void writeOptionalTimestamp(final String field, final Date value) throws ProcessingException, IOException {
135 if (value != null) {
136 SimpleDateFormat format = new SimpleDateFormat(TIMESTAMP_FORMAT);
137 writeOptionalString(field, format.format(value));
138 }
139 }
140
141 private void writeOptionalEnvironment(final String field, final Properties properties) throws ProcessingException, IOException {
142 if (properties != null) {
143 generator.writeObjectFieldStart(field);
144 for (Entry<Object, Object> property : properties.entrySet()) {
145 writeOptionalString(property.getKey().toString(), property.getValue().toString());
146 }
147 generator.writeEndObject();
148 }
149 }
150 }