Java Code Examples for org.osgl.util.S#Buffer

The following examples show how to use org.osgl.util.S#Buffer . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActErrorResult.java    From actframework with Apache License 2.0 6 votes vote down vote up
public static Result of(Throwable t) {
    if (t instanceof Result) {
        return (Result) t;
    }
    if (t instanceof PersistenceException && null != t.getCause()) {
        t = t.getCause();
    }
    if (t instanceof ConstraintViolationException) {
        ConstraintViolationException e = (ConstraintViolationException) t;
        Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
        Map<String, ConstraintViolation> lookup = new HashMap<>();
        for (ConstraintViolation v : violations) {
            lookup.put(S.pathConcat(v.getRootBeanClass().getSimpleName(), '.', v.getPropertyPath().toString()), v);
        }
        S.Buffer buf = S.buffer();
        ActionContext.buildViolationMessage(lookup, buf, ";");
        String msg = buf.toString();
        return ActBadRequest.create(msg);
    } else if (t instanceof org.rythmengine.exception.RythmException) {
        return Act.isDev() ? new RythmTemplateException((org.rythmengine.exception.RythmException) t) : ErrorResult.of(INTERNAL_SERVER_ERROR);
    } else if (t instanceof AsmException) {
        return new ActErrorResult((AsmException) t, true);
    }
    $.Function<Throwable, Result> transformer = transformerOf(t);
    return null == transformer ? new ActErrorResult(t) : transformer.apply(t);
}
 
Example 2
Source File: CliOverHttpContext.java    From actframework with Apache License 2.0 6 votes vote down vote up
private static String line(ActionContext actionContext) {
    String cmd = null;
    S.Buffer sb = S.buffer();
    for (String s : actionContext.paramKeys()) {
        if ("cmd".equals(s)) {
            cmd = actionContext.paramVal(s);
        } else if (s.startsWith("-")) {
            String val = actionContext.paramVal(s);
            if (S.notBlank(val)) {
                val = val.replaceAll("[\n\r]+", "<br/>").trim();
                if (val.contains(" ")) {
                    char quote = val.contains("\"") ? '\'' : '\\';
                    val = S.wrap(val, quote);
                }
                if (s.contains(",")) {
                    s = S.before(s, ",");
                }
                sb.append(s).append(" ").append(val).append(" ");
            }
        }
    }
    E.illegalArgumentIf(null == cmd, "cmd param required");
    return S.builder(cmd).append(" ").append(sb.toString()).toString();
}
 
Example 3
Source File: JsonDtoClassGenerator.java    From actframework with Apache License 2.0 6 votes vote down vote up
private String typeDesc(BeanSpec spec) {
    String root = classDesc(spec.rawType());
    List<java.lang.reflect.Type> typeParams = spec.typeParams();
    if (typeParams.isEmpty()) {
        return root;
    }
    S.Buffer sb = S.newBuffer("<");
    for (java.lang.reflect.Type type : typeParams) {
        BeanSpec specx = BeanSpec.of(type, null, spec.injector(), typeParamLookup);
        sb.append(typeDesc(specx));
    }
    sb.append(">");
    FastStr str = FastStr.of(root);
    str = str.take(str.length() - 1).append(sb.toString()).append(";");
    return str.toString();
}
 
Example 4
Source File: GeneralAnnoInfo.java    From actframework with Apache License 2.0 6 votes vote down vote up
@Override
public String toString() {
    C.List<String> keys = C.newList(attributes.keySet()).append(listAttributes.keySet()).sorted();
    S.Buffer sb = S.newBuffer("@").append(type.getClassName()).append("(");
    for (String key: keys) {
        Object v = attributes.get(key);
        if (null == v) {
            v = listAttributes.get(v);
        }
        sb.append(key).append("=").append(v).append(", ");
    }
    if (!keys.isEmpty()) {
        sb.delete(sb.length() - 2, sb.length());
    }
    sb.append(")");
    return sb.toString();
}
 
