Merge pull request #185 from glyptodon/merge-translation-files

This commit is contained in:
Michael Jumper
2015-05-19 10:11:49 -07:00
2 changed files with 132 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
@@ -33,6 +34,9 @@ import java.util.regex.Pattern;
import javax.servlet.ServletContext;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;
import org.glyptodon.guacamole.net.basic.resource.ByteArrayResource;
import org.glyptodon.guacamole.net.basic.resource.Resource;
import org.glyptodon.guacamole.net.basic.resource.WebApplicationResource;
import org.slf4j.Logger;
@@ -102,6 +106,45 @@ public class LanguageResourceService {
}
/**
* Merges the given JSON objects. Any leaf node in overlay will overwrite
* the corresponding path in original.
*
* @param original
* The original JSON object to which changes should be applied.
*
* @param overlay
* The JSON object containing changes that should be applied.
*
* @return
* The newly constructed JSON object that is the result of merging
* original and overlay.
*/
private JsonNode mergeTranslations(JsonNode original, JsonNode overlay) {
// If we are at a leaf node, the result of merging is simply the overlay
if (!overlay.isObject() || original == null)
return overlay;
// Create mutable copy of original
ObjectNode newNode = JsonNodeFactory.instance.objectNode();
Iterator<String> fieldNames = original.getFieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
newNode.put(fieldName, original.get(fieldName));
}
// Merge each field
fieldNames = overlay.getFieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
newNode.put(fieldName, mergeTranslations(original.get(fieldName), overlay.get(fieldName)));
}
return newNode;
}
/**
* Adds or overlays the given language resource, which need not exist in
* the ServletContext. If a language resource is already defined for the
@@ -123,8 +166,33 @@ public class LanguageResourceService {
// Merge language resources if already defined
Resource existing = resources.get(key);
if (existing != null) {
// TODO: Merge
logger.debug("Merged strings with existing language: \"{}\"", key);
try {
// Get resource stream
InputStream existingStream = existing.asStream();
InputStream resourceStream = resource.asStream();
if (existingStream == null || resourceStream == null) {
logger.warn("Language resource \"{}\" does not exist.", key);
return;
}
// Read the original and new language resources
JsonNode existingTree = mapper.readTree(existingStream);
JsonNode resourceTree = mapper.readTree(resourceStream);
// Merge the language resources
JsonNode mergedTree = mergeTranslations(existingTree, resourceTree);
resources.put(key, new ByteArrayResource("application/json", mapper.writeValueAsBytes(mergedTree)));
logger.debug("Merged strings with existing language: \"{}\"", key);
}
catch (IOException e) {
logger.error("Unable to merge language resource \"{}\": {}", key, e.getMessage());
logger.debug("Error merging language resource.", e);
}
}
// Otherwise, add new language resource

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2015 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.glyptodon.guacamole.net.basic.resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
/**
* A resource which contains a defined byte array.
*
* @author Michael Jumper
*/
public class ByteArrayResource extends AbstractResource {
/**
* The bytes contained by this resource.
*/
private final byte[] bytes;
/**
* Creates a new ByteArrayResource which provides access to the given byte
* array. Changes to the given byte array will affect this resource even
* after the resource is created. Changing the byte array while an input
* stream from this resource is in use has undefined behavior.
*
* @param mimetype
* The mimetype of the resource.
*
* @param bytes
* The bytes that this resource should contain.
*/
public ByteArrayResource(String mimetype, byte[] bytes) {
super(mimetype);
this.bytes = bytes;
}
@Override
public InputStream asStream() {
return new ByteArrayInputStream(bytes);
}
}