use of java.io.StringWriter in project bigbluebutton by bigbluebutton.
the class StackTraceUtil method getStackTrace.
public static String getStackTrace(Throwable aThrowable) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
aThrowable.printStackTrace(printWriter);
return result.toString();
}
use of java.io.StringWriter in project jvm-tools by aragozin.
the class FlameCheck method check.
@Test
public void check() throws IOException {
FlameGraphGenerator fg = new FlameGraphGenerator();
StackTraceReader r = read();
if (!r.isLoaded()) {
r.loadNext();
}
while (r.isLoaded()) {
fg.feed(r.getStackTrace());
r.loadNext();
}
StringWriter sw = new StringWriter();
fg.renderSVG("Flame Graph", 1200, sw);
FileWriter fw = new FileWriter(new File("target/flame.svg"));
fw.append(sw.getBuffer());
fw.close();
}
use of java.io.StringWriter in project solo by b3log.
the class CommentProcessor method addArticleComment.
/**
* Adds a comment to an article.
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "oId": generatedCommentId,
* "sc": "COMMENT_ARTICLE_SUCC",
* "commentDate": "", // yyyy/MM/dd HH:mm:ss
* "commentSharpURL": "",
* "commentThumbnailURL": "",
* "commentOriginalCommentName": "", // if exists this key, the comment is an reply
* "commentContent": "" // HTML
* }
* </pre>
* </p>
*
* @param context the specified context, including a request json object, for example,
* "captcha": "",
* "oId": articleId,
* "commentName": "",
* "commentEmail": "",
* "commentURL": "",
* "commentContent": "",
* "commentOriginalCommentId": "" // optional, if exists this key, the comment is an reply
* @throws ServletException servlet exception
* @throws IOException io exception
*/
@RequestProcessing(value = "/add-article-comment.do", method = HTTPRequestMethod.POST)
public void addArticleComment(final HTTPRequestContext context) throws ServletException, IOException {
final HttpServletRequest httpServletRequest = context.getRequest();
final HttpServletResponse httpServletResponse = context.getResponse();
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(httpServletRequest, httpServletResponse);
requestJSONObject.put(Common.TYPE, Article.ARTICLE);
fillCommenter(requestJSONObject, httpServletRequest, httpServletResponse);
final JSONObject jsonObject = commentMgmtService.checkAddCommentRequest(requestJSONObject);
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
renderer.setJSONObject(jsonObject);
if (!jsonObject.optBoolean(Keys.STATUS_CODE)) {
LOGGER.log(Level.WARN, "Can't add comment[msg={0}]", jsonObject.optString(Keys.MSG));
return;
}
final HttpSession session = httpServletRequest.getSession(false);
if (null == session) {
jsonObject.put(Keys.STATUS_CODE, false);
jsonObject.put(Keys.MSG, langPropsService.get("captchaErrorLabel"));
return;
}
final String storedCaptcha = (String) session.getAttribute(CaptchaProcessor.CAPTCHA);
session.removeAttribute(CaptchaProcessor.CAPTCHA);
if (!userQueryService.isLoggedIn(httpServletRequest, httpServletResponse)) {
final String captcha = requestJSONObject.optString(CaptchaProcessor.CAPTCHA);
if (null == storedCaptcha || !storedCaptcha.equals(captcha)) {
jsonObject.put(Keys.STATUS_CODE, false);
jsonObject.put(Keys.MSG, langPropsService.get("captchaErrorLabel"));
return;
}
}
try {
final JSONObject addResult = commentMgmtService.addArticleComment(requestJSONObject);
final Map<String, Object> dataModel = new HashMap<>();
dataModel.put(Comment.COMMENT, addResult);
final JSONObject article = addResult.optJSONObject(Article.ARTICLE);
article.put(Common.COMMENTABLE, addResult.opt(Common.COMMENTABLE));
article.put(Common.PERMALINK, addResult.opt(Common.PERMALINK));
dataModel.put(Article.ARTICLE, article);
// https://github.com/b3log/solo/issues/12246
try {
final String skinDirName = (String) httpServletRequest.getAttribute(Keys.TEMAPLTE_DIR_NAME);
final Template template = Templates.MAIN_CFG.getTemplate("common-comment.ftl");
final JSONObject preference = preferenceQueryService.getPreference();
Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), skinDirName, dataModel);
Keys.fillServer(dataModel);
final StringWriter stringWriter = new StringWriter();
template.process(dataModel, stringWriter);
stringWriter.close();
addResult.put("cmtTpl", stringWriter.toString());
} catch (final Exception e) {
// 1.9.0 向后兼容
}
addResult.put(Keys.STATUS_CODE, true);
renderer.setJSONObject(addResult);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Can not add comment on article", e);
jsonObject.put(Keys.STATUS_CODE, false);
jsonObject.put(Keys.MSG, langPropsService.get("addFailLabel"));
}
}
use of java.io.StringWriter in project CoreNLP by stanfordnlp.
the class SemanticGraphUtils method semgrexFromGraphHelper.
/**
* Recursive call to generate the Semgrex pattern based off of this SemanticGraph.
* nodeValuesTranformation is a function that converts a vertex (IndexedWord) to the value. For an example, see {@code semgrexFromGraph}
* function implementations.
*/
protected static String semgrexFromGraphHelper(IndexedWord vertice, SemanticGraph sg, Set<IndexedWord> tabu, Set<SemanticGraphEdge> seenEdges, boolean useWordAsLabel, boolean nameEdges, Collection<IndexedWord> wildcardNodes, Map<IndexedWord, String> nodeNameMap, boolean orderedNodes, Function<IndexedWord, String> nodeValuesTransformation) {
StringWriter buf = new StringWriter();
// be labeled, but this may change later.
if (wildcardNodes != null && wildcardNodes.contains(vertice)) {
buf.append("{}");
} else {
String vertexStr = nodeValuesTransformation.apply(vertice);
if (vertexStr != null && !vertexStr.isEmpty()) {
buf.append(vertexStr);
}
// buf.append("{");
// int i = 0;
// for(String corekey: useNodeCoreAnnotations){
// AnnotationLookup.KeyLookup lookup = AnnotationLookup.getCoreKey(corekey);
// assert lookup != null : "Invalid key " + corekey;
// if(i > 0)
// buf.append("; ");
// String value = vertice.containsKey(lookup.coreKey) ? vertice.get(lookup.coreKey).toString() : "null";
// buf.append(corekey+":"+nodeValuesTransformation.apply(value));
// i++;
// }
// if (useTag) {
//
// buf.append("tag:"); buf.append(vertice.tag());
// if (useWord)
// buf.append(";");
// }
// if (useWord) {
// buf.append("word:"); buf.append(wordTransformation.apply(vertice.word()));
// }
// buf.append("}");
}
if (nodeNameMap != null) {
buf.append("=");
buf.append(nodeNameMap.get(vertice));
buf.append(" ");
} else if (useWordAsLabel) {
buf.append("=");
buf.append(sanitizeForSemgrexName(vertice.word()));
buf.append(" ");
}
tabu.add(vertice);
Iterable<SemanticGraphEdge> edgeIter = null;
if (!orderedNodes) {
edgeIter = sg.outgoingEdgeIterable(vertice);
} else {
edgeIter = CollectionUtils.sorted(sg.outgoingEdgeList(vertice), (arg0, arg1) -> (arg0.getRelation().toString().compareTo(arg1.getRelation().toString())));
}
// If we will proceed down that node, add parens if it will continue recursing down.
for (SemanticGraphEdge edge : edgeIter) {
seenEdges.add(edge);
IndexedWord tgtVert = edge.getDependent();
boolean applyParens = sg.outDegree(tgtVert) > 0 && !tabu.contains(tgtVert);
buf.append(" >");
buf.append(edge.getRelation().toString());
if (nameEdges) {
buf.append("=E");
buf.write(String.valueOf(seenEdges.size()));
}
buf.append(" ");
if (applyParens)
buf.append("(");
if (tabu.contains(tgtVert)) {
buf.append("{tag:");
buf.append(tgtVert.tag());
buf.append("}");
if (useWordAsLabel) {
buf.append("=");
buf.append(tgtVert.word());
buf.append(" ");
}
} else {
buf.append(semgrexFromGraphHelper(tgtVert, sg, tabu, seenEdges, useWordAsLabel, nameEdges, wildcardNodes, nodeNameMap, orderedNodes, nodeValuesTransformation));
if (applyParens)
buf.append(")");
}
}
return buf.toString();
}
use of java.io.StringWriter in project CoreNLP by stanfordnlp.
the class AddDep method cheapWordToString.
// Simple mapping of all the stuff we care about (until IndexedFeatureLabel --> CoreLabel map pain is fixed)
/**
* This converts the node into a simple string based representation.
* NOTE: this is extremely brittle, and presumes values do not contain delimiters
*/
public static String cheapWordToString(IndexedWord node) {
StringWriter buf = new StringWriter();
buf.write("{");
buf.write(WORD_KEY);
buf.write(TUPLE_DELIMITER);
buf.write(nullShield(node.word()));
buf.write(ATOM_DELIMITER);
buf.write(LEMMA_KEY);
buf.write(TUPLE_DELIMITER);
buf.write(nullShield(node.lemma()));
buf.write(ATOM_DELIMITER);
buf.write(POS_KEY);
buf.write(TUPLE_DELIMITER);
buf.write(nullShield(node.tag()));
buf.write(ATOM_DELIMITER);
buf.write(VALUE_KEY);
buf.write(TUPLE_DELIMITER);
buf.write(nullShield(node.value()));
buf.write(ATOM_DELIMITER);
buf.write(CURRENT_KEY);
buf.write(TUPLE_DELIMITER);
buf.write(nullShield(node.originalText()));
buf.write("}");
return buf.toString();
}
Aggregations