Example 5
Source File: AppCompiler.java    From actframework with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isPackage(char[][] parentPackageName, char[] packageName) {
    S.Buffer sb = S.buffer();
    if (parentPackageName != null) {
        for (char[] p : parentPackageName) {
            sb.append(new String(p));
            sb.append(".");
        }
    }
    sb.append(new String(packageName));
    String name = sb.toString();
    if (packagesCache.containsKey(name)) {
        return packagesCache.get(name);
    }
    if (classLoader.source(name) != null) {
        packagesCache.put(name, false);
        return false;
    }
    // Check if there is a .java or .class for this resource
    if (classLoader.bytecode(name) != null) {
        packagesCache.put(name, false);
        return false;
    }
    packagesCache.put(name, true);
    return true;
}
 
Example 6
Source File: Gh103.java    From java-tool with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    S.Buffer buf = S.buffer();
    buf.write("abc");
    eq("abc", buf.toString());

    buf = S.buffer();
    buf.write(new char[]{'a', 'b', 'c'});
    eq("abc", buf.toString());

    buf = S.buffer();
    buf.write(66);
    eq("B", buf.toString());

    buf = S.buffer();
    buf.write("abcdef", 2, 3);
    eq("cde", buf.toString());

}
 
Example 7
Source File: ControllerClassMetaInfo.java    From actframework with Apache License 2.0 6 votes vote down vote up
public String templateContext() {
    if (null != parent) {
        if (S.notBlank(templateContext) && templateContext.length() > 1 && templateContext.startsWith("/")) {
            return templateContext;
        }
        String parentContextPath = parent.templateContext();
        if (null == templateContext) {
            return parentContextPath;
        }
        if (null == parentContextPath) {
            return templateContext;
        }
        S.Buffer sb = S.newBuffer(parentContextPath);
        if (parentContextPath.endsWith("/")) {
            sb.deleteCharAt(sb.length() - 1);
        }
        if (!templateContext.startsWith("/")) {
            sb.append("/");
        }
        sb.append(templateContext);
        return sb.toString();
    }
    return templateContext;
}
 
Example 8
Source File: TemplatePathResolver.java    From actframework with Apache License 2.0 6 votes vote down vote up
public final String resolveWithContextMethodPath(ActContext context) {
    String methodPath = context.methodPath();
    String path = context.templatePath();
    String[] sa = path.split("\\.");
    int level = sa.length + 1;
    S.Buffer sb;
    if (S.notBlank(methodPath)) {
        while (--level > 0) {
            methodPath = S.beforeLast(methodPath, ".");
        }
        sb = S.newBuffer(methodPath);
    } else {
        sb = S.newBuffer();
    }
    if (!path.startsWith("/")) {
        sb.append("/");
    }
    path = sb.append(path).toString().replace('.', '/');
    return resolveTemplatePath(path, context);
}
 
Example 9
Source File: SimpleASCIITableImpl.java    From actframework with Apache License 2.0 5 votes vote down vote up
private String getRowDataBuf(int colCount, List<Integer> colMaxLenList, 
		String[] row, ASCIITableHeader[] headerObjs, boolean isHeader, int rowNo) {
	
	S.Buffer rowBuilder = S.buffer();
	String formattedData;
	int align;
	
	for (int i = 0 ; i < colCount ; i ++) {
	
		align = isHeader ? DEFAULT_HEADER_ALIGN : DEFAULT_DATA_ALIGN;
		
		if (headerObjs != null && i < headerObjs.length) {
			if (isHeader) {
				align = headerObjs[i].getHeaderAlign();
			} else {
				align = headerObjs[i].getDataAlign();
			}
		}
			 
		formattedData = i < row.length ? row[i] : ""; 
		
		//format = "| %" + colFormat.get(i) + "s ";
		formattedData = "| " + 
			getFormattedData(colMaxLenList.get(i), formattedData, align) + " ";
		
		if (i+1 == colCount) {
			formattedData += "|";
		}
		
		rowBuilder.append(formattedData);
	}
	
	String raw = rowBuilder.toString();
	if (rowNo % 2 == 0) {
		raw = Ansi.ansi().render("@|NEGATIVE_ON,FAINT " + raw + "|@").toString();
	} else {
		raw = Ansi.ansi().render("@|BOLD " + raw + "|@").toString();
	}
	return raw + "\n";
}
 
