public PagedResult<Repository> readRepositories(String source) throws ReviewboardException { try { JSONObject rootObject = checkedGetJSonRootObject(source); int totalResults = rootObject.getInt("total_results"); JSONArray jsonRepositories = rootObject.getJSONArray("repositories"); List<Repository> repositories = new ArrayList<Repository>(); for (int i = 0; i < jsonRepositories.length(); i++) { JSONObject jsonRepository = jsonRepositories.getJSONObject(i); Repository repository = new Repository(); repository.setId(jsonRepository.getInt("id")); repository.setName(jsonRepository.getString("name")); repository.setTool(RepositoryType.fromDisplayName(jsonRepository.getString("tool"))); repository.setPath(jsonRepository.getString("path")); repositories.add(repository); } return PagedResult.create(repositories, totalResults); } catch (JSONException e) { throw new ReviewboardException(e.getMessage(), e); } }
public List<Requirement> parseDeps(Iterable<String> lines) throws InvalidFormatException { int line = 0; String[] depParts = null; String currentLine = null; List<Requirement> reqs = new LinkedList<Requirement>(); for (String strLine : lines) { currentLine = strLine; ++line; depParts = spaces.split(strLine); // try to parse the file line // if cannot, die and report (error in file syntax ) try { ReqVersion ptype = tryToParseType(depParts[3], defaultReqType); Requirement req = new Requirement( depParts[0], ptype.reqType, ptype.version, RepositoryType.valueOf(depParts[1]), depParts[2]); reqs.add(req); } catch (IllegalArgumentException e) { String message = MessageFormat.format( "Wrong type: {0}; must be one of {1}", depParts[1], Arrays.toString(RepositoryType.values())); throw new InvalidFormatException(message); } catch (ArrayIndexOutOfBoundsException e) { String msg = MessageFormat.format( "Error in deps file : \nIn line {0}, not enough parameter for dependency {1}, expected format:\n\n\t<name> <type> <uri> <version>\n\nactual entry:\n\n\t{2}\n\n", line, depParts[1], currentLine); throw new InvalidFormatException(msg); } } return reqs; }