List of usage examples for org.apache.commons.io FileUtils openInputStream
public static FileInputStream openInputStream(File file) throws IOException
new FileInputStream(file)
. From source file:hd3gtv.embddb.MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); PoolManager poolmanager = new PoolManager("test"); poolmanager.startLocalServers();/*ww w. j ava 2s . co m*/ // TODO manage white/black range addr list for autodiscover Thread.sleep(50); Properties conf = new Properties(); conf.load(FileUtils.openInputStream(new File("conf.properties"))); poolmanager.setBootstrapPotentialNodes( importConf(poolmanager, conf, poolmanager.getProtocol().getDefaultTCPPort())); poolmanager.connectToBootstrapPotentialNodes("Startup"); Thread.sleep(50); poolmanager.startConsole(); }
From source file:com.ibm.watson.developer_cloud.conversation_tone_analyzer_integration.v1.ToneConversationIntegrationV1.java
public static void main(String[] args) throws Exception { // load the properties file Properties props = new Properties(); props.load(FileUtils.openInputStream(new File("tone_conversation_integration.properties"))); // instantiate the conversation service ConversationService conversationService = new ConversationService( ConversationService.VERSION_DATE_2016_07_11); conversationService.setUsernameAndPassword( props.getProperty("CONVERSATION_USERNAME", "<conversation_username>"), props.getProperty("CONVERSATION_PASSWORD", "<conversation_password>")); // instantiate the tone analyzer service ToneAnalyzer toneService = new ToneAnalyzer(ToneAnalyzer.VERSION_DATE_2016_05_19); toneService.setUsernameAndPassword(props.getProperty("TONE_ANALYZER_USERNAME", "<tone_analyzer_username>"), props.getProperty("TONE_ANALYZER_PASSWORD", "<tone_analyzer_password>")); // workspace id final String workspaceId = props.getProperty("WORKSPACE_ID", "<workspace_id>"); // maintain history in the context variable - will add a history variable to // each of the emotion, social and language tones final Boolean maintainHistory = false; /**/*from w ww . ja va 2s . co m*/ * Input for the conversation service: input (String): an input string (the user's conversation turn) and context * (Map<String,Object>: any context that needs to be maintained - either added by the client app or passed in the * response from the conversation service on the previous conversation turn. */ final String input = "I am happy"; final Map<String, Object> context = new HashMap<String, Object>(); // UPDATE CONTEXT HERE IF CONTINUING AN ONGOING CONVERSATION // set local context variable to the context from the last response from the // Conversation Service // (see the getContext() method of the MessageResponse class in // com.ibm.watson.developer_cloud.conversation.v1.model) // async call to Tone Analyzer toneService.getTone(input, null).enqueue(new ServiceCallback<ToneAnalysis>() { @Override public void onResponse(ToneAnalysis toneResponsePayload) { // update context with the tone data returned by the Tone Analyzer ToneDetection.updateUserTone(context, toneResponsePayload, maintainHistory); // call Conversation Service with the input and tone-aware context MessageRequest newMessage = new MessageRequest.Builder().inputText(input).context(context).build(); conversationService.message(workspaceId, newMessage) .enqueue(new ServiceCallback<MessageResponse>() { @Override public void onResponse(MessageResponse response) { System.out.println(response); } @Override public void onFailure(Exception e) { } }); } @Override public void onFailure(Exception e) { } }); }
From source file:com.textocat.textokit.morph.opencorpora.resource.XmlDictionaryParserLauncher.java
public static void main(String[] args) throws Exception { XmlDictionaryParserLauncher cfg = new XmlDictionaryParserLauncher(); new JCommander(cfg, args); MorphDictionaryImpl dict = new MorphDictionaryImpl(); DictionaryExtension ext = cfg.dictExtensionClass.newInstance(); FileInputStream fis = FileUtils.openInputStream(cfg.dictXmlFile); try {/*from w w w .j av a2 s.co m*/ new XmlDictionaryParser(dict, ext, fis).run(); } finally { IOUtils.closeQuietly(fis); } log.info("Preparing to serialization..."); long timeBefore = currentTimeMillis(); OutputStream fout = new BufferedOutputStream(FileUtils.openOutputStream(cfg.outputFile), 8192 * 8); ObjectOutputStream out = new ObjectOutputStream(fout); try { out.writeObject(dict.getGramModel()); out.writeObject(dict); } finally { out.close(); } log.info("Serialization finished in {} ms.\nOutput size: {} bytes", currentTimeMillis() - timeBefore, cfg.outputFile.length()); }
From source file:com.machinelinking.cli.loader.java
public static void main(String[] args) { if (args.length != 2) { System.err.println("Usage: $0 <config-file> <dump>"); System.exit(1);/*w w w . ja v a 2 s.co m*/ } try { final File configFile = check(args[0]); final File dumpFile = check(args[1]); final Properties properties = new Properties(); properties.load(FileUtils.openInputStream(configFile)); final Flag[] flags = WikiPipelineFactory.getInstance().toFlags(getPropertyOrFail(properties, LOADER_FLAGS_PROP, "valid flags: " + Arrays.toString(WikiPipelineFactory.getInstance().getDefinedFlags()))); final JSONStorageFactory jsonStorageFactory = MultiJSONStorageFactory .loadJSONStorageFactory(getPropertyOrFail(properties, LOADER_STORAGE_FACTORY_PROP, null)); final String jsonStorageConfig = getPropertyOrFail(properties, LOADER_STORAGE_CONFIG_PROP, null); final URL prefixURL = readURL(getPropertyOrFail(properties, LOADER_PREFIX_URL_PROP, "expected a valid URL prefix like: http://en.wikipedia.org/"), LOADER_PREFIX_URL_PROP); final DefaultJSONStorageLoader[] loader = new DefaultJSONStorageLoader[1]; final boolean[] finalReportProduced = new boolean[] { false }; Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { if (!finalReportProduced[0] && loader[0] != null) { System.err.println( "Process interrupted. Partial loading report: " + loader[0].createReport()); } System.err.println("Shutting down."); } })); final JSONStorageConfiguration storageConfig = jsonStorageFactory .createConfiguration(jsonStorageConfig); try (final JSONStorage storage = jsonStorageFactory.createStorage(storageConfig)) { loader[0] = new DefaultJSONStorageLoader(WikiPipelineFactory.getInstance(), flags, storage); final StorageLoaderReport report = loader[0].load(prefixURL, FileUtil.openDecompressedInputStream(dumpFile)); System.err.println("Loading report: " + report); finalReportProduced[0] = true; } System.exit(0); } catch (Exception e) { e.printStackTrace(); System.exit(2); } }
From source file:com.textocat.textokit.morph.opencorpora.resource.XmlDictionaryPSP.java
public static void main(String[] args) throws Exception { XmlDictionaryPSP cfg = new XmlDictionaryPSP(); new JCommander(cfg, args); MorphDictionaryImpl dict = new MorphDictionaryImpl(); DictionaryExtension ext = cfg.dictExtensionClass.newInstance(); FileInputStream fis = FileUtils.openInputStream(cfg.dictXmlFile); try {/* w w w .ja v a 2 s . c o m*/ new XmlDictionaryParser(dict, ext, fis).run(); } finally { IOUtils.closeQuietly(fis); } System.out.println("Preparing to serialization..."); long timeBefore = currentTimeMillis(); OutputStream fout = new BufferedOutputStream(FileUtils.openOutputStream(cfg.outputJarFile), 8192 * 8); Manifest manifest = new Manifest(); manifest.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0"); manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_VERSION, dict.getVersion()); manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_REVISION, dict.getRevision()); manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_VARIANT, cfg.variant); String dictEntryName = String.format( OpencorporaMorphDictionaryAPI.FILENAME_PATTERN_OPENCORPORA_SERIALIZED_DICT, dict.getVersion(), dict.getRevision(), cfg.variant); JarOutputStream jarOut = new JarOutputStream(fout, manifest); jarOut.putNextEntry(new ZipEntry(dictEntryName)); ObjectOutputStream serOut = new ObjectOutputStream(jarOut); try { serOut.writeObject(dict.getGramModel()); serOut.writeObject(dict); } finally { serOut.flush(); jarOut.closeEntry(); serOut.close(); } System.out.println(String.format("Serialization finished in %s ms.\nOutput size: %s bytes", currentTimeMillis() - timeBefore, cfg.outputJarFile.length())); }
From source file:com.textocat.textokit.postagger.opennlp.OpenNLPPosTaggerTrainerCLI.java
public static void main(String[] args) throws Exception { OpenNLPPosTaggerTrainerCLI cli = new OpenNLPPosTaggerTrainerCLI(); new JCommander(cli, args); ///* ww w. j av a 2 s .com*/ OpenNLPPosTaggerTrainer trainer = new OpenNLPPosTaggerTrainer(); trainer.setLanguageCode(cli.languageCode); trainer.setModelOutFile(cli.modelOutFile); // train params { FileInputStream fis = FileUtils.openInputStream(cli.trainParamsFile); TrainingParameters trainParams; try { trainParams = new TrainingParameters(fis); } finally { IOUtils.closeQuietly(fis); } trainer.setTrainingParameters(trainParams); } // feature extractors { FileInputStream fis = FileUtils.openInputStream(cli.extractorParams); Properties props = new Properties(); try { props.load(fis); } finally { IOUtils.closeQuietly(fis); } MorphDictionary morphDict = getMorphDictionaryAPI().getCachedInstance().getResource(); trainer.setTaggerFactory(new POSTaggerFactory(DefaultFeatureExtractors.from(props, morphDict))); } // input sentence stream { ExternalResourceDescription morphDictDesc = getMorphDictionaryAPI() .getResourceDescriptionForCachedInstance(); TypeSystemDescription tsd = createTypeSystemDescription( "com.textocat.textokit.commons.Commons-TypeSystem", TokenizerAPI.TYPESYSTEM_TOKENIZER, SentenceSplitterAPI.TYPESYSTEM_SENTENCES, PosTaggerAPI.TYPESYSTEM_POSTAGGER); CollectionReaderDescription colReaderDesc = CollectionReaderFactory.createReaderDescription( XmiCollectionReader.class, tsd, XmiCollectionReader.PARAM_INPUTDIR, cli.trainingXmiDir); AnalysisEngineDescription posTrimmerDesc = PosTrimmingAnnotator .createDescription(cli.gramCategories.toArray(new String[cli.gramCategories.size()])); bindExternalResource(posTrimmerDesc, PosTrimmingAnnotator.RESOURCE_GRAM_MODEL, morphDictDesc); AnalysisEngineDescription tagAssemblerDesc = TagAssembler.createDescription(); bindExternalResource(tagAssemblerDesc, GramModelBasedTagMapper.RESOURCE_GRAM_MODEL, morphDictDesc); AnalysisEngineDescription aeDesc = createEngineDescription(posTrimmerDesc, tagAssemblerDesc); Iterator<Sentence> sentIter = AnnotationIteratorOverCollection.createIterator(Sentence.class, colReaderDesc, aeDesc); SpanStreamOverCollection<Sentence> sentStream = new SpanStreamOverCollection<Sentence>(sentIter); trainer.setSentenceStream(sentStream); } trainer.train(); }
From source file:com.xinge.sample.samples.pptx4j.org.pptx4j.samples.SlideNotes.java
public static void main(String[] args) throws Exception { // Where will we save our new .ppxt? String outputfilepath = System.getProperty("user.dir") + "/sample-docs/OUT_SlideNotes.pptx"; // Create skeletal package, including a MainPresentationPart and a SlideLayoutPart PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage(); // Need references to these parts to create a slide // Please note that these parts *already exist* - they are // created by createPackage() above. See that method // for instruction on how to create and add a part. MainPresentationPart pp = (MainPresentationPart) presentationMLPackage.getParts().getParts() .get(new PartName("/ppt/presentation.xml")); SlideLayoutPart layoutPart = (SlideLayoutPart) presentationMLPackage.getParts().getParts() .get(new PartName("/ppt/slideLayouts/slideLayout1.xml")); // OK, now we can create a slide SlidePart slidePart = new SlidePart(new PartName("/ppt/slides/slide1.xml")); slidePart.setContents(SlidePart.createSld()); pp.addSlide(0, slidePart);// www . j ava2 s.co m // Slide layout part slidePart.addTargetPart(layoutPart); // Create and add shape Shape sample = ((Shape) XmlUtils.unmarshalString(SAMPLE_SHAPE, Context.jcPML)); slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample); // Now add notes slide. // 1. Notes master NotesMasterPart nmp = new NotesMasterPart(); NotesMaster notesmaster = (NotesMaster) XmlUtils.unmarshalString(notesMasterXml, Context.jcPML); nmp.setJaxbElement(notesmaster); // .. connect it to /ppt/presentation.xml Relationship ppRelNmp = pp.addTargetPart(nmp); /* * <p:notesMasterIdLst> <p:notesMasterId r:id="rId3"/> </p:notesMasterIdLst> */ pp.getJaxbElement().setNotesMasterIdLst(createNotesMasterIdListPlusEntry(ppRelNmp.getId())); // .. NotesMasterPart typically has a rel to a theme // .. can we get away without it? // Nope .. read this in from a file ThemePart themePart = new ThemePart(new PartName("/ppt/theme/theme2.xml")); // TODO: read it from a string instead themePart.unmarshal(FileUtils.openInputStream(new File(System.getProperty("user.dir") + "/theme2.xml"))); nmp.addTargetPart(themePart); // 2. Notes slide NotesSlidePart nsp = new NotesSlidePart(); Notes notes = (Notes) XmlUtils.unmarshalString(notesXML, Context.jcPML); nsp.setJaxbElement(notes); // .. connect it to the slide slidePart.addTargetPart(nsp); // .. it also has a rel to the slide nsp.addTargetPart(slidePart); // .. and the slide master nsp.addTargetPart(nmp); // All done: save it presentationMLPackage.save(new File(outputfilepath)); System.out.println("\n\n done .. saved " + outputfilepath); }
From source file:com.vmware.photon.controller.core.Main.java
public static void main(String[] args) throws Throwable { try {//from w w w. j ava 2 s . c om LoggingFactory.bootstrap(); logger.info("args: " + Arrays.toString(args)); ArgumentParser parser = ArgumentParsers.newArgumentParser("PhotonControllerCore").defaultHelp(true) .description("Photon Controller Core"); parser.addArgument("config-file").help("photon controller configuration file"); parser.addArgument("--manual").type(Boolean.class).setDefault(false) .help("If true, create default deployment."); Namespace namespace = parser.parseArgsOrFail(args); PhotonControllerConfig photonControllerConfig = getPhotonControllerConfig(namespace); DeployerConfig deployerConfig = photonControllerConfig.getDeployerConfig(); new LoggingFactory(photonControllerConfig.getLogging(), "photon-controller-core").configure(); SSLContext sslContext; if (deployerConfig.getDeployerContext().isAuthEnabled()) { sslContext = SSLContext.getInstance(KeyStoreUtils.THRIFT_PROTOCOL); TrustManagerFactory tmf = null; tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); KeyStore keyStore = KeyStore.getInstance("JKS"); InputStream in = FileUtils .openInputStream(new File(deployerConfig.getDeployerContext().getKeyStorePath())); keyStore.load(in, deployerConfig.getDeployerContext().getKeyStorePassword().toCharArray()); tmf.init(keyStore); sslContext.init(null, tmf.getTrustManagers(), null); } else { KeyStoreUtils.generateKeys("/thrift/"); sslContext = KeyStoreUtils.acceptAllCerts(KeyStoreUtils.THRIFT_PROTOCOL); } ThriftModule thriftModule = new ThriftModule(sslContext); PhotonControllerXenonHost xenonHost = startXenonHost(photonControllerConfig, thriftModule, deployerConfig, sslContext); if ((Boolean) namespace.get("manual")) { DefaultDeployment.createDefaultDeployment(photonControllerConfig.getXenonConfig().getPeerNodes(), deployerConfig, xenonHost); } // Creating a temp configuration file for apife with modification to some named sections in photon-controller-config // so that it can match the Configuration class of dropwizard. File apiFeTempConfig = File.createTempFile("apiFeTempConfig", ".tmp"); File source = new File(args[0]); FileInputStream fis = new FileInputStream(source); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); FileWriter fstream = new FileWriter(apiFeTempConfig, true); BufferedWriter out = new BufferedWriter(fstream); String aLine = null; while ((aLine = in.readLine()) != null) { if (aLine.equals("apife:")) { aLine = aLine.replace("apife:", "server:"); } out.write(aLine); out.newLine(); } in.close(); out.close(); // This approach can be simplified once the apife container is gone, but for the time being // it expects the first arg to be the string "server". String[] apiFeArgs = new String[2]; apiFeArgs[0] = "server"; apiFeArgs[1] = apiFeTempConfig.getAbsolutePath(); ApiFeService.setupApiFeConfigurationForServerCommand(apiFeArgs); ApiFeService.addServiceHost(xenonHost); ApiFeService.setSSLContext(sslContext); ApiFeService apiFeService = new ApiFeService(); apiFeService.run(apiFeArgs); apiFeTempConfig.deleteOnExit(); LocalApiClient localApiClient = apiFeService.getInjector().getInstance(LocalApiClient.class); xenonHost.setApiClient(localApiClient); // in the non-auth enabled scenario we need to be able to accept any self-signed certificate if (!deployerConfig.getDeployerContext().isAuthEnabled()) { KeyStoreUtils.acceptAllCerts(KeyStoreUtils.THRIFT_PROTOCOL); } Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { logger.info("Shutting down"); xenonHost.stop(); logger.info("Done"); LoggingFactory.detachAndStop(); } }); } catch (Exception e) { logger.error("Failed to start photon controller ", e); throw e; } }
From source file:edu.cornell.med.icb.goby.reads.ColorSpaceConverter.java
public static void main(final String[] args) throws JSAPException, IOException { final JSAP jsap = new JSAP(); final FlaggedOption sequenceOption = new FlaggedOption("input"); sequenceOption.setRequired(true);/*from w w w. j a va 2s . c om*/ sequenceOption.setLongFlag("input"); sequenceOption.setShortFlag('i'); sequenceOption.setStringParser(FileStringParser.getParser().setMustBeFile(true).setMustExist(true)); sequenceOption.setHelp("The input file (in Fasta format) to convert"); jsap.registerParameter(sequenceOption); final FlaggedOption outputOption = new FlaggedOption("output"); outputOption.setRequired(false); outputOption.setLongFlag("output"); outputOption.setShortFlag('o'); outputOption.setStringParser(FileStringParser.getParser().setMustBeFile(true)); outputOption.setHelp("The output file to write to (default = stdout)"); jsap.registerParameter(outputOption); final FlaggedOption titleOption = new FlaggedOption("title"); titleOption.setRequired(false); titleOption.setLongFlag("title"); titleOption.setShortFlag('t'); titleOption.setHelp("Title for this conversion"); jsap.registerParameter(titleOption); final Switch verboseOption = new Switch("verbose"); verboseOption.setLongFlag("verbose"); verboseOption.setShortFlag('v'); verboseOption.setHelp("Verbose output"); jsap.registerParameter(verboseOption); final Switch helpOption = new Switch("help"); helpOption.setLongFlag("help"); helpOption.setShortFlag('h'); helpOption.setHelp("Print this message"); jsap.registerParameter(helpOption); jsap.setUsage("Usage: " + ColorSpaceConverter.class.getName() + " " + jsap.getUsage()); final JSAPResult result = jsap.parse(args); if (result.getBoolean("help")) { System.out.println(jsap.getHelp()); System.exit(0); } if (!result.success()) { final Iterator<String> errors = result.getErrorMessageIterator(); while (errors.hasNext()) { System.err.println(errors.next()); } System.err.println(jsap.getUsage()); System.exit(1); } final boolean verbose = result.getBoolean("verbose"); final File sequenceFile = result.getFile("input"); if (verbose) { System.out.println("Reading sequence from: " + sequenceFile); } // extract the title to use for the output header final String title; if (result.contains("title")) { title = result.getString("title"); } else { title = sequenceFile.getName(); } Reader inputReader = null; PrintWriter outputWriter = null; try { if ("gz".equals(FilenameUtils.getExtension(sequenceFile.getName()))) { inputReader = new InputStreamReader(new GZIPInputStream(FileUtils.openInputStream(sequenceFile))); } else { inputReader = new FileReader(sequenceFile); } final FastaParser fastaParser = new FastaParser(inputReader); final File outputFile = result.getFile("output"); final OutputStream outputStream; if (outputFile != null) { outputStream = FileUtils.openOutputStream(outputFile); if (verbose) { System.out.println("Writing sequence : " + outputFile); } } else { outputStream = System.out; } outputWriter = new PrintWriter(outputStream); // write the header portion of the output outputWriter.print("# "); outputWriter.print(new Date()); outputWriter.print(' '); outputWriter.print(ColorSpaceConverter.class.getName()); for (final String arg : args) { outputWriter.print(' '); outputWriter.print(arg); } outputWriter.println(); outputWriter.print("# Cwd: "); outputWriter.println(new File(".").getCanonicalPath()); outputWriter.print("# Title: "); outputWriter.println(title); // now parse the input sequence long sequenceCount = 0; final MutableString descriptionLine = new MutableString(); final MutableString sequence = new MutableString(); final MutableString colorSpaceSequence = new MutableString(); while (fastaParser.hasNext()) { fastaParser.next(descriptionLine, sequence); outputWriter.print('>'); outputWriter.println(descriptionLine); convert(sequence, colorSpaceSequence); CompactToFastaMode.writeSequence(outputWriter, colorSpaceSequence); sequenceCount++; if (verbose && sequenceCount % 10000 == 0) { System.out.println("Converted " + sequenceCount + " entries"); } } if (verbose) { System.out.println("Conversion complete!"); } } finally { IOUtils.closeQuietly(inputReader); IOUtils.closeQuietly(outputWriter); } }
From source file:it.cnr.ilc.tokenizer.utils.Utilities.java
public static String readFileContent(String filepath) throws IOException { String message = ""; File initialFile;/*from w w w. j ava2s . c o m*/ InputStream targetStream = null; String theString = ""; try { initialFile = new File(filepath); targetStream = FileUtils.openInputStream(initialFile); theString = IOUtils.toString(targetStream, "UTF-8"); } catch (IOException e) { message = "IOaaException in reading the stream for " + filepath + " " + e.getMessage(); Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, message); //System.exit(-1); } finally { if (targetStream != null) { try { targetStream.close(); } catch (IOException e) { message = "IOException in closing the stream for " + filepath + " " + e.getMessage(); Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, message); //System.exit(-1); } } } //System.err.println(theString); return theString; }