public void run() { Manager manager = Manager.getInstance(); if (manager.getFormatFile() == 1) { // if file format =1, start XML Parser xmlFile.downloadFile(); synchronized (xmlParseThread) { xmlParseThread.notify(); } } if (manager.getFormatFile() == 2) { // if file format = 2, start JSON Parser jsonFile.downloadFile(); synchronized (jsonParseThread) { jsonParseThread.notify(); } } }
public void jsonParser() { int iterator = 0; Manager manager = Manager.getInstance(); // Create the object of singleton Element jacksonElement = new Element(); JsonFactory jsonFactory = new JsonFactory(); try { File f = new File(FileDownload.getFileName()); JsonParser parser = jsonFactory.createParser(f); // Parse the file by Jackson parser while (!parser.isClosed()) { JsonToken jsonToken = parser.nextToken(); if (JsonToken.FIELD_NAME.equals(jsonToken)) { String fieldName = parser.getCurrentName(); jsonToken = parser.nextToken(); // The JSON file has the next fields: // location,date,name and object stock (this object // includes:name,bid,id,visible,minPrice,maxPrice) if ("location".equals(fieldName)) { manager.setLocation(parser.getValueAsString()); } if ("date".equals(fieldName)) { manager.setDate(parser.getValueAsString()); } if ("name".equals(fieldName) && (parser.getValueAsString().equals("Moscow Stock"))) manager.setName(parser.getValueAsString()); if ("bid".equals(fieldName)) { jacksonElement.setBid(parser.getValueAsDouble()); iterator++; } else if ("id".equals(fieldName)) { jacksonElement.setId(parser.getValueAsInt()); iterator++; } else if ("name".equals(fieldName) && (!parser.getValueAsString().equals("Moscow Stock"))) { jacksonElement.setName( parser.getValueAsString().substring(0, parser.getValueAsString().length() - 4)); iterator++; } else if ("minPrice".equals(fieldName)) { jacksonElement.setMinPrice(parser.getValueAsDouble()); iterator++; } else if ("maxPrice".equals(fieldName)) { jacksonElement.setMaxPrice(parser.getValueAsDouble()); iterator++; } else if ("visible".equals(fieldName)) { jacksonElement.setVisible(parser.getValueAsBoolean()); iterator++; } if (iterator == 6) { manager.setJsonList(jacksonElement); // add the data to the list jacksonElement = new Element(); iterator = 0; } } } } catch (JsonParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }