List of usage examples for java.lang StringBuilder insert
@Override public StringBuilder insert(int offset, double d)
From source file:it.unibas.spicy.model.algebra.query.operators.xquery.XQUtility.java
public static String toStringWithoutVirtualNodes(VariablePathExpression variablePath, INode root) { PathExpression absolutePath = variablePath.getAbsolutePath(); List<INode> absolutePathNodes = new GeneratePathExpression() .generatePathStepNodes(absolutePath.getPathSteps(), root); List<String> variablePathSteps = variablePath.getPathSteps(); StringBuilder result = new StringBuilder(); int offset = absolutePathNodes.size() - variablePathSteps.size(); for (int i = variablePathSteps.size() - 1; i > 0; i--) { INode pathNode = absolutePathNodes.get(i + offset); if (!pathNode.isVirtual()) { if (result.length() != 0) { result.insert(0, "/"); }//from w ww. ja v a2 s.c o m result.insert(0, variablePathSteps.get(i)); } } return result.toString(); }
From source file:com.appspresso.api.AxLog.java
private static String getTagName(Class<?> klass) { if (klass == null) { return DEF_TAG; }//from www. j a v a2 s . com StringBuilder result = new StringBuilder(MAX_TAG_LEN); String className = klass.getSimpleName(); result.append(className); String packageName = klass.getPackage().getName(); for (String token : packageName.split(".")) { if (className.length() > MAX_TAG_LEN) { result.insert(0, ".."); break; } result.insert(0, token.charAt(0)).insert(0, '.'); } return result.toString(); }
From source file:de.xaniox.heavyspleef.core.flag.FlagManager.java
public static String generatePath(Flag flagAnnotation) { //Generate the full path StringBuilder pathBuilder = new StringBuilder(); Flag lastParentFlagData = flagAnnotation; while (lastParentFlagData != null) { pathBuilder.insert(0, lastParentFlagData.name()); Class<? extends AbstractFlag<?>> parentClass = lastParentFlagData.parent(); lastParentFlagData = parentClass.getAnnotation(Flag.class); if (lastParentFlagData != null) { pathBuilder.insert(0, ":"); }/*from w w w. ja v a2s. co m*/ } String path = pathBuilder.toString(); return path; }
From source file:com.googlecode.jsonplugin.JSONUtil.java
public static void writeJSONToResponse(SerializationParams serializationParams) throws IOException { StringBuilder stringBuilder = new StringBuilder(); if (TextUtils.stringSet(serializationParams.getSerializedJSON())) stringBuilder.append(serializationParams.getSerializedJSON()); if (TextUtils.stringSet(serializationParams.getWrapPrefix())) stringBuilder.insert(0, serializationParams.getWrapPrefix()); else if (serializationParams.isWrapWithComments()) { stringBuilder.insert(0, "/* "); stringBuilder.append(" */"); } else if (serializationParams.isPrefix()) stringBuilder.insert(0, "{}&& "); if (TextUtils.stringSet(serializationParams.getWrapSuffix())) stringBuilder.append(serializationParams.getWrapSuffix()); String json = stringBuilder.toString(); if (log.isDebugEnabled()) { log.debug("[JSON]" + json); }//from w ww . j av a 2 s . co m HttpServletResponse response = serializationParams.getResponse(); //status or error code if (serializationParams.getStatusCode() > 0) response.setStatus(serializationParams.getStatusCode()); else if (serializationParams.getErrorCode() > 0) response.sendError(serializationParams.getErrorCode()); //content type if (serializationParams.isSmd()) response.setContentType("application/json-rpc;charset=" + serializationParams.getEncoding()); else response.setContentType( serializationParams.getContentType() + ";charset=" + serializationParams.getEncoding()); if (serializationParams.isNoCache()) { response.setHeader("Cache-Control", "no-cache"); response.setHeader("Expires", "0"); response.setHeader("Pragma", "No-cache"); } if (serializationParams.isGzip()) { response.addHeader("Content-Encoding", "gzip"); GZIPOutputStream out = null; InputStream in = null; try { out = new GZIPOutputStream(response.getOutputStream()); in = new ByteArrayInputStream(json.getBytes()); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } finally { if (in != null) in.close(); if (out != null) { out.finish(); out.close(); } } } else { response.setContentLength(json.getBytes(serializationParams.getEncoding()).length); PrintWriter out = response.getWriter(); out.print(json); } }
From source file:net.vpc.common.jsf.FacesUtils.java
public static SelectItem createSelectItem(String value, String label, String styleClass, String style, String desc, boolean disabled) { StringBuilder sb = new StringBuilder(); if (style != null && style.length() > 0) { sb.append(" style='").append(style).append('\''); }/* w w w .j a v a2 s . c o m*/ if (styleClass != null && styleClass.length() > 0) { sb.append(" class='").append(styleClass).append('\''); } if (sb.length() > 0) { sb.insert(0, "<span "); sb.append(">"); sb.append(StringEscapeUtils.escapeHtml4(label)); sb.append("</span>"); return new SelectItem(value, sb.toString(), desc, disabled, false); } else { return new SelectItem(value, label, desc, disabled, true); } }
From source file:com.qwazr.utils.LinkUtils.java
public final static String urlHostPathWrapReduce(String url, int maxSize) { URL u;/*from w ww. ja v a 2 s . c o m*/ try { u = new URL(url); } catch (MalformedURLException e) { return url; } String path = StringUtils.fastConcat(u.getHost(), '/', u.getPath()); String[] frags = StringUtils.split(path, '/'); if (frags.length < 2) return path; int startPos = 1; int endPos = frags.length - 2; StringBuilder sbStart = new StringBuilder(frags[0]); StringBuilder sbEnd = new StringBuilder(frags[frags.length - 1]); int length = sbStart.length() + sbEnd.length(); for (;;) { boolean bHandled = false; if (startPos != -1 && startPos < endPos) { if (frags[startPos].length() + length < maxSize) { sbStart.append('/'); sbStart.append(frags[startPos++]); bHandled = true; } } if (endPos != -1 && endPos > startPos) { if (frags[endPos].length() + length < maxSize) { sbEnd.insert(0, '/'); sbEnd.insert(0, frags[endPos--]); bHandled = true; } } if (!bHandled) break; } return StringUtils.fastConcat(sbStart, "//", sbEnd); }
From source file:org.cocos2dx.lib.Cocos2dxBitmap.java
private static String refactorString(String str) { // Avoid error when content is "" if (str.compareTo("") == 0) { return " "; }//w w w .j av a 2 s . c o m /* * If the font of "\n" is "" or "\n", insert " " in front of it. * * For example: * "\nabc" -> " \nabc" * "\nabc\n\n" -> " \nabc\n \n" */ StringBuilder strBuilder = new StringBuilder(str); int start = 0; int index = strBuilder.indexOf("\n"); while (index != -1) { if (index == 0 || strBuilder.charAt(index - 1) == '\n') { strBuilder.insert(start, " "); start = index + 2; } else { start = index + 1; } if (start > strBuilder.length() || index == strBuilder.length()) { break; } index = strBuilder.indexOf("\n", start); } return strBuilder.toString(); }
From source file:com.sri.ai.expresso.core.DefaultSyntaxLeaf.java
/** * Escape a String that is intended to be converted to a symbol of value * string so that it can be parsed safely. * //from w ww . j a v a 2s. co m * @param aStringSymbol * a string that is intended to be used to instantiate a string * valued symbol. * @return a string that can be safely parsed with surrounding ' and * embedded escape characters, as necessary. */ public static String makeStringValuedSymbolParseSafe(String aStringSymbol) { String result = aStringSymbol; boolean containsSpace = aStringSymbol.contains(" "); // Note: trailing ''' are allowed for symbol names, e.g. aSymbol' boolean containsSingleQuote = StringUtils.stripEnd(aStringSymbol, "'").contains("'"); if (containsSpace || containsSingleQuote) { StringBuilder sb = new StringBuilder(); if (!aStringSymbol.startsWith("'")) { sb.insert(0, "'"); } sb.append(aStringSymbol); if (!aStringSymbol.endsWith("'")) { sb.append("'"); } String substring; for (int i = 1; i < sb.length() - 1; i++) { substring = sb.substring(i, i + 1); if ("'".equals(substring)) { if (!escapedAlready(sb, i)) { sb.insert(i, "\\"); // move forward an additional 1 as have inserted i++; } } } result = sb.toString(); } return result; }
From source file:com.brewcrewfoo.performance.util.Helpers.java
public static void get_assetsScript(String fn, Context c, String prefix, String postfix) { byte[] buffer; final AssetManager assetManager = c.getAssets(); try {// w w w .j a v a2s.c om InputStream f = assetManager.open(fn); buffer = new byte[f.available()]; f.read(buffer); f.close(); final String s = new String(buffer); final StringBuilder sb = new StringBuilder(s); if (!postfix.equals("")) { sb.append("\n\n").append(postfix); } if (!prefix.equals("")) { sb.insert(0, prefix + "\n"); } sb.insert(0, "#!" + Helpers.binExist("sh") + "\n\n"); try { FileOutputStream fos; fos = c.openFileOutput(fn, Context.MODE_PRIVATE); fos.write(sb.toString().getBytes()); fos.close(); } catch (IOException e) { Log.d(TAG, "error write " + fn + " file"); e.printStackTrace(); } } catch (IOException e) { Log.d(TAG, "error read " + fn + " file"); e.printStackTrace(); } }
From source file:com.bedatadriven.renjin.appengine.AppEngineContextFactory.java
@VisibleForTesting static String findHomeDirectory(File servletContextRoot, String sexpClassPath) throws IOException { LOG.fine("Found SEXP in '" + sexpClassPath); File jarFile = jarFileFromResource(sexpClassPath); StringBuilder homePath = new StringBuilder(); homePath.append('/').append(jarFile.getName()).append("!/r"); File parent = jarFile.getParentFile(); while (!servletContextRoot.equals(parent)) { if (parent == null) { throw new IllegalStateException( "Expected the renjin-core jar to be in the WEB-INF, bound found it in:\n" + jarFile.toString() + "\nAre you sure you are running in a servlet environment?"); }//w w w . j av a2 s . c o m homePath.insert(0, parent.getName()); homePath.insert(0, '/'); parent = parent.getParentFile(); } homePath.insert(0, "jar:file://"); return homePath.toString(); }