@@ -74,38 +74,41 @@ public Bundle parse(InputStream inStream) throws IOException {
7474 return bundle ;
7575 }
7676
77- private int addBundleStrings (JsonObject obj , String keyPrefix , Bundle bundle , int sequenceNum ) {
77+ protected int addBundleStrings (JsonObject obj , String keyPrefix , Bundle bundle , int sequenceNum ) {
7878 for (Map .Entry <String , JsonElement > entry : obj .entrySet ()) {
7979 String key = entry .getKey ();
8080 JsonElement value = entry .getValue ();
8181 if (value .isJsonObject ()) {
82- sequenceNum = addBundleStrings (value .getAsJsonObject (), modifiedKeyPrefix (keyPrefix ,key ,"$." ) , bundle , sequenceNum );
82+ sequenceNum = addBundleStrings (value .getAsJsonObject (), modifiedKeyPrefix (keyPrefix , key , "$." ), bundle ,
83+ sequenceNum );
8384 } else if (value .isJsonArray ()) {
8485 JsonArray ar = value .getAsJsonArray ();
8586 for (int i = 0 ; i < ar .size (); i ++) {
8687 JsonElement arrayEntry = ar .get (i );
8788 if (arrayEntry .isJsonPrimitive () && arrayEntry .getAsJsonPrimitive ().isString ()) {
8889 sequenceNum ++;
89- bundle .addResourceString (modifiedKeyPrefix (keyPrefix ,key ,"$." ) + "[" + Integer .toString (i ) + "]" ,
90+ bundle .addResourceString (
91+ modifiedKeyPrefix (keyPrefix , key , "$." ) + "[" + Integer .toString (i ) + "]" ,
9092 arrayEntry .getAsString (), sequenceNum );
9193
9294 } else {
9395 sequenceNum = addBundleStrings (arrayEntry .getAsJsonObject (),
94- modifiedKeyPrefix (keyPrefix ,key ,"$." ) + "[" + Integer .toString (i ) + "]" , bundle , sequenceNum );
96+ modifiedKeyPrefix (keyPrefix , key , "$." ) + "[" + Integer .toString (i ) + "]" , bundle ,
97+ sequenceNum );
9598 }
9699 }
97100 } else if (!value .isJsonPrimitive () || !value .getAsJsonPrimitive ().isString ()) {
98101 throw new IllegalResourceFormatException ("The value of JSON element " + key + " is not a string." );
99102 } else {
100103 sequenceNum ++;
101- bundle .addResourceString (modifiedKeyPrefix (keyPrefix ,key ,"" ), value .getAsString (), sequenceNum );
104+ bundle .addResourceString (modifiedKeyPrefix (keyPrefix , key , "" ), value .getAsString (), sequenceNum );
102105 }
103106 }
104107 return sequenceNum ;
105108 }
106109
107- private String modifiedKeyPrefix ( String keyPrefix , String key , String addPrefixIfEmpty ) {
108-
110+ protected String modifiedKeyPrefix (String keyPrefix , String key , String addPrefixIfEmpty ) {
111+
109112 final Pattern specialSequences = Pattern .compile ("[.'\\ [\\ ]]" );
110113 if (key .isEmpty ()) {
111114 return keyPrefix ;
@@ -120,19 +123,29 @@ private String modifiedKeyPrefix( String keyPrefix, String key, String addPrefix
120123 return keyPrefix + "." + key ;
121124 }
122125 }
123-
126+
124127 @ Override
125128 public void write (OutputStream outStream , String language , Bundle bundle ) throws IOException {
126129 // extracts key value pairs in original sequence order
127130 TreeSet <ResourceString > sortedResources = new TreeSet <>(new ResourceStringComparator ());
128131 sortedResources .addAll (bundle .getResourceStrings ());
129132 JsonObject output = new JsonObject ();
133+ JsonObject top_level ;
134+
135+ if (this instanceof GlobalizeJsResource ) {
136+ top_level = new JsonObject ();
137+ top_level .add (language , output );
138+ } else {
139+ top_level = output ;
140+ }
141+
130142 for (ResourceString res : sortedResources ) {
131143 String key = res .getKey ();
132144 List <KeyPiece > keyPieces = splitKeyPieces (key );
133145 JsonElement current = output ;
134146 for (int i = 0 ; i < keyPieces .size (); i ++) {
135- if (i + 1 < keyPieces .size ()) { // There is structure under this key piece
147+ if (i + 1 < keyPieces .size ()) { // There is structure under this
148+ // key piece
136149 if (current .isJsonObject ()) {
137150 JsonObject currentObject = current .getAsJsonObject ();
138151 if (!currentObject .has (keyPieces .get (i ).keyValue )) {
@@ -146,7 +159,7 @@ public void write(OutputStream outStream, String language, Bundle bundle) throws
146159 } else {
147160 JsonArray currentArray = current .getAsJsonArray ();
148161 Integer idx = Integer .valueOf (keyPieces .get (i ).keyValue );
149- for ( int arrayIndex = currentArray .size (); arrayIndex <= idx ; arrayIndex ++) {
162+ for (int arrayIndex = currentArray .size (); arrayIndex <= idx ; arrayIndex ++) {
150163 currentArray .add (JsonNull .INSTANCE );
151164 }
152165 if (currentArray .get (idx ).isJsonNull ()) {
@@ -155,15 +168,15 @@ public void write(OutputStream outStream, String language, Bundle bundle) throws
155168 } else {
156169 currentArray .set (idx , new JsonObject ());
157170 }
158- }
171+ }
159172 current = currentArray .get (idx );
160173 }
161174 } else { // This is the leaf node
162175 if (keyPieces .get (i ).keyType == JsonToken .BEGIN_ARRAY ) {
163176 JsonArray currentArray = current .getAsJsonArray ();
164177 Integer idx = Integer .valueOf (keyPieces .get (i ).keyValue );
165178 JsonPrimitive e = new JsonPrimitive (res .getValue ());
166- for ( int arrayIndex = currentArray .size (); arrayIndex <= idx ; arrayIndex ++) {
179+ for (int arrayIndex = currentArray .size (); arrayIndex <= idx ; arrayIndex ++) {
167180 currentArray .add (JsonNull .INSTANCE );
168181 }
169182 current .getAsJsonArray ().set (idx , e );
@@ -175,7 +188,7 @@ public void write(OutputStream outStream, String language, Bundle bundle) throws
175188 }
176189 try (OutputStreamWriter writer = new OutputStreamWriter (new BufferedOutputStream (outStream ),
177190 StandardCharsets .UTF_8 )) {
178- new GsonBuilder ().setPrettyPrinting ().disableHtmlEscaping ().create ().toJson (output , writer );
191+ new GsonBuilder ().setPrettyPrinting ().disableHtmlEscaping ().create ().toJson (top_level , writer );
179192 }
180193 }
181194
@@ -209,10 +222,10 @@ private static List<String> findTokens(String data) {
209222 StringCharacterIterator i = new StringCharacterIterator (data );
210223 while (i .current () != StringCharacterIterator .DONE ) {
211224 char c = i .current ();
212- if ( c == '\'' ) {
225+ if (c == '\'' ) {
213226 inQuotes = !inQuotes ;
214227 }
215- if (!inQuotes && ( c == '.' || c == '[' || c == ']' )) {
228+ if (!inQuotes && (c == '.' || c == '[' || c == ']' )) {
216229 tokens .add (currentToken .toString ());
217230 currentToken .setLength (0 );
218231 } else {
0 commit comments