Example 10
Source File: GroupInterceptorMetaInfo.java    From actframework with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    S.Buffer sb = S.buffer();
    appendList("BEFORE", beforeList, sb);
    appendList("AFTER", afterList, sb);
    appendList("CATCH", catchList, sb);
    appendList("FINALLY", finallyList, sb);
    return sb.toString();
}
 
Example 11
Source File: TreeNodeFilter.java    From actframework with Apache License 2.0 5 votes vote down vote up
private static String path(List<? extends TreeNode> context, TreeNode theNode) {
    S.Buffer sb = S.buffer();
    for (TreeNode n : context) {
        sb.append(n.id()).append("/");
    }
    sb.append(theNode.label());
    return sb.toString();
}
 
Example 12
Source File: AppCompiler.java    From actframework with Apache License 2.0 5 votes vote down vote up
@Override
public NameEnvironmentAnswer findType(char[][] chars) {
    final S.Buffer result = S.buffer();
    for (int i = 0; i < chars.length; i++) {
        if (i != 0) {
            result.append('.');
        }
        result.append(chars[i]);
    }
    return findType(result.toString());
}
 
Example 13
Source File: AppCompiler.java    From actframework with Apache License 2.0 5 votes vote down vote up
@Override
public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
    final S.Buffer result = S.buffer();
    for (int i = 0; i < packageName.length; i++) {
        result.append(packageName[i]);
        result.append('.');
    }
    result.append(typeName);
    return findType(result.toString());
}
 
Example 14
Source File: SenderEnhancer.java    From actframework with Apache License 2.0 5 votes vote down vote up
void appendTo(InsnList list, Segment segment, S.Buffer paramNames) {
    LocalVariableMetaInfo var = var(segment);
    if (null == var) return;
    LdcInsnNode ldc = new LdcInsnNode(var.name());
    list.add(ldc);
    insn.appendTo(list, index, var.type());
    MethodInsnNode invokeRenderArg = new MethodInsnNode(INVOKEVIRTUAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, RENDER_NM, RENDER_DESC, false);
    list.add(invokeRenderArg);
    if (paramNames.length() != 0) {
        paramNames.append(',');
    }
    paramNames.append(var.name());
}
 
Example 15
Source File: IdGenerator.java    From actframework with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a unique ID across the cluster
 * @return generated ID
 */
public String genId() {
    S.Buffer sb = S.newBuffer();
    sb.a(longEncoder.longToStr(nodeIdProvider.nodeId()))
      .a(longEncoder.longToStr(startIdProvider.startId()))
      .a(longEncoder.longToStr(sequenceProvider.seqId()));
    return sb.toString();
}
 
Example 16
Source File: SenderMethodMetaInfo.java    From actframework with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    S.Buffer sb = S.newBuffer();
    sb.append(_invokeType())
            .append(_return())
            .append(fullName())
            .append("(")
            .append(_params())
            .append(")");
    return sb.toString();
}
 
Example 17
Source File: JobMethodMetaInfo.java    From actframework with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    S.Buffer sb = S.newBuffer();
    sb.append(_invokeType())
            .append(_return())
            .append(fullName());
    return sb.toString();
}
 
Example 18
Source File: ParamTree.java    From actframework with Apache License 2.0 5 votes vote down vote up
private static void addTokenToList(List<String> list, S.Buffer token) {
    String s = token.toString();
    if (S.notEmpty(s)) {
        list.add(s);
    } else {
        LOGGER.warn("empty index encountered");
    }
    token.delete(0, s.length() + 1);
}
 
Example 19
Source File: ActionMethodMetaInfo.java    From actframework with Apache License 2.0 4 votes vote down vote up
@Override
protected S.Buffer toStrBuffer(S.Buffer buffer) {
    return super.toStrBuffer(buffer).append("\n").append(interceptors);
}
 
Example 20
Source File: CatchMethodMetaInfo.java    From actframework with Apache License 2.0 4 votes vote down vote up
@Override
protected S.Buffer toStrBuffer(S.Buffer sb) {
    StringBuilder prependix = S.builder("catch").append(targetExceptionClassNames).append(" ");
    return super.toStrBuffer(sb).prepend(prependix);
}