List of usage examples for java.lang Appendable toString
public String toString()
From source file:ProxyAdapter.java
/** * Usage example//ww w . j a v a2 s.c o m */ public static void main(String[] args) throws Exception { //Create adapter for Appendable ProxyAdapter<Appendable> pa = new ProxyAdapter(Appendable.class) { private StringBuilder sb = new StringBuilder(); //Override only 2 methods: append and toString public Appendable append(char c) { System.out.println("Proxy append(char c) method. Append " + c); sb.append(c); return (Appendable) getProxy(); } public String toString() { return "Proxy toString method: " + sb; } }; final Appendable a = pa.getProxy(); a.append('1').append('2'); System.out.println("a.toString() = " + a.toString()); //this invocation fails because no method has been created a.append("Not implemented"); }
From source file:ddf.catalog.transformer.csv.common.CsvTransformer.java
public static BinaryContent createResponse(final Appendable csv) { InputStream inputStream = new ByteArrayInputStream(csv.toString().getBytes(StandardCharsets.UTF_8)); return new BinaryContentImpl(inputStream, CSV_MIME_TYPE); }
From source file:ddf.catalog.transformer.csv.CsvQueryResponseTransformer.java
private BinaryContent createResponse(final Appendable csv) { InputStream inputStream = new ByteArrayInputStream(csv.toString().getBytes(Charset.defaultCharset())); BinaryContent binaryContent = new BinaryContentImpl(inputStream); return binaryContent; }
From source file:ddf.catalog.transformer.csv.common.CsvTransformerTest.java
@Test public void writeSearchResultsToCsvWithAliasMap() throws CatalogTransformerException { List<AttributeDescriptor> requestedAttributes = new ArrayList<>(); requestedAttributes.add(buildAttributeDescriptor("attribute1", BasicTypes.STRING_TYPE)); Map<String, String> aliasMap = ImmutableMap.of("attribute1", "column1"); Appendable csvText = CsvTransformer.writeMetacardsToCsv(metacardList, requestedAttributes, aliasMap); Scanner scanner = new Scanner(csvText.toString()); scanner.useDelimiter(CSV_ITEM_SEPARATOR_REGEX); String[] expectedHeaders = { "column1" }; validate(scanner, expectedHeaders);/*from w w w . j a v a 2 s. c o m*/ String[] expectedValues = { "", "value1" }; for (int i = 0; i < METACARD_COUNT; i++) { validate(scanner, expectedValues); } // final new line causes an extra "" value at end of file assertThat(scanner.hasNext(), is(true)); assertThat(scanner.next(), is("")); assertThat(scanner.hasNext(), is(false)); }
From source file:ddf.catalog.transformer.csv.common.CsvTransformerTest.java
@Test public void writeSearchResultsToCsv() throws CatalogTransformerException { List<AttributeDescriptor> requestedAttributes = new ArrayList<>(); requestedAttributes.add(buildAttributeDescriptor("attribute1", BasicTypes.STRING_TYPE)); Appendable csvText = CsvTransformer.writeMetacardsToCsv(metacardList, requestedAttributes, Collections.emptyMap()); Scanner scanner = new Scanner(csvText.toString()); scanner.useDelimiter(CSV_ITEM_SEPARATOR_REGEX); String[] expectedHeaders = { "attribute1" }; validate(scanner, expectedHeaders);/*from ww w . ja va 2 s . com*/ String[] expectedValues = { "", "value1" }; for (int i = 0; i < METACARD_COUNT; i++) { validate(scanner, expectedValues); } // final new line causes an extra "" value at end of file assertThat(scanner.hasNext(), is(true)); assertThat(scanner.next(), is("")); assertThat(scanner.hasNext(), is(false)); }
From source file:com.github.xbn.list.lister.ListLister.java
/** <pre>getOverallConfig()//w w w.j av a2s . c o m getIfNull() <i>(short circuit)</i> getIfNonNull() <i>(short circuit)</i> getVVForElementLength() <i>(short circuit)</i> getIfElementLength() getIndexFilter() getValueFilter() getElementConfig() getOverallConfig() getVPCFinalOutput() getPrefix() getPostfix()</pre> */ public static final <E> Appendable appendX(ListLister<E> lister, Appendable to_appendTo, List<E> list) throws IOException { LLCfgOverall<E> llco = null; try { llco = lister.getOverallConfig(); } catch (RuntimeException rx) { throw CrashIfObject.nullOrReturnCause(lister, "lister", null, rx); } if (list == null) { return StringWithNullDefault.append(to_appendTo, llco.getIfNull(), null); } ListLister.lr4SZ_MRK.replaceWith(list.size()); StringWithNullDefault.append(to_appendTo, llco.getPrefix(), null); Appendable apblTmp = (llco.getVPCFinalOutput().doesNothing() ? to_appendTo : new StringBuilder()); if (llco.getIfNonNull() != null) { return lr4SZ_MRK.appendReplaced(apblTmp, llco.getIfNonNull()); } if (llco.getVVForElementLength().getRuleType().isRestricted() && llco.getVVForElementLength().isValid(list.size())) { return lr4SZ_MRK.appendReplaced(apblTmp, llco.getIfElementLength()); } String sBetween = StringWithNullDefault.get(lister.getBetween(), null); boolean bFilterUsed = (lister.getIndexFilter().getRuleType().isRestricted() || lister.getValueFilter().getRuleType().isRestricted()); int ix = -1; if (!bFilterUsed) { boolean b1stLmntWritten = false; for (E e : list) { ix++; //First iteration: Was -1, now 0 if (!lister.getIndexFilter().isValid(ix) || !lister.getValueFilter().isValid(e)) { continue; } if (b1stLmntWritten) { apblTmp.append(sBetween); } else { b1stLmntWritten = true; } appendElement(lister.getElementConfig(), apblTmp, ix, e); } //ELSE: bFilterUsed is false } else { for (E e : list) { if (ix > -1) { apblTmp.append(sBetween); } //First iteration: Was -1, now 0 appendElement(lister.getElementConfig(), apblTmp, ++ix, e); } } if (!llco.getVPCFinalOutput().doesNothing()) { llco.getVPCFinalOutput().append(to_appendTo, apblTmp.toString()); } return StringWithNullDefault.append(to_appendTo, llco.getPostfix(), null); }
From source file:org.apache.cayenne.access.trans.QualifierTranslator.java
public void finishedChild(Expression node, int childIndex, boolean hasMoreChildren) { if (!hasMoreChildren) { return;/* w w w. ja v a 2 s .c om*/ } Appendable out = (matchingObject) ? new StringBuilder() : this.out; try { switch (node.getType()) { case Expression.AND: out.append(" AND "); break; case Expression.OR: out.append(" OR "); break; case Expression.EQUAL_TO: // translate NULL as IS NULL if (childIndex == 0 && node.getOperandCount() == 2 && node.getOperand(1) == null) { out.append(" IS "); } else { out.append(" = "); } break; case Expression.NOT_EQUAL_TO: // translate NULL as IS NOT NULL if (childIndex == 0 && node.getOperandCount() == 2 && node.getOperand(1) == null) { out.append(" IS NOT "); } else { out.append(" <> "); } break; case Expression.LESS_THAN: out.append(" < "); break; case Expression.GREATER_THAN: out.append(" > "); break; case Expression.LESS_THAN_EQUAL_TO: out.append(" <= "); break; case Expression.GREATER_THAN_EQUAL_TO: out.append(" >= "); break; case Expression.IN: out.append(" IN "); break; case Expression.NOT_IN: out.append(" NOT IN "); break; case Expression.LIKE: out.append(" LIKE "); break; case Expression.NOT_LIKE: out.append(" NOT LIKE "); break; case Expression.LIKE_IGNORE_CASE: if (caseInsensitive) { out.append(" LIKE "); } else { out.append(") LIKE UPPER("); } break; case Expression.NOT_LIKE_IGNORE_CASE: if (caseInsensitive) { out.append(" NOT LIKE "); } else { out.append(") NOT LIKE UPPER("); } break; case Expression.ADD: out.append(" + "); break; case Expression.SUBTRACT: out.append(" - "); break; case Expression.MULTIPLY: out.append(" * "); break; case Expression.DIVIDE: out.append(" / "); break; case Expression.BETWEEN: if (childIndex == 0) out.append(" BETWEEN "); else if (childIndex == 1) out.append(" AND "); break; case Expression.NOT_BETWEEN: if (childIndex == 0) out.append(" NOT BETWEEN "); else if (childIndex == 1) out.append(" AND "); break; case Expression.BITWISE_OR: out.append(" ").append(operandForBitwiseOr()).append(" "); break; case Expression.BITWISE_AND: out.append(" ").append(operandForBitwiseAnd()).append(" "); break; case Expression.BITWISE_XOR: out.append(" ").append(operandForBitwiseXor()).append(" "); break; } } catch (IOException ioex) { throw new CayenneRuntimeException("Error appending content", ioex); } if (matchingObject) { objectMatchTranslator.setOperation(out.toString()); objectMatchTranslator.setExpression(node); } }
From source file:org.apache.cayenne.access.translator.select.QualifierTranslator.java
@Override public void finishedChild(Expression node, int childIndex, boolean hasMoreChildren) { if (waitingForEndNode != null) { return;// w w w. j a va 2 s. c o m } if (!hasMoreChildren) { return; } Appendable out = (matchingObject) ? new StringBuilder() : this.out; try { switch (node.getType()) { case Expression.AND: out.append(" AND "); break; case Expression.OR: out.append(" OR "); break; case Expression.EQUAL_TO: // translate NULL as IS NULL if (childIndex == 0 && node.getOperandCount() == 2 && node.getOperand(1) == null) { out.append(" IS "); } else { out.append(" = "); } break; case Expression.NOT_EQUAL_TO: // translate NULL as IS NOT NULL if (childIndex == 0 && node.getOperandCount() == 2 && node.getOperand(1) == null) { out.append(" IS NOT "); } else { out.append(" <> "); } break; case Expression.LESS_THAN: out.append(" < "); break; case Expression.GREATER_THAN: out.append(" > "); break; case Expression.LESS_THAN_EQUAL_TO: out.append(" <= "); break; case Expression.GREATER_THAN_EQUAL_TO: out.append(" >= "); break; case Expression.IN: out.append(" IN "); break; case Expression.NOT_IN: out.append(" NOT IN "); break; case Expression.LIKE: out.append(" LIKE "); break; case Expression.NOT_LIKE: out.append(" NOT LIKE "); break; case Expression.LIKE_IGNORE_CASE: if (caseInsensitive) { out.append(" LIKE "); } else { out.append(") LIKE UPPER("); } break; case Expression.NOT_LIKE_IGNORE_CASE: if (caseInsensitive) { out.append(" NOT LIKE "); } else { out.append(") NOT LIKE UPPER("); } break; case Expression.ADD: out.append(" + "); break; case Expression.SUBTRACT: out.append(" - "); break; case Expression.MULTIPLY: out.append(" * "); break; case Expression.DIVIDE: out.append(" / "); break; case Expression.BETWEEN: if (childIndex == 0) out.append(" BETWEEN "); else if (childIndex == 1) out.append(" AND "); break; case Expression.NOT_BETWEEN: if (childIndex == 0) out.append(" NOT BETWEEN "); else if (childIndex == 1) out.append(" AND "); break; case Expression.BITWISE_OR: out.append(" ").append(operandForBitwiseOr()).append(" "); break; case Expression.BITWISE_AND: out.append(" ").append(operandForBitwiseAnd()).append(" "); break; case Expression.BITWISE_XOR: out.append(" ").append(operandForBitwiseXor()).append(" "); break; case Expression.BITWISE_LEFT_SHIFT: out.append(" ").append(operandForBitwiseLeftShift()).append(" "); break; case Expression.BITWISE_RIGHT_SHIFT: out.append(" ").append(operandForBitwiseRightShift()).append(""); break; } } catch (IOException ioex) { throw new CayenneRuntimeException("Error appending content", ioex); } if (matchingObject) { objectMatchTranslator.setOperation(out.toString()); objectMatchTranslator.setExpression(node); } }
From source file:org.apache.ofbiz.content.data.DataResourceWorker.java
public static String renderDataResourceAsText(Delegator delegator, String dataResourceId, Appendable out, Map<String, Object> templateContext, Locale locale, String targetMimeTypeId, boolean cache) throws GeneralException, IOException { renderDataResourceAsText(delegator, dataResourceId, out, templateContext, locale, targetMimeTypeId, cache, null);//from w w w . j av a 2 s . c o m return out.toString(); }
From source file:org.apache.ofbiz.widget.WidgetWorker.java
public static void buildHyperlinkUrl(Appendable externalWriter, String target, String targetType, Map<String, String> parameterMap, String prefix, boolean fullPath, boolean secure, boolean encode, HttpServletRequest request, HttpServletResponse response, Map<String, Object> context) throws IOException { // We may get an encoded request like: /projectmgr/control/EditTaskContents?workEffortId=10003 // Try to reducing a possibly encoded string down to its simplest form: /projectmgr/control/EditTaskContents?workEffortId=10003 // This step make sure the following appending externalLoginKey operation to work correctly String localRequestName = StringEscapeUtils.unescapeHtml(target); localRequestName = UtilHttp.encodeAmpersands(localRequestName); Appendable localWriter = new StringWriter(); if ("intra-app".equals(targetType)) { if (request != null && response != null) { ServletContext servletContext = request.getSession().getServletContext(); RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_"); externalWriter//from w w w . ja va2 s.co m .append(rh.makeLink(request, response, "/" + localRequestName, fullPath, secure, encode)); } else if (prefix != null) { externalWriter.append(prefix); externalWriter.append(localRequestName); } else { externalWriter.append(localRequestName); } } else if ("inter-app".equals(targetType)) { String fullTarget = localRequestName; localWriter.append(fullTarget); String externalLoginKey = (String) request.getAttribute("externalLoginKey"); if (UtilValidate.isNotEmpty(externalLoginKey)) { if (fullTarget.indexOf('?') == -1) { localWriter.append('?'); } else { localWriter.append("&"); } localWriter.append("externalLoginKey="); localWriter.append(externalLoginKey); } } else if ("content".equals(targetType)) { appendContentUrl(localWriter, localRequestName, request); } else if ("plain".equals(targetType)) { localWriter.append(localRequestName); } else { localWriter.append(localRequestName); } if (UtilValidate.isNotEmpty(parameterMap)) { String localUrl = localWriter.toString(); externalWriter.append(localUrl); boolean needsAmp = true; if (localUrl.indexOf('?') == -1) { externalWriter.append('?'); needsAmp = false; } for (Map.Entry<String, String> parameter : parameterMap.entrySet()) { String parameterValue = null; if (parameter.getValue() instanceof String) { parameterValue = parameter.getValue(); } else { Object parameterObject = parameter.getValue(); // skip null values if (parameterObject == null) continue; if (parameterObject instanceof String[]) { // it's probably a String[], just get the first value String[] parameterArray = (String[]) parameterObject; parameterValue = parameterArray[0]; Debug.logInfo("Found String array value for parameter [" + parameter.getKey() + "], using first value: " + parameterValue, module); } else { // not a String, and not a String[], just use toString parameterValue = parameterObject.toString(); } } if (needsAmp) { externalWriter.append("&"); } else { needsAmp = true; } externalWriter.append(parameter.getKey()); externalWriter.append('='); UtilCodec.SimpleEncoder simpleEncoder = (UtilCodec.SimpleEncoder) context.get("simpleEncoder"); if (simpleEncoder != null && parameterValue != null) { externalWriter.append(simpleEncoder .encode(URLEncoder.encode(parameterValue, Charset.forName("UTF-8").displayName()))); } else { externalWriter.append(parameterValue); } } } else { externalWriter.append(localWriter.toString()); } }