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.
2323import java .io .OutputStream ;
2424import java .io .OutputStreamWriter ;
2525import java .text .BreakIterator ;
26+ import java .util .ArrayList ;
2627import java .util .HashMap ;
28+ import java .util .Iterator ;
2729import java .util .LinkedList ;
2830import java .util .List ;
2931import java .util .Locale ;
4042public class IOSStringsResource implements ResourceFilter {
4143
4244 private static final String CHAR_SET = "UTF-8" ;
43-
45+ private static final String COMMENT_BEGIN = "/*" ;
46+ private static final String COMMENT_END = "*/" ;
47+
4448 @ Override
4549 public Bundle parse (InputStream in ) throws IOException {
4650 Bundle bundle = new Bundle ();
4751 BufferedReader reader = new BufferedReader (new InputStreamReader (in , CHAR_SET ));
4852
4953 int sqNum = 0 ;
5054 String line ;
55+ List <String > notes = new ArrayList <>();
56+ Boolean commentIsGlobal = true ;
5157 while ((line = reader .readLine ()) != null ) {
52- if (line .matches ( "^ \\ s*/ \\ *.*" )) {// begins with /*
58+ if (line .trim (). startsWith ( COMMENT_BEGIN )) {// begins with /*
5359 // skip comments until line ends with */
5460 // the short circuit expression evaluation handles both
5561 // single and multi lined comments
56- while (!line .matches (".*\\ */$" ) && (line = reader .readLine ()) != null )
57- ;
62+ String comment = line .substring (line .indexOf (COMMENT_BEGIN )+2 );
63+ Boolean commentEndProcessed = false ;
64+ while (!commentEndProcessed && line != null ) {
65+ if (comment .trim ().endsWith (COMMENT_END )) {
66+ comment = comment .substring (0 , comment .lastIndexOf (COMMENT_END ));
67+ commentEndProcessed = true ;
68+ }
69+ notes .add (comment );
70+ if (!commentEndProcessed ) {
71+ line = reader .readLine ();
72+ comment = line ;
73+ }
74+ }
75+ } else if (commentIsGlobal && line .isEmpty ()) {
76+ commentIsGlobal = false ;
77+ if (!notes .isEmpty ()) {
78+ bundle .addNotes (notes );
79+ notes .clear ();
80+ }
5881 } else if (line .matches ("^\\ s*\" .*" )) { // begins with quote char
5982 // new entry
6083 StringBuilder entry = new StringBuilder (128 );
@@ -69,7 +92,12 @@ public Bundle parse(InputStream in) throws IOException {
6992 String key = parts [0 ].substring (parts [0 ].indexOf ('"' ) + 1 ).trim ();
7093 String value = parts [1 ].substring (0 , parts [1 ].lastIndexOf ('"' )).trim ();
7194
72- bundle .addResourceString (key , value , ++sqNum );
95+ if (notes .isEmpty ()) {
96+ bundle .addResourceString (key , value , ++sqNum );
97+ } else {
98+ bundle .addResourceString (key , value , ++sqNum , notes );
99+ notes .clear ();
100+ }
73101 }
74102 }
75103
@@ -83,9 +111,19 @@ public void write(OutputStream os, String language, Bundle bundle) throws IOExce
83111
84112 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (os , CHAR_SET ));
85113
114+ boolean globalNotesWritten = false ;
115+ for (String globalNote : bundle .getNotes ()) {
116+ writer .write (COMMENT_BEGIN +globalNote +COMMENT_END );
117+ writer .newLine ();
118+ globalNotesWritten = true ;
119+ }
120+ if (globalNotesWritten ) {
121+ writer .newLine ();
122+ }
123+
86124 for (ResourceString res : sortedResources ) {
87125 // empties the buffer
88- writer .write (formatEntry (res .getKey (), res .getValue (), language ));
126+ writer .write (formatEntry (res .getKey (), res .getValue (), language , res . getNotes () ));
89127 }
90128
91129 writer .flush ();
@@ -95,9 +133,9 @@ public void write(OutputStream os, String language, Bundle bundle) throws IOExce
95133 public void merge (InputStream base , OutputStream os , String language , Bundle bundle )
96134 throws IOException {
97135 // put res data into a map for easier searching
98- Map <String , String > resMap = new HashMap <String , String >(bundle .getResourceStrings ().size () * 4 / 3 + 1 );
136+ Map <String , ResourceString > resMap = new HashMap <String , ResourceString >(bundle .getResourceStrings ().size () * 4 / 3 + 1 );
99137 for (ResourceString res : bundle .getResourceStrings ()) {
100- resMap .put (res .getKey (), res . getValue () );
138+ resMap .put (res .getKey (), res );
101139 }
102140
103141 BufferedReader reader = new BufferedReader (new InputStreamReader (base , CHAR_SET ));
@@ -130,7 +168,7 @@ public void merge(InputStream base, OutputStream os, String language, Bundle bun
130168 continue ;
131169 }
132170
133- writer .write (formatEntry (key , resMap .get (key ), language ));
171+ writer .write (formatEntry (key , resMap .get (key ). getValue () , language , resMap . get ( key ). getNotes () ));
134172 } else {
135173 writer .write (line );
136174 writer .newLine ();
@@ -140,11 +178,23 @@ public void merge(InputStream base, OutputStream os, String language, Bundle bun
140178 writer .flush ();
141179 }
142180
143- static String formatEntry (String key , String value , String localeStr ) {
181+ static String formatEntry (String key , String value , String localeStr , List < String > notes ) {
144182 int maxLineLen = 80 ;
145183
146184 StringBuilder output = new StringBuilder ();
147-
185+ StringBuilder comments = new StringBuilder ();
186+
187+ if (notes .size () > 0 ) {
188+ comments .append (COMMENT_BEGIN );
189+ Iterator <String > i = notes .iterator ();
190+ while (i .hasNext ()) {
191+ comments .append (i .next ());
192+ if (i .hasNext ()) {
193+ comments .append ("\n " );
194+ }
195+ }
196+ comments .append (COMMENT_END ).append ("\n " );
197+ }
148198 // construct the entire entry, i.e.
149199 // "key" = "value";
150200 StringBuilder entryBuilder = new StringBuilder (key .length () + value .length () + 7 );
@@ -154,7 +204,7 @@ static String formatEntry(String key, String value, String localeStr) {
154204 int entryLen = entry .length ();
155205 // entry fits on one line
156206 if (maxLineLen > entryLen ) {
157- return entry ;
207+ return comments . toString ()+ entry ;
158208 }
159209
160210 // entry needs to be split onto multiple lines
@@ -197,7 +247,7 @@ static String formatEntry(String key, String value, String localeStr) {
197247 }
198248 }
199249
200- return output .toString ();
250+ return comments . toString ()+ output .toString ();
201251 }
202252
203253}
0 commit comments