private void emitConfig(ModuleMetaModel metaModel) { JSON descriptor = new JSON(); // Application configs for (ApplicationMetaModel application : metaModel.getChildren(ApplicationMetaModel.class)) { // metaModel.processingContext.log( "Emitting application " + application.getHandle() + " config"); // Recycle descriptor.clear(); // Emit config for (ApplicationMetaModelPlugin plugin : context.getPlugins()) { JSON pluginDescriptor = plugin.getDescriptor(application); if (pluginDescriptor != null) { descriptor.set(plugin.getName(), pluginDescriptor); } } // Writer writer = null; try { FileObject fo = metaModel.processingContext.createResource( StandardLocation.CLASS_OUTPUT, application.getName(), "config.json"); writer = fo.openWriter(); descriptor.toString(writer, 2); } catch (IOException e) { throw ApplicationMetaModel.CANNOT_WRITE_APPLICATION_CONFIG.failure( e, metaModel.processingContext.get(application.getHandle()), application.getName()); } finally { Tools.safeClose(writer); } } }
private static <A extends Appendable> A toString( Object o, A appendable, final int margin, final int indent) throws IOException { if (o == null) { appendable.append("null"); } else if (o instanceof JSON) { JSON m = (JSON) o; appendable.append('{'); for (Iterator<? extends Map.Entry<?, ?>> iterator = m.entries.entrySet().iterator(); iterator.hasNext(); ) { Map.Entry<?, ?> entry = iterator.next(); if (indent > 0) { appendable.append('\n'); pad(appendable, margin + indent); } appendable.append('"'); appendable.append(entry.getKey().toString()); appendable.append("\":"); toString(entry.getValue(), appendable, margin + indent, indent); if (iterator.hasNext()) { appendable.append(','); } else if (indent > 0) { appendable.append('\n'); pad(appendable, margin); } } appendable.append('}'); } else if (o instanceof List) { appendable.append('['); List<?> list = (List<?>) o; for (Iterator<?> i = list.iterator(); i.hasNext(); ) { Object e = i.next(); toString(e, appendable, margin + indent, indent); if (i.hasNext()) { appendable.append(','); } } appendable.append("]"); } else if (o instanceof Boolean || o instanceof Number) { appendable.append(o.toString()); } else { appendable.append('"'); CharSequence s = o instanceof CharSequence ? (CharSequence) o : o.toString(); for (int i = 0, len = s.length(); i < len; i++) { char c = s.charAt(i); switch (c) { case '"': appendable.append("\\\""); break; case '\n': appendable.append("\\n"); break; case '\r': appendable.append("\\r"); break; case '\b': appendable.append("\\b"); break; case '\f': appendable.append("\\f"); break; case '\t': appendable.append("\\t"); break; default: appendable.append(c); } } appendable.append('"'); } // return appendable; }