1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.javadpkg.field.impl;
20
21 import java.io.BufferedWriter;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.util.List;
25
26 import net.sourceforge.javadpkg.BuildException;
27 import net.sourceforge.javadpkg.Context;
28 import net.sourceforge.javadpkg.GlobalConstants;
29 import net.sourceforge.javadpkg.field.Field;
30 import net.sourceforge.javadpkg.field.FieldBuilder;
31 import net.sourceforge.javadpkg.io.DataTarget;
32
33
34
35
36
37
38
39
40
41
42 public class FieldBuilderImpl implements FieldBuilder, GlobalConstants {
43
44
45
46
47
48
49
50 public FieldBuilderImpl() {
51 super();
52 }
53
54
55 @Override
56 public void buildFields(List<Field> fields, DataTarget target, Context context) throws IOException, BuildException {
57 StringBuilder sb;
58 String value;
59
60
61 if (fields == null)
62 throw new IllegalArgumentException("Argument fields is null.");
63 if (target == null)
64 throw new IllegalArgumentException("Argument target is null.");
65 if (context == null)
66 throw new IllegalArgumentException("Argument context is null.");
67
68 sb = new StringBuilder();
69 for (Field field : fields) {
70 if (sb.length() > 0) {
71 sb.append('\n');
72 }
73
74 if (field.isEmpty()) {
75 continue;
76 }
77
78 if (!field.isNameless()) {
79 sb.append(field.getName());
80 sb.append(": ");
81 }
82 value = this.getFormattedValue(field.getValue());
83 sb.append(value);
84 }
85 if (sb.length() > 0) {
86 sb.append('\n');
87 }
88
89 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(target.getOutputStream(), UTF_8_CHARSET))) {
90 out.write(sb.toString());
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104 private String getFormattedValue(String value) {
105 String[] values;
106 StringBuilder sb;
107
108
109 if (value.indexOf('\n') == -1)
110 return value;
111
112
113 values = value.split("\n", -1);
114 sb = new StringBuilder();
115 for (String v : values) {
116 if (sb.length() > 0) {
117 sb.append("\n ");
118 }
119 sb.append(v);
120 }
121 return sb.toString();
122 }
123
124
125 }