generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from marhali/feat/json5
Feat/json5
- Loading branch information
Showing
11 changed files
with
371 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/de/marhali/easyi18n/io/parser/json5/Json5ArrayMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package de.marhali.easyi18n.io.parser.json5; | ||
|
||
import de.marhali.easyi18n.io.parser.ArrayMapper; | ||
import de.marhali.easyi18n.util.StringUtil; | ||
import de.marhali.json5.Json5; | ||
import de.marhali.json5.Json5Array; | ||
import de.marhali.json5.Json5Primitive; | ||
|
||
import org.apache.commons.lang.math.NumberUtils; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Map json5 array values. | ||
* @author marhali | ||
*/ | ||
public class Json5ArrayMapper extends ArrayMapper { | ||
|
||
private static final Json5 JSON5 = Json5.builder(builder -> | ||
builder.allowInvalidSurrogate().quoteSingle().indentFactor(0).build()); | ||
|
||
public static String read(Json5Array array) { | ||
return read(array.iterator(), (jsonElement -> { | ||
try { | ||
return jsonElement.isJson5Array() || jsonElement.isJson5Object() | ||
? "\\" + JSON5.serialize(jsonElement) | ||
: jsonElement.getAsString(); | ||
} catch (IOException e) { | ||
throw new AssertionError(e.getMessage(), e.getCause()); | ||
} | ||
})); | ||
} | ||
|
||
public static Json5Array write(String concat) { | ||
Json5Array array = new Json5Array(); | ||
|
||
write(concat, (element) -> { | ||
if(element.startsWith("\\")) { | ||
array.add(JSON5.parse(element.replace("\\", ""))); | ||
} else { | ||
if(StringUtil.isHexString(element)) { | ||
array.add(Json5Primitive.of(element, true)); | ||
} else if(NumberUtils.isNumber(element)) { | ||
array.add(Json5Primitive.of(NumberUtils.createNumber(element))); | ||
} else { | ||
array.add(Json5Primitive.of(element)); | ||
} | ||
} | ||
}); | ||
|
||
return array; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/de/marhali/easyi18n/io/parser/json5/Json5Mapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package de.marhali.easyi18n.io.parser.json5; | ||
|
||
import de.marhali.easyi18n.model.Translation; | ||
import de.marhali.easyi18n.model.TranslationNode; | ||
import de.marhali.easyi18n.util.StringUtil; | ||
|
||
import de.marhali.json5.Json5Element; | ||
import de.marhali.json5.Json5Object; | ||
import de.marhali.json5.Json5Primitive; | ||
import org.apache.commons.lang.StringEscapeUtils; | ||
import org.apache.commons.lang.math.NumberUtils; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Mapper for mapping json5 objects into translation nodes and backwards. | ||
* @author marhali | ||
*/ | ||
public class Json5Mapper { | ||
public static void read(String locale, Json5Object json, TranslationNode node) { | ||
for(Map.Entry<String, Json5Element> entry : json.entrySet()) { | ||
String key = entry.getKey(); | ||
Json5Element value = entry.getValue(); | ||
|
||
TranslationNode childNode = node.getOrCreateChildren(key); | ||
|
||
if(value.isJson5Object()) { | ||
// Nested element - run recursively | ||
read(locale, value.getAsJson5Object(), childNode); | ||
} else { | ||
Translation translation = childNode.getValue(); | ||
|
||
String content = value.isJson5Array() | ||
? Json5ArrayMapper.read(value.getAsJson5Array()) | ||
: StringUtil.escapeControls(value.getAsString(), true); | ||
|
||
translation.put(locale, content); | ||
childNode.setValue(translation); | ||
} | ||
} | ||
} | ||
|
||
public static void write(String locale, Json5Object json, TranslationNode node) { | ||
for(Map.Entry<String, TranslationNode> entry : node.getChildren().entrySet()) { | ||
String key = entry.getKey(); | ||
TranslationNode childNode = entry.getValue(); | ||
|
||
if(!childNode.isLeaf()) { | ||
// Nested node - run recursively | ||
Json5Object childJson = new Json5Object(); | ||
write(locale, childJson, childNode); | ||
if(childJson.size() > 0) { | ||
json.add(key, childJson); | ||
} | ||
|
||
} else { | ||
Translation translation = childNode.getValue(); | ||
String content = translation.get(locale); | ||
if(content != null) { | ||
if(Json5ArrayMapper.isArray(content)) { | ||
json.add(key, Json5ArrayMapper.write(content)); | ||
} else if(StringUtil.isHexString(content)) { | ||
json.add(key, Json5Primitive.of(content, true)); | ||
} else if(NumberUtils.isNumber(content)) { | ||
json.add(key, Json5Primitive.of(NumberUtils.createNumber(content))); | ||
} else { | ||
json.add(key, Json5Primitive.of(StringEscapeUtils.unescapeJava(content))); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/de/marhali/easyi18n/io/parser/json5/Json5ParserStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package de.marhali.easyi18n.io.parser.json5; | ||
|
||
import com.intellij.openapi.vfs.VirtualFile; | ||
|
||
import de.marhali.easyi18n.io.parser.ParserStrategy; | ||
import de.marhali.easyi18n.model.SettingsState; | ||
import de.marhali.easyi18n.model.TranslationData; | ||
import de.marhali.easyi18n.model.TranslationFile; | ||
import de.marhali.easyi18n.model.TranslationNode; | ||
import de.marhali.json5.Json5; | ||
import de.marhali.json5.Json5Element; | ||
import de.marhali.json5.Json5Object; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Json5 file format parser strategy | ||
* @author marhali | ||
*/ | ||
public class Json5ParserStrategy extends ParserStrategy { | ||
|
||
private static final Json5 JSON5 = Json5.builder(builder -> | ||
builder.allowInvalidSurrogate().trailingComma().indentFactor(4).build()); | ||
|
||
public Json5ParserStrategy(@NotNull SettingsState settings) { | ||
super(settings); | ||
} | ||
|
||
@Override | ||
public void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws Exception { | ||
data.addLocale(file.getLocale()); | ||
|
||
VirtualFile vf = file.getVirtualFile(); | ||
TranslationNode targetNode = super.getOrCreateTargetNode(file, data); | ||
|
||
try (Reader reader = new InputStreamReader(vf.getInputStream(), vf.getCharset())) { | ||
Json5Element input = JSON5.parse(reader); | ||
if(input != null && input.isJson5Object()) { | ||
Json5Mapper.read(file.getLocale(), input.getAsJson5Object(), targetNode); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception { | ||
TranslationNode targetNode = super.getTargetNode(data, file); | ||
|
||
Json5Object output = new Json5Object(); | ||
Json5Mapper.write(file.getLocale(), output, Objects.requireNonNull(targetNode)); | ||
|
||
VirtualFile vf = file.getVirtualFile(); | ||
vf.setBinaryContent(JSON5.serialize(output).getBytes(vf.getCharset())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.