View Javadoc
1   /*
2    * dpkg - Debian Package library and the Debian Package Maven plugin
3    * (c) Copyright 2016 Gerrit Hohl
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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   * <p>
36   * A {@link FieldBuilder} implementation.
37   * </p>
38   *
39   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
40   * @version <b>1.0</b>, 29.04.2016 by Gerrit Hohl
41   */
42  public class FieldBuilderImpl implements FieldBuilder, GlobalConstants {
43  	
44  	
45  	/**
46  	 * <p>
47  	 * Creates a builder.
48  	 * </p>
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  	 * <p>
97  	 * Returns the formatted value.
98  	 * </p>
99  	 *
100 	 * @param value
101 	 *            The value.
102 	 * @return The formatted value.
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 		// ROADMAP Throw a BuildException if a line is empty.
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 }