11/*
2- * Copyright IBM Corp. 2015, 2016
2+ * Copyright IBM Corp. 2015, 2017
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2525import java .text .BreakIterator ;
2626import java .util .ArrayList ;
2727import java .util .Collections ;
28+ import java .util .Date ;
2829import java .util .Enumeration ;
2930import java .util .HashMap ;
3031import java .util .HashSet ;
4142/**
4243 * Java properties resource filter implementation.
4344 *
44- * @author Yoshito Umaoka
45+ * @author Yoshito Umaoka, JCEmmons
4546 */
4647public class JavaPropertiesResource implements ResourceFilter {
4748
@@ -161,11 +162,19 @@ public void write(OutputStream outStream, String language, Bundle resource) thro
161162 TreeSet <ResourceString > sortedResources = new TreeSet <>(new ResourceStringComparator ());
162163 sortedResources .addAll (resource .getResourceStrings ());
163164
164- LinkedProperties props = new LinkedProperties ();
165+ PrintWriter pw = new PrintWriter (new OutputStreamWriter (outStream , PROPS_ENC ));
166+ for (String note : resource .getNotes ()) {
167+ pw .println ("#" +note );
168+ }
169+ if (!resource .getNotes ().isEmpty ()) {
170+ pw .println ();
171+ }
172+ pw .println ("#" +new Date ().toString ());
165173 for (ResourceString res : sortedResources ) {
166- props .setProperty (res .getKey (), res .getValue ());
174+ PropDef pd = new PropDef (res .getKey (),res .getValue (),PropDef .PropSeparator .EQUAL ,res .getNotes ());
175+ pd .print (pw , language );
167176 }
168- props . store ( outStream , null );
177+ pw . close ( );
169178 }
170179
171180 private static final String PROPS_ENC = "ISO-8859-1" ;
@@ -174,7 +183,8 @@ static class PropDef {
174183 private String key ;
175184 private String value ;
176185 private PropSeparator separator ;
177-
186+ private List <String > notes ;
187+
178188 public enum PropSeparator {
179189 EQUAL ('=' ), COLON (':' ), SPACE (' ' );
180190
@@ -192,10 +202,23 @@ public char getCharacter() {
192202 private static final String INDENT = " " ;
193203 private static final int COLMAX = 80 ;
194204
205+ public PropDef (String key , String value , PropSeparator separator , List <String > notes ) {
206+ this .key = key ;
207+ this .value = value ;
208+ this .separator = separator ;
209+ if (notes != null ) {
210+ this .notes = new ArrayList <>();
211+ this .notes .addAll (notes );
212+ } else {
213+ this .notes = null ;
214+ }
215+ };
216+
195217 public PropDef (String key , String value , PropSeparator separator ) {
196218 this .key = key ;
197219 this .value = value ;
198220 this .separator = separator ;
221+ this .notes = null ;
199222 };
200223
201224 public static PropDef parseLine (String line ) {
@@ -239,7 +262,7 @@ public static PropDef parseLine(String line) {
239262 String key = unescapePropKey (line .substring (0 , sepIdx ).trim ());
240263 String value = unescapePropValue (stripLeadingSpaces (line .substring (sepIdx + 1 )));
241264
242- PropDef pl = new PropDef (key , value , sep );
265+ PropDef pl = new PropDef (key , value , sep , null );
243266 return pl ;
244267 }
245268
@@ -255,11 +278,22 @@ public PropSeparator getSeparator() {
255278 return separator ;
256279 }
257280
281+ public List <String > getNotes () {
282+ return Collections .unmodifiableList (notes );
283+ }
284+
258285 public void print (PrintWriter pw , String language ) throws IOException {
259286 StringBuilder buf = new StringBuilder (100 );
260287 int len = key .length () + value .length ()
261288 + 3 ; /* 3 - length of separator plus two SPs */
262289
290+ // Write out any notes (comments) associated with this resource.
291+ if (notes != null ) {
292+ for (String note : notes ) {
293+ pw .println ("#" + note );
294+ }
295+ }
296+
263297 if (len <= COLMAX ) {
264298 // Print this property in a single line
265299 if (separator .getCharacter () == PropSeparator .SPACE .getCharacter ()) {
@@ -290,7 +324,9 @@ public void print(PrintWriter pw, String language) throws IOException {
290324 buf .append (INDENT );
291325 }
292326
293- BreakIterator brk = BreakIterator .getWordInstance (Locale .forLanguageTag (language ));
327+
328+ BreakIterator brk = BreakIterator .getWordInstance (
329+ language == null ? Locale .getDefault () : Locale .forLanguageTag (language ));
294330 brk .setText (value );
295331
296332 int start = 0 ;
@@ -355,6 +391,8 @@ public String toString() {
355391 builder .append (getValue ());
356392 builder .append (" Sep=" );
357393 builder .append ("'" + getSeparator ().getCharacter () + "'" );
394+ builder .append (" Notes=" );
395+ builder .append (getNotes ().toString ());
358396 return builder .toString ();
359397 }
360398 }
@@ -611,7 +649,7 @@ public void merge(InputStream base, OutputStream outStream, String language, Bun
611649 }
612650 // Write the property key and value
613651 String key = pd .getKey ();
614- PropDef modPd = new PropDef (key , resMap .get (key ), pd .getSeparator ());
652+ PropDef modPd = new PropDef (key , resMap .get (key ), pd .getSeparator (), null );
615653 modPd .print (outWriter , language );
616654 } else {
617655 if (orgLines .isEmpty ()) {
0 commit comments