public void baseBuiltinFormats() { Workbook wb = getTestDataProvider().createWorkbook(); DataFormat df = wb.createDataFormat(); Map<Integer, String> formats = BuiltinFormats.getBuiltinFormats(); for (int idx : formats.keySet()) { String fmt = formats.get(new Integer(idx)); assertEquals(idx, df.getFormat(fmt)); } // default format for new cells is General Sheet sheet = wb.createSheet(); Cell cell = sheet.createRow(0).createCell(0); assertEquals(0, cell.getCellStyle().getDataFormat()); assertEquals("General", cell.getCellStyle().getDataFormatString()); // create a custom data format String customFmt = "#0.00 AM/PM"; // check it is not in built-in formats assertEquals(-1, BuiltinFormats.getBuiltinFormat(customFmt)); int customIdx = df.getFormat(customFmt); // The first user-defined format starts at 164. assertTrue(customIdx >= BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX); // read and verify the string representation assertEquals(customFmt, df.getFormat((short) customIdx)); }
public CustomApiTests() { super("Custom API tests"); apiNames = new HashMap<CustomApiTests.ApiPermissions, String>(); apiNames.put(ApiPermissions.Admin, ADMIN_API_NAME); apiNames.put(ApiPermissions.User, USER_API_NAME); apiNames.put(ApiPermissions.Application, APP_API_NAME); apiNames.put(ApiPermissions.Public, PUBLIC_API_NAME); Random rndGen = new Random(); this.addTest(LoginTests.createLogoutTest()); for (ApiPermissions permission : ApiPermissions.values()) { for (int i = 0; i < 10; i++) { this.addTest(createJsonApiTest(permission, false, rndGen, i)); } } this.addTest(LoginTests.createLoginTest(MobileServiceAuthenticationProvider.Facebook)); this.addTest(createJsonApiTest(ApiPermissions.User, true, rndGen, 0)); this.addTest(LoginTests.createLogoutTest()); for (TypedTestType testType : TypedTestType.values()) { this.addTest(createTypedApiTest(rndGen, testType)); } for (DataFormat inputFormat : DataFormat.values()) { for (DataFormat outputFormat : DataFormat.values()) { this.addTest(createHttpContentApiTest(inputFormat, outputFormat, rndGen)); } } }
@Test public void numericCells() throws Exception { Workbook workbook = _testDataProvider.createWorkbook(); fixFonts(workbook); DataFormat df = workbook.getCreationHelper().createDataFormat(); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); row.createCell(0) .setCellValue(0); // getCachedFormulaResult() returns 0 for not evaluated formula cells row.createCell(1).setCellValue(10); row.createCell(2).setCellValue("10"); row.createCell(3).setCellFormula("(A1+B1)*1.0"); // a formula that returns '10' Cell cell4 = row.createCell(4); // numeric cell with a custom style CellStyle style4 = workbook.createCellStyle(); style4.setDataFormat(df.getFormat("0.0000")); cell4.setCellStyle(style4); cell4.setCellValue(10); // formatted as '10.0000' row.createCell(5).setCellValue("10.0000"); // autosize not-evaluated cells, formula cells are sized as if the result is 0 for (int i = 0; i < 6; i++) sheet.autoSizeColumn(i); assertTrue( sheet.getColumnWidth(0) < sheet.getColumnWidth(1)); // width of '0' is less then width of '10' assertEquals( sheet.getColumnWidth(1), sheet.getColumnWidth(2)); // 10 and '10' should be sized equally assertEquals( sheet.getColumnWidth(3), sheet.getColumnWidth(0)); // formula result is unknown, the width is calculated for '0' assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(5)); // 10.0000 and '10.0000' // evaluate formulas and re-autosize evaluateWorkbook(workbook); for (int i = 0; i < 6; i++) sheet.autoSizeColumn(i); assertTrue( sheet.getColumnWidth(0) < sheet.getColumnWidth(1)); // width of '0' is less then width of '10' assertEquals( sheet.getColumnWidth(1), sheet.getColumnWidth(2)); // columns 1, 2 and 3 should have the same width assertEquals( sheet.getColumnWidth(2), sheet.getColumnWidth(3)); // columns 1, 2 and 3 should have the same width assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(5)); // 10.0000 and '10.0000' workbook.close(); }
public static Object getBodyFromCamel(org.apache.camel.Message out, DataFormat dataFormat) { Object answer = null; if (dataFormat == DataFormat.POJO) { answer = out.getBody(); } else if (dataFormat == DataFormat.PAYLOAD) { answer = out.getBody(CxfPayload.class); } else if (dataFormat.dealias() == DataFormat.RAW) { answer = out.getBody(InputStream.class); } else if (dataFormat.dealias() == DataFormat.CXF_MESSAGE) { answer = out.getBody(); } return answer; }
@Override public DDF loadSpecialFormat(DataFormat format, URI fileURI, Boolean flatten) throws DDFException { SparkDDFManager sparkDDFManager = (SparkDDFManager) mDDFManager; HiveContext sqlContext = sparkDDFManager.getHiveContext(); DataFrame jdf = null; switch (format) { case JSON: jdf = sqlContext.jsonFile(fileURI.toString()); break; case PQT: jdf = sqlContext.parquetFile(fileURI.toString()); break; default: throw new DDFException(String.format("Unsupported data format: %s", format.toString())); } DataFrame df = SparkUtils.getDataFrameWithValidColnames(jdf); DDF ddf = sparkDDFManager.newDDF( sparkDDFManager, df, new Class<?>[] {DataFrame.class}, null, SparkUtils.schemaFromDataFrame(df)); if (flatten == true) return ddf.getFlattenedDDF(); else return ddf; }
@SuppressWarnings("unchecked") private <T> T unserializeData(InputStream inputStream) { T object = null; try { if (dataFormat.equals(DataFormat.CBOR)) { object = (T) cborMapper.readValue(inputStream, TargetModel.class); } else if (dataFormat.equals(DataFormat.JSON)) { object = (T) jsonMapper.readValue(inputStream, TargetModelJson.class); } else if (dataFormat.equals(DataFormat.HTML)) { byte[] fileData = IOUtils.toByteArray(inputStream); object = (T) new String(fileData); } } catch (IOException e) { throw new RuntimeException("Failed to unserialize object.", e); } return object; }
// mainline public static final void main(String[] args) throws Exception { // parse command line if (args.length != 5) throw new Xcept( "Example2 usage: project-file model input-variable input-value output-variable"); JSReadable projFile = new JSReadable(args[0]); String modelName = args[1]; String uname = args[2]; String uvalue = args[3]; String vname = args[4]; // load application server and project PApplication appl = new PApplication(); Project proj = new Project("proj", appl); proj.importXML(projFile); // find and compile model PModel pmodel = (PModel) proj.child(modelName); PJob pjob = new PModelBuildJob(pmodel); pjob.simpleRun(); ASModel rt = pmodel.rt(); // set input variable ASVar u = rt.getASVar(uname); u.setAssign(uvalue); // run model pjob = new PModelRunJob(pmodel); pjob.simpleRun(); // query output variable ASVar v = rt.getASVar(vname); Data data = rt.getData(0, v); // print output in PrettyFormat DataFormat fmt = new PrettyDataFormat(); DataWriter wrt = fmt.createWriter(); Data.List list = new Data.List(1); list.add(data); wrt.writeData(System.out, list); }
protected static Object getContentFromCxf(Message message, DataFormat dataFormat) { Set<Class<?>> contentFormats = message.getContentFormats(); Object answer = null; if (contentFormats != null) { if (LOG.isTraceEnabled()) { for (Class<?> contentFormat : contentFormats) { LOG.trace("Content format={} value={}", contentFormat, message.getContent(contentFormat)); } } if (dataFormat == DataFormat.POJO) { answer = message.getContent(List.class); if (answer == null) { answer = message.getContent(Object.class); if (answer != null) { answer = new MessageContentsList(answer); } } } else if (dataFormat == DataFormat.PAYLOAD) { List<SoapHeader> headers = CastUtils.cast((List<?>) message.get(Header.HEADER_LIST)); Map<String, String> nsMap = new HashMap<String, String>(); answer = new CxfPayload<SoapHeader>(headers, getPayloadBodyElements(message, nsMap), nsMap); } else if (dataFormat.dealias() == DataFormat.RAW) { answer = message.getContent(InputStream.class); } else if (dataFormat.dealias() == DataFormat.CXF_MESSAGE && message.getContent(List.class) != null) { // CAMEL-6404 added check point of message content // The message content of list could be null if there is a fault message is received answer = message.getContent(List.class).get(0); } LOG.trace("Extracted body from CXF message = {}", answer); } return answer; }
@Override public void prepare(ProgressMonitor m) throws IOException { // round up all the files with the same name suppFiles = new ArrayList(); prjFile = null; // getBaseName only gets the LAST extension so beware for .shp.aux.xml stuff final String baseName = getBaseName(file.name()); for (Resource f : file.parent().list()) { if (f.equals(file)) { continue; } if (!f.name().startsWith(baseName)) { continue; } if (f.getType() != Type.RESOURCE) { continue; } String ext = f.name().substring(baseName.length()); // once the basename is stripped, extension(s) should be present if (ext.charAt(0) == '.') { if (".prj".equalsIgnoreCase(ext)) { prjFile = f; } else { suppFiles.add(f); } } } if (format == null) { format = DataFormat.lookup(file); } // fix the prj file (match to official epsg wkt) try { fixPrjFile(); } catch (Exception e) { LOGGER.log(Level.WARNING, "Error fixing prj file", e); } }
public void finalize() throws Throwable { super.finalize(); }
@Test public void dateCells() throws Exception { Workbook workbook = _testDataProvider.createWorkbook(); fixFonts(workbook); Sheet sheet = workbook.createSheet(); DataFormat df = workbook.getCreationHelper().createDataFormat(); CellStyle style1 = workbook.createCellStyle(); style1.setDataFormat(df.getFormat("m")); CellStyle style3 = workbook.createCellStyle(); style3.setDataFormat(df.getFormat("mmm")); CellStyle style5 = workbook.createCellStyle(); // rotated text style5.setDataFormat(df.getFormat("mmm/dd/yyyy")); Calendar calendar = LocaleUtil.getLocaleCalendar(2010, 0, 1); // Jan 1 2010 Row row = sheet.createRow(0); row.createCell(0).setCellValue(DateUtil.getJavaDate(0)); // default date Cell cell1 = row.createCell(1); cell1.setCellValue(calendar); cell1.setCellStyle(style1); row.createCell(2).setCellValue("1"); // column 1 should be sized as '1' Cell cell3 = row.createCell(3); cell3.setCellValue(calendar); cell3.setCellStyle(style3); row.createCell(4).setCellValue("Jan"); Cell cell5 = row.createCell(5); cell5.setCellValue(calendar); cell5.setCellStyle(style5); row.createCell(6).setCellValue("Jan/01/2010"); Cell cell7 = row.createCell(7); cell7.setCellFormula("DATE(2010,1,1)"); cell7.setCellStyle(style3); // should be sized as 'Jan' // autosize not-evaluated cells, formula cells are sized as if the result is 0 for (int i = 0; i < 8; i++) sheet.autoSizeColumn(i); assertEquals(sheet.getColumnWidth(2), sheet.getColumnWidth(1)); // date formatted as 'm' assertTrue(sheet.getColumnWidth(3) > sheet.getColumnWidth(1)); // 'mmm' is wider than 'm' assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(3)); // date formatted as 'mmm' assertTrue( sheet.getColumnWidth(5) > sheet.getColumnWidth(3)); // 'mmm/dd/yyyy' is wider than 'mmm' assertEquals( sheet.getColumnWidth(6), sheet.getColumnWidth(5)); // date formatted as 'mmm/dd/yyyy' // YK: width of not-evaluated formulas that return data is not determined // POI seems to conevert '0' to Excel date which is the beginng of the Excel's date system // evaluate formulas and re-autosize evaluateWorkbook(workbook); for (int i = 0; i < 8; i++) sheet.autoSizeColumn(i); assertEquals(sheet.getColumnWidth(2), sheet.getColumnWidth(1)); // date formatted as 'm' assertTrue(sheet.getColumnWidth(3) > sheet.getColumnWidth(1)); // 'mmm' is wider than 'm' assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(3)); // date formatted as 'mmm' assertTrue( sheet.getColumnWidth(5) > sheet.getColumnWidth(3)); // 'mmm/dd/yyyy' is wider than 'mmm' assertEquals( sheet.getColumnWidth(6), sheet.getColumnWidth(5)); // date formatted as 'mmm/dd/yyyy' assertEquals( sheet.getColumnWidth(4), sheet.getColumnWidth(7)); // date formula formatted as 'mmm' workbook.close(); }
@SuppressWarnings("unchecked") protected void propagateHeadersFromCamelToCxf( Exchange camelExchange, Map<String, Object> camelHeaders, org.apache.cxf.message.Exchange cxfExchange, Map<String, Object> cxfContext) { // get cxf transport headers (if any) from camel exchange // use a treemap to keep ordering and ignore key case Map<String, List<String>> transportHeaders = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER); if (camelExchange != null) { Map<String, List<String>> h = CastUtils.cast((Map<?, ?>) camelExchange.getProperty(Message.PROTOCOL_HEADERS)); if (h != null) { transportHeaders.putAll(h); } } Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) camelHeaders.get(Message.PROTOCOL_HEADERS)); if (headers != null) { transportHeaders.putAll(headers); } DataFormat dataFormat = camelExchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class); for (Map.Entry<String, Object> entry : camelHeaders.entrySet()) { // put response code in request context so it will be copied to CXF message's property if (Message.RESPONSE_CODE.equals(entry.getKey()) || Exchange.HTTP_RESPONSE_CODE.equals(entry.getKey())) { LOG.debug("Propagate to CXF header: {} value: {}", Message.RESPONSE_CODE, entry.getValue()); cxfContext.put(Message.RESPONSE_CODE, entry.getValue()); continue; } // We need to copy the content-type if the dataformat is RAW if (Message.CONTENT_TYPE.equalsIgnoreCase(entry.getKey()) && dataFormat.equals(DataFormat.RAW)) { LOG.debug("Propagate to CXF header: {} value: {}", Message.CONTENT_TYPE, entry.getValue()); cxfContext.put(Message.CONTENT_TYPE, entry.getValue().toString()); continue; } // need to filter the User-Agent ignore the case, as CXF just check the header with // "User-Agent" if (entry.getKey().equalsIgnoreCase("User-Agent")) { List<String> listValue = new ArrayList<String>(); listValue.add(entry.getValue().toString()); transportHeaders.put("User-Agent", listValue); } // this header should be filtered, continue to the next header if (headerFilterStrategy.applyFilterToCamelHeaders( entry.getKey(), entry.getValue(), camelExchange)) { continue; } LOG.debug("Propagate to CXF header: {} value: {}", entry.getKey(), entry.getValue()); // put SOAP/protocol header list in exchange if (Header.HEADER_LIST.equals(entry.getKey())) { List<Header> headerList = (List<Header>) entry.getValue(); for (Header header : headerList) { header.setDirection(Header.Direction.DIRECTION_OUT); LOG.trace( "Propagate SOAP/protocol header: {} : {}", header.getName(), header.getObject()); } // cxfExchange.put(Header.HEADER_LIST, headerList); cxfContext.put(entry.getKey(), headerList); continue; } // things that are not filtered and not specifically copied will be put in transport headers if (entry.getValue() instanceof List) { transportHeaders.put(entry.getKey(), (List<String>) entry.getValue()); } else { List<String> listValue = new ArrayList<String>(); listValue.add(entry.getValue().toString()); transportHeaders.put(entry.getKey(), listValue); } } if (transportHeaders.size() > 0) { cxfContext.put(Message.PROTOCOL_HEADERS, transportHeaders); } else { // no propagated transport headers does really mean no headers, not the ones // from the previous request or response propagated with the invocation context cxfContext.remove(Message.PROTOCOL_HEADERS); } }
/** * @param cxfMessage * @param camelMessage * @param exchange provides context for filtering */ protected void propagateHeadersFromCxfToCamel( Message cxfMessage, org.apache.camel.Message camelMessage, Exchange exchange) { Map<String, List<String>> cxfHeaders = CastUtils.cast((Map<?, ?>) cxfMessage.get(Message.PROTOCOL_HEADERS)); Map<String, Object> camelHeaders = camelMessage.getHeaders(); camelHeaders.put(CxfConstants.CAMEL_CXF_MESSAGE, cxfMessage); // Copy the http header to CAMEL as we do in camel-cxfrs CxfUtils.copyHttpHeadersFromCxfToCamel(cxfMessage, camelMessage); if (cxfHeaders != null) { for (Map.Entry<String, List<String>> entry : cxfHeaders.entrySet()) { if (!headerFilterStrategy.applyFilterToExternalHeaders( entry.getKey(), entry.getValue(), exchange)) { // We need to filter the content type with multi-part, // as the multi-part stream is already consumed by AttachmentInInterceptor, // it will cause some trouble when route this message to another CXF endpoint. if ("Content-Type".compareToIgnoreCase(entry.getKey()) == 0 && entry.getValue().get(0) != null && entry.getValue().get(0).startsWith("multipart/related")) { // We need to keep the Content-Type if the data format is RAW message DataFormat dataFormat = exchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class); if (dataFormat.equals(DataFormat.RAW)) { camelHeaders.put(entry.getKey(), getContentTypeString(entry.getValue())); } else { String contentType = replaceMultiPartContentType(entry.getValue().get(0)); LOG.trace("Find the multi-part Conent-Type, and replace it with {}", contentType); camelHeaders.put(entry.getKey(), contentType); } } else { LOG.trace( "Populate header from CXF header={} value={}", entry.getKey(), entry.getValue()); List<String> values = entry.getValue(); Object evalue; if (values.size() > 1) { if (exchange.getProperty( CxfConstants.CAMEL_CXF_PROTOCOL_HEADERS_MERGED, Boolean.FALSE, Boolean.class)) { StringBuilder sb = new StringBuilder(); for (Iterator<String> it = values.iterator(); it.hasNext(); ) { sb.append(it.next()); if (it.hasNext()) { sb.append(',').append(' '); } } evalue = sb.toString(); } else { evalue = values; } } else { evalue = values.get(0); } camelHeaders.put(entry.getKey(), evalue); } } } } // propagate SOAP/protocol header list String key = Header.HEADER_LIST; Object value = cxfMessage.get(key); if (value != null) { if (!headerFilterStrategy.applyFilterToExternalHeaders(key, value, exchange)) { camelHeaders.put(key, value); LOG.trace("Populate header from CXF header={} value={}", key, value); } else { ((List<?>) value).clear(); } } // propagate the SOAPAction header String soapAction = (String) camelHeaders.get(SoapBindingConstants.SOAP_ACTION); // Remove SOAPAction from the protocol header, as it will not be overrided if (ObjectHelper.isEmpty(soapAction) || "\"\"".equals(soapAction)) { camelHeaders.remove(SoapBindingConstants.SOAP_ACTION); } soapAction = (String) cxfMessage.get(SoapBindingConstants.SOAP_ACTION); if (soapAction != null) { if (!headerFilterStrategy.applyFilterToExternalHeaders( SoapBindingConstants.SOAP_ACTION, soapAction, exchange)) { camelHeaders.put(SoapBindingConstants.SOAP_ACTION, soapAction); LOG.trace( "Populate header from CXF header={} value={}", SoapBindingConstants.SOAP_ACTION, soapAction); } } }