use of com.eclipsesource.json.JsonValue in project kontraktor by RuedigerMoeller.
the class JSXIntrinsicTranspiler method processNodeDir.
private File processNodeDir(File file, FileResolver resolver, Map<String, Object> alreadyResolved) {
File jfi = new File(file, "package.json");
if (jfi.exists()) {
try {
JsonObject pkg = Json.parse(new FileReader(jfi)).asObject();
JsonValue browser = pkg.get("browser");
if (browser != null) {
if (browser.isBoolean() && !browser.asBoolean()) {
return falseFile;
}
if (browser.isString()) {
// Log.Info(this,"package.json browser entry map to "+browser.asString());
return new File(file, browser.asString());
}
if (browser.isObject()) {
String nodeModuleDir = file.getCanonicalPath();
JsonObject members = browser.asObject();
members.forEach(member -> {
String key = "browser_" + nodeModuleDir + "_" + member.getName();
alreadyResolved.put(key, member.getValue());
// System.out.println("put browser:"+key);
// System.out.println(" val:"+member.getValue());
});
} else {
Log.Warn(this, "unrecognized 'browser' entry in package.json, " + file.getCanonicalPath());
return null;
}
}
String main = pkg.getString("main", null);
if (main != null) {
if (!main.endsWith(".js"))
// omg
main = main + ".js";
File newF = new File(file, main);
return newF;
}
File indexf = new File(file, "index.js");
if (indexf.exists()) {
return indexf;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (new File(file, "index.js").exists()) {
return new File(file, "index.js");
} else if (new File(file.getParentFile(), file.getName() + ".js").exists()) {
return new File(file.getParentFile(), file.getName() + ".js");
}
return null;
}
use of com.eclipsesource.json.JsonValue in project kontraktor by RuedigerMoeller.
the class JSXIntrinsicTranspiler method processJSX.
protected byte[] processJSX(boolean dev, File f, FileResolver resolver, Map<String, Object> alreadyResolved) {
try {
boolean isInitialIndexJSX = "index.jsx".equals(f.getName());
if (isInitialIndexJSX) {
jnpmConfigFileCached = null;
if (dev) {
readFiles = new ArrayList<>();
nodeTopLevelImports = new HashMap<>();
if (watcher != null) {
// watcher.stopWatching(); is now singleton
watcher = null;
}
}
}
NodeLibNameResolver nodeLibNameResolver = createNodeLibNameResolver(resolver);
JSXGenerator.ParseResult result = JSXGenerator.process(f, dev, nodeLibNameResolver, getConfig());
if (dev) {
if (isNotInNodeModules(f)) {
String finalLibName = nodeLibNameResolver.getFinalLibName(f, resolver, f.getName());
readFiles.add(new WatchedFile(f, this, resolver, finalLibName));
}
}
List<ImportSpec> specs = result.getImports();
byte[] res = result.getFiledata();
if (isInitialIndexJSX) {
alreadyResolved.put("JSXIndexStart", System.currentTimeMillis());
ByteArrayOutputStream baos = new ByteArrayOutputStream(1_000_000);
baos.write((getInitialShims() + "\n").getBytes("UTF-8"));
alreadyResolved.put("JSXIndex", baos);
}
ByteArrayOutputStream indexBaos = (ByteArrayOutputStream) alreadyResolved.get("JSXIndex");
if (alreadyResolved.get("_Ignored") == null) {
alreadyResolved.put("_Ignored", result.getIgnoredRequires());
}
Set ignoredRequires = (Set) alreadyResolved.get("_Ignored");
ignoredRequires.addAll(result.getIgnoredRequires());
for (int i = 0; i < specs.size(); i++) {
ImportSpec importSpec = specs.get(i);
File redirected = resolveImportSpec(f, importSpec, resolver, alreadyResolved, ignoredRequires);
if (redirected == null)
continue;
}
ByteArrayOutputStream mainBao = dev ? new ByteArrayOutputStream(20_000) : indexBaos;
if (result.generateESWrap())
mainBao.write(generateImportPrologue(result, resolver).getBytes("UTF-8"));
if (result.generateCommonJSWrap())
mainBao.write(generateCommonJSPrologue(f, result, resolver).getBytes("UTF-8"));
mainBao.write(res);
if (result.generateESWrap())
mainBao.write(generateImportEnd(result, resolver).getBytes("UTF-8"));
if (result.generateCommonJSWrap())
mainBao.write(generateCommonJSEnd(f, result, resolver).getBytes("UTF-8"));
if (dev) {
String dirName = "_node_modules";
if (isNotInNodeModules(f)) {
dirName = "_appsrc";
}
String name = constructLibName(f, resolver) + ".transpiled";
resolver.install("/" + dirName + "/" + name, mainBao.toByteArray());
indexBaos.write(("document.write( '<script src=\"" + dirName + "/" + name + "\"></script>');\n").getBytes("UTF-8"));
}
if (isInitialIndexJSX) {
if (dev) {
indexBaos.write("document.write('<script>_kreporterr = true; kinitfuns.forEach( fun => fun() );</script>')\n".getBytes("UTF-8"));
watcher = FileWatcher.get();
watcher.setFiles(readFiles);
if (dev && getConfig().isGeneratePackageDotJson()) {
System.out.println("============================= TOP LEVEL IMPORTS ======================================");
nodeTopLevelImports.forEach((s, fi) -> {
try {
JsonValue packjson = Json.parse(new FileReader(new File(fi, "package.json")));
String version = packjson.asObject().getString("version", "*");
System.out.println("\"" + s + "\":" + "\"" + version + "\",");
} catch (IOException e) {
e.printStackTrace();
}
});
}
} else
indexBaos.write("_kreporterr = true; kinitfuns.forEach( fun => fun() );\n".getBytes("UTF-8"));
Long tim = (Long) alreadyResolved.get("JSXIndexStart");
Log.Info(this, "Transpilation time:" + (System.currentTimeMillis() - tim) / 1000.0);
return indexBaos.toByteArray();
}
return mainBao.toByteArray();
} catch (Exception e) {
Log.Error(this, e);
StringWriter out = new StringWriter();
e.printStackTrace(new PrintWriter(out));
try {
return out.getBuffer().toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
return new byte[0];
}
use of com.eclipsesource.json.JsonValue in project zencash-swing-wallet-ui by ZencashOfficial.
the class MessagingPanel method importContactIdentity.
/**
* Imports a contact's identity from file.
*/
public void importContactIdentity() {
try {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Import contact's messaging identity from file...");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(this.parentFrame);
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File f = fileChooser.getSelectedFile();
JsonObject topIdentityObject = null;
Reader r = null;
try {
r = new InputStreamReader(new FileInputStream(f), "UTF-8");
topIdentityObject = Util.parseJsonObject(r);
} finally {
if (r != null) {
r.close();
}
}
// Validate the fields inside the objects, make sure this is indeed an identity
// verify mandatory etc.
JsonValue innerValue = topIdentityObject.get("zenmessagingidentity");
JsonObject innerIdentity = (innerValue != null) ? innerValue.asObject() : null;
if ((innerValue == null) || (innerIdentity == null) || (innerIdentity.get("nickname") == null) || (innerIdentity.get("sendreceiveaddress") == null) || (innerIdentity.get("senderidaddress") == null)) {
JOptionPane.showMessageDialog(this.parentFrame, "The selected JSON file has a wrong format or is not a messaging identity file!", "Messaging identity has wrong format!", JOptionPane.ERROR_MESSAGE);
return;
}
MessagingIdentity contactIdentity = new MessagingIdentity(innerIdentity);
// Search through the existing contact identities, to make sure we are not adding it a second time
for (MessagingIdentity mi : this.messagingStorage.getContactIdentities(false)) {
if (mi.isIdenticalTo(contactIdentity)) {
int choice = JOptionPane.showConfirmDialog(this.parentFrame, "There is already a contact in your contact list with the same identity. \n\n" + "Existing contact identity: " + mi.getDiplayString() + "\n" + "Contact identity being imported: " + contactIdentity.getDiplayString() + "\n\n" + "Two identities are consiered the same if their T/Z addresses are the same. \n" + "Do you want to replace the details of the existing messaging identity, with\n" + "the one being imported?", "The same contact identity is already available", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
this.messagingStorage.updateContactIdentityForSenderIDAddress(contactIdentity.getSenderidaddress(), contactIdentity);
JOptionPane.showMessageDialog(this.parentFrame, "Your contact's messaging identity has been successfully updated.\n", "Messaging identity is successfully updated", JOptionPane.INFORMATION_MESSAGE);
this.contactList.reloadMessagingIdentities();
}
// In any case - not a new identity to add
return;
}
}
// Check for the existence of an "Unknown" type of identity already - that could be
// updated. Search can be done by T address only.
MessagingIdentity existingUnknownID = this.messagingStorage.getContactIdentityForSenderIDAddress(contactIdentity.getSenderidaddress());
if (existingUnknownID != null) {
int choice = JOptionPane.showConfirmDialog(this.parentFrame, "There is a contact in your contact list with the same sender identification address \n" + "but with yet unknown/not yet imported full identity:\n\n" + "Existing contact identity: " + existingUnknownID.getDiplayString() + "\n" + "Contact identity being imported: " + contactIdentity.getDiplayString() + "\n\n" + "Please confirm that you want to update the details of the existing contact identity\n" + "with the one being imported?", "Contact with the same sender identification address is already available.", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
this.messagingStorage.updateContactIdentityForSenderIDAddress(contactIdentity.getSenderidaddress(), contactIdentity);
JOptionPane.showMessageDialog(this.parentFrame, "Your contact's messaging identity has been successfully updated.\n", "Messaging identity is successfully updated", JOptionPane.INFORMATION_MESSAGE);
this.contactList.reloadMessagingIdentities();
}
return;
}
// Add the new identity normally!
this.messagingStorage.addContactIdentity(contactIdentity);
int sendIDChoice = JOptionPane.showConfirmDialog(this.parentFrame, "Your contact's messaging identity has been successfully imported: \n" + contactIdentity.getDiplayString() + "\n" + "You can now send and receive messages from this contact. Do you wish\n" + "to send a limited sub-set of your contact details to this new contact\n" + "as a special message?\n\n" + "This will allow him/her to establish contact with you without manaully\n" + "importing your messaging identity (the way you imported his identity).", "Successfully imported. Send your identity over?", JOptionPane.YES_NO_OPTION);
this.contactList.reloadMessagingIdentities();
if (sendIDChoice == JOptionPane.YES_OPTION) {
this.sendIdentityMessageTo(contactIdentity);
}
} catch (Exception ex) {
Log.error("Unexpected error in importing contact messaging identity from file!", ex);
this.errorReporter.reportError(ex, false);
}
}
use of com.eclipsesource.json.JsonValue in project zencash-swing-wallet-ui by ZencashOfficial.
the class ZCashClientCaller method executeCommandAndGetJsonValue.
private JsonValue executeCommandAndGetJsonValue(String command1, String command2, String command3) throws WalletCallException, IOException, InterruptedException {
String strResponse = this.executeCommandAndGetSingleStringResponse(command1, command2, command3);
JsonValue response = null;
try {
response = Json.parse(strResponse);
} catch (ParseException pe) {
throw new WalletCallException(strResponse + "\n" + pe.getMessage() + "\n", pe);
}
return response;
}
use of com.eclipsesource.json.JsonValue in project hazelcast by hazelcast.
the class JsonUtil method getObject.
/**
* Returns a field in a Json object as an object.
* Throws IllegalArgumentException if the field value is null.
*
* @param object the Json object
* @param field the field in the Json object to return
* @return the Json field value as a Json object
*/
public static JsonObject getObject(JsonObject object, String field) {
final JsonValue value = object.get(field);
throwExceptionIfNull(value, field);
return value.asObject();
}
Aggregations