View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2016 ForgeRock AS.
15   */
16  package org.forgerock.api.models;
17  
18  import static org.forgerock.json.JsonValue.array;
19  import static org.forgerock.json.JsonValue.json;
20  import static org.forgerock.json.JsonValue.object;
21  
22  import org.forgerock.json.JsonValue;
23  import org.forgerock.util.Function;
24  import org.forgerock.util.i18n.LocalizableString;
25  import org.forgerock.util.promise.NeverThrowsException;
26  
27  /**
28   * Iterates over each JsonValue node in the JsonValue structure and if it's a String marked for translation,
29   * It replaces the String with a LocalizableString.
30   */
31  public class TranslateJsonSchema implements Function<JsonValue, JsonValue, NeverThrowsException> {
32  
33      private final ClassLoader loader;
34  
35      /**
36       * Constructor which takes a {@code ClassLoader} where the String is defined.
37       * @param loader the {@code ClassLoader} where the translation resources are defined
38       */
39      public TranslateJsonSchema(ClassLoader loader) {
40          this.loader = loader;
41      }
42  
43      /**
44       * Applies the translation to string values by converting them to {@code LocalizableString}.
45       * It traverses the JsonValue structure, iteratively applying the function to each item
46       * in a collection.
47       * @param value A JsonValue.
48       * @return a transformed copy of the JsonValue input.
49       */
50      @Override
51      public JsonValue apply(JsonValue value) {
52          JsonValue returnValue = value;
53          if (value.isCollection()) {
54              JsonValue transformedValue = json(array());
55              for (JsonValue item : value) {
56                  transformedValue.add(item.as(this).getObject());
57              }
58              returnValue = transformedValue;
59          } else if (value.isMap()) {
60              JsonValue transformedValue = json(object());
61              for (String key : value.keys()) {
62                  transformedValue.put(key, value.get(key).as(this).getObject());
63              }
64              returnValue = transformedValue;
65          } else if (value.isString() && value.asString().startsWith(LocalizableString.TRANSLATION_KEY_PREFIX)) {
66              returnValue = json(new LocalizableString(value.asString(), loader));
67          }
68          return returnValue;
69      }
70  }