List of usage examples for java.io File exists
public boolean exists()
From source file:com.linkedin.pinotdruidbenchmark.PinotResponseTime.java
public static void main(String[] args) throws Exception { if (args.length != 4 && args.length != 5) { System.err.println(// w ww . ja va 2 s.c o m "4 or 5 arguments required: QUERY_DIR, RESOURCE_URL, WARM_UP_ROUNDS, TEST_ROUNDS, RESULT_DIR (optional)."); return; } File queryDir = new File(args[0]); String resourceUrl = args[1]; int warmUpRounds = Integer.parseInt(args[2]); int testRounds = Integer.parseInt(args[3]); File resultDir; if (args.length == 4) { resultDir = null; } else { resultDir = new File(args[4]); if (!resultDir.exists()) { if (!resultDir.mkdirs()) { throw new RuntimeException("Failed to create result directory: " + resultDir); } } } File[] queryFiles = queryDir.listFiles(); assert queryFiles != null; Arrays.sort(queryFiles); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(resourceUrl); for (File queryFile : queryFiles) { String query = new BufferedReader(new FileReader(queryFile)).readLine(); httpPost.setEntity(new StringEntity("{\"pql\":\"" + query + "\"}")); System.out.println( "--------------------------------------------------------------------------------"); System.out.println("Running query: " + query); System.out.println( "--------------------------------------------------------------------------------"); // Warm-up Rounds System.out.println("Run " + warmUpRounds + " times to warm up..."); for (int i = 0; i < warmUpRounds; i++) { CloseableHttpResponse httpResponse = httpClient.execute(httpPost); httpResponse.close(); System.out.print('*'); } System.out.println(); // Test Rounds System.out.println("Run " + testRounds + " times to get response time statistics..."); long[] responseTimes = new long[testRounds]; long totalResponseTime = 0L; for (int i = 0; i < testRounds; i++) { long startTime = System.currentTimeMillis(); CloseableHttpResponse httpResponse = httpClient.execute(httpPost); httpResponse.close(); long responseTime = System.currentTimeMillis() - startTime; responseTimes[i] = responseTime; totalResponseTime += responseTime; System.out.print(responseTime + "ms "); } System.out.println(); // Store result. if (resultDir != null) { File resultFile = new File(resultDir, queryFile.getName() + ".result"); CloseableHttpResponse httpResponse = httpClient.execute(httpPost); try (BufferedInputStream bufferedInputStream = new BufferedInputStream( httpResponse.getEntity().getContent()); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(resultFile))) { int length; while ((length = bufferedInputStream.read(BYTE_BUFFER)) > 0) { bufferedWriter.write(new String(BYTE_BUFFER, 0, length)); } } httpResponse.close(); } // Process response times. double averageResponseTime = (double) totalResponseTime / testRounds; double temp = 0; for (long responseTime : responseTimes) { temp += (responseTime - averageResponseTime) * (responseTime - averageResponseTime); } double standardDeviation = Math.sqrt(temp / testRounds); System.out.println("Average response time: " + averageResponseTime + "ms"); System.out.println("Standard deviation: " + standardDeviation); } } }
From source file:eu.cognitum.readandwrite.App.java
public static void main(String[] args) { try {/* www.j a va 2 s .co m*/ String configFile = 0 == args.length ? "example.properties" : args[0]; CONFIGURATION = new Properties(); File f = new File(configFile); if (!f.exists()) { LOGGER.warning("configuration not found at " + configFile); return; } LOGGER.info("loading configuration file " + f.getAbsoluteFile()); CONFIGURATION.load(new FileInputStream(f)); String ip = CONFIGURATION.getProperty(PROP_STORAGE_HOSTNAME); String keyspace = CONFIGURATION.getProperty(PROP_STORAGE_KEYSPACE); String directory = CONFIGURATION.getProperty(PROP_STORAGE_DIRECTORY); // N of articles to be generated. int Narticles = 100000; // size of the buffer to commit each time int commitBufferSize = 100; // N of articles to commit before trying reads int readStep = 100; String currentNamespace = "http://mynamespace#"; LOGGER.log(Level.INFO, "Generating the rdf..."); GenerateRdf rdfGenerator = new GenerateRdf(currentNamespace, "tmp.rdf"); rdfGenerator.generateAndSaveRdf(Narticles); LOGGER.log(Level.INFO, "Generated the rdf!"); ArrayList<SimulateReadAndWrite> simulateAll = new ArrayList<SimulateReadAndWrite>(); int Ndbs = 0; DBS[] chosenDbs = { DBS.NATIVE }; //DBS[] chosenDbs = DBS.values(); for (DBS dbs : chosenDbs) { SailRepository sr; switch (dbs) { case NATIVE: sr = createNativeStoreConnection(directory); break; case TITAN: sr = createTitanConnection(ip, keyspace); break; case NEO4J: sr = createNeo4jConnection(keyspace); break; case ORIENT: sr = createOrientConnection(keyspace); break; default: sr = null; break; } if (sr == null) { throw new Exception("Something wrong while connecting to " + dbs.toString()); } simulateAll.add(new SimulateReadAndWrite(sr, "test" + dbs.toString(), Narticles, readStep, commitBufferSize, dbs.toString(), keyspace, currentNamespace, rdfGenerator)); simulateAll.get(Ndbs).start(); Ndbs++; } int Nfinished = 0; int k; while (Nfinished != Ndbs) { Nfinished = 0; k = 0; for (DBS dbs : chosenDbs) { if (simulateAll.get(k).IsProcessCompleted()) { Nfinished++; } else { System.out.println(String.format("Process for db %s is at %.2f", dbs.toString(), simulateAll.get(k).GetProgress())); } k++; } Thread.sleep(10000); } } catch (Exception ex) { LOGGER.log(Level.SEVERE, null, ex); } }
From source file:com.xiaoxiaomo.flink.batch.distcp.DistCp.java
public static void main(String[] args) throws Exception { // set up the execution environment final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); ParameterTool params = ParameterTool.fromArgs(args); if (!params.has("input") || !params.has("output")) { System.err.println("Usage: --input <path> --output <path> [--parallelism <n>]"); return;/*w ww .j a v a2s . c o m*/ } final Path sourcePath = new Path(params.get("input")); final Path targetPath = new Path(params.get("output")); if (!isLocal(env) && !(isOnDistributedFS(sourcePath) && isOnDistributedFS(targetPath))) { System.out.println("In a distributed mode only HDFS input/output paths are supported"); return; } final int parallelism = params.getInt("parallelism", 10); if (parallelism <= 0) { System.err.println("Parallelism should be greater than 0"); return; } // make parameters available in the web interface env.getConfig().setGlobalJobParameters(params); env.setParallelism(parallelism); long startTime = System.currentTimeMillis(); LOGGER.info("Initializing copy tasks"); List<FileCopyTask> tasks = getCopyTasks(sourcePath); LOGGER.info("Copy task initialization took " + (System.currentTimeMillis() - startTime) + "ms"); DataSet<FileCopyTask> inputTasks = new DataSource<FileCopyTask>(env, new FileCopyTaskInputFormat(tasks), new GenericTypeInfo<FileCopyTask>(FileCopyTask.class), "fileCopyTasks"); FlatMapOperator<FileCopyTask, Object> res = inputTasks .flatMap(new RichFlatMapFunction<FileCopyTask, Object>() { private static final long serialVersionUID = 1109254230243989929L; private LongCounter fileCounter; private LongCounter bytesCounter; @Override public void open(Configuration parameters) throws Exception { bytesCounter = getRuntimeContext().getLongCounter(BYTES_COPIED_CNT_NAME); fileCounter = getRuntimeContext().getLongCounter(FILES_COPIED_CNT_NAME); } @Override public void flatMap(FileCopyTask task, Collector<Object> out) throws Exception { LOGGER.info("Processing task: " + task); Path outPath = new Path(targetPath, task.getRelativePath()); FileSystem targetFs = targetPath.getFileSystem(); // creating parent folders in case of a local FS if (!targetFs.isDistributedFS()) { //dealing with cases like file:///tmp or just /tmp File outFile = outPath.toUri().isAbsolute() ? new File(outPath.toUri()) : new File(outPath.toString()); File parentFile = outFile.getParentFile(); if (!parentFile.mkdirs() && !parentFile.exists()) { throw new RuntimeException( "Cannot create local file system directories: " + parentFile); } } FSDataOutputStream outputStream = null; FSDataInputStream inputStream = null; try { outputStream = targetFs.create(outPath, true); inputStream = task.getPath().getFileSystem().open(task.getPath()); int bytes = IOUtils.copy(inputStream, outputStream); bytesCounter.add(bytes); } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } fileCounter.add(1L); } }); // no data sinks are needed, therefore just printing an empty result res.print(); Map<String, Object> accumulators = env.getLastJobExecutionResult().getAllAccumulatorResults(); LOGGER.info("== COUNTERS =="); for (Map.Entry<String, Object> e : accumulators.entrySet()) { LOGGER.info(e.getKey() + ": " + e.getValue()); } }
From source file:edu.cmu.lti.oaqa.knn4qa.apps.LuceneIndexer.java
public static void main(String[] args) { Options options = new Options(); options.addOption(CommonParams.ROOT_DIR_PARAM, null, true, CommonParams.ROOT_DIR_DESC); options.addOption(CommonParams.SUB_DIR_TYPE_PARAM, null, true, CommonParams.SUB_DIR_TYPE_DESC); options.addOption(CommonParams.MAX_NUM_REC_PARAM, null, true, CommonParams.MAX_NUM_REC_DESC); options.addOption(CommonParams.SOLR_FILE_NAME_PARAM, null, true, CommonParams.SOLR_FILE_NAME_DESC); options.addOption(CommonParams.OUT_INDEX_PARAM, null, true, CommonParams.OUT_MINDEX_DESC); CommandLineParser parser = new org.apache.commons.cli.GnuParser(); try {/*from www . j a va 2s. c o m*/ CommandLine cmd = parser.parse(options, args); String rootDir = null; rootDir = cmd.getOptionValue(CommonParams.ROOT_DIR_PARAM); if (null == rootDir) Usage("Specify: " + CommonParams.ROOT_DIR_DESC, options); String outputDirName = cmd.getOptionValue(CommonParams.OUT_INDEX_PARAM); if (null == outputDirName) Usage("Specify: " + CommonParams.OUT_MINDEX_DESC, options); String subDirTypeList = cmd.getOptionValue(CommonParams.SUB_DIR_TYPE_PARAM); if (null == subDirTypeList || subDirTypeList.isEmpty()) Usage("Specify: " + CommonParams.SUB_DIR_TYPE_DESC, options); String solrFileName = cmd.getOptionValue(CommonParams.SOLR_FILE_NAME_PARAM); if (null == solrFileName) Usage("Specify: " + CommonParams.SOLR_FILE_NAME_DESC, options); int maxNumRec = Integer.MAX_VALUE; String tmp = cmd.getOptionValue(CommonParams.MAX_NUM_REC_PARAM); if (tmp != null) { try { maxNumRec = Integer.parseInt(tmp); if (maxNumRec <= 0) { Usage("The maximum number of records should be a positive integer", options); } } catch (NumberFormatException e) { Usage("The maximum number of records should be a positive integer", options); } } File outputDir = new File(outputDirName); if (!outputDir.exists()) { if (!outputDir.mkdirs()) { System.out.println("couldn't create " + outputDir.getAbsolutePath()); System.exit(1); } } if (!outputDir.isDirectory()) { System.out.println(outputDir.getAbsolutePath() + " is not a directory!"); System.exit(1); } if (!outputDir.canWrite()) { System.out.println("Can't write to " + outputDir.getAbsolutePath()); System.exit(1); } String subDirs[] = subDirTypeList.split(","); int docNum = 0; // No English analyzer here, all language-related processing is done already, // here we simply white-space tokenize and index tokens verbatim. Analyzer analyzer = new WhitespaceAnalyzer(); FSDirectory indexDir = FSDirectory.open(outputDir); IndexWriterConfig indexConf = new IndexWriterConfig(analyzer.getVersion(), analyzer); System.out.println("Creating a new Lucene index, maximum # of docs to process: " + maxNumRec); indexConf.setOpenMode(OpenMode.CREATE); IndexWriter indexWriter = new IndexWriter(indexDir, indexConf); for (int subDirId = 0; subDirId < subDirs.length && docNum < maxNumRec; ++subDirId) { String inputFileName = rootDir + "/" + subDirs[subDirId] + "/" + solrFileName; System.out.println("Input file name: " + inputFileName); BufferedReader inpText = new BufferedReader( new InputStreamReader(CompressUtils.createInputStream(inputFileName))); String docText = XmlHelper.readNextXMLIndexEntry(inpText); for (; docText != null && docNum < maxNumRec; docText = XmlHelper.readNextXMLIndexEntry(inpText)) { ++docNum; Map<String, String> docFields = null; Document luceneDoc = new Document(); try { docFields = XmlHelper.parseXMLIndexEntry(docText); } catch (Exception e) { System.err.println(String.format("Parsing error, offending DOC #%d:\n%s", docNum, docText)); System.exit(1); } String id = docFields.get(UtilConst.TAG_DOCNO); if (id == null) { System.err.println(String.format("No ID tag '%s', offending DOC #%d:\n%s", UtilConst.TAG_DOCNO, docNum, docText)); } luceneDoc.add(new StringField(UtilConst.TAG_DOCNO, id, Field.Store.YES)); for (Map.Entry<String, String> e : docFields.entrySet()) if (!e.getKey().equals(UtilConst.TAG_DOCNO)) { luceneDoc.add(new TextField(e.getKey(), e.getValue(), Field.Store.YES)); } indexWriter.addDocument(luceneDoc); if (docNum % 1000 == 0) System.out.println("Indexed " + docNum + " docs"); } System.out.println("Indexed " + docNum + " docs"); } indexWriter.commit(); indexWriter.close(); } catch (ParseException e) { Usage("Cannot parse arguments", options); } catch (Exception e) { System.err.println("Terminating due to an exception: " + e); System.exit(1); } }
From source file:it.sayservice.platform.smartplanner.utils.LegGenerator.java
public static void main(String[] args) throws IOException { Mongo m = new Mongo("localhost"); // default port 27017 DB db = m.getDB("smart-planner-15x"); DBCollection coll = db.getCollection("stops"); // read trips.txt(trips,serviceId). List<String[]> trips = readFileGetLines("src/main/resources/schedules/17/trips.txt"); List<String[]> stopTimes = readFileGetLines("src/main/resources/schedules/17/stop_times.txt"); for (String[] words : trips) { try {/*from w ww . ja v a2s .c o m*/ String routeId = words[0].trim(); String serviceId = words[1].trim(); String tripId = words[2].trim(); // fetch schedule for trips. for (int i = 0; i < stopTimes.size(); i++) { // already ordered by occurence. String[] scheduleLeg = stopTimes.get(i); if (scheduleLeg[0].equalsIgnoreCase(tripId)) { // check if next leg belongs to same trip if (stopTimes.get(i + 1)[0].equalsIgnoreCase(tripId)) { String arrivalT = scheduleLeg[1]; String departT = scheduleLeg[2]; String sourceId = scheduleLeg[3]; String destId = stopTimes.get(i + 1)[3]; // get coordinates of stops. /** * make sure that mongo stop collection is * populated. if, not, invoke * http://localhost:7070/smart * -planner/rest/getTransitTimes * /TB_R2_R/1366776000000/1366819200000 */ Stop source = (Stop) getObjectByField(db, "id", sourceId, coll, Stop.class); Stop destination = (Stop) getObjectByField(db, "id", destId, coll, Stop.class); // System.out.println(tripId + "," // + routeId + "," // + source.getId() + "," // + source.getLatitude() + "," // + source.getLongitude() + "," // + arrivalT + "," // + destination.getId() + "," // + destination.getLatitude() + "," // + destination.getLongitude() + "," // + departT + "," // + serviceId // ); String content = tripId + "," + routeId + "," + source.getStopId() + "," + source.getLatitude() + "," + source.getLongitude() + "," + arrivalT + "," + destination.getStopId() + "," + destination.getLatitude() + "," + destination.getLongitude() + "," + departT + "," + "Giornaliero" + "\n"; File file = new File("src/main/resources/legs/legs.txt"); // single leg file if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); // individual trip leg file. File fileT = new File("src/main/resources/legs/legs_" + routeId + ".txt"); FileWriter fwT = new FileWriter(fileT.getAbsoluteFile(), true); BufferedWriter bwT = new BufferedWriter(fwT); bwT.write(content); bwT.close(); } } } } catch (Exception e) { System.out.println("Error parsing trip: " + words[0] + "," + words[1] + "," + words[2]); } } System.out.println("Done"); }
From source file:at.tuwien.ifs.feature.evaluation.SimilarityRetrievalWriter.java
public static void main(String[] args) throws SOMToolboxException, IOException { // register and parse all options JSAPResult config = OptionFactory.parseResults(args, OPTIONS); File inputVectorFile = config.getFile("inputVectorFile"); String outputDirStr = AbstractOptionFactory.getFilePath(config, "outputDirectory"); File outputDirBase = new File(outputDirStr); outputDirBase.mkdirs();//from w w w .j ava 2s .c om String metricName = config.getString("metric"); DistanceMetric metric = AbstractMetric.instantiateNice(metricName); int neighbours = config.getInt("numberNeighbours"); int startIndex = config.getInt("startIndex"); int numberItems = config.getInt("numberItems", -1); try { SOMLibSparseInputData data = new SOMLibSparseInputData(inputVectorFile.getAbsolutePath()); int endIndex = data.numVectors(); if (numberItems != -1) { if (startIndex + numberItems > endIndex) { System.out.println("Specified number of items (" + numberItems + ") exceeds maximum (" + data.numVectors() + "), limiting to " + (endIndex - startIndex) + "."); } else { endIndex = startIndex + numberItems; } } StdErrProgressWriter progress = new StdErrProgressWriter(endIndex - startIndex, "processing vector "); // SortedSet<InputDistance> distances; for (int inputDatumIndex = startIndex; inputDatumIndex < endIndex; inputDatumIndex++) { InputDatum inputDatum = data.getInputDatum(inputDatumIndex); String inputLabel = inputDatum.getLabel(); if (inputDatumIndex == -1) { throw new IllegalArgumentException( "Input with label '" + inputLabel + "' not found in vector file '" + inputVectorFile + "'; possible labels are: " + StringUtils.toString(data.getLabels(), 15)); } File outputDir = new File(outputDirBase, inputLabel.charAt(2) + "/" + inputLabel.charAt(3) + "/" + inputLabel.charAt(4)); outputDir.mkdirs(); File outputFile = new File(outputDir, inputLabel + ".txt"); boolean fileExistsAndValid = false; if (outputFile.exists()) { // check if it the valid data String linesInvalid = ""; int validLineCount = 0; ArrayList<String> lines = FileUtils.readLinesAsList(outputFile.getAbsolutePath()); for (String string : lines) { if (string.trim().length() == 0) { continue; } String[] parts = string.split("\t"); if (parts.length != 2) { linesInvalid += "Line '" + string + "' invalid - contains " + parts.length + " elements.\n"; } else if (!NumberUtils.isNumber(parts[1])) { linesInvalid = "Line '" + string + "' invalid - 2nd part is not a number.\n"; } else { validLineCount++; } } if (validLineCount != neighbours) { linesInvalid = "Not enough valid lines; expected " + neighbours + ", found " + validLineCount + ".\n"; } fileExistsAndValid = true; if (org.apache.commons.lang.StringUtils.isNotBlank(linesInvalid)) { System.out.println("File " + outputFile.getAbsolutePath() + " exists, but is not valid:\n" + linesInvalid); } } if (fileExistsAndValid) { Logger.getLogger("at.tuwien.ifs.feature.evaluation").finer( "File " + outputFile.getAbsolutePath() + " exists and is valid; not recomputing"); } else { PrintWriter p = new PrintWriter(outputFile); SmallestElementSet<InputDistance> distances = data.getNearestDistances(inputDatumIndex, neighbours, metric); for (InputDistance inputDistance : distances) { p.println(inputDistance.getInput().getLabel() + "\t" + inputDistance.getDistance()); } p.close(); } progress.progress(); } } catch (IllegalArgumentException e) { System.out.println(e.getMessage() + ". Aborting."); System.exit(-1); } }
From source file:com.codename1.tools.javadoc.sourceembed.javadocsourceembed.Main.java
public static void main(String[] args) throws Exception { // this accepts two arguments source directory and destination directory where the modfied files will // be written File sourceDir = new File(args[0]); File destDir = new File(args[1]); System.out//from www .j a v a 2s. co m .println("JavaDoc conversion " + sourceDir.getAbsolutePath() + " to " + destDir.getAbsolutePath()); if (!sourceDir.exists() || !sourceDir.isDirectory()) { System.out.println("Source directory doesn't exist"); System.exit(1); return; } CHARSET = Charset.forName("UTF-8"); directoryWalker(sourceDir, destDir); }
From source file:net.geoprism.FileMerger.java
public static void main(String[] args) throws ParseException, IOException { CommandLineParser parser = new DefaultParser(); Options options = new Options(); options.addOption(Option.builder("b").hasArg().argName("baseFile").longOpt("baseFile") .desc("The path to the base properties file.").build()); options.addOption(Option.builder("o").hasArg().argName("overrideFile").longOpt("overrideFile") .desc("The path to the override properties file.").build()); options.addOption(Option.builder("e").hasArg().argName("exportFile").longOpt("exportFile") .desc("The path to the export properties file. A null value defaults to base.").build()); options.addOption(Option.builder("B").hasArg().argName("baseDir").longOpt("baseDir") .desc("The path to the base directory.").build()); options.addOption(Option.builder("O").hasArg().argName("overrideDir").longOpt("overrideDir") .desc("The path to the override directory.").build()); options.addOption(Option.builder("E").hasArg().argName("exportDir").longOpt("exportDir") .desc("The path to the export directory. A null value defaults to base.").build()); CommandLine line = parser.parse(options, args); String sBase = line.getOptionValue("b"); String sOverride = line.getOptionValue("o"); String sExport = line.getOptionValue("e"); String sBaseDir = line.getOptionValue("B"); String sOverrideDir = line.getOptionValue("O"); String sExportDir = line.getOptionValue("E"); if (sBase != null && sOverride != null) { if (sExport == null) { sExport = sBase;//from www .jav a 2 s . c o m } File fBase = new File(sBase); File fOverride = new File(sOverride); File fExport = new File(sExport); if (!fBase.exists() || !fOverride.exists()) { throw new RuntimeException( "The base [" + sBase + "] and the override [" + sOverride + "] paths must both exist."); } FileMerger merger = new FileMerger(); merger.mergeProperties(fBase, fOverride, fExport); } else if (sBaseDir != null && sOverrideDir != null) { if (sExportDir == null) { sExportDir = sBaseDir; } File fBaseDir = new File(sBaseDir); File fOverrideDir = new File(sOverrideDir); File fExportDir = new File(sExportDir); if (!fBaseDir.exists() || !fOverrideDir.exists()) { throw new RuntimeException("The base [" + sBaseDir + "] and the override [" + sOverrideDir + "] paths must both exist."); } FileMerger merger = new FileMerger(); merger.mergeDirectories(fBaseDir, fOverrideDir, fExportDir); } else { throw new RuntimeException("Invalid arguments"); } }
From source file:act.installer.pubchem.PubchemSynonymFinder.java
public static void main(String[] args) throws Exception { org.apache.commons.cli.Options opts = new org.apache.commons.cli.Options(); for (Option.Builder b : OPTION_BUILDERS) { opts.addOption(b.build());/*from w w w . j av a2 s. c om*/ } CommandLine cl = null; try { CommandLineParser parser = new DefaultParser(); cl = parser.parse(opts, args); } catch (ParseException e) { System.err.format("Argument parsing failed: %s\n", e.getMessage()); HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); System.exit(1); } if (cl.hasOption("help")) { HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); return; } File rocksDBFile = new File(cl.getOptionValue(OPTION_INDEX_PATH)); if (!rocksDBFile.isDirectory()) { System.err.format("Index directory does not exist or is not a directory at '%s'", rocksDBFile.getAbsolutePath()); HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); System.exit(1); } List<String> compoundIds = null; if (cl.hasOption(OPTION_PUBCHEM_COMPOUND_ID)) { compoundIds = Collections.singletonList(cl.getOptionValue(OPTION_PUBCHEM_COMPOUND_ID)); } else if (cl.hasOption(OPTION_IDS_FILE)) { File idsFile = new File(cl.getOptionValue(OPTION_IDS_FILE)); if (!idsFile.exists()) { System.err.format("Cannot find Pubchem CIDs file at %s", idsFile.getAbsolutePath()); HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); System.exit(1); } compoundIds = getCIDsFromFile(idsFile); if (compoundIds.size() == 0) { System.err.format("Found zero Pubchem CIDs to process in file at '%s', exiting", idsFile.getAbsolutePath()); HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); System.exit(1); } } else { System.err.format("Must specify one of '%s' or '%s'; index is too big to print all synonyms.", OPTION_PUBCHEM_COMPOUND_ID, OPTION_IDS_FILE); HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); System.exit(1); } // Run a quick check to warn users of malformed ids. compoundIds.forEach(x -> { if (!PC_CID_PATTERN.matcher(x).matches()) { // Use matches() for complete matching. LOGGER.warn("Specified compound id does not match expected format: %s", x); } }); LOGGER.info("Opening DB and searching for %d Pubchem CIDs", compoundIds.size()); Pair<RocksDB, Map<PubchemTTLMerger.COLUMN_FAMILIES, ColumnFamilyHandle>> dbAndHandles = null; Map<String, PubchemSynonyms> results = new LinkedHashMap<>(compoundIds.size()); try { dbAndHandles = PubchemTTLMerger.openExistingRocksDB(rocksDBFile); RocksDB db = dbAndHandles.getLeft(); ColumnFamilyHandle cidToSynonymsCfh = dbAndHandles.getRight() .get(PubchemTTLMerger.COLUMN_FAMILIES.CID_TO_SYNONYMS); for (String cid : compoundIds) { PubchemSynonyms synonyms = null; byte[] val = db.get(cidToSynonymsCfh, cid.getBytes(UTF8)); if (val != null) { ObjectInputStream oi = new ObjectInputStream(new ByteArrayInputStream(val)); // We're relying on our use of a one-value-type per index model here so we can skip the instanceof check. synonyms = (PubchemSynonyms) oi.readObject(); } else { LOGGER.warn("No synonyms available for compound id '%s'", cid); } results.put(cid, synonyms); } } finally { if (dbAndHandles != null) { dbAndHandles.getLeft().close(); } } try (OutputStream outputStream = cl.hasOption(OPTION_OUTPUT) ? new FileOutputStream(cl.getOptionValue(OPTION_OUTPUT)) : System.out) { OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValue(outputStream, results); new OutputStreamWriter(outputStream).append('\n'); } LOGGER.info("Done searching for Pubchem synonyms"); }
From source file:android.databinding.tool.MakeCopy.java
public static void main(String[] args) { if (args.length < 5) { System.out.println("required parameters: [-l] manifest adk-dir src-out-dir xml-out-dir " + "res-out-dir res-in-dir..."); System.out.println("Creates an android data binding class and copies resources from"); System.out.println("res-source to res-target and modifies binding layout files"); System.out.println("in res-target. Binding data is extracted into XML files"); System.out.println("and placed in xml-out-dir."); System.out.println(" -l indicates that this is a library"); System.out.println(" manifest path to AndroidManifest.xml file"); System.out.println(" src-out-dir path to where generated source goes"); System.out.println(" xml-out-dir path to where generated binding XML goes"); System.out.println(" res-out-dir path to the where modified resources should go"); System.out.println(/*w ww . j a v a2 s .c o m*/ " res-in-dir path to source resources \"res\" directory. One" + " or more are allowed."); System.exit(1); } final boolean isLibrary = args[0].equals("-l"); final int indexOffset = isLibrary ? 1 : 0; final String applicationPackage; final int minSdk; final Document androidManifest = readAndroidManifest(new File(args[MANIFEST_INDEX + indexOffset])); try { final XPathFactory xPathFactory = XPathFactory.newInstance(); final XPath xPath = xPathFactory.newXPath(); applicationPackage = xPath.evaluate("string(/manifest/@package)", androidManifest); final Double minSdkNumber = (Double) xPath.evaluate("number(/manifest/uses-sdk/@android:minSdkVersion)", androidManifest, XPathConstants.NUMBER); minSdk = minSdkNumber == null ? 1 : minSdkNumber.intValue(); } catch (XPathExpressionException e) { e.printStackTrace(); System.exit(6); return; } final File srcDir = new File(args[SRC_INDEX + indexOffset], APP_SUBPATH); if (!makeTargetDir(srcDir)) { System.err.println("Could not create source directory " + srcDir); System.exit(2); } final File resTarget = new File(args[RES_OUT_INDEX + indexOffset]); if (!makeTargetDir(resTarget)) { System.err.println("Could not create resource directory: " + resTarget); System.exit(4); } final File xmlDir = new File(args[XML_INDEX + indexOffset]); if (!makeTargetDir(xmlDir)) { System.err.println("Could not create xml output directory: " + xmlDir); System.exit(5); } System.out.println("Application Package: " + applicationPackage); System.out.println("Minimum SDK: " + minSdk); System.out.println("Target Resources: " + resTarget.getAbsolutePath()); System.out.println("Target Source Dir: " + srcDir.getAbsolutePath()); System.out.println("Target XML Dir: " + xmlDir.getAbsolutePath()); System.out.println("Library? " + isLibrary); boolean foundSomeResources = false; for (int i = RES_IN_INDEX + indexOffset; i < args.length; i++) { final File resDir = new File(args[i]); if (!resDir.exists()) { System.out.println("Could not find resource directory: " + resDir); } else { System.out.println("Source Resources: " + resDir.getAbsolutePath()); try { FileUtils.copyDirectory(resDir, resTarget); addFromFile(resDir, resTarget); foundSomeResources = true; } catch (IOException e) { System.err.println("Could not copy resources from " + resDir + " to " + resTarget + ": " + e.getLocalizedMessage()); System.exit(3); } } } if (!foundSomeResources) { System.err.println("No resource directories were found."); System.exit(7); } processLayoutFiles(applicationPackage, resTarget, srcDir, xmlDir, minSdk, isLibrary); }